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

Conversation

siddharth-mehta
Copy link
Contributor

Resolves Issue: #5980

Adds a modulo operator to the _equal_up_to_global_phase_ function

@siddharth-mehta siddharth-mehta requested review from a team, vtomole and cduck as code owners April 12, 2023 19:15
@CirqBot CirqBot added the Size: XS <10 lines changed label Apr 12, 2023
Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

Please add unit tests and use the near_zero_mod function for exponent comparison.

@@ -388,7 +388,7 @@ def _equal_up_to_global_phase_(self, other, atol):
return False

period = self_without_phase._period()
canonical_diff = (exponents[0] - exponents[1]) % period
canonical_diff = abs((exponents[0] - exponents[1])) % period
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for taking care of this! The comparison seems to be still failing the #5980 (comment) example for

gate1 = cirq.Z
gate2 = cirq.Z ** (3 - 1e-10)

The exponents are compared at a period=2, but the current formula fails for differences just below like 1.99999.

Please use near_zero_mod to compare the exponents instead.

Also please add unit tests for the exponent values where comparison fails before the fix.
I'd suggest to check [1-eps, 1, 1+eps, 2-eps, 2, 2+eps, 3-eps, 3, 3+eps] where the comparison should return True for exponents close to 1 and 3 and False for the exponents near 2.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@siddharth-mehta - seems like this issue went off the radar.

Are you OK with me finishing this off or would you prefer to update the code yourself?

@CirqBot CirqBot added the size: S 10< lines changed <50 label Jun 5, 2023
Copy link
Collaborator

@verult verult left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

(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.

Copy link
Collaborator

@verult verult left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@pavoljuhas pavoljuhas enabled auto-merge (squash) June 5, 2023 22:12
@pavoljuhas pavoljuhas merged commit 49b2cfb into quantumlib:master Jun 6, 2023
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50 Size: XS <10 lines changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants