diff --git a/crates/accelerate/src/sabre_swap/mod.rs b/crates/accelerate/src/sabre_swap/mod.rs index 792313699bfa..3bbc631faded 100644 --- a/crates/accelerate/src/sabre_swap/mod.rs +++ b/crates/accelerate/src/sabre_swap/mod.rs @@ -148,12 +148,12 @@ fn obtain_swaps<'a>( layout: &'a NLayout, ) -> impl Iterator + 'a { front_layer.iter_active().flat_map(move |&v| { - neighbors[v.to_phys(layout)] + neighbors.neighbors[v.to_phys(layout).index()] .iter() - .filter_map(move |p_neighbor| { - let neighbor = p_neighbor.to_virt(layout); - if neighbor > v || !front_layer.is_active(neighbor) { - Some([v, neighbor]) + .filter_map(move |&neighbor| { + let virtual_neighbor = neighbor.to_virt(layout); + if virtual_neighbor > v || !front_layer.is_active(virtual_neighbor) { + Some([v, virtual_neighbor]) } else { None } @@ -196,6 +196,16 @@ fn populate_extended_set( } } +fn cmap_from_neighor_table(neighbor_table: &NeighborTable) -> DiGraph<(), ()> { + DiGraph::<(), ()>::from_edges(neighbor_table.neighbors.iter().enumerate().flat_map( + |(u, targets)| { + targets + .iter() + .map(move |v| (NodeIndex::new(u), NodeIndex::new(v.index()))) + }, + )) +} + /// Run sabre swap on a circuit /// /// Returns: @@ -261,7 +271,7 @@ pub fn build_swap_map_inner( Some(run_in_parallel) => run_in_parallel, None => getenv_use_multiple_threads() && num_trials > 1, }; - let coupling_graph = neighbor_table.coupling_graph(); + let coupling_graph: DiGraph<(), ()> = cmap_from_neighor_table(neighbor_table); let outer_rng = match seed { Some(seed) => Pcg64Mcg::seed_from_u64(seed), None => Pcg64Mcg::from_entropy(), diff --git a/crates/accelerate/src/sabre_swap/neighbor_table.rs b/crates/accelerate/src/sabre_swap/neighbor_table.rs index 6cb44536dc0e..52032d2284cb 100644 --- a/crates/accelerate/src/sabre_swap/neighbor_table.rs +++ b/crates/accelerate/src/sabre_swap/neighbor_table.rs @@ -15,7 +15,6 @@ use ndarray::prelude::*; use numpy::PyReadonlyArray2; use pyo3::prelude::*; use rayon::prelude::*; -use rustworkx_core::petgraph::prelude::*; use crate::nlayout::PhysicalQubit; @@ -32,26 +31,7 @@ use crate::nlayout::PhysicalQubit; #[pyclass(module = "qiskit._accelerate.sabre_swap")] #[derive(Clone, Debug)] pub struct NeighborTable { - neighbors: Vec>, -} - -impl NeighborTable { - /// Regenerate a Rust-space coupling graph from the table. - pub fn coupling_graph(&self) -> DiGraph<(), ()> { - DiGraph::from_edges(self.neighbors.iter().enumerate().flat_map(|(u, targets)| { - targets - .iter() - .map(move |v| (NodeIndex::new(u), NodeIndex::new(v.index()))) - })) - } -} - -impl std::ops::Index for NeighborTable { - type Output = [PhysicalQubit]; - - fn index(&self, index: PhysicalQubit) -> &Self::Output { - &self.neighbors[index.index()] - } + pub neighbors: Vec>, } #[pymethods]