diff --git a/qiskit/transpiler/target.py b/qiskit/transpiler/target.py index 769ad9fe56a4..0a1a57894ac0 100644 --- a/qiskit/transpiler/target.py +++ b/qiskit/transpiler/target.py @@ -1043,24 +1043,25 @@ def target_to_backend_properties(target: Target): continue qubit = qargs[0] props_list = [] - if props.error is not None: - props_list.append( - { - "date": datetime.datetime.utcnow(), - "name": "readout_error", - "unit": "", - "value": props.error, - } - ) - if props.duration is not None: - props_list.append( - { - "date": datetime.datetime.utcnow(), - "name": "readout_length", - "unit": "s", - "value": props.duration, - } - ) + if props is not None: + if props.error is not None: + props_list.append( + { + "date": datetime.datetime.utcnow(), + "name": "readout_error", + "unit": "", + "value": props.error, + } + ) + if props.duration is not None: + props_list.append( + { + "date": datetime.datetime.utcnow(), + "name": "readout_length", + "unit": "s", + "value": props.duration, + } + ) if not props_list: qubit_props = {} break diff --git a/releasenotes/notes/fix-transpile-ideal-measurement-c37e04533e196ded.yaml b/releasenotes/notes/fix-transpile-ideal-measurement-c37e04533e196ded.yaml new file mode 100644 index 000000000000..a1a30dfafb8d --- /dev/null +++ b/releasenotes/notes/fix-transpile-ideal-measurement-c37e04533e196ded.yaml @@ -0,0 +1,10 @@ +--- +fixes: + - | + Fixed an issue with :func:`~.transpile` when targeting a :class:`~.Target` + (either directly via the ``target`` argument or via a + :class:`~.BackendV2` instance from the ``backend`` argument) that + contained an ideal :class:`~.Measure` instruction (one that does not have + any properties defined). Previously this would raise an exception + trying to parse the target. + Fixed `#8969 `__ diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py index 292134f9b58b..7351fe6deb27 100644 --- a/test/python/compiler/test_transpiler.py +++ b/test/python/compiler/test_transpiler.py @@ -1726,6 +1726,19 @@ def test_qasm3_output_control_flow(self, optimization_level): # itself doesn't throw an error, though. self.assertIsInstance(qasm3.dumps(transpiled).strip(), str) + @data(0, 1, 2, 3) + def test_transpile_target_no_measurement_error(self, opt_level): + """Test that transpile with a target which contains ideal measurement works + + Reproduce from https://github.com/Qiskit/qiskit-terra/issues/8969 + """ + target = Target() + target.add_instruction(Measure(), {(0,): None}) + qc = QuantumCircuit(1, 1) + qc.measure(0, 0) + res = transpile(qc, target=target, optimization_level=opt_level) + self.assertEqual(qc, res) + class StreamHandlerRaiseException(StreamHandler): """Handler class that will raise an exception on formatting errors."""