Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid ExtraInstructionAttributes allocation on unit="dt" #13078

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions crates/circuit/src/circuit_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use numpy::IntoPyArray;
use pyo3::basic::CompareOp;
use pyo3::exceptions::{PyDeprecationWarning, PyTypeError};
use pyo3::prelude::*;
use pyo3::types::{PyList, PyTuple, PyType};
use pyo3::types::{PyList, PyString, PyTuple, PyType};
use pyo3::{intern, IntoPy, PyObject, PyResult};

use smallvec::SmallVec;
Expand Down Expand Up @@ -63,6 +63,20 @@ impl ExtraInstructionAttributes {
None
}
}

/// Get the Python-space version of the stored `unit`. This evalutes the Python-space default
/// (`"dt"`) value if we're storing a `None`.
pub fn py_unit(&self, py: Python) -> Py<PyString> {
self.unit
.as_deref()
.map(|unit| <&str as IntoPy<Py<PyString>>>::into_py(unit, py))
.unwrap_or_else(|| Self::default_unit(py).clone().unbind())
}

/// Get the Python-space default value for the `unit` field.
pub fn default_unit(py: Python) -> &Bound<PyString> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I would put an #[inline] on this since the compiler is less likely to inline a public function. But it doesn't really matter in practice, especially since this is the python path so that won't help anything.

intern!(py, "dt")
}
}

/// A single instruction in a :class:`.QuantumCircuit`, comprised of the :attr:`operation` and
Expand Down Expand Up @@ -267,10 +281,17 @@ impl CircuitInstruction {
}

#[getter]
fn unit(&self) -> Option<&str> {
fn unit(&self, py: Python) -> Py<PyString> {
// Python space uses `"dt"` as the default, whereas we simply don't store the extra
// attributes at all if they're none.
self.extra_attrs
.as_ref()
.and_then(|attrs| attrs.unit.as_deref())
.map(|attrs| attrs.py_unit(py))
.unwrap_or_else(|| {
ExtraInstructionAttributes::default_unit(py)
.clone()
.unbind()
})
}

/// Is the :class:`.Operation` contained in this instruction a Qiskit standard gate?
Expand Down Expand Up @@ -524,10 +545,18 @@ impl<'py> FromPyObject<'py> for OperationFromPython {
.map(|params| params.unwrap_or_default())
};
let extract_extra = || -> PyResult<_> {
let unit = {
// We accept Python-space `None` or `"dt"` as both meaning the default `"dt"`.
let raw_unit = ob.getattr(intern!(py, "unit"))?;
(!(raw_unit.is_none()
|| raw_unit.eq(ExtraInstructionAttributes::default_unit(py))?))
.then(|| raw_unit.extract::<String>())
.transpose()?
};
Ok(ExtraInstructionAttributes::new(
ob.getattr(intern!(py, "label"))?.extract()?,
ob.getattr(intern!(py, "duration"))?.extract()?,
ob.getattr(intern!(py, "unit"))?.extract()?,
unit,
ob.getattr(intern!(py, "condition"))?.extract()?,
)
.map(Box::from))
Expand Down
2 changes: 1 addition & 1 deletion crates/circuit/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl StandardGate {
if let Some(extra) = extra_attrs {
let kwargs = [
("label", extra.label.to_object(py)),
("unit", extra.unit.to_object(py)),
("unit", extra.py_unit(py).into_any()),
("duration", extra.duration.to_object(py)),
]
.into_py_dict_bound(py);
Expand Down
Loading