diff --git a/qiskit/transpiler/passes/routing/sabre_swap.py b/qiskit/transpiler/passes/routing/sabre_swap.py index 8f1bdf4b676b..bb5c66fafe82 100644 --- a/qiskit/transpiler/passes/routing/sabre_swap.py +++ b/qiskit/transpiler/passes/routing/sabre_swap.py @@ -15,7 +15,6 @@ import logging from copy import copy, deepcopy -import numpy as np import retworkx from qiskit.circuit.library.standard_gates import SwapGate @@ -150,12 +149,7 @@ def __init__(self, coupling_map, heuristic="basic", seed=None, fake_run=False, t self._neighbor_table = NeighborTable(retworkx.adjacency_matrix(self.coupling_map.graph)) self.heuristic = heuristic - - if seed is None: - ii32 = np.iinfo(np.int32) - self.seed = np.random.default_rng(None).integers(0, ii32.max, dtype=int) - else: - self.seed = seed + self.seed = seed if trials is None: self.trials = CPU_COUNT else: diff --git a/releasenotes/notes/fix-sabre-swap-random-seed-dcf3dace63042791.yaml b/releasenotes/notes/fix-sabre-swap-random-seed-dcf3dace63042791.yaml new file mode 100644 index 000000000000..ed0fd2e4ad43 --- /dev/null +++ b/releasenotes/notes/fix-sabre-swap-random-seed-dcf3dace63042791.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fixed an issue with the :class:`~.SabreSwap` pass which would cause the + output of multiple runs of the pass without the ``seed`` argument specified + to reuse the same random number generator seed between runs instead of + using different seeds. This previously caused identical results to be + returned between runs even when no ``seed`` was specified. diff --git a/src/sabre_swap/mod.rs b/src/sabre_swap/mod.rs index a66611982e4d..a6fb51cdc1f5 100644 --- a/src/sabre_swap/mod.rs +++ b/src/sabre_swap/mod.rs @@ -152,14 +152,17 @@ pub fn build_swap_map( neighbor_table: &NeighborTable, distance_matrix: PyReadonlyArray2, heuristic: &Heuristic, - seed: u64, + seed: Option, layout: &mut NLayout, num_trials: usize, ) -> (SwapMap, PyObject) { let run_in_parallel = getenv_use_multiple_threads() && num_trials > 1; let dist = distance_matrix.as_array(); let coupling_graph: DiGraph<(), ()> = cmap_from_neighor_table(neighbor_table); - let outer_rng = Pcg64Mcg::seed_from_u64(seed); + let outer_rng = match seed { + Some(seed) => Pcg64Mcg::seed_from_u64(seed), + None => Pcg64Mcg::from_entropy(), + }; let seed_vec: Vec = outer_rng .sample_iter(&rand::distributions::Standard) .take(num_trials)