Skip to content

Commit

Permalink
Separate out type-safe enforcement in NeighborTable to another commit
Browse files Browse the repository at this point in the history
This reverts the changes to the coupling-map generation and the indexing
into `NeighborTable` that were designed to make it more type safe in
favour of doing that in a separate commit.
  • Loading branch information
jakelishman committed Sep 5, 2023
1 parent 6c16345 commit 8018716
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
22 changes: 16 additions & 6 deletions crates/accelerate/src/sabre_swap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ fn obtain_swaps<'a>(
layout: &'a NLayout,
) -> impl Iterator<Item = [VirtualQubit; 2]> + '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
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(),
Expand Down
22 changes: 1 addition & 21 deletions crates/accelerate/src/sabre_swap/neighbor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,26 +31,7 @@ use crate::nlayout::PhysicalQubit;
#[pyclass(module = "qiskit._accelerate.sabre_swap")]
#[derive(Clone, Debug)]
pub struct NeighborTable {
neighbors: Vec<Vec<PhysicalQubit>>,
}

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<PhysicalQubit> for NeighborTable {
type Output = [PhysicalQubit];

fn index(&self, index: PhysicalQubit) -> &Self::Output {
&self.neighbors[index.index()]
}
pub neighbors: Vec<Vec<PhysicalQubit>>,
}

#[pymethods]
Expand Down

0 comments on commit 8018716

Please sign in to comment.