Skip to content

Commit

Permalink
Unroll circuit_op when validating containment in a gateset (quantumli…
Browse files Browse the repository at this point in the history
…b#4770)

Fully unroll a circuit operation using `op.mapped_circuit(deep=True)` when checking containment of underlying operations in a gateset. This correctly handles circuit ops with negative reps, which lead to inverting all operations in the underlying circuit.  

Fixes quantumlib#4727.
  • Loading branch information
tanujkhattar authored and MichaelBroughton committed Jan 22, 2022
1 parent 82e6fed commit af4016a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
8 changes: 1 addition & 7 deletions cirq-core/cirq/ops/gateset.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,7 @@ def _validate_operation(self, op: raw_types.Operation) -> bool:
if isinstance(op, raw_types.TaggedOperation):
return self._validate_operation(op.sub_operation)
elif isinstance(op, circuit_operation.CircuitOperation) and self._unroll_circuit_op:
op_circuit = protocols.resolve_parameters(
op.circuit.unfreeze(), op.param_resolver, recursive=False
)
op_circuit = op_circuit.transform_qubits(
lambda q: cast(circuit_operation.CircuitOperation, op).qubit_map.get(q, q)
)
return self.validate(op_circuit)
return self.validate(op.mapped_circuit(deep=True))
else:
return False

Expand Down
7 changes: 7 additions & 0 deletions cirq-core/cirq/ops/gateset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ def assert_validate_and_contains_consistent(gateset, op_tree, result):
)


def test_gateset_validate_circuit_op_negative_reps():
gate = CustomXPowGate(exponent=0.5)
op = cirq.CircuitOperation(cirq.FrozenCircuit(gate.on(cirq.LineQubit(0))), repetitions=-1)
assert op not in cirq.Gateset(gate)
assert op ** -1 in cirq.Gateset(gate)


def test_with_params():
assert gateset.with_params() is gateset
assert (
Expand Down

0 comments on commit af4016a

Please sign in to comment.