@@ -18,49 +18,9 @@ use pyo3::prelude::*;
1818use pyo3:: sync:: PyOnceLock ;
1919
2020use 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
6525pub static OPERATION : ImportOnceCell = ImportOnceCell :: new ( "qiskit.circuit.operation" , "Operation" ) ;
6626pub static INSTRUCTION : ImportOnceCell =
@@ -111,18 +71,15 @@ pub static CASE_DEFAULT: ImportOnceCell = ImportOnceCell::new("qiskit.circuit",
11171pub static CLBIT : ImportOnceCell = ImportOnceCell :: new ( "qiskit.circuit" , "Clbit" ) ;
11272pub static CLASSICAL_REGISTER : ImportOnceCell =
11373 ImportOnceCell :: new ( "qiskit.circuit" , "ClassicalRegister" ) ;
114- pub static DEEPCOPY : ImportOnceCell = ImportOnceCell :: new ( "copy" , "deepcopy" ) ;
11574pub static QI_OPERATOR : ImportOnceCell = ImportOnceCell :: new ( "qiskit.quantum_info" , "Operator" ) ;
11675pub static CLIFFORD : ImportOnceCell =
11776 ImportOnceCell :: new ( "qiskit.quantum_info.operators.symplectic" , "Clifford" ) ;
11877pub static SPARSE_PAULI_OP : ImportOnceCell =
11978 ImportOnceCell :: new ( "qiskit.quantum_info.operators" , "SparsePauliOp" ) ;
120- pub static WARNINGS_WARN : ImportOnceCell = ImportOnceCell :: new ( "warnings" , "warn" ) ;
12179pub static CIRCUIT_TO_DAG : ImportOnceCell =
12280 ImportOnceCell :: new ( "qiskit.converters" , "circuit_to_dag" ) ;
12381pub static DAG_TO_CIRCUIT : ImportOnceCell =
12482 ImportOnceCell :: new ( "qiskit.converters" , "dag_to_circuit" ) ;
125- pub static UUID : ImportOnceCell = ImportOnceCell :: new ( "uuid" , "UUID" ) ;
12683pub static BARRIER : ImportOnceCell = ImportOnceCell :: new ( "qiskit.circuit" , "Barrier" ) ;
12784pub static DELAY : ImportOnceCell = ImportOnceCell :: new ( "qiskit.circuit" , "Delay" ) ;
12885pub 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" ) ;
164121pub 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" ) ;
168123pub 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 =
179134pub static TRANSPILER_LAYOUT : ImportOnceCell =
180135 ImportOnceCell :: new ( "qiskit.transpiler.layout" , "TranspileLayout" ) ;
181136pub static LAYOUT : ImportOnceCell = ImportOnceCell :: new ( "qiskit.transpiler.layout" , "Layout" ) ;
182- pub static NUMPY_ARRAY : ImportOnceCell = ImportOnceCell :: new ( "numpy" , "ndarray" ) ;
183137pub static SYMPIFY_PARAMETER_EXPRESSION : ImportOnceCell =
184138 ImportOnceCell :: new ( "qiskit.circuit.parameterexpression" , "sympify" ) ;
185139pub static TRANSPILE_LAYOUT : ImportOnceCell =
0 commit comments