Skip to content

Bring canonicalization of FSimGate and PhasedFSimGate angles b/w [-pi, pi) #4576

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

Merged
merged 4 commits into from
Oct 16, 2021

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Oct 14, 2021

Fixes #4574

Breaking Change: FSimGate and PhasedFSimGate's angles would now be canonicalized between [-pi, pi) instead of [-pi, pi]. This means for cases where angles where pi, the diagrams will now change (now print -pi instead of pi), and value equality would now return true when compared with angles equal to -pi.

@tanujkhattar tanujkhattar requested review from cduck, vtomole and a team as code owners October 14, 2021 03:21
@tanujkhattar tanujkhattar requested a review from viathor October 14, 2021 03:21
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Oct 14, 2021
@CirqBot CirqBot added the size: S 10< lines changed <50 label Oct 14, 2021
@tanujkhattar tanujkhattar added the BREAKING CHANGE For pull requests that are important to mention in release notes. label Oct 14, 2021
if protocols.is_parameterized(value):
return value
period = 2 * np.pi
return value - period * np.round(value / period)
numerator = value + np.pi - 1e-10
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not be introducing numerical error. Can you think of a better way of canonicalizing into a half-open interval?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I realized that subtracting the constant is not actually necessary. Modified the code to remove the numerical error and bring the canonicalization in [pi, pi).

This reason this works is because (x + pi) % (2 * pi) - pi will bring everything between [-pi, pi) and I used division and floor instead of built-in modulo for better accuracy and lower error.

@tanujkhattar tanujkhattar changed the title Bring canonicalization of FSimGate and PhasedFSimGate angles b/w (-pi, pi] Bring canonicalization of FSimGate and PhasedFSimGate angles b/w [-pi, pi) Oct 16, 2021
@tanujkhattar tanujkhattar requested a review from viathor October 16, 2021 00:58
"""Returns True iff param, assumed to be in [-pi, pi], is 0 (mod pi)."""
return param in (-np.pi, 0.0, np.pi, -sympy.pi, sympy.pi)
"""Returns True iff param, assumed to be in [-pi, pi), is 0 (mod pi)."""
return param in (0.0, -np.pi, -sympy.pi)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: We could add an assert to verify that param is in [-pi, pi). Now that the result of these calculations will be used for equality checks, it's probably better to have the code crash and signal a bug than silently pretend equal things are different.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The equality checks are done by directly comparing the self.theta etc. angles, and these functions are also called only with the class angles, which are always canonicalized. I could add an assert to the _canonicalize method which ensures that the return value is between [-pi, pi), but the tests already ensure that the method works as expected so I don't think the assert is needed.

@tanujkhattar tanujkhattar merged commit 3326f36 into quantumlib:master Oct 16, 2021
@tanujkhattar tanujkhattar deleted the fsim_canonicalization branch October 16, 2021 03:47
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…, pi) (quantumlib#4576)

* Bring canonicalization of fsim/phasedfsim angles b/w [-pi, pi)

* Get rid of numerical error in fsim angles canonicalization
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…, pi) (quantumlib#4576)

* Bring canonicalization of fsim/phasedfsim angles b/w [-pi, pi)

* Get rid of numerical error in fsim angles canonicalization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE For pull requests that are important to mention in release notes. cla: yes Makes googlebot stop complaining. size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FSim and PhasedFSim angles should be canonicalized between (-pi, pi] / [-pi, pi) instead of [-pi, pi]
3 participants