Skip to content

Commit 8f0f4d5

Browse files
daxfohltonybruguier
authored andcommitted
Allow any object with supporting protocols to be the action in act_on (quantumlib#5111)
* Allow any object with supporting protocols to be the action in act_on * lint
1 parent ffa722a commit 8f0f4d5

11 files changed

+36
-23
lines changed

Diff for: cirq-core/cirq/contrib/quimb/mps_simulator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ def apply_op(self, op: 'cirq.Operation', prng: np.random.RandomState):
677677

678678
def _act_on_fallback_(
679679
self,
680-
action: Union['cirq.Operation', 'cirq.Gate'],
680+
action: Any,
681681
qubits: Sequence['cirq.Qid'],
682682
allow_decompose: bool = True,
683683
) -> bool:

Diff for: cirq-core/cirq/protocols/act_on_protocol.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import TYPE_CHECKING, Union, Sequence
15+
from typing import Any, Sequence, TYPE_CHECKING, Union
1616

1717
from typing_extensions import Protocol
1818

@@ -89,7 +89,7 @@ def _act_on_(
8989

9090

9191
def act_on(
92-
action: Union['cirq.Operation', 'cirq.Gate'],
92+
action: Any,
9393
args: 'cirq.OperationTarget',
9494
qubits: Sequence['cirq.Qid'] = None,
9595
*,

Diff for: cirq-core/cirq/protocols/act_on_protocol_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Any, Tuple, Union, Sequence
14+
from typing import Any, Sequence, Tuple
1515

1616
import numpy as np
1717
import pytest
@@ -36,7 +36,7 @@ def copy(self, deep_copy_buffers: bool = True):
3636

3737
def _act_on_fallback_(
3838
self,
39-
action: Union['cirq.Operation', 'cirq.Gate'],
39+
action: Any,
4040
qubits: Sequence['cirq.Qid'],
4141
allow_decompose: bool = True,
4242
):

Diff for: cirq-core/cirq/sim/act_on_args_container.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import warnings
1717
from collections import abc
1818
from typing import (
19+
Any,
1920
Dict,
2021
Generic,
2122
Iterator,
@@ -25,7 +26,6 @@
2526
Sequence,
2627
Tuple,
2728
TYPE_CHECKING,
28-
Union,
2929
)
3030

3131
import numpy as np
@@ -120,16 +120,26 @@ def create_merged_state(self) -> TActOnArgs:
120120

121121
def _act_on_fallback_(
122122
self,
123-
action: Union['cirq.Operation', 'cirq.Gate'],
123+
action: Any,
124124
qubits: Sequence['cirq.Qid'],
125125
allow_decompose: bool = True,
126126
) -> bool:
127-
gate = action.gate if isinstance(action, ops.Operation) else action
127+
gate_opt = (
128+
action
129+
if isinstance(action, ops.Gate)
130+
else action.gate
131+
if isinstance(action, ops.Operation)
132+
else None
133+
)
128134

129-
if isinstance(gate, ops.IdentityGate):
135+
if isinstance(gate_opt, ops.IdentityGate):
130136
return True
131137

132-
if isinstance(gate, ops.SwapPowGate) and gate.exponent % 2 == 1 and gate.global_shift == 0:
138+
if (
139+
isinstance(gate_opt, ops.SwapPowGate)
140+
and gate_opt.exponent % 2 == 1
141+
and gate_opt.global_shift == 0
142+
):
133143
q0, q1 = qubits
134144
args0 = self.args[q0]
135145
args1 = self.args[q1]
@@ -160,8 +170,10 @@ def _act_on_fallback_(
160170

161171
# Decouple any measurements or resets
162172
if self.split_untangled_states and (
163-
isinstance(gate, ops.ResetChannel)
164-
or (isinstance(gate, ops.MeasurementGate) and not op_args.ignore_measurement_results)
173+
isinstance(gate_opt, ops.ResetChannel)
174+
or (
175+
isinstance(gate_opt, ops.MeasurementGate) and not op_args.ignore_measurement_results
176+
)
165177
):
166178
for q in qubits:
167179
if op_args.allows_factoring:

Diff for: cirq-core/cirq/sim/act_on_args_container_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Dict, List, Optional, Sequence, Union
14+
from typing import Any, Dict, List, Optional, Sequence
1515

1616
import cirq
1717

@@ -35,7 +35,7 @@ def copy(self) -> 'EmptyActOnArgs': # type: ignore
3535

3636
def _act_on_fallback_(
3737
self,
38-
action: Union['cirq.Operation', 'cirq.Gate'],
38+
action: Any,
3939
qubits: Sequence['cirq.Qid'],
4040
allow_decompose: bool = True,
4141
) -> bool:

Diff for: cirq-core/cirq/sim/act_on_args_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Sequence, Union
14+
from typing import Any, Sequence
1515

1616
import numpy as np
1717
import pytest
@@ -32,7 +32,7 @@ def _perform_measurement(self, qubits):
3232

3333
def _act_on_fallback_(
3434
self,
35-
action: Union['cirq.Operation', 'cirq.Gate'],
35+
action: Any,
3636
qubits: Sequence['cirq.Qid'],
3737
allow_decompose: bool = True,
3838
) -> bool:

Diff for: cirq-core/cirq/sim/act_on_density_matrix_args.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def __init__(
331331

332332
def _act_on_fallback_(
333333
self,
334-
action: Union['cirq.Operation', 'cirq.Gate'],
334+
action: Any,
335335
qubits: Sequence['cirq.Qid'],
336336
allow_decompose: bool = True,
337337
) -> bool:

Diff for: cirq-core/cirq/sim/act_on_state_vector_args.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def subspace_index(
476476

477477
def _act_on_fallback_(
478478
self,
479-
action: Union['cirq.Operation', 'cirq.Gate'],
479+
action: Any,
480480
qubits: Sequence['cirq.Qid'],
481481
allow_decompose: bool = True,
482482
) -> bool:

Diff for: cirq-core/cirq/sim/clifford/act_on_stabilizer_args.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def state(self) -> TStabilizerState:
7272

7373
def _act_on_fallback_(
7474
self,
75-
action: Union['cirq.Operation', 'cirq.Gate'],
75+
action: Any,
7676
qubits: Sequence['cirq.Qid'],
7777
allow_decompose: bool = True,
7878
) -> Union[bool, NotImplementedType]:

Diff for: cirq-core/cirq/sim/operation_target.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""An interface for quantum states as targets for operations."""
1515
import abc
1616
from typing import (
17+
Any,
1718
Dict,
1819
Generic,
1920
Iterator,
@@ -49,14 +50,14 @@ def create_merged_state(self) -> TActOnArgs:
4950
@abc.abstractmethod
5051
def _act_on_fallback_(
5152
self,
52-
action: Union['cirq.Operation', 'cirq.Gate'],
53+
action: Any,
5354
qubits: Sequence['cirq.Qid'],
5455
allow_decompose: bool = True,
5556
) -> Union[bool, NotImplementedType]:
5657
"""Handles the act_on protocol fallback implementation.
5758
5859
Args:
59-
action: Either a gate or an operation to act on.
60+
action: A gate, operation, or other to act on.
6061
qubits: The applicable qubits if a gate is passed as the action.
6162
allow_decompose: Flag to allow decomposition.
6263

Diff for: cirq-core/cirq/sim/simulator_base_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import math
15-
from typing import Any, Dict, List, Sequence, Tuple, Union
15+
from typing import Any, Dict, List, Sequence, Tuple
1616

1717
import numpy as np
1818
import pytest
@@ -68,7 +68,7 @@ def __init__(self, state, qubits, classical_data):
6868

6969
def _act_on_fallback_(
7070
self,
71-
action: Union['cirq.Operation', 'cirq.Gate'],
71+
action: Any,
7272
qubits: Sequence['cirq.Qid'],
7373
allow_decompose: bool = True,
7474
) -> bool:

0 commit comments

Comments
 (0)