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

Avoid state vector normalization if it worsens the round offs #6556

Conversation

pavoljuhas
Copy link
Collaborator

This fixes failure of

check/pytest -n0 cirq-core/cirq/circuits/circuit_test.py::test_final_state_vector

which happened because normalization of a state vector at np.complex64
precision can subtly increase the overall round-off error.

Follow up to #6522 and #6402

This fixes failure of

check/pytest -n0 cirq-core/cirq/circuits/circuit_test.py::test_final_state_vector

which happened because normalization of a state vector at `np.complex64`
precision can subtly increase the overall round-off error.

Follow up to quantumlib#6522 and quantumlib#6402
@pavoljuhas pavoljuhas requested review from vtomole, cduck and a team as code owners April 9, 2024 23:37
@pavoljuhas pavoljuhas requested a review from viathor April 9, 2024 23:37
@pavoljuhas
Copy link
Collaborator Author

I have got consistent failures for the cirq-core/cirq/circuits/circuit_test.py::test_final_state_vector
test on my Debian-based desktop which were bisected to #6522.
For some reason these do not show in CI runs, but it is preferable to
have passing tests in local environments too.

Copy link

codecov bot commented Apr 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.79%. Comparing base (1bec6b5) to head (43cb287).
Report is 1 commits behind head on main.

❗ Current head 43cb287 differs from pull request most recent head 754f40e. Consider uploading reports for the commit 754f40e to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6556   +/-   ##
=======================================
  Coverage   97.79%   97.79%           
=======================================
  Files        1124     1124           
  Lines       95486    95489    +3     
=======================================
+ Hits        93376    93380    +4     
+ Misses       2110     2109    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a comment

Choose a reason for hiding this comment

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

maybe we should remove it normalization then?

@pavoljuhas
Copy link
Collaborator Author

maybe we should remove it normalization then?

The normalization does help for your original round-off example in #6402.
I think the case where it worsen the outcome is for state vectors with few
nonzero elements. Here is how test_final_state_vector fails on my
desktop (the outcome may be different for other OS or CPU versions):

>>> x = np.exp(1j * np.pi/4, dtype=np.complex64)
>>> xn = x / np.linalg.norm(x)
>>> abs(x * x.conj() - 1)
5.960464477539063e-08
>>> abs(xn * xn.conj() - 1)
1.1920928955078125e-07

where the latter value for normalized xn is above the test tolerance.

With this PR the test passes and the round-off is better for #6402.

@pavoljuhas pavoljuhas enabled auto-merge (squash) April 10, 2024 01:18
@pavoljuhas pavoljuhas merged commit d16b12c into quantumlib:main Apr 10, 2024
29 checks passed
@pavoljuhas pavoljuhas deleted the avoid-extra-round-offs-from-normalization branch April 11, 2024 00:19
jselig-rigetti pushed a commit to jselig-rigetti/Cirq that referenced this pull request May 28, 2024
…mlib#6556)

This fixes failure of

check/pytest -n0 cirq-core/cirq/circuits/circuit_test.py::test_final_state_vector

which happened because normalization of a state vector at `np.complex64`
precision can subtly increase the overall round-off error.

Follow up to quantumlib#6522 and quantumlib#6402
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…mlib#6556)

This fixes failure of

check/pytest -n0 cirq-core/cirq/circuits/circuit_test.py::test_final_state_vector

which happened because normalization of a state vector at `np.complex64`
precision can subtly increase the overall round-off error.

Follow up to quantumlib#6522 and quantumlib#6402
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants