-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathconverters.rs
138 lines (130 loc) · 4.75 KB
/
converters.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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.
#[cfg(feature = "cache_pygates")]
use std::sync::OnceLock;
use pyo3::intern;
use pyo3::prelude::*;
use crate::circuit_data::CircuitData;
use crate::dag_circuit::{DAGCircuit, NodeType};
use crate::packed_instruction::PackedInstruction;
/// An extractable representation of a QuantumCircuit reserved only for
/// conversion purposes.
#[derive(Debug, Clone)]
pub struct QuantumCircuitData<'py> {
pub data: CircuitData,
pub name: Option<Bound<'py, PyAny>>,
pub metadata: Option<Bound<'py, PyAny>>,
pub input_vars: Vec<Bound<'py, PyAny>>,
pub captured_vars: Vec<Bound<'py, PyAny>>,
pub declared_vars: Vec<Bound<'py, PyAny>>,
pub captured_stretches: Vec<Bound<'py, PyAny>>,
pub declared_stretches: Vec<Bound<'py, PyAny>>,
}
impl<'py> FromPyObject<'py> for QuantumCircuitData<'py> {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let py = ob.py();
let circuit_data = ob.getattr("_data")?;
let data_borrowed = circuit_data.extract::<CircuitData>()?;
Ok(QuantumCircuitData {
data: data_borrowed,
name: ob.getattr(intern!(py, "name")).ok(),
metadata: ob.getattr(intern!(py, "metadata")).ok(),
input_vars: ob
.call_method0(intern!(py, "iter_input_vars"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
captured_vars: ob
.call_method0(intern!(py, "iter_captured_vars"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
declared_vars: ob
.call_method0(intern!(py, "iter_declared_vars"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
captured_stretches: ob
.call_method0(intern!(py, "iter_captured_stretches"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
declared_stretches: ob
.call_method0(intern!(py, "iter_declared_stretches"))?
.try_iter()?
.collect::<PyResult<Vec<_>>>()?,
})
}
}
#[pyfunction(signature = (quantum_circuit, copy_operations = true, qubit_order = None, clbit_order = None))]
pub fn circuit_to_dag(
py: Python,
quantum_circuit: QuantumCircuitData,
copy_operations: bool,
qubit_order: Option<Vec<Bound<PyAny>>>,
clbit_order: Option<Vec<Bound<PyAny>>>,
) -> PyResult<DAGCircuit> {
DAGCircuit::from_circuit(
py,
quantum_circuit,
copy_operations,
qubit_order,
clbit_order,
)
}
#[pyfunction(signature = (dag, copy_operations = true))]
pub fn dag_to_circuit(
py: Python,
dag: &DAGCircuit,
copy_operations: bool,
) -> PyResult<CircuitData> {
CircuitData::from_packed_instructions(
py,
dag.qubits().clone(),
dag.clbits().clone(),
dag.qargs_interner().clone(),
dag.cargs_interner().clone(),
dag.qregs_data().clone(),
dag.cregs_data().clone(),
dag.qubit_locations().clone(),
dag.clbit_locations().clone(),
dag.topological_op_nodes()?.map(|node_index| {
let NodeType::Operation(ref instr) = dag[node_index] else {
unreachable!(
"The received node from topological_op_nodes() is not an Operation node."
)
};
if copy_operations {
let op = instr.op.py_deepcopy(py, None)?;
Ok(PackedInstruction {
op,
qubits: instr.qubits,
clbits: instr.clbits,
params: Some(Box::new(
instr
.params_view()
.iter()
.map(|param| param.clone_ref(py))
.collect(),
)),
label: instr.label.clone(),
#[cfg(feature = "cache_pygates")]
py_op: OnceLock::new(),
})
} else {
Ok(instr.clone())
}
}),
dag.get_global_phase(),
)
}
pub fn converters(m: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(circuit_to_dag, m)?)?;
m.add_function(wrap_pyfunction!(dag_to_circuit, m)?)?;
Ok(())
}