Skip to content

Commit 66b0876

Browse files
committed
Address review comments for PR quantumlib#7390
There were two nits that I didn't address in the original PR and I do here. For the for loop calling merged_ops.extend(), I couldn't rewrite it entirely as suggested because mypy complained about type mismatch.
1 parent 5c96d02 commit 66b0876

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

cirq-core/cirq/transformers/analytical_decompositions/two_qubit_to_cz.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,15 @@ def _merge_single_qubit_gates(
226226
transformation.
227227
Returns:
228228
new sequence of operations after merging gates
229+
230+
Raises:
231+
ValueError: if one of the operations is not on 1 or 2 qubits
229232
"""
230233
merged_ops: list[ops.Operation] = []
231234
pending_ops: dict[tuple[cirq.Qid, ...], list[ops.Operation]] = dict()
232235
for op in operations:
233236
if protocols.num_qubits(op) == 2:
234-
for _, qubit_ops in pending_ops.items():
237+
for qubit_ops in pending_ops.values():
235238
merged_ops.extend(
236239
_transform_single_qubit_operations_to_phased_x_and_z(qubit_ops, atol=atol)
237240
)
@@ -242,8 +245,10 @@ def _merge_single_qubit_gates(
242245
if op.qubits not in pending_ops:
243246
pending_ops[op.qubits] = []
244247
pending_ops[op.qubits].append(op)
248+
else:
249+
raise ValueError(f'operation is on {protocols.num_qubits(op)} qubits, expected 1 or 2')
245250
# Merge remaining pending operations
246-
for _, qubit_ops in pending_ops.items():
251+
for qubit_ops in pending_ops.values():
247252
merged_ops.extend(
248253
_transform_single_qubit_operations_to_phased_x_and_z(qubit_ops, atol=atol)
249254
)

cirq-core/cirq/transformers/analytical_decompositions/two_qubit_to_cz_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from cirq.transformers.analytical_decompositions.two_qubit_to_cz import (
2727
_is_trivial_angle,
2828
_parity_interaction,
29+
cleanup_operations,
2930
two_qubit_matrix_to_diagonal_and_cz_operations,
3031
)
3132

@@ -294,3 +295,11 @@ def test_remove_partial_czs_or_fail() -> None:
294295
_ = cirq.transformers.analytical_decompositions.two_qubit_to_cz._remove_partial_czs_or_fail(
295296
[CZ**-0.5], atol=1e-9
296297
)
298+
299+
300+
@pytest.mark.parametrize("gate", [cirq.CCZ, cirq.GlobalPhaseGate(1.0)])
301+
def test_cleanup_operations_raises_if_op_not_on_1_or_2_qubits(gate: cirq.Gate) -> None:
302+
qubits = cirq.LineQubit.range(gate.num_qubits())
303+
op = gate.on(*qubits)
304+
with pytest.raises(ValueError, match="expected 1 or 2"):
305+
cleanup_operations([op], atol=1e-8)

0 commit comments

Comments
 (0)