Skip to content

Commit

Permalink
Fix transpile() with a Target containing an ideal Measure
Browse files Browse the repository at this point in the history
This commit fixes an oversight in the transpile() function when running
with a Target (either directly or via a backend) that contains a
Measurement operation that is ideal (either globally or locally) with no
properties defined. This was not handled correctly in the function used
to convert a Target to a BackendProperties payload for passes that are
not yet target aware and this would cause an exception to be raised.
This commit fixes this edge case and excludes readout properties from
the generated BackendProperties in this case.

Fixes Qiskit#8969
  • Loading branch information
mtreinish committed Oct 25, 2022
1 parent de8e4dd commit 4fa6f30
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
37 changes: 19 additions & 18 deletions qiskit/transpiler/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/Qiskit/qiskit-terra/issues/8969>`__
13 changes: 13 additions & 0 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 4fa6f30

Please sign in to comment.