Skip to content

Commit 64a6723

Browse files
authored
Add support for deep=True to cirq.eject_z transformer (#5115)
- Adds support to recursively run `cirq.eject_z` transformer on circuits wrapped inside a circuit operation by setting deep=True in transformer context. - Part of #5039
1 parent 04d58c4 commit 64a6723

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

cirq-core/cirq/transformers/eject_z.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _is_swaplike(gate: 'cirq.Gate'):
4343
return False
4444

4545

46-
@transformer_api.transformer
46+
@transformer_api.transformer(add_deep_support=True)
4747
def eject_z(
4848
circuit: 'cirq.AbstractCircuit',
4949
*,
@@ -96,7 +96,7 @@ def map_func(op: 'cirq.Operation', moment_index: int) -> 'cirq.OP_TREE':
9696
gate = op.gate
9797
# Return if circuit operation.
9898
if gate is None:
99-
return op
99+
return [dump_tracked_phase(op.qubits), op]
100100

101101
# Swap phases if `op` is a swap operation.
102102
if _is_swaplike(gate):

cirq-core/cirq/transformers/eject_z_test.py

+30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
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+
15+
import dataclasses
16+
1417
import pytest
1518
import numpy as np
1619
import sympy
@@ -39,6 +42,33 @@ def assert_optimizes(
3942
circuit = cirq.eject_z(before, eject_parameterized=eject_parameterized, context=context)
4043
cirq.testing.assert_same_circuits(circuit, expected)
4144

45+
# Nested sub-circuits should also get optimized.
46+
q = before.all_qubits()
47+
c_nested = cirq.Circuit(
48+
[(cirq.Z ** 0.5).on_each(*q), (cirq.Y ** 0.25).on_each(*q)],
49+
cirq.Moment(cirq.CircuitOperation(before.freeze()).repeat(2).with_tags("ignore")),
50+
[(cirq.Z ** 0.5).on_each(*q), (cirq.Y ** 0.25).on_each(*q)],
51+
cirq.Moment(cirq.CircuitOperation(before.freeze()).repeat(3).with_tags("preserve_tag")),
52+
)
53+
c_expected = cirq.Circuit(
54+
cirq.PhasedXPowGate(phase_exponent=0, exponent=0.25).on_each(*q),
55+
(cirq.Z ** 0.5).on_each(*q),
56+
cirq.Moment(cirq.CircuitOperation(before.freeze()).repeat(2).with_tags("ignore")),
57+
cirq.PhasedXPowGate(phase_exponent=0, exponent=0.25).on_each(*q),
58+
(cirq.Z ** 0.5).on_each(*q),
59+
cirq.Moment(cirq.CircuitOperation(expected.freeze()).repeat(3).with_tags("preserve_tag")),
60+
)
61+
if context is None:
62+
context = cirq.TransformerContext(tags_to_ignore=("ignore",), deep=True)
63+
else:
64+
context = dataclasses.replace(
65+
context, tags_to_ignore=context.tags_to_ignore + ("ignore",), deep=True
66+
)
67+
c_nested = cirq.eject_z(c_nested, context=context, eject_parameterized=eject_parameterized)
68+
cirq.testing.assert_same_circuits(c_nested, c_expected)
69+
c_nested = cirq.eject_z(c_nested, context=context, eject_parameterized=eject_parameterized)
70+
cirq.testing.assert_same_circuits(c_nested, c_expected)
71+
4272

4373
def assert_removes_all_z_gates(circuit: cirq.Circuit, eject_parameterized: bool = True):
4474
optimized = cirq.eject_z(circuit, eject_parameterized=eject_parameterized)

0 commit comments

Comments
 (0)