Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transpile() with a Target containing an ideal Measure #8995

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines 1065 to 1067
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a too small nitpick in the original code, but these lines suggest we cannot mix ideal Measures and real Measures (if do that, real Measures will be silently converted to ideal Measures here, right?). Warning here (or allowing the mixture) might be nice. That said, I don't think of any practical use case of such a mixture, so I'm fine with keeping the code as is until we encounter any concrete issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I do think it's unlikely in practice for there to be mixed ideal and real measurements but we should try to support that especially since it's possible combination and allowed in the target. The only reason we have to special case measurement here is because it's special in BackendProperties.

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