|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
| 14 | + |
| 15 | +import dataclasses |
| 16 | + |
14 | 17 | import pytest
|
15 | 18 | import numpy as np
|
16 | 19 | import sympy
|
@@ -39,6 +42,33 @@ def assert_optimizes(
|
39 | 42 | circuit = cirq.eject_z(before, eject_parameterized=eject_parameterized, context=context)
|
40 | 43 | cirq.testing.assert_same_circuits(circuit, expected)
|
41 | 44 |
|
| 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 | + |
42 | 72 |
|
43 | 73 | def assert_removes_all_z_gates(circuit: cirq.Circuit, eject_parameterized: bool = True):
|
44 | 74 | optimized = cirq.eject_z(circuit, eject_parameterized=eject_parameterized)
|
|
0 commit comments