Skip to content

Commit e0a2688

Browse files
committed
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.
1 parent d81abce commit e0a2688

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

crates/accelerate/src/sparse_pauli_op.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use numpy::prelude::*;
2020
use numpy::{PyArray1, PyArray2, PyReadonlyArray1, PyReadonlyArray2, PyUntypedArrayMethods};
2121

2222
use hashbrown::HashMap;
23+
use indexmap::IndexMap;
2324
use ndarray::{s, ArrayView1, ArrayView2, Axis};
2425
use num_complex::Complex64;
2526
use num_traits::Zero;
@@ -298,7 +299,7 @@ impl MatrixCompressedPaulis {
298299
/// explicitly stored operations, if there are duplicates. After the summation, any terms that
299300
/// have become zero are dropped.
300301
pub fn combine(&mut self) {
301-
let mut hash_table = HashMap::<(u64, u64), Complex64>::with_capacity(self.coeffs.len());
302+
let mut hash_table = IndexMap::<(u64, u64), Complex64>::with_capacity(self.coeffs.len());
302303
for (key, coeff) in self
303304
.x_like
304305
.drain(..)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a per-process based non-determinism in `SparsePauliOp.to_matrix`. The exact order of the
5+
floating-point operations in the summation would previously vary per process, but will now be
6+
identical between different invocations of the same script. See `#13413 <https://github.com/Qiskit/qiskit/issues/13413>`__.

0 commit comments

Comments
 (0)