diff --git a/crates/accelerate/src/convert_2q_block_matrix.rs b/crates/accelerate/src/convert_2q_block_matrix.rs index e311c129b11b..de72287293a7 100644 --- a/crates/accelerate/src/convert_2q_block_matrix.rs +++ b/crates/accelerate/src/convert_2q_block_matrix.rs @@ -74,7 +74,6 @@ pub fn change_basis(matrix: ArrayView2) -> Array2 { trans_matrix } -#[pymodule] pub fn convert_2q_block_matrix(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(blocks_to_matrix))?; Ok(()) diff --git a/crates/accelerate/src/dense_layout.rs b/crates/accelerate/src/dense_layout.rs index 7cb54140761e..488f7d023cb4 100644 --- a/crates/accelerate/src/dense_layout.rs +++ b/crates/accelerate/src/dense_layout.rs @@ -223,7 +223,6 @@ pub fn best_subset( )) } -#[pymodule] pub fn dense_layout(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(best_subset))?; Ok(()) diff --git a/crates/accelerate/src/error_map.rs b/crates/accelerate/src/error_map.rs index 8dbd2e4290a5..b61733ae1512 100644 --- a/crates/accelerate/src/error_map.rs +++ b/crates/accelerate/src/error_map.rs @@ -111,7 +111,6 @@ impl ErrorMap { } } -#[pymodule] pub fn error_map(m: &Bound) -> PyResult<()> { m.add_class::()?; Ok(()) diff --git a/crates/accelerate/src/euler_one_qubit_decomposer.rs b/crates/accelerate/src/euler_one_qubit_decomposer.rs index 1fd5fd7834ff..399a0d5f5fa9 100644 --- a/crates/accelerate/src/euler_one_qubit_decomposer.rs +++ b/crates/accelerate/src/euler_one_qubit_decomposer.rs @@ -883,7 +883,6 @@ pub fn params_zxz(unitary: PyReadonlyArray2) -> [f64; 4] { params_zxz_inner(mat) } -#[pymodule] pub fn euler_one_qubit_decomposer(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(params_zyz))?; m.add_wrapped(wrap_pyfunction!(params_xyx))?; diff --git a/crates/accelerate/src/isometry.rs b/crates/accelerate/src/isometry.rs index a4e83358a7da..3ed3127eb562 100644 --- a/crates/accelerate/src/isometry.rs +++ b/crates/accelerate/src/isometry.rs @@ -348,7 +348,6 @@ fn b(k: usize, s: usize) -> usize { k - (a(k, s) * 2_usize.pow(s as u32)) } -#[pymodule] pub fn isometry(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(diag_is_identity_up_to_global_phase))?; m.add_wrapped(wrap_pyfunction!(find_squs_for_disentangling))?; diff --git a/crates/accelerate/src/nlayout.rs b/crates/accelerate/src/nlayout.rs index 1a0b73b25fed..b6b7be5613c3 100644 --- a/crates/accelerate/src/nlayout.rs +++ b/crates/accelerate/src/nlayout.rs @@ -216,7 +216,6 @@ impl NLayout { } } -#[pymodule] pub fn nlayout(m: &Bound) -> PyResult<()> { m.add_class::()?; Ok(()) diff --git a/crates/accelerate/src/optimize_1q_gates.rs b/crates/accelerate/src/optimize_1q_gates.rs index a683604c19e0..64924a339136 100644 --- a/crates/accelerate/src/optimize_1q_gates.rs +++ b/crates/accelerate/src/optimize_1q_gates.rs @@ -90,7 +90,6 @@ pub fn compose_u3_rust( out_angles } -#[pymodule] pub fn optimize_1q_gates(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(compose_u3_rust))?; Ok(()) diff --git a/crates/accelerate/src/pauli_exp_val.rs b/crates/accelerate/src/pauli_exp_val.rs index 29f741f6cf46..66e07c4621a4 100644 --- a/crates/accelerate/src/pauli_exp_val.rs +++ b/crates/accelerate/src/pauli_exp_val.rs @@ -192,7 +192,6 @@ pub fn density_expval_pauli_with_x( } } -#[pymodule] pub fn pauli_expval(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(expval_pauli_no_x))?; m.add_wrapped(wrap_pyfunction!(expval_pauli_with_x))?; diff --git a/crates/accelerate/src/results/mod.rs b/crates/accelerate/src/results/mod.rs index 36282a749e66..6d5a79363977 100644 --- a/crates/accelerate/src/results/mod.rs +++ b/crates/accelerate/src/results/mod.rs @@ -16,7 +16,6 @@ pub mod marginalization; use pyo3::prelude::*; use pyo3::wrap_pyfunction; -#[pymodule] pub fn results(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(marginalization::marginal_counts))?; m.add_wrapped(wrap_pyfunction!(marginalization::marginal_distribution))?; diff --git a/crates/accelerate/src/sabre/mod.rs b/crates/accelerate/src/sabre/mod.rs index 3eb8ebb3a219..6305fb8f3bf7 100644 --- a/crates/accelerate/src/sabre/mod.rs +++ b/crates/accelerate/src/sabre/mod.rs @@ -113,7 +113,6 @@ impl BlockResult { } } -#[pymodule] pub fn sabre(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(route::sabre_routing))?; m.add_wrapped(wrap_pyfunction!(layout::sabre_layout_and_routing))?; diff --git a/crates/accelerate/src/sampled_exp_val.rs b/crates/accelerate/src/sampled_exp_val.rs index b51ca3c98f0e..28cb4981df4a 100644 --- a/crates/accelerate/src/sampled_exp_val.rs +++ b/crates/accelerate/src/sampled_exp_val.rs @@ -86,7 +86,6 @@ pub fn sampled_expval_complex( Ok(out.re) } -#[pymodule] pub fn sampled_exp_val(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(sampled_expval_float))?; m.add_wrapped(wrap_pyfunction!(sampled_expval_complex))?; diff --git a/crates/accelerate/src/sparse_pauli_op.rs b/crates/accelerate/src/sparse_pauli_op.rs index 5d6a82df7940..e17d2faca713 100644 --- a/crates/accelerate/src/sparse_pauli_op.rs +++ b/crates/accelerate/src/sparse_pauli_op.rs @@ -816,7 +816,6 @@ impl_to_matrix_sparse!( u64 ); -#[pymodule] pub fn sparse_pauli_op(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(unordered_unique))?; m.add_wrapped(wrap_pyfunction!(decompose_dense))?; diff --git a/crates/accelerate/src/stochastic_swap.rs b/crates/accelerate/src/stochastic_swap.rs index bc13325d8d96..7b18500240eb 100644 --- a/crates/accelerate/src/stochastic_swap.rs +++ b/crates/accelerate/src/stochastic_swap.rs @@ -335,7 +335,6 @@ pub fn swap_trials( Ok((best_edges, best_layout, best_depth)) } -#[pymodule] pub fn stochastic_swap(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(swap_trials))?; m.add_class::()?; diff --git a/crates/accelerate/src/two_qubit_decompose.rs b/crates/accelerate/src/two_qubit_decompose.rs index 5e833bd86fda..3750f6b006b0 100644 --- a/crates/accelerate/src/two_qubit_decompose.rs +++ b/crates/accelerate/src/two_qubit_decompose.rs @@ -2042,7 +2042,6 @@ impl TwoQubitBasisDecomposer { } } -#[pymodule] pub fn two_qubit_decompose(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(_num_basis_gates))?; m.add_class::()?; diff --git a/crates/accelerate/src/uc_gate.rs b/crates/accelerate/src/uc_gate.rs index 3a5f74a6f0b1..917361389d63 100644 --- a/crates/accelerate/src/uc_gate.rs +++ b/crates/accelerate/src/uc_gate.rs @@ -156,7 +156,6 @@ pub fn dec_ucg_help( ) } -#[pymodule] pub fn uc_gate(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(dec_ucg_help))?; Ok(()) diff --git a/crates/accelerate/src/utils.rs b/crates/accelerate/src/utils.rs index 6df00f7f8b76..598256192f83 100644 --- a/crates/accelerate/src/utils.rs +++ b/crates/accelerate/src/utils.rs @@ -41,7 +41,6 @@ pub fn eigenvalues(py: Python, unitary: PyReadonlyArray2>) -> PyObj .into() } -#[pymodule] pub fn utils(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(eigenvalues))?; Ok(()) diff --git a/crates/accelerate/src/vf2_layout.rs b/crates/accelerate/src/vf2_layout.rs index e84b2bfbfb8c..476197397aff 100644 --- a/crates/accelerate/src/vf2_layout.rs +++ b/crates/accelerate/src/vf2_layout.rs @@ -106,7 +106,6 @@ pub fn score_layout( Ok(1. - fidelity) } -#[pymodule] pub fn vf2_layout(m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(score_layout))?; m.add_class::()?; diff --git a/crates/circuit/src/lib.rs b/crates/circuit/src/lib.rs index cd560bad7387..e74c19d675c3 100644 --- a/crates/circuit/src/lib.rs +++ b/crates/circuit/src/lib.rs @@ -27,8 +27,7 @@ pub enum SliceOrInt<'a> { Slice(Bound<'a, PySlice>), } -#[pymodule] -pub fn circuit(m: Bound) -> PyResult<()> { +pub fn circuit(m: &Bound) -> PyResult<()> { m.add_class::()?; m.add_class::()?; Ok(()) diff --git a/crates/pyext/src/lib.rs b/crates/pyext/src/lib.rs index a21b1307a88f..b7874d740906 100644 --- a/crates/pyext/src/lib.rs +++ b/crates/pyext/src/lib.rs @@ -11,7 +11,6 @@ // that they have been altered from the originals. use pyo3::prelude::*; -use pyo3::wrap_pymodule; use qiskit_accelerate::{ convert_2q_block_matrix::convert_2q_block_matrix, dense_layout::dense_layout, @@ -23,27 +22,38 @@ use qiskit_accelerate::{ vf2_layout::vf2_layout, }; +#[inline(always)] +#[doc(hidden)] +fn add_submodule(m: &Bound, constructor: F, name: &str) -> PyResult<()> +where + F: FnOnce(&Bound) -> PyResult<()>, +{ + let new_mod = PyModule::new_bound(m.py(), name)?; + constructor(&new_mod)?; + m.add_submodule(&new_mod) +} + #[pymodule] fn _accelerate(m: &Bound) -> PyResult<()> { - m.add_wrapped(wrap_pymodule!(qiskit_circuit::circuit))?; - m.add_wrapped(wrap_pymodule!(qiskit_qasm2::qasm2))?; - m.add_wrapped(wrap_pymodule!(qiskit_qasm3::qasm3))?; - m.add_wrapped(wrap_pymodule!(convert_2q_block_matrix))?; - m.add_wrapped(wrap_pymodule!(dense_layout))?; - m.add_wrapped(wrap_pymodule!(error_map))?; - m.add_wrapped(wrap_pymodule!(euler_one_qubit_decomposer))?; - m.add_wrapped(wrap_pymodule!(isometry))?; - m.add_wrapped(wrap_pymodule!(nlayout))?; - m.add_wrapped(wrap_pymodule!(optimize_1q_gates))?; - m.add_wrapped(wrap_pymodule!(pauli_expval))?; - m.add_wrapped(wrap_pymodule!(results))?; - m.add_wrapped(wrap_pymodule!(sabre))?; - m.add_wrapped(wrap_pymodule!(sampled_exp_val))?; - m.add_wrapped(wrap_pymodule!(sparse_pauli_op))?; - m.add_wrapped(wrap_pymodule!(stochastic_swap))?; - m.add_wrapped(wrap_pymodule!(two_qubit_decompose))?; - m.add_wrapped(wrap_pymodule!(uc_gate))?; - m.add_wrapped(wrap_pymodule!(utils))?; - m.add_wrapped(wrap_pymodule!(vf2_layout))?; + add_submodule(m, qiskit_circuit::circuit, "circuit")?; + add_submodule(m, qiskit_qasm2::qasm2, "qasm2")?; + add_submodule(m, qiskit_qasm3::qasm3, "qasm3")?; + add_submodule(m, convert_2q_block_matrix, "convert_2q_block_matrix")?; + add_submodule(m, dense_layout, "dense_layout")?; + add_submodule(m, error_map, "error_map")?; + add_submodule(m, euler_one_qubit_decomposer, "euler_one_qubit_decomposer")?; + add_submodule(m, isometry, "isometry")?; + add_submodule(m, nlayout, "nlayout")?; + add_submodule(m, optimize_1q_gates, "optimize_1q_gates")?; + add_submodule(m, pauli_expval, "pauli_expval")?; + add_submodule(m, results, "results")?; + add_submodule(m, sabre, "sabre")?; + add_submodule(m, sampled_exp_val, "sampled_exp_val")?; + add_submodule(m, sparse_pauli_op, "sparse_pauli_op")?; + add_submodule(m, stochastic_swap, "stochastic_swap")?; + add_submodule(m, two_qubit_decompose, "two_qubit_decompose")?; + add_submodule(m, uc_gate, "uc_gate")?; + add_submodule(m, utils, "utils")?; + add_submodule(m, vf2_layout, "vf2_layout")?; Ok(()) } diff --git a/crates/qasm2/src/lib.rs b/crates/qasm2/src/lib.rs index 7129b17f7fdf..beac72674e99 100644 --- a/crates/qasm2/src/lib.rs +++ b/crates/qasm2/src/lib.rs @@ -123,7 +123,6 @@ fn bytecode_from_file( /// An interface to the Rust components of the parser stack, and the types it uses to represent the /// output. The principal entry points for Python are :func:`bytecode_from_string` and /// :func:`bytecode_from_file`, which produce iterables of :class:`Bytecode` objects. -#[pymodule] pub fn qasm2(module: &Bound) -> PyResult<()> { module.add_class::()?; module.add_class::()?; diff --git a/crates/qasm3/src/lib.rs b/crates/qasm3/src/lib.rs index 9a651a5c12e2..bca862406f39 100644 --- a/crates/qasm3/src/lib.rs +++ b/crates/qasm3/src/lib.rs @@ -153,7 +153,6 @@ pub fn load( /// Internal module supplying the OpenQASM 3 import capabilities. The entries in it should largely /// be re-exposed directly to public Python space. -#[pymodule] pub fn qasm3(module: &Bound) -> PyResult<()> { module.add_function(wrap_pyfunction!(loads, module)?)?; module.add_function(wrap_pyfunction!(load, module)?)?;