Skip to content

Fix global phase computation in matrix gate decomposition #7482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cirq-core/cirq/ops/matrix_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from cirq import _import, linalg, protocols
from cirq._compat import proper_repr
from cirq.ops import global_phase_op, identity, phased_x_z_gate, raw_types
from cirq.ops import global_phase_op, phased_x_z_gate, raw_types

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -170,8 +170,8 @@ def _decompose_(self, qubits: tuple[cirq.Qid, ...]) -> cirq.OP_TREE:
return NotImplemented
# The above algorithms ignore phase, but phase is important to maintain if the gate is
# controlled. Here, we add it back in with a global phase op.
ident = identity.IdentityGate(qid_shape=self._qid_shape).on(*qubits) # Preserve qid order
u = protocols.unitary(Circuit(ident, *decomposed)).reshape(self._matrix.shape)
circuit = Circuit(*decomposed)
u = circuit.unitary(qubit_order=qubits, qubits_that_should_be_present=qubits)
phase_delta = linalg.phase_delta(u, self._matrix)
# Phase delta is on the complex unit circle, so if real(phase_delta) >= 1, that means
# no phase delta. (>1 is rounding error).
Expand Down
13 changes: 13 additions & 0 deletions cirq-core/cirq/ops/matrix_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,16 @@ def test_matrixgate_name_serialization():
gate_after_serialization3 = cirq.read_json(json_text=cirq.to_json(gate3))
assert gate3._name == ''
assert gate_after_serialization3._name == ''


def test_decompose_when_qubits_not_in_ascending_order():
# Previous code for preserving global phase would misorder qubits
q0, q1 = cirq.LineQubit.range(2)
circuit1 = cirq.Circuit()
matrix = cirq.testing.random_unitary(4, random_state=0)
circuit1.append(cirq.MatrixGate(matrix).on(q1, q0))
u1 = cirq.unitary(circuit1)
decomposed = cirq.decompose(circuit1)
circuit2 = cirq.Circuit(decomposed)
u2 = cirq.unitary(circuit2)
np.testing.assert_allclose(u1, u2, atol=1e-14)