Skip to content

Commit

Permalink
Expose internal rust interface to DenseLayout (Qiskit#12104)
Browse files Browse the repository at this point in the history
* Expose internal rust interface to DenseLayout

This commit makes a small change to the rust code for DenseLayout that
enables calling it more easily from rust. The primary obstacle was the
pyfunction used PyReadonlyArray2<f64> inputs which precludes calling it
with rust constructed Array2Views<f64>. This adds a new inner public
function which takes the array view directly and then the pyfunction's
only job is to convert the inputs and outputs to Python. The python side
of the function is still building a sparse matrix and then runs reverse
Cuthill–McKee to get a permutation of the densest subgraph so any rust
consumers will want to keep that in mind (and maybe use sprs to do the
same).

At the same time it corrects an oversight in the original implementation
where the returned numpy arrays of the densest subgraph are copied
instead of being returned as references. This should slightly improve
performance by eliminating 3 array copies that weren't needed.

* Remove PyResult

---------

Co-authored-by: Henry Zou <87874865+henryzou50@users.noreply.github.com>
  • Loading branch information
2 people authored and ElePT committed May 31, 2024
1 parent 62253f4 commit e14fd8c
Showing 1 changed file with 29 additions and 8 deletions.
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

0 comments on commit e14fd8c

Please sign in to comment.