Skip to content
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

Add a default fallback for the equal_up_to_global_phase protocol #6950

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions cirq-core/cirq/ops/raw_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ def _qid_shape_(self) -> Tuple[int, ...]:
"""
raise NotImplementedError

def _equal_up_to_global_phase_(
self, other: Any, atol: Union[int, float] = 1e-8
) -> Union[NotImplementedType, bool]:
"""Default fallback for gates that do not implement this protocol."""
try:
return protocols.equal_up_to_global_phase(
protocols.unitary(self), protocols.unitary(other), atol=atol
)
except TypeError:
# Gate has no unitary protocol
return NotImplemented

def _commutes_on_qids_(
self, qids: 'Sequence[cirq.Qid]', other: Any, *, atol: float = 1e-8
) -> Union[bool, NotImplementedType, None]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,9 @@ def test_equal_up_to_global_phase_eq_supported():
# cast types
assert cirq.equal_up_to_global_phase(A(0.1), A(0.1j), atol=1e-2)
assert not cirq.equal_up_to_global_phase(1e-8j, B(0.0), atol=1e-10)


def test_equal_up_to_global_phase_non_eigen_gates():
gate1 = cirq.PhasedXPowGate(phase_exponent=1.5, exponent=1.0)
gate2 = cirq.PhasedXPowGate(phase_exponent=0.5, exponent=1.0)
assert cirq.equal_up_to_global_phase(gate1, gate2)