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

Fix process-based non-determinism in SparsePauliOp.to_matrix #13439

Merged
merged 3 commits into from
Nov 14, 2024

Conversation

jakelishman
Copy link
Member

Summary

The simplification step of the summed Pauli terms in SparsePauliOp.to_matrix had introduced a non-determinism via hash-map iteration. In practice, the base hash seed was set per initialisation of the extension module, then stayed the same. This is hard to detect from within tests, but caused unexpected numerical behaviour on different invocations of the same script.

Details and comments

This fixes the bug part of #13413, but doesn't address the feature request. It should be eligible for backport to either 1.3.0 or 1.3.1, depending on whether there's time to make 1.3.0.

Writing a test that would exercise this would be pretty complex, so I haven't done it. I can if we feel strongly about it - we'd likely embed a little script into a test and spawn some sys.executable instances to run it and check the outputs all matched bit-for-bit.

The simplification step of the summed Pauli terms in
`SparsePauliOp.to_matrix` had introduced a non-determinism via hash-map
iteration.  In practice, the base hash seed was set per initialisation
of the extension module, then stayed the same.  This is hard to detect
from within tests, but caused unexpected numerical behaviour on
different invocations of the same script.
@jakelishman jakelishman added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Nov 14, 2024
@jakelishman jakelishman added this to the 1.3.0 milestone Nov 14, 2024
@jakelishman jakelishman requested a review from a team as a code owner November 14, 2024 13:44
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Nov 14, 2024

Pull Request Test Coverage Report for Build 11840492573

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • 11 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 88.947%

Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
crates/qasm2/src/lex.rs 4 92.48%
crates/qasm2/src/parse.rs 6 97.62%
Totals Coverage Status
Change from base Build 11834348142: 0.02%
Covered Lines: 79411
Relevant Lines: 89279

💛 - Coveralls

@jakelishman jakelishman added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Nov 14, 2024
@@ -298,7 +299,7 @@ impl MatrixCompressedPaulis {
/// explicitly stored operations, if there are duplicates. After the summation, any terms that
/// have become zero are dropped.
pub fn combine(&mut self) {
let mut hash_table = HashMap::<(u64, u64), Complex64>::with_capacity(self.coeffs.len());
let mut hash_table = IndexMap::<(u64, u64), Complex64>::with_capacity(self.coeffs.len());
Copy link
Member

Choose a reason for hiding this comment

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

Do you want to use ahash for the hasher here so the lookup performance is basically the same as HashMap?

Copy link
Member Author

Choose a reason for hiding this comment

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

It should have no meaningful effect on the whole algorithm's performance because this bit is immeasurable compared to the cost of constructing the matrix, but the only reason I didn't do it is because I forgot.

My kids are awake now, so if somebody wanted to push up that change in time to get this into 1.3.0, it's fine by me - I won't have time til tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done in 23ba5f4 🙂

@kevinhartman kevinhartman self-assigned this Nov 14, 2024
Copy link
Contributor

@kevinhartman kevinhartman left a comment

Choose a reason for hiding this comment

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

LGTM. I don't feel strongly that this needs a test given the circumstances.

If anyone disagrees, we can add one in a separate PR.

@kevinhartman kevinhartman added this pull request to the merge queue Nov 14, 2024
Merged via the queue into Qiskit:main with commit 74b32c9 Nov 14, 2024
17 checks passed
mergify bot pushed a commit that referenced this pull request Nov 14, 2024
* Fix process-based non-determinism in `SparsePauliOp.to_matrix`

The simplification step of the summed Pauli terms in
`SparsePauliOp.to_matrix` had introduced a non-determinism via hash-map
iteration.  In practice, the base hash seed was set per initialisation
of the extension module, then stayed the same.  This is hard to detect
from within tests, but caused unexpected numerical behaviour on
different invocations of the same script.

* Use ahash::RandomState

---------

Co-authored-by: Kevin Hartman <[email protected]>
(cherry picked from commit 74b32c9)
@jakelishman jakelishman deleted the spo-to-matrix-determinism branch November 14, 2024 17:50
github-merge-queue bot pushed a commit that referenced this pull request Nov 14, 2024
… (#13440)

* Fix process-based non-determinism in `SparsePauliOp.to_matrix`

The simplification step of the summed Pauli terms in
`SparsePauliOp.to_matrix` had introduced a non-determinism via hash-map
iteration.  In practice, the base hash seed was set per initialisation
of the extension module, then stayed the same.  This is hard to detect
from within tests, but caused unexpected numerical behaviour on
different invocations of the same script.

* Use ahash::RandomState

---------

Co-authored-by: Kevin Hartman <[email protected]>
(cherry picked from commit 74b32c9)

Co-authored-by: Jake Lishman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants