Skip to content

Commit

Permalink
Use ahash for IndexMap and IndexSet hasher (#12935)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mtreinish authored Aug 9, 2024
1 parent a4f28f2 commit 6aa933c
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ndarray = "^0.15.6"
numpy = "0.21.0"
smallvec = "1.13"
thiserror = "1.0"
ahash = "0.8.11"

# Most of the crates don't need the feature `extension-module`, since only `qiskit-pyext` builds an
# actual C extension (the feature disables linking in `libpython`, which is forbidden in Python
Expand Down
2 changes: 1 addition & 1 deletion crates/accelerate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ numpy.workspace = true
rand = "0.8"
rand_pcg = "0.3"
rand_distr = "0.4.3"
ahash = "0.8.11"
ahash.workspace = true
num-traits = "0.2"
num-complex.workspace = true
num-bigint.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions crates/accelerate/src/synthesis/clifford/greedy_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

use ahash::RandomState;
use indexmap::IndexSet;
use ndarray::{s, ArrayView2};
use smallvec::smallvec;
Expand Down Expand Up @@ -102,7 +103,7 @@ pub struct GreedyCliffordSynthesis<'a> {
symplectic_matrix: SymplecticMatrix,

/// Unprocessed qubits.
unprocessed_qubits: IndexSet<usize>,
unprocessed_qubits: IndexSet<usize, RandomState>,
}

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

let unprocessed_qubits: IndexSet<usize> = (0..num_qubits).collect();
let unprocessed_qubits = (0..num_qubits).collect();

Ok(GreedyCliffordSynthesis {
tableau,
Expand Down
1 change: 1 addition & 0 deletions crates/qasm3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pyo3.workspace = true
indexmap.workspace = true
hashbrown.workspace = true
oq3_semantics = "0.6.0"
ahash.workspace = true
5 changes: 4 additions & 1 deletion crates/qasm3/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use pyo3::prelude::*;
use pyo3::types::{PySequence, PyString, PyTuple};

use ahash::RandomState;

use hashbrown::HashMap;
use indexmap::IndexMap;

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

0 comments on commit 6aa933c

Please sign in to comment.