|
| 1 | +// This code is part of Qiskit. |
| 2 | +// |
| 3 | +// (C) Copyright IBM 2024 |
| 4 | +// |
| 5 | +// This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +// obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// Any modifications or derivative works of this code must retain this |
| 10 | +// copyright notice, and modified files need to carry a notice indicating |
| 11 | +// that they have been altered from the originals. |
| 12 | + |
| 13 | +use hashbrown::{HashMap, HashSet}; |
| 14 | +use pyo3::intern; |
| 15 | +use pyo3::prelude::*; |
| 16 | +use pyo3::wrap_pyfunction; |
| 17 | + |
| 18 | +use qiskit_circuit::circuit_data::CircuitData; |
| 19 | +use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType}; |
| 20 | +use qiskit_circuit::imports::CIRCUIT_TO_DAG; |
| 21 | +use qiskit_circuit::operations::{Operation, OperationRef}; |
| 22 | +use qiskit_circuit::Qubit; |
| 23 | + |
| 24 | +fn recurse<'py>( |
| 25 | + py: Python<'py>, |
| 26 | + dag: &'py DAGCircuit, |
| 27 | + edge_set: &'py HashSet<[u32; 2]>, |
| 28 | + wire_map: Option<&'py HashMap<Qubit, Qubit>>, |
| 29 | +) -> PyResult<Option<(String, [u32; 2])>> { |
| 30 | + let check_qubits = |qubits: &[Qubit]| -> bool { |
| 31 | + match wire_map { |
| 32 | + Some(wire_map) => { |
| 33 | + let mapped_bits = [wire_map[&qubits[0]], wire_map[&qubits[1]]]; |
| 34 | + edge_set.contains(&[mapped_bits[0].into(), mapped_bits[1].into()]) |
| 35 | + } |
| 36 | + None => edge_set.contains(&[qubits[0].into(), qubits[1].into()]), |
| 37 | + } |
| 38 | + }; |
| 39 | + for node in dag.op_nodes(false) { |
| 40 | + if let NodeType::Operation(inst) = &dag.dag[node] { |
| 41 | + let qubits = dag.get_qubits(inst.qubits); |
| 42 | + if inst.op.control_flow() { |
| 43 | + if let OperationRef::Instruction(py_inst) = inst.op.view() { |
| 44 | + let raw_blocks = py_inst.instruction.getattr(py, "blocks")?; |
| 45 | + let circuit_to_dag = CIRCUIT_TO_DAG.get_bound(py); |
| 46 | + for raw_block in raw_blocks.bind(py).iter().unwrap() { |
| 47 | + let block_obj = raw_block?; |
| 48 | + let block = block_obj |
| 49 | + .getattr(intern!(py, "_data"))? |
| 50 | + .extract::<CircuitData>()?; |
| 51 | + let new_dag: DAGCircuit = |
| 52 | + circuit_to_dag.call1((block_obj.clone(),))?.extract()?; |
| 53 | + let wire_map = (0..block.num_qubits()) |
| 54 | + .map(|x| Qubit(x as u32)) |
| 55 | + .zip(qubits) |
| 56 | + .map(|(inner, outer)| match wire_map { |
| 57 | + Some(wire_map) => (inner, wire_map[outer]), |
| 58 | + None => (inner, *outer), |
| 59 | + }) |
| 60 | + .collect(); |
| 61 | + let res = recurse(py, &new_dag, edge_set, Some(&wire_map))?; |
| 62 | + if res.is_some() { |
| 63 | + return Ok(res); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } else if qubits.len() == 2 |
| 68 | + && (dag.calibrations_empty() || !dag.has_calibration_for_index(py, node)?) |
| 69 | + && !check_qubits(qubits) |
| 70 | + { |
| 71 | + return Ok(Some(( |
| 72 | + inst.op.name().to_string(), |
| 73 | + [qubits[0].0, qubits[1].0], |
| 74 | + ))); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + Ok(None) |
| 79 | +} |
| 80 | + |
| 81 | +#[pyfunction] |
| 82 | +pub fn check_map( |
| 83 | + py: Python, |
| 84 | + dag: &DAGCircuit, |
| 85 | + edge_set: HashSet<[u32; 2]>, |
| 86 | +) -> PyResult<Option<(String, [u32; 2])>> { |
| 87 | + recurse(py, dag, &edge_set, None) |
| 88 | +} |
| 89 | + |
| 90 | +pub fn check_map_mod(m: &Bound<PyModule>) -> PyResult<()> { |
| 91 | + m.add_wrapped(wrap_pyfunction!(check_map))?; |
| 92 | + Ok(()) |
| 93 | +} |
0 commit comments