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 migrates the entirety of the CheckMap analysis pass to Rust. The pass operates solely in the rust domain and returns an `Option<(String, [u32; 2])>` to Python which is used to set the two property set fields appropriately. All the analysis of the dag is done in Rust. There is still Python interaction required though because control flow operations are only defined in Python. However the interaction is minimal and only to get the circuits for control flow blocks and converting them into DAGs (at least until Qiskit#13001 is complete). This commit is based on top of Qiskit#12959 and will need to be rebased after that merges. Closes Qiskit#12251 Part of Qiskit#12208
- Loading branch information
Showing
5 changed files
with
114 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 hashbrown::{HashMap, HashSet}; | ||
use pyo3::intern; | ||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
use qiskit_circuit::circuit_data::CircuitData; | ||
use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType}; | ||
use qiskit_circuit::imports::CIRCUIT_TO_DAG; | ||
use qiskit_circuit::operations::{Operation, OperationRef}; | ||
use qiskit_circuit::Qubit; | ||
|
||
fn recurse<'py>( | ||
py: Python<'py>, | ||
dag: &'py DAGCircuit, | ||
edge_set: &'py HashSet<[u32; 2]>, | ||
wire_map: Option<&'py HashMap<Qubit, Qubit>>, | ||
) -> PyResult<Option<(String, [u32; 2])>> { | ||
let check_qubits = |qubits: &[Qubit]| -> bool { | ||
match wire_map { | ||
Some(wire_map) => { | ||
let mapped_bits = [wire_map[&qubits[0]], wire_map[&qubits[1]]]; | ||
edge_set.contains(&[mapped_bits[0].into(), mapped_bits[1].into()]) | ||
} | ||
None => edge_set.contains(&[qubits[0].into(), qubits[1].into()]), | ||
} | ||
}; | ||
for node in dag.op_nodes(false) { | ||
if let NodeType::Operation(inst) = &dag.dag[node] { | ||
let qubits = dag.get_qubits(inst.qubits); | ||
if inst.op.control_flow() { | ||
if let OperationRef::Instruction(py_inst) = inst.op.view() { | ||
let raw_blocks = py_inst.instruction.getattr(py, "blocks")?; | ||
let circuit_to_dag = CIRCUIT_TO_DAG.get_bound(py); | ||
for raw_block in raw_blocks.bind(py).iter().unwrap() { | ||
let block_obj = raw_block?; | ||
let block = block_obj | ||
.getattr(intern!(py, "_data"))? | ||
.extract::<CircuitData>()?; | ||
let new_dag: DAGCircuit = | ||
circuit_to_dag.call1((block_obj.clone(),))?.extract()?; | ||
let wire_map = (0..block.num_qubits()) | ||
.map(|x| Qubit(x as u32)) | ||
.zip(qubits) | ||
.map(|(inner, outer)| match wire_map { | ||
Some(wire_map) => (inner, wire_map[outer]), | ||
None => (inner, *outer), | ||
}) | ||
.collect(); | ||
let res = recurse(py, &new_dag, edge_set, Some(&wire_map))?; | ||
if res.is_some() { | ||
return Ok(res); | ||
} | ||
} | ||
} | ||
} else if qubits.len() == 2 | ||
&& (dag.calibrations_empty() || !dag.has_calibration_for_index(py, node)?) | ||
&& !check_qubits(qubits) | ||
{ | ||
return Ok(Some(( | ||
inst.op.name().to_string(), | ||
[qubits[0].0, qubits[1].0], | ||
))); | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
|
||
#[pyfunction] | ||
pub fn check_map( | ||
py: Python, | ||
dag: &DAGCircuit, | ||
edge_set: HashSet<[u32; 2]>, | ||
) -> PyResult<Option<(String, [u32; 2])>> { | ||
recurse(py, dag, &edge_set, None) | ||
} | ||
|
||
pub fn check_map_mod(m: &Bound<PyModule>) -> PyResult<()> { | ||
m.add_wrapped(wrap_pyfunction!(check_map))?; | ||
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