Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any object with supporting protocols to be the action in act_on #5111

Merged
merged 3 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cirq-core/cirq/contrib/quimb/mps_simulator.py
Expand Up @@ -677,7 +677,7 @@ def apply_op(self, op: 'cirq.Operation', prng: np.random.RandomState):

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/protocols/act_on_protocol.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING, Union, Sequence
from typing import Any, Sequence, TYPE_CHECKING, Union

from typing_extensions import Protocol

Expand Down Expand Up @@ -89,7 +89,7 @@ def _act_on_(


def act_on(
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
args: 'cirq.OperationTarget',
qubits: Sequence['cirq.Qid'] = None,
*,
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/protocols/act_on_protocol_test.py
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Tuple, Union, Sequence
from typing import Any, Sequence, Tuple

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

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
):
Expand Down
26 changes: 19 additions & 7 deletions cirq-core/cirq/sim/act_on_args_container.py
Expand Up @@ -16,6 +16,7 @@
import warnings
from collections import abc
from typing import (
Any,
Dict,
Generic,
Iterator,
Expand All @@ -25,7 +26,6 @@
Sequence,
Tuple,
TYPE_CHECKING,
Union,
)

import numpy as np
Expand Down Expand Up @@ -120,16 +120,26 @@ def create_merged_state(self) -> TActOnArgs:

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> bool:
gate = action.gate if isinstance(action, ops.Operation) else action
gate_opt = (
action
if isinstance(action, ops.Gate)
else action.gate
if isinstance(action, ops.Operation)
else None
)

if isinstance(gate, ops.IdentityGate):
if isinstance(gate_opt, ops.IdentityGate):
return True

if isinstance(gate, ops.SwapPowGate) and gate.exponent % 2 == 1 and gate.global_shift == 0:
if (
isinstance(gate_opt, ops.SwapPowGate)
and gate_opt.exponent % 2 == 1
and gate_opt.global_shift == 0
):
q0, q1 = qubits
args0 = self.args[q0]
args1 = self.args[q1]
Expand Down Expand Up @@ -160,8 +170,10 @@ def _act_on_fallback_(

# Decouple any measurements or resets
if self.split_untangled_states and (
isinstance(gate, ops.ResetChannel)
or (isinstance(gate, ops.MeasurementGate) and not op_args.ignore_measurement_results)
isinstance(gate_opt, ops.ResetChannel)
or (
isinstance(gate_opt, ops.MeasurementGate) and not op_args.ignore_measurement_results
)
):
for q in qubits:
if op_args.allows_factoring:
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/sim/act_on_args_container_test.py
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, List, Optional, Sequence, Union
from typing import Any, Dict, List, Optional, Sequence

import cirq

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

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/sim/act_on_args_test.py
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Sequence, Union
from typing import Any, Sequence

import numpy as np
import pytest
Expand All @@ -32,7 +32,7 @@ def _perform_measurement(self, qubits):

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/sim/act_on_density_matrix_args.py
Expand Up @@ -320,7 +320,7 @@ def __init__(

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/sim/act_on_state_vector_args.py
Expand Up @@ -470,7 +470,7 @@ def subspace_index(

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/sim/clifford/act_on_stabilizer_args.py
Expand Up @@ -71,7 +71,7 @@ def state(self) -> TStabilizerState:

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> Union[bool, NotImplementedType]:
Expand Down
5 changes: 3 additions & 2 deletions cirq-core/cirq/sim/operation_target.py
Expand Up @@ -14,6 +14,7 @@
"""An interface for quantum states as targets for operations."""
import abc
from typing import (
Any,
Dict,
Generic,
Iterator,
Expand Down Expand Up @@ -49,14 +50,14 @@ def create_merged_state(self) -> TActOnArgs:
@abc.abstractmethod
def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> Union[bool, NotImplementedType]:
"""Handles the act_on protocol fallback implementation.

Args:
action: Either a gate or an operation to act on.
action: A gate, operation, or other to act on.
qubits: The applicable qubits if a gate is passed as the action.
allow_decompose: Flag to allow decomposition.

Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/sim/simulator_base_test.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import math
from typing import Any, Dict, List, Sequence, Union
from typing import Any, Dict, List, Sequence

import numpy as np
import pytest
Expand Down Expand Up @@ -48,7 +48,7 @@ def copy(self, deep_copy_buffers: bool = True) -> 'CountingActOnArgs':

def _act_on_fallback_(
self,
action: Union['cirq.Operation', 'cirq.Gate'],
action: Any,
qubits: Sequence['cirq.Qid'],
allow_decompose: bool = True,
) -> bool:
Expand Down