Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix random seed initialization between runs in SabreSwap #9127

Merged
merged 2 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions qiskit/transpiler/passes/routing/sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 5 additions & 2 deletions src/sabre_swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,17 @@ pub fn build_swap_map(
neighbor_table: &NeighborTable,
distance_matrix: PyReadonlyArray2<f64>,
heuristic: &Heuristic,
seed: u64,
seed: Option<u64>,
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<u64> = outer_rng
.sample_iter(&rand::distributions::Standard)
.take(num_trials)
Expand Down