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 modulo to _equal_up_to_global_phase_ #6058

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions cirq-core/cirq/ops/eigen_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import sympy

from cirq import value, protocols
from cirq.linalg import tolerance
from cirq.ops import raw_types
from cirq.type_workarounds import NotImplementedType

Expand Down Expand Up @@ -385,8 +386,8 @@ def _equal_up_to_global_phase_(self, other, atol):
return False

period = self_without_phase._period()
canonical_diff = (exponents[0] - exponents[1]) % period
return np.isclose(canonical_diff, 0, atol=atol)
exponents_diff = exponents[0] - exponents[1]
return tolerance.near_zero_mod(exponents_diff, period, atol=atol)

def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, ['exponent', 'global_shift'])
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/ops/eigen_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ def _with_exponent(self, exponent):
(cirq.X, cirq.Y, False),
(cirq.rz(np.pi), cirq.Z, True),
(cirq.X**0.3, cirq.Z**0.3, False),
(cirq.Z, cirq.Z ** (1 - 1e-10), True),
(cirq.Z, cirq.Z ** (1 + 1e-10), True),
(cirq.Z**2, cirq.Z ** (2 - 1e-10), True),
(cirq.Z**2, cirq.Z ** (2 + 1e-10), True),
(cirq.Z, cirq.Z ** (3 - 1e-10), True),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Does this test for any behavior different from the 1 - 1e10 case above?

Copy link
Collaborator

@pavoljuhas pavoljuhas Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the critical part is to verify the exponents are recognized as equivalent w/r to a period if their difference is eps-larger or eps-smaller than the period. This was not right in master and not quite right in the first iteration here.
Taking a second look, it would make more sense to compare (Z**2, Z**(4±eps)) so I am going to update it that way.

(cirq.Z, cirq.Z ** (3 + 1e-10), True),
],
)
def test_equal_up_to_global_phase(gate1, gate2, eq_up_to_global_phase):
Expand Down