Skip to content

Commit

Permalink
Avoid exception in Target.has_calibration for instruction without p…
Browse files Browse the repository at this point in the history
…roperties (#12526)

`Target.add_instruction` allows passing `None` in place of an
`InstructionProperties` instance. In this case, there will be no
`_calibration` attribute, so the `getattr` call properties needs to
provide a default value.

(cherry picked from commit 72f09ad)
  • Loading branch information
wshanks authored and mergify[bot] committed Jun 7, 2024
1 parent 4182aed commit def5be8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/transpiler/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ def has_calibration(
return False
if qargs not in self._gate_map[operation_name]:
return False
return getattr(self._gate_map[operation_name][qargs], "_calibration") is not None
return getattr(self._gate_map[operation_name][qargs], "_calibration", None) is not None

def get_calibration(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
:meth:`.Target.has_calibration` has been updated so that it does not raise
an exception for an instruction that has been added to the target with
``None`` for its instruction properties. Fixes
`#12525 <https://github.com/Qiskit/qiskit/issues/12525>`__.
25 changes: 25 additions & 0 deletions test/python/transpiler/test_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,31 @@ def test_get_empty_target_calibration(self):

self.assertIsNone(target["x"][(0,)].calibration)

def test_has_calibration(self):
target = Target()
properties = {
(0,): InstructionProperties(duration=100, error=0.1),
(1,): None,
}
target.add_instruction(XGate(), properties)

# Test false for properties with no calibration
self.assertFalse(target.has_calibration("x", (0,)))
# Test false for no properties
self.assertFalse(target.has_calibration("x", (1,)))

properties = {
(0,): InstructionProperties(
duration=self.custom_sx_q0.duration,
error=None,
calibration=self.custom_sx_q0,
)
}
target.add_instruction(SXGate(), properties)

# Test true for properties with calibration
self.assertTrue(target.has_calibration("sx", (0,)))

def test_loading_legacy_ugate_instmap(self):
# This is typical IBM backend situation.
# IBM provider used to have u1, u2, u3 in the basis gates and
Expand Down

0 comments on commit def5be8

Please sign in to comment.