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

Ensure the result of simulation is normalized #6522

Merged
merged 6 commits into from
Apr 3, 2024

Conversation

NoureldinYosri
Copy link
Collaborator

@NoureldinYosri NoureldinYosri commented Mar 22, 2024

@NoureldinYosri NoureldinYosri requested review from vtomole, cduck and a team as code owners March 22, 2024 23:08
@NoureldinYosri NoureldinYosri requested a review from dabacon March 22, 2024 23:08
Copy link

codecov bot commented Mar 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.78%. Comparing base (f955650) to head (7856e81).
Report is 1 commits behind head on main.

❗ Current head 7856e81 differs from pull request most recent head 9aac89e. Consider uploading reports for the commit 9aac89e to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6522      +/-   ##
==========================================
- Coverage   97.78%   97.78%   -0.01%     
==========================================
  Files        1107     1105       -2     
  Lines       95182    95113      -69     
==========================================
- Hits        93073    93004      -69     
  Misses       2109     2109              

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

@daxfohl
Copy link
Contributor

daxfohl commented Mar 23, 2024

I considered doing this in the past when the earlier issue came up, but was concerned it was like sweeping dirt under the rug. Does doing this then make other invariants start failing? I think it's worth looking at what other projects do here, and try to follow prior art on the subject.

@NoureldinYosri
Copy link
Collaborator Author

@daxfohl for state vector there is not an issue

for density matrix it doesn't work (I reverted my changes and restricted this PR to state vector simulation). the invariant of density matrix is that $Tr[\rho^2] \leq 1$ with equality if and only if $\rho$ is pure. I don't think we should implicitly add the computation $Tr[\rho^2]$

Comment on lines 127 to 128
ret = self._get_merged_sim_state().target_tensor.reshape(-1)
return ret / np.linalg.norm(ret)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we raise UserWarning if abs(norm(state_vector) - 1) > np.sqrt(np.finfo(state_vector.dtype).eps)?

That should be sufficient for an accumulated round off from ~1/eps operations, but give some warning in case the state vector would be widely off.

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.

I think we should have some check here so we don't re-normalize incorrect state vector.

@NoureldinYosri NoureldinYosri enabled auto-merge (squash) April 2, 2024 23:53
@NoureldinYosri NoureldinYosri merged commit 45c5fa3 into main Apr 3, 2024
32 checks passed
pavoljuhas added a commit to pavoljuhas/Cirq that referenced this pull request Apr 9, 2024
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 added a commit that referenced this pull request Apr 10, 2024
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
jselig-rigetti pushed a commit to jselig-rigetti/Cirq that referenced this pull request May 28, 2024
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
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.

3 participants