-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add experimental OpenQASM 3 support for switch #9916
Conversation
This changes Terra's OpenQASM 3 exporter to add support for the IBM QSS `switch` extension to the OpenQASM 3 language. This is currently only a pull request to the OpenQASM 3 specification, so its syntax might be subject to some amount of change, and it is hidden behind an "experimental" flag in the exporter.
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this:
|
Pull Request Test Coverage Report for Build 4705344190
💛 - Coveralls |
Squashed versions of Qiskitgh-9916, Qiskitgh-9919 and Qiskitgh-9926.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this LGTM, just a few minor comments inline mostly docs stuff.
I tagged this as on hold to put the block in place until the manual validation is done. Once you confirm it's all working we can remove the tag (not that it blocks the merge queue like it did with mergify) |
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
I've fixed the documentation, and with all of the current from qiskit import (
QuantumCircuit,
QuantumRegister,
ClassicalRegister,
transpile,
qasm3,
)
from qiskit_ibm_provider import IBMProvider
backend = IBMProvider().get_backend("ibm_wellington")
def hacky_execute(qc, backend):
# Temporary workaround 1: trick Qiskit into knowing Wellington supports
# switch-case. Fixing this requires re-deployment of Wellington's config,
# and for the IBM Provider to learn about `SwitchCaseOp` in order to update
# its reported `Target`.
if "switch_case" not in backend.target:
from qiskit.circuit import SwitchCaseOp
backend.target.add_instruction(SwitchCaseOp, name="switch_case")
transpiled = transpile(qc, backend)
# Temporary workaround 2: I can't directly call `backend.run` with the
# circuit, because the IBM Provider doesn't yet know how to
# serialise/deserialise the new operation.
qasm3_program = qasm3.dumps(
transpiled,
disable_constants=True,
experimental=qasm3.ExperimentalFeatures.SWITCH_CASE_V1,
)
print(qasm3_program)
job = backend.run(qasm3_program, dynamic=True)
print(job.job_id())
print(job.result().get_counts())
regs = [QuantumRegister(3), ClassicalRegister(3)]
# Generate |000> + |001> + ... + |111>.
qc1 = QuantumCircuit(*regs)
qc1.h(regs[0])
qc1.measure(regs[0], regs[1])
with qc1.switch(regs[1]) as case:
# Reset back to zero, unless we measured 7, in which case leave it in
# place. This is obviously an inefficient way of doing that on hardware.
for i in range(7):
with case(i):
for j in range(3):
if i & (1 << j):
qc1.x(j)
# qss-compiler requires a `default` case (but Qiskit doesn't).
with case(case.DEFAULT):
pass
qc1.measure(regs[0], regs[1])
# The builder interface can also accept multiple cases pointing to the same
# block. The syntax for that is like:
qc2 = QuantumCircuit(*regs)
qc2.h([0, 1, 2])
qc2.measure([0, 1, 2], [0, 1, 2])
with qc2.switch(regs[1]) as case:
with case(0, 1, 2, 3):
qc2.x(0)
with case(4, 5, 6, 7):
qc2.h(0)
qc2.cx(0, 1)
with case(case.DEFAULT):
pass
qc2.measure([0, 1, 2], [0, 1, 2])
hacky_execute(qc1, backend)
print("\n" + "-" * 80 + "\n")
hacky_execute(qc2, backend) with the result:
So it works in principle. There's only so much testing I can do as a single person manually before we have support all the way through the stack, but at least the base cases work. |
The OED can claim that "-ize" is more correct etymologically all they like, but I don't see them re-erasing the "l" from "could". Why does "-ize" get special etymological treatment? Noah Webster wanted to go further and spell "is" as "iz", so hiz choices clearly can't be trusted either.
62fe914
to
816d7c2
Compare
* Add experimental OpenQASM 3 support for switch This changes Terra's OpenQASM 3 exporter to add support for the IBM QSS `switch` extension to the OpenQASM 3 language. This is currently only a pull request to the OpenQASM 3 specification, so its syntax might be subject to some amount of change, and it is hidden behind an "experimental" flag in the exporter. * Fix lint * Decolonise spelling Co-authored-by: Matthew Treinish <mtreinish@kortar.org> * Update documentation * Spelling The OED can claim that "-ize" is more correct etymologically all they like, but I don't see them re-erasing the "l" from "could". Why does "-ize" get special etymological treatment? Noah Webster wanted to go further and spell "is" as "iz", so hiz choices clearly can't be trusted either. --------- Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Summary
This changes Terra's OpenQASM 3 exporter to add support for the IBM QSS
switch
extension to the OpenQASM 3 language. This is currently only a pull request to the OpenQASM 3 specification, so its syntax might be subject to some amount of change, and it is hidden behind an "experimental" flag in the exporter.Details and comments
See openqasm/openqasm#463. Since the PR is not formally accepted yet, it is possible that the syntax might change slightly. Terra is adding support for the feature eagerly, before acceptance, but essentially hidden behind a "vendor" flag (c.f. web browsers'
--moz
,--webkit
etc CSS prefixes) until the proposal is formally accepted into the language. The experimental feature flag is versioned as a precaution, so we can eagerly add support for new syntaxes if they are suggested, but the merge process still takes time. Without any flag specified, Terra will not produce any extra-spec OpenQASM 3, but instead will raise an exception.Note: before merge, I need to re-verify that this can pass all the way through the IBM QSS stack. I last did that in December, so there's a chance that I've screwed something up / things have changed a little since.