-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fully port Split2QUnitaries to rust (#13025)
* Fully port Split2QUnitaries to rust This commit builds off of #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 #12208 * Update pass logic with changes from #13095 Some of the logic inside the Split2QUnitaries pass was updated in a recently merged PR. This commit makes those changes so the rust implementation matches the current state of the previous python version. * Use op_nodes() instead of topological_op_nodes() * Use Fn trait instead of FnMut for callback We don't need the callback to be mutable currently so relax the trait to just be `Fn` instead of `FnMut`. If we have a need for a mutable environment callback in the future we can change this easily enough without any issues. * Avoid extra edge operations in replace_on_incoming_qubits * Rename function
- Loading branch information
Showing
8 changed files
with
164 additions
and
52 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,75 @@ | ||
// 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.op_nodes(false).collect(); | ||
for node in nodes { | ||
if let NodeType::Operation(inst) = &dag.dag[node] { | ||
let qubits = dag.get_qargs(inst.qubits).to_vec(); | ||
let matrix = inst.op.matrix(inst.params_view()); | ||
// We only attempt to split UnitaryGate objects, but this could be extended in future | ||
// -- however we need to ensure that we can compile the resulting single-qubit unitaries | ||
// to the supported basis gate set. | ||
if qubits.len() != 2 || inst.op.name() != "unitary" { | ||
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_node_with_1q_ops(py, node, insert_fn)?; | ||
dag.add_global_phase(py, &Param::Float(decomp.global_phase))?; | ||
} | ||
// TODO: also look into splitting on Specialization::Swap and just | ||
// swap the virtual qubits. Doing this we will need to update the | ||
// permutation like in ElidePermutations | ||
} | ||
} | ||
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