Skip to content

Commit

Permalink
Deduplicate SliceOrInt (#11844)
Browse files Browse the repository at this point in the history
* Deduplicate SliceOrInt

After doing some post-merge analysis on the impact of #11842 it turns
out the enum being modified in that PR was duplicated in the rust code
unnecessarily. The same enum was already defined in the
euler_one_qubit_decomposer module for it's custom 1 qubit gate sequence
return type. This commit updates the code so it just use the one
defined in the circuit data module that has the fix from #11842 applied
already.

* Move enum to common location
  • Loading branch information
mtreinish committed Feb 21, 2024
1 parent e448134 commit 802ac51
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
9 changes: 2 additions & 7 deletions crates/accelerate/src/euler_one_qubit_decomposer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,15 @@ use std::f64::consts::PI;

use pyo3::exceptions::{PyIndexError, PyTypeError};
use pyo3::prelude::*;
use pyo3::types::PySlice;
use pyo3::wrap_pyfunction;
use pyo3::Python;

use ndarray::prelude::*;
use numpy::PyReadonlyArray2;

const DEFAULT_ATOL: f64 = 1e-12;
use crate::utils::SliceOrInt;

#[derive(FromPyObject)]
enum SliceOrInt<'a> {
Slice(&'a PySlice),
Int(isize),
}
const DEFAULT_ATOL: f64 = 1e-12;

#[pyclass(module = "qiskit._accelerate.euler_one_qubit_decomposer")]
pub struct OneQubitGateErrorMap {
Expand Down
11 changes: 1 addition & 10 deletions crates/accelerate/src/quantum_circuit/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use crate::quantum_circuit::circuit_instruction::CircuitInstruction;
use crate::quantum_circuit::intern_context::{BitType, IndexType, InternContext};
use crate::quantum_circuit::py_ext;
use crate::utils::SliceOrInt;
use hashbrown::HashMap;
use pyo3::exceptions::{PyIndexError, PyKeyError, PyRuntimeError, PyValueError};
use pyo3::prelude::*;
Expand Down Expand Up @@ -153,16 +154,6 @@ pub struct CircuitData {
clbits: Py<PyList>,
}

/// A private enumeration type used to extract arguments to pymethods
/// that may be either an index or a slice.
#[derive(FromPyObject)]
pub enum SliceOrInt<'a> {
// The order here defines the order the variants are tried in the `FromPyObject` derivation.
// `Int` is _much_ more common, so that should be first.
Int(isize),
Slice(&'a PySlice),
}

#[pymethods]
impl CircuitData {
#[new]
Expand Down
11 changes: 11 additions & 0 deletions crates/accelerate/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
// that they have been altered from the originals.

use pyo3::prelude::*;
use pyo3::types::PySlice;

use faer::prelude::*;
use faer::IntoFaerComplex;
use num_complex::Complex;
use numpy::{IntoPyArray, PyReadonlyArray2};

/// A private enumeration type used to extract arguments to pymethod
/// that may be either an index or a slice
#[derive(FromPyObject)]
pub enum SliceOrInt<'a> {
// The order here defines the order the variants are tried in the FromPyObject` derivation.
// `Int` is _much_ more common, so that should be first.
Int(isize),
Slice(&'a PySlice),
}

/// Return indices that sort partially ordered data.
/// If `data` contains two elements that are incomparable,
/// an error will be thrown.
Expand Down

0 comments on commit 802ac51

Please sign in to comment.