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

Add experimental OpenQASM 3 support for switch #9916

Merged
merged 5 commits into from
Apr 17, 2023

Conversation

jakelishman
Copy link
Member

@jakelishman jakelishman commented Apr 5, 2023

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.

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.
@jakelishman jakelishman added the mod: qasm2 Relating to OpenQASM 2 import or export label Apr 5, 2023
@jakelishman jakelishman added this to the 0.24.0 milestone Apr 5, 2023
@jakelishman jakelishman requested a review from a team as a code owner April 5, 2023 20:40
@qiskit-bot
Copy link
Collaborator

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:

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented Apr 5, 2023

Pull Request Test Coverage Report for Build 4705344190

  • 132 of 136 (97.06%) changed or added relevant lines in 5 files are covered.
  • 15 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.04%) to 85.821%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qasm3/exporter.py 62 63 98.41%
qiskit/qasm3/printer.py 44 47 93.62%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/tools/pi_check.py 1 91.23%
qiskit/pulse/library/waveform.py 3 91.67%
crates/accelerate/src/sabre_swap/layer.rs 4 97.32%
crates/qasm2/src/lex.rs 7 90.89%
Totals Coverage Status
Change from base Build 4704167625: 0.04%
Covered Lines: 70600
Relevant Lines: 82264

💛 - Coveralls

jakelishman added a commit to jakelishman/qiskit-terra that referenced this pull request Apr 6, 2023
@mtreinish mtreinish self-assigned this Apr 11, 2023
Copy link
Member

@mtreinish mtreinish left a 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.

qiskit/qasm3/__init__.py Outdated Show resolved Hide resolved
qiskit/qasm3/ast.py Outdated Show resolved Hide resolved
qiskit/qasm3/experimental.py Outdated Show resolved Hide resolved
qiskit/qasm3/__init__.py Show resolved Hide resolved
qiskit/qasm3/exporter.py Outdated Show resolved Hide resolved
qiskit/qasm3/exporter.py Outdated Show resolved Hide resolved
@mtreinish mtreinish added on hold Can not fix yet Changelog: New Feature Include in the "Added" section of the changelog labels Apr 14, 2023
@mtreinish
Copy link
Member

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)

jakelishman and others added 2 commits April 15, 2023 01:37
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@jakelishman
Copy link
Member Author

jakelishman commented Apr 15, 2023

I've fixed the documentation, and with all of the current switch PRs merged together, I was able to run this script on Wellington:

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:

OPENQASM 3;
include "stdgates.inc";
bit[3] c2;
int switch_dummy;
rz(1.5707963267949) $0;
sx $0;
rz(1.5707963267949) $0;
rz(1.5707963267949) $1;
sx $1;
rz(1.5707963267949) $1;
rz(1.5707963267949) $2;
sx $2;
rz(1.5707963267949) $2;
c2[0] = measure $0;
c2[1] = measure $1;
c2[2] = measure $2;
switch_dummy = c2;
switch (switch_dummy) {
  case 0: {
  }
  break;
  case 1: {
    x $0;
  }
  break;
  case 2: {
    x $1;
  }
  break;
  case 3: {
    x $0;
    x $1;
  }
  break;
  case 4: {
    x $2;
  }
  break;
  case 5: {
    x $2;
    x $0;
  }
  break;
  case 6: {
    x $2;
    x $1;
  }
  break;
  default: {
  }
  break;
}
c2[0] = measure $0;
c2[1] = measure $1;
c2[2] = measure $2;

cgsvh9tc26uins2q1aig
{'0': 3234, '1': 39, '10': 367, '11': 4, '100': 76, '101': 259, '110': 6, '111': 15}

--------------------------------------------------------------------------------

OPENQASM 3;
include "stdgates.inc";
bit[3] c2;
int switch_dummy;
rz(1.5707963267949) $0;
sx $0;
rz(1.5707963267949) $0;
rz(1.5707963267949) $1;
sx $1;
rz(1.5707963267949) $1;
rz(1.5707963267949) $2;
sx $2;
rz(1.5707963267949) $2;
c2[0] = measure $0;
c2[1] = measure $1;
c2[2] = measure $2;
switch_dummy = c2;
switch (switch_dummy) {
  case 0:
  case 1:
  case 2:
  case 3: {
    x $0;
  }
  break;
  case 4:
  case 5:
  case 6:
  case 7: {
    rz(1.5707963267949) $0;
    sx $0;
    rz(1.5707963267949) $0;
    cx $0, $1;
  }
  break;
  default: {
  }
  break;
}
c2[0] = measure $0;
c2[1] = measure $1;
c2[2] = measure $2;

cgsvhdg2tle0vokl6h3g
{'0': 1260, '1': 723, '10': 37, '11': 9, '100': 1025, '101': 736, '110': 122, '111': 88}

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.
@mtreinish mtreinish added this pull request to the merge queue Apr 17, 2023
Merged via the queue into Qiskit:main with commit 4d4d292 Apr 17, 2023
@jakelishman jakelishman deleted the switch/qasm3 branch April 17, 2023 20:55
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request May 22, 2023
* 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>
@jakelishman jakelishman added mod: qasm3 Related to OpenQASM 3 import or export and removed mod: qasm2 Relating to OpenQASM 2 import or export labels Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: qasm3 Related to OpenQASM 3 import or export
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants