Skip to content

Commit

Permalink
Fix: Bad compatibility with InstructionProperties
Browse files Browse the repository at this point in the history
- Make `InstructionProperties` "_calibration" attribute visible.
- Removed attribute "calibration", treat as class property.
- Other tweaks and fixes
  • Loading branch information
raynelfss committed Apr 12, 2024
1 parent bc089cc commit 6fc8679
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
13 changes: 6 additions & 7 deletions crates/accelerate/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ pub struct InstructionProperties {
pub duration: Option<f32>,
#[pyo3(get, set)]
pub error: Option<f32>,
pub calibration: Option<PyObject>,
#[pyo3(get)]
_calibration: Option<PyObject>,
}

#[pymethods]
Expand All @@ -74,33 +75,31 @@ impl InstructionProperties {
calibration: Schedule | ScheduleBlock | CalibrationEntry | None = None,)")]
pub fn new(duration: Option<f32>, error: Option<f32>, calibration: Option<PyObject>) -> Self {
InstructionProperties {
calibration,
error,
duration,
_calibration: calibration,
}
}

#[getter]
pub fn get_calibration(&self, py: Python<'_>) -> Option<PyObject> {
match &self.calibration {
match &self._calibration {
Some(calibration) => calibration.call_method0(py, "get_schedule").ok(),
None => None,
}
}

#[setter]
pub fn set_calibration(&mut self, py: Python<'_>, calibration: Bound<PyAny>) -> PyResult<()> {
self.calibration = Some(calibration.to_object(py));
self._calibration = Some(calibration.to_object(py));
Ok(())
}

fn __repr__(&self, py: Python<'_>) -> String {
if let Some(calibration) = self.get_calibration(py) {
format!(
"InstructionProperties(duration={:?}, error={:?}, calibration={:?})",
self.duration,
self.error,
calibration.call_method0(py, "__repr__")
self.duration, self.error, calibration
)
} else {
format!(
Expand Down
8 changes: 6 additions & 2 deletions qiskit/transpiler/_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def duration(self):
def error(self):
return self._InsrProp.error

@property
def _calibration(self):
return self._InsrProp._calibration

@error.setter
def error(self, other):
self._InsrProp.error = other
Expand Down Expand Up @@ -332,7 +336,7 @@ def update_from_instruction_schedule_map(self, inst_map, inst_name_map=None, err
props = None

entry = get_calibration(inst_name, qargs)
if entry.user_provided and getattr(props, "calibration", None) != entry:
if entry.user_provided and getattr(props, "_calibration", None) != entry:
# It only copies user-provided calibration from the inst map.
# Backend defined entry must already exist in Target.
if self.dt is not None:
Expand Down Expand Up @@ -380,7 +384,7 @@ def update_from_instruction_schedule_map(self, inst_map, inst_name_map=None, err
if isinstance(qargs, int):
qargs = (qargs,)
qlen.add(len(qargs))
cal = getattr(out_props[tuple(qargs)], "calibration")
cal = getattr(out_props[tuple(qargs)], "_calibration")
param_names.add(tuple(cal.get_signature().parameters.keys()))
if len(qlen) > 1 or len(param_names) > 1:
raise QiskitError(
Expand Down

0 comments on commit 6fc8679

Please sign in to comment.