Skip to content

Commit

Permalink
Fix performance regression in UnitarySynthesis
Browse files Browse the repository at this point in the history
This commit fixes a performance regression that was introduced in
PR Qiskit#13141. When the pass is looking up the preferred synthesis direction
for a unitary based on the connectvity constraints the connectivity was
being provided as a PyList. To look up the edge in connectivity set this
meant we needed to iterate over the list and then create a set that rust
could lookup if it contains an edge or it's reverse. This has
significant overhead because its iterating via python and also iterating
per decomposition. This commit addresses this by changing the input type
to be a HashSet from Python so Pyo3 will convert a pyset directly to a
HashSet once at call time and that's used by reference for lookups
directly instead of needing to iterate over the list each time.
  • Loading branch information
mtreinish committed Oct 28, 2024
1 parent 19c5c06 commit e8e5efc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
22 changes: 8 additions & 14 deletions crates/accelerate/src/unitary_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use smallvec::{smallvec, SmallVec};

use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::{IntoPyDict, PyDict, PyList, PyString};
use pyo3::types::{IntoPyDict, PyDict, PyString};
use pyo3::wrap_pyfunction;
use pyo3::Python;

Expand Down Expand Up @@ -225,7 +225,7 @@ fn py_run_main_loop(
qubit_indices: Vec<usize>,
min_qubits: usize,
target: &Target,
coupling_edges: &Bound<'_, PyList>,
coupling_edges: HashSet<[PhysicalQubit; 2]>,
approximation_degree: Option<f64>,
natural_direction: Option<bool>,
) -> PyResult<DAGCircuit> {
Expand Down Expand Up @@ -268,7 +268,7 @@ fn py_run_main_loop(
new_ids,
min_qubits,
target,
coupling_edges,
coupling_edges.clone(),
approximation_degree,
natural_direction,
)?;
Expand Down Expand Up @@ -352,7 +352,7 @@ fn py_run_main_loop(
py,
unitary,
ref_qubits,
coupling_edges,
&coupling_edges,
target,
approximation_degree,
natural_direction,
Expand Down Expand Up @@ -383,7 +383,7 @@ fn run_2q_unitary_synthesis(
py: Python,
unitary: Array2<Complex64>,
ref_qubits: &[PhysicalQubit; 2],
coupling_edges: &Bound<'_, PyList>,
coupling_edges: &HashSet<[PhysicalQubit; 2]>,
target: &Target,
approximation_degree: Option<f64>,
natural_direction: Option<bool>,
Expand Down Expand Up @@ -794,7 +794,7 @@ fn preferred_direction(
decomposer: &DecomposerElement,
ref_qubits: &[PhysicalQubit; 2],
natural_direction: Option<bool>,
coupling_edges: &Bound<'_, PyList>,
coupling_edges: &HashSet<[PhysicalQubit; 2]>,
target: &Target,
) -> PyResult<Option<bool>> {
// Returns:
Expand Down Expand Up @@ -830,14 +830,8 @@ fn preferred_direction(
Some(false) => None,
_ => {
// None or Some(true)
let mut edge_set = HashSet::new();
for item in coupling_edges.iter() {
if let Ok(tuple) = item.extract::<(usize, usize)>() {
edge_set.insert(tuple);
}
}
let zero_one = edge_set.contains(&(qubits[0].0 as usize, qubits[1].0 as usize));
let one_zero = edge_set.contains(&(qubits[1].0 as usize, qubits[0].0 as usize));
let zero_one = coupling_edges.contains(&qubits);
let one_zero = coupling_edges.contains(&[qubits[1], qubits[0]]);

match (zero_one, one_zero) {
(true, false) => Some(true),
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/passes/synthesis/unitary_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:

if self.method == "default" and isinstance(kwargs["target"], Target):
_coupling_edges = (
list(self._coupling_map.get_edges()) if self._coupling_map is not None else []
set(self._coupling_map.get_edges()) if self._coupling_map is not None else set()
)

out = run_default_main_loop(
Expand Down

0 comments on commit e8e5efc

Please sign in to comment.