diff --git a/crates/accelerate/src/dense_layout.rs b/crates/accelerate/src/dense_layout.rs index a7466748417a..cbe8b9ff8cc7 100644 --- a/crates/accelerate/src/dense_layout.rs +++ b/crates/accelerate/src/dense_layout.rs @@ -30,6 +30,7 @@ struct SubsetResult { pub error: f64, pub map: Vec, pub subgraph: Vec<[usize; 2]>, + pub index: usize, } fn bfs_sort(adj_matrix: ArrayView2, start: usize, num_qubits: usize) -> Vec { @@ -190,6 +191,7 @@ pub fn best_subset_inner( error, map: bfs, subgraph, + index: k, } }; @@ -199,17 +201,20 @@ pub fn best_subset_inner( map: Vec::new(), error: f64::INFINITY, subgraph: Vec::new(), + index: usize::MAX, } }; let reduce_fn = |best: SubsetResult, curr: SubsetResult| -> SubsetResult { if use_error { - if curr.count >= best.count && curr.error < best.error { + if (curr.count >= best.count && curr.error < best.error) + || (curr.count == best.count && curr.error == best.error && curr.index < best.index) + { curr } else { best } - } else if curr.count > best.count { + } else if curr.count > best.count || (curr.count == best.count && curr.index < best.index) { curr } else { best diff --git a/releasenotes/notes/fix-potential-non-determinism-dense-layout-da66de0217121146.yaml b/releasenotes/notes/fix-potential-non-determinism-dense-layout-da66de0217121146.yaml new file mode 100644 index 000000000000..ddf68fc9d49b --- /dev/null +++ b/releasenotes/notes/fix-potential-non-determinism-dense-layout-da66de0217121146.yaml @@ -0,0 +1,9 @@ +--- +fixes: + - | + Fixed a potential source of non-determinism in :class:`~.DenseLayout` (and + by extension :class:`~.SabreLayout`) when targeting a + :class:`.CouplingMap` or :class:`.Target` that has more than one subgraph + with the same degree of connectivity. In these case the exact output + layout from the pass could previously fluctuate based on the number of + local CPUs and thread execution speed.