Skip to content

Commit aea5cd6

Browse files
committed
Move Python utilities from circuit to util
The base definitions of these objects are unrelated to `qiskit-circuit` and the core circuit IR concepts, and can easily be moved into the `qiskit-util` crate to break further spurious dependencies on the IR definitions.
1 parent ed07923 commit aea5cd6

21 files changed

Lines changed: 86 additions & 68 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/circuit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ doctest = false
1313
workspace = true
1414

1515
[dependencies]
16-
qiskit-util.workspace = true
16+
qiskit-util = { workspace = true, features = ["py"] }
1717
rayon.workspace = true
1818
ahash.workspace = true
1919
rustworkx-core.workspace = true

crates/circuit/src/bit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use pyo3::{
2929

3030
use crate::circuit_data::CircuitError;
3131
use crate::dag_circuit::PyBitLocations;
32-
use crate::slice::{PySequenceIndex, SequenceIndex};
32+
use qiskit_util::py::{PySequenceIndex, SequenceIndex};
3333

3434
/// Describes a relationship between a bit and all the registers it belongs to
3535
#[derive(Debug, Clone)]

crates/circuit/src/circuit_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ use crate::parameter::parameter_expression::{ParameterError, ParameterExpression
3737
use crate::parameter::symbol_expr::{Symbol, Value};
3838
use crate::parameter_table::{ParameterTable, ParameterTableError, ParameterUse, ParameterUuid};
3939
use crate::register_data::RegisterData;
40-
use crate::slice::{PySequenceIndex, SequenceIndex};
4140
use crate::var_stretch_container::{
4241
StretchType, VarStretchContainer, VarStretchContainerError, VarType,
4342
};
4443
use crate::{
4544
Block, BlocksMode, CapacityError, Clbit, ControlFlowBlocks, Qubit, Stretch, Var, VarsMode,
4645
instruction,
4746
};
47+
use qiskit_util::py::{PySequenceIndex, SequenceIndex};
4848

4949
use ndarray::ArrayView1;
5050
use num_complex::Complex64;

crates/circuit/src/dag_circuit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ use crate::operations::{
3939
use crate::packed_instruction::{PackedInstruction, PackedOperation};
4040
use crate::parameter::parameter_expression::ParameterExpression;
4141
use crate::register_data::RegisterData;
42-
use crate::slice::PySequenceIndex;
4342
use crate::var_stretch_container::{StretchType, VarStretchContainer, VarType};
4443
use crate::variable_mapper::VariableMapper;
4544
use crate::{
4645
Block, BlockMapper, BlocksMode, Clbit, ControlFlowBlocks, Qubit, Stretch, TupleLikeArg, Var,
4746
VarsMode, imports, instruction, vf2,
4847
};
48+
use qiskit_util::py::{PySequenceIndex, SequenceIndex};
4949

5050
use hashbrown::{HashMap, HashSet};
5151
use indexmap::{IndexMap, IndexSet};
@@ -334,7 +334,7 @@ impl PyBitLocations {
334334
};
335335
if let Ok(index) = index.with_len(2) {
336336
match index {
337-
crate::slice::SequenceIndex::Int(index) => getter(index),
337+
SequenceIndex::Int(index) => getter(index),
338338
_ => PyTuple::new(py, index.iter().map(|idx| getter(idx).unwrap()))
339339
.map(|obj| obj.into_any().unbind()),
340340
}

crates/circuit/src/imports.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,9 @@ use pyo3::prelude::*;
1818
use pyo3::sync::PyOnceLock;
1919

2020
use crate::operations::{STANDARD_GATE_SIZE, StandardGate};
21+
use qiskit_util::py::ImportOnceCell;
2122

22-
/// Helper wrapper around `PyOnceLock` instances that are just intended to store a Python object
23-
/// that is lazily imported.
24-
pub struct ImportOnceCell {
25-
module: &'static str,
26-
object: &'static str,
27-
cell: PyOnceLock<Py<PyAny>>,
28-
}
29-
30-
impl ImportOnceCell {
31-
pub const fn new(module: &'static str, object: &'static str) -> Self {
32-
Self {
33-
module,
34-
object,
35-
cell: PyOnceLock::new(),
36-
}
37-
}
38-
39-
/// Get the underlying GIL-independent reference to the contained object, importing if
40-
/// required.
41-
#[inline]
42-
pub fn get(&self, py: Python) -> &Py<PyAny> {
43-
self.cell.get_or_init(py, || {
44-
py.import(self.module)
45-
.unwrap()
46-
.getattr(self.object)
47-
.unwrap()
48-
.unbind()
49-
})
50-
}
51-
52-
/// Get a GIL-bound reference to the contained object, importing if required.
53-
#[inline]
54-
pub fn get_bound<'py>(&self, py: Python<'py>) -> &Bound<'py, PyAny> {
55-
self.get(py).bind(py)
56-
}
57-
}
58-
59-
pub static BUILTIN_LIST: ImportOnceCell = ImportOnceCell::new("builtins", "list");
60-
pub static BUILTIN_SET: ImportOnceCell = ImportOnceCell::new("builtins", "set");
61-
pub static BUILTIN_RANGE: ImportOnceCell = ImportOnceCell::new("builtins", "range");
62-
pub static BUILTIN_USER_WARNING: ImportOnceCell = ImportOnceCell::new("builtins", "UserWarning");
63-
pub static BUILTIN_HASH: ImportOnceCell = ImportOnceCell::new("builtins", "hash");
23+
pub use qiskit_util::py::imports::*;
6424

6525
pub static OPERATION: ImportOnceCell = ImportOnceCell::new("qiskit.circuit.operation", "Operation");
6626
pub static INSTRUCTION: ImportOnceCell =
@@ -111,18 +71,15 @@ pub static CASE_DEFAULT: ImportOnceCell = ImportOnceCell::new("qiskit.circuit",
11171
pub static CLBIT: ImportOnceCell = ImportOnceCell::new("qiskit.circuit", "Clbit");
11272
pub static CLASSICAL_REGISTER: ImportOnceCell =
11373
ImportOnceCell::new("qiskit.circuit", "ClassicalRegister");
114-
pub static DEEPCOPY: ImportOnceCell = ImportOnceCell::new("copy", "deepcopy");
11574
pub static QI_OPERATOR: ImportOnceCell = ImportOnceCell::new("qiskit.quantum_info", "Operator");
11675
pub static CLIFFORD: ImportOnceCell =
11776
ImportOnceCell::new("qiskit.quantum_info.operators.symplectic", "Clifford");
11877
pub static SPARSE_PAULI_OP: ImportOnceCell =
11978
ImportOnceCell::new("qiskit.quantum_info.operators", "SparsePauliOp");
120-
pub static WARNINGS_WARN: ImportOnceCell = ImportOnceCell::new("warnings", "warn");
12179
pub static CIRCUIT_TO_DAG: ImportOnceCell =
12280
ImportOnceCell::new("qiskit.converters", "circuit_to_dag");
12381
pub static DAG_TO_CIRCUIT: ImportOnceCell =
12482
ImportOnceCell::new("qiskit.converters", "dag_to_circuit");
125-
pub static UUID: ImportOnceCell = ImportOnceCell::new("uuid", "UUID");
12683
pub static BARRIER: ImportOnceCell = ImportOnceCell::new("qiskit.circuit", "Barrier");
12784
pub static DELAY: ImportOnceCell = ImportOnceCell::new("qiskit.circuit", "Delay");
12885
pub static MEASURE: ImportOnceCell = ImportOnceCell::new("qiskit.circuit", "Measure");
@@ -163,8 +120,6 @@ pub static XX_DECOMPOSER: ImportOnceCell =
163120
ImportOnceCell::new("qiskit.synthesis.two_qubit.xx_decompose", "XXDecomposer");
164121
pub static XX_EMBODIMENTS: ImportOnceCell =
165122
ImportOnceCell::new("qiskit.synthesis.two_qubit.xx_decompose", "XXEmbodiments");
166-
pub static NUMPY_COPY_ONLY_IF_NEEDED: ImportOnceCell =
167-
ImportOnceCell::new("qiskit._numpy_compat", "COPY_ONLY_IF_NEEDED");
168123
pub static HLS_SYNTHESIZE_OP_USING_PLUGINS: ImportOnceCell = ImportOnceCell::new(
169124
"qiskit.transpiler.passes.synthesis.high_level_synthesis",
170125
"_synthesize_op_using_plugins",
@@ -179,7 +134,6 @@ pub static CONTROL_FLOW_BOX_OP: ImportOnceCell =
179134
pub static TRANSPILER_LAYOUT: ImportOnceCell =
180135
ImportOnceCell::new("qiskit.transpiler.layout", "TranspileLayout");
181136
pub static LAYOUT: ImportOnceCell = ImportOnceCell::new("qiskit.transpiler.layout", "Layout");
182-
pub static NUMPY_ARRAY: ImportOnceCell = ImportOnceCell::new("numpy", "ndarray");
183137
pub static SYMPIFY_PARAMETER_EXPRESSION: ImportOnceCell =
184138
ImportOnceCell::new("qiskit.circuit.parameterexpression", "sympify");
185139
pub static TRANSPILE_LAYOUT: ImportOnceCell =

crates/circuit/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub mod packed_instruction;
3535
pub mod parameter;
3636
pub mod parameter_table;
3737
pub mod register_data;
38-
pub mod slice;
3938
pub mod var_stretch_container;
4039
mod variable_mapper;
4140
pub mod vf2;

crates/quantum_info/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ doctest = false
1111

1212
[dependencies]
1313
qiskit-circuit.workspace = true
14-
qiskit-util.workspace = true
14+
qiskit-util = { workspace = true, features = ["py"] }
1515
numpy.workspace = true
1616
num-complex.workspace = true
1717
num-traits.workspace = true

crates/quantum_info/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod test;
2525
use pyo3::import_exception;
2626

2727
pub(crate) mod imports {
28-
use qiskit_circuit::imports::ImportOnceCell;
28+
use qiskit_util::py::ImportOnceCell;
2929

3030
pub static PAULI_TYPE: ImportOnceCell = ImportOnceCell::new("qiskit.quantum_info", "Pauli");
3131
pub static PAULI_LIST_TYPE: ImportOnceCell =

crates/quantum_info/src/pauli_lindblad_map/pauli_lindblad_map_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rand::prelude::*;
2828
use rand_distr::Bernoulli;
2929
use rand_pcg::Pcg64Mcg;
3030

31-
use qiskit_circuit::slice::{PySequenceIndex, SequenceIndex};
31+
use qiskit_util::py::{PySequenceIndex, SequenceIndex};
3232

3333
use super::qubit_sparse_pauli::{
3434
ArithmeticError, CoherenceError, InnerReadError, InnerWriteError, LabelError, Pauli,

0 commit comments

Comments
 (0)