Skip to content

Commit

Permalink
Merge branch 'main' into move-target
Browse files Browse the repository at this point in the history
  • Loading branch information
raynelfss authored May 23, 2024
2 parents ae55a38 + 531f91c commit 6e2742d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
13 changes: 8 additions & 5 deletions CITATION.bib
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@misc{Qiskit,
author = {{Qiskit contributors}},
title = {Qiskit: An Open-source Framework for Quantum Computing},
year = {2023},
doi = {10.5281/zenodo.2573505}
@misc{qiskit2024,
title={Quantum computing with {Q}iskit},
author={Javadi-Abhari, Ali and Treinish, Matthew and Krsulich, Kevin and Wood, Christopher J. and Lishman, Jake and Gacon, Julien and Martiel, Simon and Nation, Paul D. and Bishop, Lev S. and Cross, Andrew W. and Johnson, Blake R. and Gambetta, Jay M.},
year={2024},
doi={10.48550/arXiv.2405.08810},
eprint={2405.08810},
archivePrefix={arXiv},
primaryClass={quant-ph}
}
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/accelerate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ num-complex = "0.4"
num-bigint = "0.4"
rustworkx-core = "0.14"
faer = "0.18.2"
itertools = "0.12.1"
itertools = "0.13.0"
qiskit-circuit.workspace = true

[dependencies.smallvec]
Expand Down
37 changes: 29 additions & 8 deletions crates/accelerate/src/dense_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use ahash::RandomState;
use hashbrown::{HashMap, HashSet};
use indexmap::IndexSet;
use ndarray::prelude::*;
use numpy::IntoPyArray;
use numpy::PyReadonlyArray2;
use numpy::ToPyArray;
use rayon::prelude::*;

use pyo3::prelude::*;
Expand Down Expand Up @@ -108,10 +108,35 @@ pub fn best_subset(
use_error: bool,
symmetric_coupling_map: bool,
error_matrix: PyReadonlyArray2<f64>,
) -> PyResult<(PyObject, PyObject, PyObject)> {
) -> (PyObject, PyObject, PyObject) {
let coupling_adj_mat = coupling_adjacency.as_array();
let coupling_shape = coupling_adj_mat.shape();
let err = error_matrix.as_array();
let [rows, cols, best_map] = best_subset_inner(
num_qubits,
coupling_adj_mat,
num_meas,
num_cx,
use_error,
symmetric_coupling_map,
err,
);
(
rows.into_pyarray_bound(py).into(),
cols.into_pyarray_bound(py).into(),
best_map.into_pyarray_bound(py).into(),
)
}

pub fn best_subset_inner(
num_qubits: usize,
coupling_adj_mat: ArrayView2<f64>,
num_meas: usize,
num_cx: usize,
use_error: bool,
symmetric_coupling_map: bool,
err: ArrayView2<f64>,
) -> [Vec<usize>; 3] {
let coupling_shape = coupling_adj_mat.shape();
let avg_meas_err = err.diag().mean().unwrap();

let map_fn = |k| -> SubsetResult {
Expand Down Expand Up @@ -216,11 +241,7 @@ pub fn best_subset(
let rows: Vec<usize> = new_cmap.iter().map(|edge| edge[0]).collect();
let cols: Vec<usize> = new_cmap.iter().map(|edge| edge[1]).collect();

Ok((
rows.to_pyarray_bound(py).into(),
cols.to_pyarray_bound(py).into(),
best_map.to_pyarray_bound(py).into(),
))
[rows, cols, best_map]
}

#[pymodule]
Expand Down
32 changes: 32 additions & 0 deletions crates/accelerate/src/sabre/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use pyo3::prelude::*;
use pyo3::Python;

use hashbrown::HashSet;
use ndarray::prelude::*;
use numpy::{IntoPyArray, PyArray, PyReadonlyArray2};
use rand::prelude::*;
use rand_pcg::Pcg64Mcg;
Expand All @@ -29,6 +30,8 @@ use super::sabre_dag::SabreDAG;
use super::swap_map::SwapMap;
use super::{Heuristic, NodeBlockResults, SabreResult};

use crate::dense_layout::best_subset_inner;

#[pyfunction]
#[pyo3(signature = (dag, neighbor_table, distance_matrix, heuristic, max_iterations, num_swap_trials, num_random_trials, seed=None, partial_layouts=vec![]))]
pub fn sabre_layout_and_routing(
Expand All @@ -52,6 +55,12 @@ pub fn sabre_layout_and_routing(
let mut starting_layouts: Vec<Vec<Option<u32>>> =
(0..num_random_trials).map(|_| vec![]).collect();
starting_layouts.append(&mut partial_layouts);
// Run a dense layout trial
starting_layouts.push(compute_dense_starting_layout(
dag.num_qubits,
&target,
run_in_parallel,
));
let outer_rng = match seed {
Some(seed) => Pcg64Mcg::seed_from_u64(seed),
None => Pcg64Mcg::from_entropy(),
Expand Down Expand Up @@ -208,3 +217,26 @@ fn layout_trial(
.collect();
(initial_layout, final_permutation, sabre_result)
}

fn compute_dense_starting_layout(
num_qubits: usize,
target: &RoutingTargetView,
run_in_parallel: bool,
) -> Vec<Option<u32>> {
let mut adj_matrix = target.distance.to_owned();
if run_in_parallel {
adj_matrix.par_mapv_inplace(|x| if x == 1. { 1. } else { 0. });
} else {
adj_matrix.mapv_inplace(|x| if x == 1. { 1. } else { 0. });
}
let [_rows, _cols, map] = best_subset_inner(
num_qubits,
adj_matrix.view(),
0,
0,
false,
true,
aview2(&[[0.]]),
);
map.into_iter().map(|x| Some(x as u32)).collect()
}

0 comments on commit 6e2742d

Please sign in to comment.