From b0c416db67fc59bc20df3051f4530f97850055e2 Mon Sep 17 00:00:00 2001 From: Tanuj Khattar Date: Tue, 21 Dec 2021 02:54:48 +0530 Subject: [PATCH] Unroll circuit_op when validating containment in a gateset --- cirq-core/cirq/ops/gateset.py | 8 +------- cirq-core/cirq/ops/gateset_test.py | 7 +++++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cirq-core/cirq/ops/gateset.py b/cirq-core/cirq/ops/gateset.py index 7d0e47d032f..43ef71026ee 100644 --- a/cirq-core/cirq/ops/gateset.py +++ b/cirq-core/cirq/ops/gateset.py @@ -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 diff --git a/cirq-core/cirq/ops/gateset_test.py b/cirq-core/cirq/ops/gateset_test.py index 2ae3b998251..6c1e1a36224 100644 --- a/cirq-core/cirq/ops/gateset_test.py +++ b/cirq-core/cirq/ops/gateset_test.py @@ -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 (