Skip to content

Commit

Permalink
Fix: InstructionProperties could not receive calibrations
Browse files Browse the repository at this point in the history
- Fix wrong setters/getters for calibration in InstructionProperty object in rust.
  • Loading branch information
raynelfss committed Apr 11, 2024
1 parent dd7e115 commit f58fb09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
10 changes: 4 additions & 6 deletions crates/accelerate/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ impl<T: ToPyObject> IntoPy<PyObject> for HashableVec<T> {
pub struct InstructionProperties {
#[pyo3(get)]
pub duration: Option<f32>,
#[pyo3(get)]
#[pyo3(get, set)]
pub error: Option<f32>,
pub calibration: Option<PyObject>,
calibration_: Option<PyObject>,
}

#[pymethods]
Expand All @@ -61,21 +60,20 @@ impl InstructionProperties {
calibration,
error,
duration,
calibration_: Option::<PyObject>::None,
}
}

#[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, calibration: PyObject) -> PyResult<()> {
self.calibration_ = Some(calibration);
pub fn set_calibration(&mut self, py: Python<'_>, calibration: Bound<PyAny>) -> PyResult<()> {
self.calibration = Some(calibration.to_object(py));
Ok(())
}

Expand Down
20 changes: 14 additions & 6 deletions qiskit/transpiler/_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
from qiskit.providers.models.backendproperties import BackendProperties

# import target class from the rust side
from qiskit._accelerate.target import Target as Target2
from qiskit._accelerate.target import (
Target as Target2,
InstructionProperties as InstructionProperties2,
)

# TODO: Use InstructionProperties from Python side

Expand All @@ -76,15 +79,21 @@ def __init__(
error: float | None = None,
calibration: Schedule | ScheduleBlock | CalibrationEntry | None = None,
):
self._InsrProp = InstructionProperties2(duration, error, calibration)
self._InsrProp = InstructionProperties2(
duration=duration, error=error, calibration=calibration
)

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

@property
def error(self):
return self._InsrProp.eror
return self._InsrProp.error

@error.setter
def error(self, other):
self._InsrProp.error = other

@property
def calibration(self):
Expand Down Expand Up @@ -318,13 +327,12 @@ def update_from_instruction_schedule_map(self, inst_map, inst_name_map=None, err
except TypeError:
qargs = (qargs,)
try:
print(self._Target.gate_map[inst_name][qargs])
props = self._Target.gate_map[inst_name][qargs]
except (KeyError, TypeError):
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 @@ -372,7 +380,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 f58fb09

Please sign in to comment.