Skip to content

Commit

Permalink
Replace _get_op_circuit with op.untagged (#3893)
Browse files Browse the repository at this point in the history
Since `Operation` provides a canonical way to get the untagged version of itself, we should use it instead of writing a new method to do the same thing.

Using `op.untagged` in #3634 removes any residual blocking effects from #3678, although I'm still in favor of finding a proper resolution to #3678.
  • Loading branch information
95-martin-orion committed Mar 9, 2021
1 parent eab2b48 commit d4f3fc9
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,16 +773,18 @@ def are_all_matches_terminal(self, predicate: Callable[['cirq.Operation'], bool]
given predicate are terminal. Also checks within any CircuitGates
the circuit may contain.
"""
from cirq.circuits import CircuitOperation

if not all(
self.next_moment_operating_on(op.qubits, i + 1) is None
for (i, op) in self.findall_operations(predicate)
if _get_op_circuit(op) is None
if not isinstance(op.untagged, CircuitOperation)
):
return False

for i, moment in enumerate(self.moments):
for op in moment.operations:
circuit = _get_op_circuit(op)
circuit = getattr(op.untagged, 'circuit', None)
if circuit is None:
continue
if not circuit.are_all_matches_terminal(predicate):
Expand Down Expand Up @@ -813,16 +815,18 @@ def are_any_matches_terminal(self, predicate: Callable[['cirq.Operation'], bool]
given predicate are terminal. Also checks within any CircuitGates
the circuit may contain.
"""
from cirq.circuits import CircuitOperation

if any(
self.next_moment_operating_on(op.qubits, i + 1) is None
for (i, op) in self.findall_operations(predicate)
if _get_op_circuit(op) is None
if not isinstance(op.untagged, CircuitOperation)
):
return True

for i, moment in reversed(list(enumerate(self.moments))):
for op in moment.operations:
circuit = _get_op_circuit(op)
circuit = getattr(op.untagged, 'circuit', None)
if circuit is None:
continue
if not circuit.are_any_matches_terminal(predicate):
Expand Down Expand Up @@ -2201,15 +2205,6 @@ def with_noise(self, noise: 'cirq.NOISE_MODEL_LIKE') -> 'cirq.Circuit':
return c_noisy


def _get_op_circuit(op: ops.Operation) -> Optional['cirq.FrozenCircuit']:
"""Retrieves the circuit contained by an operation, if there is one."""
from cirq.circuits import CircuitOperation

while isinstance(op, ops.TaggedOperation):
op = op.sub_operation
return op.circuit if isinstance(op, CircuitOperation) else None


def _resolve_operations(
operations: Iterable['cirq.Operation'], param_resolver: 'cirq.ParamResolver', recursive: bool
) -> List['cirq.Operation']:
Expand Down

0 comments on commit d4f3fc9

Please sign in to comment.