Skip to content

Commit

Permalink
Fix process-based non-determinism in SparsePauliOp.to_matrix
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jakelishman committed Nov 14, 2024
1 parent d81abce commit 2930f43
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/accelerate/src/sparse_pauli_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use numpy::prelude::*;
use numpy::{PyArray1, PyArray2, PyReadonlyArray1, PyReadonlyArray2, PyUntypedArrayMethods};

use hashbrown::HashMap;
use indexmap::IndexMap;
use ndarray::{s, ArrayView1, ArrayView2, Axis};
use num_complex::Complex64;
use num_traits::Zero;
Expand Down Expand Up @@ -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());
for (key, coeff) in self
.x_like
.drain(..)
Expand Down

0 comments on commit 2930f43

Please sign in to comment.