Skip to content

Commit

Permalink
Fix: Use update_instruction_properties instead of inplace replacement.
Browse files Browse the repository at this point in the history
- Use `PyRef<Self>` for `__getitem__`
- Modify `generic_backend_v2` target generation.
- Modify test in `pulse_gate_pass`.
  • Loading branch information
raynelfss committed Aug 1, 2024
1 parent da3f0d2 commit fc37ddb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/accelerate/src/target_transpiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,21 +907,21 @@ impl Target {
}

/// Performs caching of python side dict objects for better access performance.
fn __getitem__(&mut self, key: &Bound<PyAny>) -> PyResult<PyObject> {
fn __getitem__(mut slf: PyRefMut<Self>, key: &Bound<PyAny>) -> PyResult<PyObject> {
let py: Python = key.py();
let Ok(key) = key.extract::<String>() else {return Err(PyKeyError::new_err(
"Invalid Key Type for Target."
));};
if let Some(val) = self._py_gate_map_cache.get(key.as_str()) {
if let Some(val) = slf._py_gate_map_cache.get(key.as_str()) {
Ok(val.as_any().clone_ref(py))
} else if let Some(val) = self.gate_map.get(key.as_str()) {
} else if let Some(val) = slf.gate_map.get(key.as_str()) {
let new_py: Vec<(Option<Bound<'_, PyTuple>>, PyObject)> = val
.clone()
.into_iter()
.map(|(key, val)| (key.map(|key| PyTuple::new_bound(py, key)), val.into_py(py)))
.collect();
let dict_py = new_py.into_py_dict_bound(py).unbind();
self._py_gate_map_cache.insert(key, dict_py.clone_ref(py));
slf._py_gate_map_cache.insert(key, dict_py.clone_ref(py));
Ok(dict_py.into())
} else {
Err(PyKeyError::new_err(format!(
Expand Down
6 changes: 5 additions & 1 deletion qiskit/providers/fake_provider/generic_backend_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,11 @@ def _add_noisy_instruction_to_target(
)
for qubit in qargs:
if qubit < self.num_qubits:
self._target[instruction.name][(qubit,)].calibration = calibration_entry
inst_prop = self._target[instruction.name][(qubit,)]
inst_prop.calibration = calibration_entry
self._target.update_instruction_properties(
instruction.name, (qubit,), inst_prop
)

def run(self, run_input, **options):
"""Run on the backend using a simulator.
Expand Down
8 changes: 6 additions & 2 deletions test/python/transpiler/test_pulse_gate_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ def test_transpile_with_custom_basis_gate_in_target(self):
seed=42,
).target

target["sx"][(0,)].calibration = self.custom_sx_q0
target["sx"][(1,)].calibration = self.custom_sx_q1
inst_prop_0 = target["sx"][(0,)]
inst_prop_0.calibration = self.custom_sx_q0
target.update_instruction_properties("sx", (0,), inst_prop_0)
inst_prop_1 = target["sx"][(1,)]
inst_prop_1.calibration = self.custom_sx_q1
target.update_instruction_properties("sx", (1,), inst_prop_1)

qc = circuit.QuantumCircuit(2)
qc.sx(0)
Expand Down

0 comments on commit fc37ddb

Please sign in to comment.