forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit builds off of Qiskit#13013 and the other data model in Rust infrastructure and migrates the InverseCancellation pass to operate fully in Rust. The full path of the transpiler pass now never leaves Rust until it has finished modifying the DAGCircuit. There is still some python interaction necessary to handle parts of the data model that are still in Python, mainly for creating `UnitaryGate` instances and `ParameterExpression` for global phase. But otherwise the entirety of the pass operates in rust now. This is just a first pass at the migration here, it moves the pass to use loops in rust. The next steps here are to look at operating the pass in parallel. There is no data dependency between the optimizations being done for different gates so we should be able to increase the throughput of the pass by leveraging multithreading to handle each gate in parallel. This commit does not attempt this though, because of the Python dependency and also the data structures around gates and the dag aren't really setup for multithreading yet and there likely will need to be some work to support that. Part of Qiskit#12208
- Loading branch information
Showing
8 changed files
with
152 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// This code is part of Qiskit. | ||
// | ||
// (C) Copyright IBM 2024 | ||
// | ||
// This code is licensed under the Apache License, Version 2.0. You may | ||
// obtain a copy of this license in the LICENSE.txt file in the root directory | ||
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// Any modifications or derivative works of this code must retain this | ||
// copyright notice, and modified files need to carry a notice indicating | ||
// that they have been altered from the originals. | ||
|
||
use pyo3::prelude::*; | ||
use rustworkx_core::petgraph::stable_graph::NodeIndex; | ||
|
||
use qiskit_circuit::circuit_instruction::OperationFromPython; | ||
use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType, Wire}; | ||
use qiskit_circuit::imports::UNITARY_GATE; | ||
use qiskit_circuit::operations::{Operation, Param}; | ||
|
||
use crate::two_qubit_decompose::{Specialization, TwoQubitWeylDecomposition}; | ||
|
||
#[pyfunction] | ||
pub fn split_2q_unitaries( | ||
py: Python, | ||
dag: &mut DAGCircuit, | ||
requested_fidelity: f64, | ||
) -> PyResult<()> { | ||
let nodes: Vec<NodeIndex> = dag.topological_op_nodes()?.collect(); | ||
for node in nodes { | ||
if let NodeType::Operation(inst) = &dag.dag[node] { | ||
let qubits = dag.get_qubits(inst.qubits).to_vec(); | ||
let matrix = inst.op.matrix(inst.params_view()); | ||
if !dag.get_clbits(inst.clbits).is_empty() | ||
|| qubits.len() != 2 | ||
|| matrix.is_none() | ||
|| inst.is_parameterized() | ||
|| inst.condition().is_some() | ||
{ | ||
continue; | ||
} | ||
let decomp = TwoQubitWeylDecomposition::new_inner( | ||
matrix.unwrap().view(), | ||
Some(requested_fidelity), | ||
None, | ||
)?; | ||
if matches!(decomp.specialization, Specialization::IdEquiv) { | ||
let k1r_arr = decomp.K1r(py); | ||
let k1l_arr = decomp.K1l(py); | ||
let k1r_gate = UNITARY_GATE.get_bound(py).call1((k1r_arr,))?; | ||
let k1l_gate = UNITARY_GATE.get_bound(py).call1((k1l_arr,))?; | ||
let insert_fn = |edge: &Wire| -> PyResult<OperationFromPython> { | ||
if let Wire::Qubit(qubit) = edge { | ||
if *qubit == qubits[0] { | ||
k1r_gate.extract() | ||
} else { | ||
k1l_gate.extract() | ||
} | ||
} else { | ||
unreachable!("This will only be called on ops with no classical wires."); | ||
} | ||
}; | ||
dag.replace_on_incoming_qubits(py, node, insert_fn)?; | ||
dag.add_global_phase(py, &Param::Float(decomp.global_phase))?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn split_2q_unitaries_mod(m: &Bound<PyModule>) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(split_2q_unitaries))?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters