Skip to content

Commit

Permalink
Add new multithreaded TwoQubitPeepholeOptimization pass
Browse files Browse the repository at this point in the history
This commit adds a new transpiler pass for physical optimization,
TwoQubitPeepholeOptimization. This replaces the use of Collect2qBlocks,
ConsolidateBlocks, and UnitarySynthesis in the optimization stage for
a default pass manager setup. The pass logically works the same way
where it analyzes the dag to get a list of 2q runs, calculates the matrix
of each run, and then synthesizes the matrix and substitutes it inplace.
The distinction this pass makes though is it does this all in a single
pass and also parallelizes the matrix calculation and synthesis steps
because there is no data dependency there.

This new pass is not meant to fully replace the Collect2qBlocks,
ConsolidateBlocks, or UnitarySynthesis passes as those also run in
contexts where we don't have a physical circuit. This is meant instead
to replace their usage in the optimization stage only. Accordingly this
new pass also changes the logic on how we select the synthesis to use
and when to make a substituion. Previously this logic was primarily done
via the ConsolidateBlocks pass by only consolidating to a UnitaryGate if
the number of basis gates needed based on the weyl chamber coordinates
was less than the number of 2q gates in the block (see #11659 for
discussion on this). Since this new pass skips the explicit
consolidation stage we go ahead and try all the available synthesizers

Right now this commit has a number of limitations, the largest are:

- Only supports the target
- It doesn't support any synthesizers besides the TwoQubitBasisDecomposer,
  because it's the only one in rust currently.

For plugin handling I left the logic as running the three pass series,
but I'm not sure this is the behavior we want. We could say keep the
synthesis plugins for `UnitarySynthesis` only and then rely on our
built-in methods for physical optimiztion only. But this also seems less
than ideal because the plugin mechanism is how we support synthesizing
to custom basis gates, and also more advanced approximate synthesis
methods. Both of those are things we need to do as part of the synthesis
here.

Additionally, this is currently missing tests and documentation and while
running it manually "works" as in it returns a circuit that looks valid,
I've not done any validation yet. This also likely will need several
rounds of performance optimization and tuning. t this point this is
just a rough proof of concept and will need a lof refinement along with
larger changes to Qiskit's rust code before this is ready to merge.

Fixes #12007
Fixes #11659
  • Loading branch information
mtreinish committed Nov 14, 2024
1 parent b258efc commit 4d160bc
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 25 deletions.
7 changes: 4 additions & 3 deletions crates/accelerate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ license.workspace = true
name = "qiskit_accelerate"
doctest = false


[features]
cache_pygates = ["qiskit-circuit/cache_pygates"]

[dependencies]
rayon.workspace = true
numpy.workspace = true
Expand Down Expand Up @@ -60,6 +64,3 @@ features = ["ndarray"]
[dependencies.pulp]
version = "0.18.22"
features = ["macro"]

[features]
cache_pygates = ["qiskit-circuit/cache_pygates"]
8 changes: 4 additions & 4 deletions crates/accelerate/src/consolidate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub(crate) fn consolidate_blocks(
dag.get_qargs(inst.qubits),
) {
all_block_gates.insert(inst_node);
let matrix = match get_matrix_from_inst(py, inst) {
let matrix = match get_matrix_from_inst(inst) {
Ok(mat) => mat,
Err(_) => continue,
};
Expand Down Expand Up @@ -198,7 +198,7 @@ pub(crate) fn consolidate_blocks(
*block_qargs.iter().min().unwrap(),
*block_qargs.iter().max().unwrap(),
];
let matrix = blocks_to_matrix(py, dag, &block, block_index_map).ok();
let matrix = blocks_to_matrix(dag, &block, block_index_map).ok();
if let Some(matrix) = matrix {
if force_consolidate
|| decomposer.num_basis_gates_inner(matrix.view()) < basis_count
Expand Down Expand Up @@ -252,7 +252,7 @@ pub(crate) fn consolidate_blocks(
first_qubits,
)
{
let matrix = match get_matrix_from_inst(py, first_inst) {
let matrix = match get_matrix_from_inst(first_inst) {
Ok(mat) => mat,
Err(_) => continue,
};
Expand All @@ -272,7 +272,7 @@ pub(crate) fn consolidate_blocks(
already_in_block = true;
}
let gate = dag.dag()[*node].unwrap_operation();
let operator = match get_matrix_from_inst(py, gate) {
let operator = match get_matrix_from_inst(gate) {
Ok(mat) => mat,
Err(_) => {
// Set this to skip this run because we can't compute the matrix of the
Expand Down
21 changes: 11 additions & 10 deletions crates/accelerate/src/convert_2q_block_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,23 @@ use crate::euler_one_qubit_decomposer::matmul_1q;
use crate::QiskitError;

#[inline]
pub fn get_matrix_from_inst(py: Python, inst: &PackedInstruction) -> PyResult<Array2<Complex64>> {
pub fn get_matrix_from_inst(inst: &PackedInstruction) -> PyResult<Array2<Complex64>> {
if let Some(mat) = inst.op.matrix(inst.params_view()) {
Ok(mat)
} else if inst.op.try_standard_gate().is_some() {
Err(QiskitError::new_err(
"Parameterized gates can't be consolidated",
))
} else if let OperationRef::Gate(gate) = inst.op.view() {
Ok(QI_OPERATOR
.get_bound(py)
.call1((gate.gate.clone_ref(py),))?
.getattr(intern!(py, "data"))?
.extract::<PyReadonlyArray2<Complex64>>()?
.as_array()
.to_owned())
Python::with_gil(|py| {
Ok(QI_OPERATOR
.get_bound(py)
.call1((gate.gate.clone_ref(py),))?
.getattr(intern!(py, "data"))?
.extract::<PyReadonlyArray2<Complex64>>()?
.as_array()
.to_owned())
})
} else {
Err(QiskitError::new_err(
"Can't compute matrix of non-unitary op",
Expand All @@ -55,7 +57,6 @@ pub fn get_matrix_from_inst(py: Python, inst: &PackedInstruction) -> PyResult<Ar

/// Return the matrix Operator resulting from a block of Instructions.
pub fn blocks_to_matrix(
py: Python,
dag: &DAGCircuit,
op_list: &[NodeIndex],
block_index_map: [Qubit; 2],
Expand All @@ -73,7 +74,7 @@ pub fn blocks_to_matrix(
let mut output_matrix: Option<Array2<Complex64>> = None;
for node in op_list {
let inst = dag.dag()[*node].unwrap_operation();
let op_matrix = get_matrix_from_inst(py, inst)?;
let op_matrix = get_matrix_from_inst(inst)?;
match dag
.get_qargs(inst.qubits)
.iter()
Expand Down
1 change: 1 addition & 0 deletions crates/accelerate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub mod synthesis;
pub mod target_transpiler;
pub mod twirling;
pub mod two_qubit_decompose;
pub mod two_qubit_peephole;
pub mod uc_gate;
pub mod unitary_synthesis;
pub mod utils;
Expand Down
19 changes: 12 additions & 7 deletions crates/accelerate/src/two_qubit_decompose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,9 +1244,9 @@ type TwoQubitSequenceVec = Vec<(Option<StandardGate>, SmallVec<[f64; 3]>, SmallV
#[derive(Clone, Debug)]
#[pyclass(sequence)]
pub struct TwoQubitGateSequence {
gates: TwoQubitSequenceVec,
pub gates: TwoQubitSequenceVec,
#[pyo3(get)]
global_phase: f64,
pub global_phase: f64,
}

impl TwoQubitGateSequence {
Expand Down Expand Up @@ -1709,7 +1709,7 @@ impl TwoQubitBasisDecomposer {
gate: String,
gate_matrix: ArrayView2<Complex64>,
basis_fidelity: f64,
euler_basis: &str,
euler_basis: EulerBasis,
pulse_optimize: Option<bool>,
) -> PyResult<Self> {
let ipz: ArrayView2<Complex64> = aview2(&IPZ);
Expand Down Expand Up @@ -1817,7 +1817,7 @@ impl TwoQubitBasisDecomposer {
Ok(TwoQubitBasisDecomposer {
gate,
basis_fidelity,
euler_basis: EulerBasis::__new__(euler_basis)?,
euler_basis,
pulse_optimize,
basis_decomposer,
super_controlled,
Expand Down Expand Up @@ -1986,7 +1986,7 @@ impl TwoQubitBasisDecomposer {
gate,
gate_matrix.as_array(),
basis_fidelity,
euler_basis,
EulerBasis::__new__(euler_basis)?,
pulse_optimize,
)
}
Expand Down Expand Up @@ -2284,8 +2284,13 @@ fn two_qubit_decompose_up_to_diagonal(
let (su4, phase) = u4_to_su4(mat_arr);
let mut real_map = real_trace_transform(su4.view());
let mapped_su4 = real_map.dot(&su4.view());
let decomp =
TwoQubitBasisDecomposer::new_inner("cx".to_string(), aview2(&CX_GATE), 1.0, "U", None)?;
let decomp = TwoQubitBasisDecomposer::new_inner(
"cx".to_string(),
aview2(&CX_GATE),
1.0,
EulerBasis::__new__("U")?,
None,
)?;

let circ_seq = decomp.call_inner(mapped_su4.view(), None, true, None)?;
let circ = CircuitData::from_standard_gates(
Expand Down
Loading

0 comments on commit 4d160bc

Please sign in to comment.