File tree Expand file tree Collapse file tree 2 files changed +16
-2
lines changed
cirq-core/cirq/transformers/analytical_decompositions Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -226,12 +226,15 @@ def _merge_single_qubit_gates(
226
226
transformation.
227
227
Returns:
228
228
new sequence of operations after merging gates
229
+
230
+ Raises:
231
+ ValueError: if one of the operations is not on 1 or 2 qubits
229
232
"""
230
233
merged_ops : list [ops .Operation ] = []
231
234
pending_ops : dict [tuple [cirq .Qid , ...], list [ops .Operation ]] = dict ()
232
235
for op in operations :
233
236
if protocols .num_qubits (op ) == 2 :
234
- for _ , qubit_ops in pending_ops .items ():
237
+ for qubit_ops in pending_ops .values ():
235
238
merged_ops .extend (
236
239
_transform_single_qubit_operations_to_phased_x_and_z (qubit_ops , atol = atol )
237
240
)
@@ -242,8 +245,10 @@ def _merge_single_qubit_gates(
242
245
if op .qubits not in pending_ops :
243
246
pending_ops [op .qubits ] = []
244
247
pending_ops [op .qubits ].append (op )
248
+ else :
249
+ raise ValueError (f'operation is on { protocols .num_qubits (op )} qubits, expected 1 or 2' )
245
250
# Merge remaining pending operations
246
- for _ , qubit_ops in pending_ops .items ():
251
+ for qubit_ops in pending_ops .values ():
247
252
merged_ops .extend (
248
253
_transform_single_qubit_operations_to_phased_x_and_z (qubit_ops , atol = atol )
249
254
)
Original file line number Diff line number Diff line change 26
26
from cirq .transformers .analytical_decompositions .two_qubit_to_cz import (
27
27
_is_trivial_angle ,
28
28
_parity_interaction ,
29
+ cleanup_operations ,
29
30
two_qubit_matrix_to_diagonal_and_cz_operations ,
30
31
)
31
32
@@ -294,3 +295,11 @@ def test_remove_partial_czs_or_fail() -> None:
294
295
_ = cirq .transformers .analytical_decompositions .two_qubit_to_cz ._remove_partial_czs_or_fail (
295
296
[CZ ** - 0.5 ], atol = 1e-9
296
297
)
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 )
You can’t perform that action at this time.
0 commit comments