-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix plot_state_qsphere phase anchor bias due to solver noise #15494
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
base: main
Are you sure you want to change the base?
Conversation
|
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the following people are relevant to this code:
|
|
|
|
|
bdff13f to
c7bbe05
Compare
| loc = np.absolute(state).argmax() | ||
| # Rounding to 13 decimals ignores machine epsilon noise (~1e-16) | ||
| # from the solver, ensuring 'argmax' finds the true analytical winner. | ||
| loc = np.round(np.absolute(state), decimals=13).argmax() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the cases with similar eigenvalues, doesn't the order returned by scipy.linalg.eigh also vary? If it does, then rounding afterwards doesn't guarantee a fixed order 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are looking at the magnitude of the elements of a vector, the vector elements are naturally ordered and argmax() returns the index of the first element when there are more than one element equal to the maximum value. So when the magnitudes are equal it will return the lowest position. In terms of adjusting the global phase, this is exactly what we want, if everything else is equal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, these are not the eigenvalues but the vector itself. All good then!
Pull Request Test Coverage Report for Build 20754331594Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
|
It would be great to add a test for this, given that there's currently only a single qsphere test which is displaying the
just using a bell state instead. Generating the reference image can be a bit tricky though, due to slight mismatches in plot package versions. You could still try generating the reference locally, or otherwise download the generated image from the CI run. Let me know if you need help with this. |
Done, I locally tested test_plot_entangled_state_qsphere and it passed the test |
Summary
This PR fixes a visualization artifact in
plot_state_qspherewhere symmetric entangled states (e.g., Bell states) were being arbitrarily rotated due to floating-point noise.The Issue:$\approx 10^{-16}$ ) that often makes the last element slightly larger than the first.
The function uses
scipy.linalg.eighinternally to decompose the input state. For symmetric states (e.g.,|00> - |11>), the solver introduces microscopic noise (The previous logic (
loc = np.absolute(state).argmax()) strictly trusted this noise, causing the visualization to select the "wrong" basis state as the global phase anchor (Phase 0).The Fix:
Added a rounding step (
np.round(..., decimals=13)) before calculatingargmax. This filters out solver noise and ensures that effectively equal amplitudes are treated as a tie, allowingargmaxto default to the canonical first index.Details and comments
qiskit/visualization/state_visualization.py(Line ~951)Fixes #15493