Skip to content

Commit f72674c

Browse files
committed
move align step after calculating sweeps as that would lead to mismatched circuit structures in parameterizations
1 parent 85586b1 commit f72674c

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

cirq-core/cirq/transformers/merge_single_qubit_gates.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _sweep_on_symbols(sweep: Sweep, symbols: set[sympy.Symbol]) -> Sweep:
179179

180180

181181
def _calc_phxz_sweeps(
182-
symbolized_circuit: cirq.Circuit, resolved_circuits: list[cirq.Circuit]
182+
symbolized_circuit: cirq.AbstractCircuit, resolved_circuits: list[cirq.AbstractCircuit]
183183
) -> Sweep:
184184
"""Return the phxz sweep of the symbolized_circuit on resolved_circuits.
185185
@@ -306,18 +306,16 @@ def merge_single_qubit_gates_to_phxz_symbolized(
306306
]
307307

308308
# Step 2, get the new symbolized circuit by symbolizing on indexed symbolized_single_tag.
309-
new_circuit = align.align_right(
310-
tag_transformers.remove_tags( # remove the temp tags used to track merges
311-
symbolize.symbolize_single_qubit_gates_by_indexed_tags(
312-
tag_transformers.index_tags( # index all 1-qubit-ops merged from ops with symbols
313-
merged_circuits[0],
314-
context=transformer_api.TransformerContext(deep=deep),
315-
target_tags={symbolized_single_tag},
316-
),
317-
symbolize_tag=symbolize.SymbolizeTag(prefix=symbolized_single_tag),
309+
new_circuit = tag_transformers.remove_tags( # remove the temp tags used to track merges
310+
symbolize.symbolize_single_qubit_gates_by_indexed_tags(
311+
tag_transformers.index_tags( # index all 1-qubit-ops merged from ops with symbols
312+
merged_circuits[0],
313+
context=transformer_api.TransformerContext(deep=deep),
314+
target_tags={symbolized_single_tag},
318315
),
319-
remove_if=lambda tag: str(tag).startswith(symbolized_single_tag),
320-
)
316+
symbolize_tag=symbolize.SymbolizeTag(prefix=symbolized_single_tag),
317+
),
318+
remove_if=lambda tag: str(tag).startswith(symbolized_single_tag),
321319
)
322320

323321
# Step 3, get N sets of parameterizations as new_sweep.
@@ -326,4 +324,4 @@ def merge_single_qubit_gates_to_phxz_symbolized(
326324
_sweep_on_symbols(sweep, remaining_symbols), # remaining sweeps
327325
)
328326

329-
return new_circuit, new_sweep
327+
return align.align_right(new_circuit), new_sweep

cirq-core/cirq/transformers/merge_single_qubit_gates_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,29 @@ def test_case1(self):
297297
{q: q for q in input_circuit.all_qubits()},
298298
)
299299

300+
def test_with_gauge_compiling_as_sweep_success(self):
301+
qubits = cirq.LineQubit.range(7)
302+
c = cirq.Circuit(
303+
cirq.Moment(cirq.H(qubits[0]), cirq.H(qubits[3])),
304+
cirq.Moment(cirq.CZ(qubits[0], qubits[2]), cirq.CZ(qubits[3], qubits[5])),
305+
cirq.Moment(cirq.CZ(qubits[0], qubits[1]), cirq.CZ(qubits[3], qubits[4])),
306+
cirq.Moment(cirq.CZ(qubits[1], qubits[3]), cirq.CZ(qubits[4], qubits[6])),
307+
cirq.Moment(cirq.M(*qubits, key='m')),
308+
)
309+
old_circuit, old_sweep = cirq.transformers.gauge_compiling.CZGaugeTransformer.as_sweep(
310+
c, N=50
311+
)
312+
new_circuit, new_sweep = cirq.merge_single_qubit_gates_to_phxz_symbolized(
313+
old_circuit, sweep=old_sweep
314+
)
315+
# Check the unitaries are preserved for each set of sweep paramerization.
316+
for old_resolver, new_resolver in zip(old_sweep, new_sweep):
317+
cirq.testing.assert_circuits_have_same_unitary_given_final_permutation(
318+
cirq.resolve_parameters(old_circuit[0:-1], old_resolver),
319+
cirq.resolve_parameters(new_circuit[0:-1], new_resolver),
320+
{q: q for q in qubits},
321+
)
322+
300323
def test_case_non_parameterized_singles(self):
301324
"""Test merge_single_qubit_gates_to_phxz_symbolized when all single qubit gates are not
302325
parameterized."""

0 commit comments

Comments
 (0)