forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial: Add
circuit_to_dag
in rust.
- Add new method `DAGCircuit::from_quantum_circuit` which uses the data from a `QuantumCircuit` instance to build a dag_circuit. - Expose the method through a `Python` interface with `circuit_to_dag` which goes by the alias of `core_circuit_to_dag` and is called by the original method. - Add an arbitrary structure `QuantumCircuitData` that successfully extract attributes from the python `QuantumCircuit` instance and makes it easier to access in rust. - This structure is for attribute extraction only and is not clonable/copyable. - Expose a new module `converters` which should store all of the rust-side converters whenever they get brought into rust. - Other small tweaks and fixes.
- Loading branch information
Showing
5 changed files
with
318 additions
and
40 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,69 @@ | ||
// This code is part of Qiskit. | ||
// | ||
// (C) Copyright IBM 2023, 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 hashbrown::HashMap; | ||
use pyo3::types::{PyDict, PyList}; | ||
|
||
use crate::{circuit_data::CircuitData, dag_circuit::DAGCircuit}; | ||
|
||
/// An extractable representation of a QuantumCircuit reserved only for | ||
/// conversion purposes. | ||
/// | ||
/// ## Notes: | ||
/// This structure does not implement `Clone`, this is the intended behavior as | ||
/// it contains callbacks to Python and should not be stored anywhere. | ||
#[derive(Debug)] | ||
pub(crate) struct QuantumCircuitData<'py> { | ||
pub data: PyRef<'py, CircuitData>, | ||
pub name: Option<Bound<'py, PyAny>>, | ||
pub calibrations: HashMap<String, Py<PyDict>>, | ||
pub metadata: Option<Bound<'py, PyAny>>, | ||
pub qregs: Option<Bound<'py, PyList>>, | ||
pub cregs: Option<Bound<'py, PyList>>, | ||
pub input_vars: Option<Bound<'py, PyAny>>, | ||
pub captured_vars: Option<Bound<'py, PyAny>>, | ||
pub declared_vars: Option<Bound<'py, PyAny>>, | ||
} | ||
|
||
impl<'py> FromPyObject<'py> for QuantumCircuitData<'py> { | ||
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> { | ||
let circuit_data = ob.getattr("_data")?; | ||
let data_borrowed = circuit_data.downcast::<CircuitData>()?.borrow(); | ||
Ok(QuantumCircuitData { | ||
data: data_borrowed, | ||
name: ob.getattr("name").ok(), | ||
calibrations: ob.getattr("calibrations")?.extract()?, | ||
metadata: ob.getattr("metadata").ok(), | ||
qregs: ob.getattr("qregs").map(|ob| ob.downcast_into())?.ok(), | ||
cregs: ob.getattr("cregs").map(|ob| ob.downcast_into())?.ok(), | ||
input_vars: ob.call_method0("iter_input_vars").ok(), | ||
captured_vars: ob.call_method0("iter_captured_vars").ok(), | ||
declared_vars: ob.call_method0("iter_declared_vars").ok(), | ||
}) | ||
} | ||
} | ||
|
||
#[pyfunction] | ||
fn circuit_to_dag( | ||
py: Python, | ||
quantum_circuit: QuantumCircuitData, | ||
qubit_order: Option<Vec<PyObject>>, | ||
clbit_order: Option<Vec<PyObject>>, | ||
) -> PyResult<DAGCircuit> { | ||
DAGCircuit::from_quantum_circuit(py, quantum_circuit, qubit_order, clbit_order) | ||
} | ||
|
||
pub fn converters(m: &Bound<PyModule>) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(circuit_to_dag, m)?)?; | ||
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
Oops, something went wrong.