-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fully port Split2QUnitaries to rust #13025
Merged
+164
−52
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bee99b1
Fully port Split2QUnitaries to rust
mtreinish 3d8af9d
Merge remote-tracking branch 'origin/main' into use-rust-split2q-unit…
mtreinish 911be95
Update pass logic with changes from #13095
mtreinish e924c8a
Use op_nodes() instead of topological_op_nodes()
mtreinish 7b0e363
Merge remote-tracking branch 'origin/main' into use-rust-split2q-unit…
mtreinish 2ff94f5
Use Fn trait instead of FnMut for callback
mtreinish b2cdb54
Avoid extra edge operations in replace_on_incoming_qubits
mtreinish 913ec20
Rename function
mtreinish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a
to_vec
here or would it be faster to omit it here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
to_vec()
here is needed to get an owned object here.dag.get_qargs()
returns a slice that refers back to qubits owned bydag
's qubits list. If I didn't have this the compiler would complain that have I have an immutable reference todag
viaqubits
when I try to mutate the dag later inreplace_on_incoming_qubits
. I only added this because the compiler errored when I originally kept it as a slice. This is the same reason I had to collect the node indices into aVec
instead of directly iterating over the nodes.