Skip to content

Commit 29a0af2

Browse files
committed
revive GlobalPhaseGate.is_identity()
1 parent 5f17a95 commit 29a0af2

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

cirq-core/cirq/ops/common_gates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,6 @@ def _extract_phase(
15321532
return NotImplemented
15331533
result = [gate_class(exponent=gate.exponent).on(*qubits)]
15341534
phase_gate = global_phase_op.from_phase_and_exponent(gate.global_shift, gate.exponent)
1535-
if phase_gate.coefficient != 1:
1535+
if not phase_gate.is_identity():
15361536
result.append(phase_gate())
15371537
return result

cirq-core/cirq/ops/global_phase_op.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def _resolve_parameters_(
9292
coefficient = protocols.resolve_parameters(self.coefficient, resolver, recursive)
9393
return GlobalPhaseGate(coefficient=coefficient)
9494

95+
def is_identity(self) -> bool:
96+
return not protocols.is_parameterized(self._coefficient) and np.isclose(self.coefficient, 1)
97+
9598
def controlled(
9699
self,
97100
num_controls: int | None = None,

cirq-core/cirq/ops/global_phase_op_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ def test_global_phase_gate_controlled(coeff, exp) -> None:
307307
)
308308

309309

310+
def test_is_identity() -> None:
311+
g = cirq.GlobalPhaseGate(1)
312+
assert g.is_identity()
313+
g = cirq.GlobalPhaseGate(1j)
314+
assert not g.is_identity()
315+
g = cirq.GlobalPhaseGate(-1)
316+
assert not g.is_identity()
317+
318+
310319
def test_from_phase_and_exponent() -> None:
311320
g = global_phase_op.from_phase_and_exponent(2.5, 0.5)
312321
assert g.coefficient == np.exp(1.25j * np.pi)

cirq-core/cirq/ops/three_qubit_gates.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ def _decompose_(self, qubits):
110110
p = common_gates.T**exp
111111
sweep_abc = [common_gates.CNOT(a, b), common_gates.CNOT(b, c)]
112112
global_phase_gate = global_phase_op.from_phase_and_exponent(self.global_shift, exp)
113-
global_phase_operation = (
114-
[] if np.isclose(global_phase_gate.coefficient, 1) else [global_phase_gate()]
115-
)
113+
global_phase_operation = [] if global_phase_gate.is_identity() else [global_phase_gate()]
116114
return global_phase_operation + [
117115
p(a),
118116
p(b),

0 commit comments

Comments
 (0)