Skip to content

Commit 65bf29f

Browse files
dstrain115BichengYing
authored andcommitted
Add a default fallback for the equal_up_to_global_phase protocol (quantumlib#6950)
* Add a default fallback for the equal_up_to_global_phase protocol - This will add a default fallback for cirq.Gate instances to use their unitary decomposition for the equal_up_to_global_phase protocol. Fixes: quantumlib#6574 * Add fallback if no unitary exists.
1 parent e808a82 commit 65bf29f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

cirq-core/cirq/ops/raw_types.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,18 @@ def _qid_shape_(self) -> Tuple[int, ...]:
464464
"""
465465
raise NotImplementedError
466466

467+
def _equal_up_to_global_phase_(
468+
self, other: Any, atol: Union[int, float] = 1e-8
469+
) -> Union[NotImplementedType, bool]:
470+
"""Default fallback for gates that do not implement this protocol."""
471+
try:
472+
return protocols.equal_up_to_global_phase(
473+
protocols.unitary(self), protocols.unitary(other), atol=atol
474+
)
475+
except TypeError:
476+
# Gate has no unitary protocol
477+
return NotImplemented
478+
467479
def _commutes_on_qids_(
468480
self, qids: 'Sequence[cirq.Qid]', other: Any, *, atol: float = 1e-8
469481
) -> Union[bool, NotImplementedType, None]:

cirq-core/cirq/protocols/equal_up_to_global_phase_protocol_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,9 @@ def test_equal_up_to_global_phase_eq_supported():
117117
# cast types
118118
assert cirq.equal_up_to_global_phase(A(0.1), A(0.1j), atol=1e-2)
119119
assert not cirq.equal_up_to_global_phase(1e-8j, B(0.0), atol=1e-10)
120+
121+
122+
def test_equal_up_to_global_phase_non_eigen_gates():
123+
gate1 = cirq.PhasedXPowGate(phase_exponent=1.5, exponent=1.0)
124+
gate2 = cirq.PhasedXPowGate(phase_exponent=0.5, exponent=1.0)
125+
assert cirq.equal_up_to_global_phase(gate1, gate2)

0 commit comments

Comments
 (0)