Skip to content

Commit 3fe3438

Browse files
authored
Speed up execution time of merge_single_qubit_moments_to_phxz transformer by avoiding redundant calls to unitary protocol (#6174)
1 parent d98be50 commit 3fe3438

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

cirq-core/cirq/transformers/merge_single_qubit_gates.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,23 @@ def merge_func(m1: 'cirq.Moment', m2: 'cirq.Moment') -> Optional['cirq.Moment']:
127127
return None
128128
ret_ops = []
129129
for q in m1.qubits | m2.qubits:
130-
mat = protocols.unitary(circuits.Circuit(m.operation_at(q) or [] for m in [m1, m2]))
131-
gate = single_qubit_decompositions.single_qubit_matrix_to_phxz(mat, atol)
132-
if gate:
133-
ret_ops.append(gate(q))
130+
op1, op2 = m1.operation_at(q), m2.operation_at(q)
131+
if op1 and op2:
132+
mat = protocols.unitary(op2) @ protocols.unitary(op1)
133+
gate = single_qubit_decompositions.single_qubit_matrix_to_phxz(mat, atol)
134+
if gate:
135+
ret_ops.append(gate(q))
136+
else:
137+
op = op1 or op2
138+
assert op is not None
139+
if isinstance(op.gate, ops.PhasedXZGate):
140+
ret_ops.append(op)
141+
else:
142+
gate = single_qubit_decompositions.single_qubit_matrix_to_phxz(
143+
protocols.unitary(op), atol
144+
)
145+
if gate:
146+
ret_ops.append(gate(q))
134147
return circuits.Moment(ret_ops)
135148

136149
return transformer_primitives.merge_moments(

0 commit comments

Comments
 (0)