Skip to content

Commit 6aa933c

Browse files
authored
Use ahash for IndexMap and IndexSet hasher (Qiskit#12935)
The ahash hasher offers better performace as a drop in replacement for the stdlib hash algorithm. This is a large reason we use the hashbrown crate for our HashMap type in Qiskit (along with rayon support). The indexmap crate doesn't use ahash by default however, so we typically switch to it anywhere we use IndexMap or IndexSet to match the lookup performance of hashbrown in places where we need to preserve insertion order. However, there were a couple of spots where we missed this in the rust code, this commit corrects these oversights.
1 parent a4f28f2 commit 6aa933c

File tree

6 files changed

+11
-4
lines changed

6 files changed

+11
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ndarray = "^0.15.6"
2323
numpy = "0.21.0"
2424
smallvec = "1.13"
2525
thiserror = "1.0"
26+
ahash = "0.8.11"
2627

2728
# Most of the crates don't need the feature `extension-module`, since only `qiskit-pyext` builds an
2829
# actual C extension (the feature disables linking in `libpython`, which is forbidden in Python

crates/accelerate/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ numpy.workspace = true
1515
rand = "0.8"
1616
rand_pcg = "0.3"
1717
rand_distr = "0.4.3"
18-
ahash = "0.8.11"
18+
ahash.workspace = true
1919
num-traits = "0.2"
2020
num-complex.workspace = true
2121
num-bigint.workspace = true

crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// copyright notice, and modified files need to carry a notice indicating
1111
// that they have been altered from the originals.
1212

13+
use ahash::RandomState;
1314
use indexmap::IndexSet;
1415
use ndarray::{s, ArrayView2};
1516
use smallvec::smallvec;
@@ -102,7 +103,7 @@ pub struct GreedyCliffordSynthesis<'a> {
102103
symplectic_matrix: SymplecticMatrix,
103104

104105
/// Unprocessed qubits.
105-
unprocessed_qubits: IndexSet<usize>,
106+
unprocessed_qubits: IndexSet<usize, RandomState>,
106107
}
107108

108109
impl GreedyCliffordSynthesis<'_> {
@@ -121,7 +122,7 @@ impl GreedyCliffordSynthesis<'_> {
121122
smat: tableau.slice(s![.., 0..2 * num_qubits]).to_owned(),
122123
};
123124

124-
let unprocessed_qubits: IndexSet<usize> = (0..num_qubits).collect();
125+
let unprocessed_qubits = (0..num_qubits).collect();
125126

126127
Ok(GreedyCliffordSynthesis {
127128
tableau,

crates/qasm3/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ pyo3.workspace = true
1414
indexmap.workspace = true
1515
hashbrown.workspace = true
1616
oq3_semantics = "0.6.0"
17+
ahash.workspace = true

crates/qasm3/src/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use pyo3::prelude::*;
1414
use pyo3::types::{PySequence, PyString, PyTuple};
1515

16+
use ahash::RandomState;
17+
1618
use hashbrown::HashMap;
1719
use indexmap::IndexMap;
1820

@@ -190,8 +192,9 @@ impl BuilderState {
190192
let qubits = if let Some(asg_qubits) = barrier.qubits().as_ref() {
191193
// We want any deterministic order for easier circuit reproducibility in Python space,
192194
// and to include each seen qubit once. This simply maintains insertion order.
193-
let mut qubits = IndexMap::<*const ::pyo3::ffi::PyObject, Py<PyAny>>::with_capacity(
195+
let mut qubits = IndexMap::<*const ::pyo3::ffi::PyObject, Py<PyAny>, RandomState>::with_capacity_and_hasher(
194196
asg_qubits.len(),
197+
RandomState::default()
195198
);
196199
for qarg in asg_qubits.iter() {
197200
let qarg = expr::expect_gate_operand(qarg)?;

0 commit comments

Comments
 (0)