Skip to content

Add importer for OpenQASM 3#9347

Merged
mergify[bot] merged 12 commits into
Qiskit:mainfrom
jakelishman:add-qasm3-import
Jan 19, 2023
Merged

Add importer for OpenQASM 3#9347
mergify[bot] merged 12 commits into
Qiskit:mainfrom
jakelishman:add-qasm3-import

Conversation

@jakelishman
Copy link
Copy Markdown
Member

@jakelishman jakelishman commented Jan 6, 2023

Summary

This adds an interface for importing OpenQASM 3 into Qiskit Terra. Right now, this is provided only as an optional dependency (the qasm3-import optional), using the package qiskit_qasm3_import, which in turn depends on the ANTLR runtime antlr4-python3-runtime.

The importer code may in the future be vendored into Terra, and will likely be replaced completely by a more efficient importer in the future, once a more concrete strategy has been decided for handling of classical components in quantum programs.

Details and comments

The added package this depends on was written entirely by me, and it's hosted here: https://github.com/jakelishman/qiskit-qasm3-import. That in turn depends on openqasm3, which is here: https://github.com/openqasm/openqasm/tree/main/source/openqasm (originally written by Li Chen and Yunong Shi at AWS with some help from me).

This adds the functionality as an optional dependency. At the moment, this to avoid adding additional dependencies on qiskit_qasm3_import and transitively on openqasm3 and antlr4-python3-runtime, as a courtesy to downstream consumers of Qiskit who may need to audit dependencies, since we may well not be sticking with ANTLR as the parsing strategy.

I could alternatively vendor all the code of qiskit_qasm3_import into Terra (there's no licensing issues, since I wrote that whole package anyway). Right now, I hard pin the dependency, so it is effectively like a vendoring, but with the ability to treat it as an optional dependency, but I could vendor the code, and do the import test on openqasm3 instead.

This adds an interface for importing OpenQASM 3 into Qiskit Terra.
Right now, this is provided only as an optional dependency (the
`qasm3-import` optional), using the package `qiskit_qasm3_import`, which
in turn depends on the ANTLR runtime `antlr4-python3-runtime`.

The importer code may in the future be vendored into Terra, and will
likely be replaced completely by a more efficient importer in the
future, once a more concrete strategy has been decided for handling of
classical components in quantum programs.
@jakelishman jakelishman added the mod: qasm2 Relating to OpenQASM 2 import or export label Jan 6, 2023
@jakelishman jakelishman added this to the 0.23.0 milestone Jan 6, 2023
@qiskit-bot
Copy link
Copy Markdown
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
Copy Markdown

coveralls commented Jan 6, 2023

Pull Request Test Coverage Report for Build 3961426967

  • 9 of 21 (42.86%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 84.927%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qasm3/init.py 6 18 33.33%
Totals Coverage Status
Change from base Build 3960963334: -0.01%
Covered Lines: 66649
Relevant Lines: 78478

💛 - Coveralls

Comment thread qiskit/qasm3/__init__.py
Comment on lines +63 to +64
.. autofunction:: load
.. autofunction:: loads
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not have any high-level disagreement with these function names on their own. It is however worth pointing out that this API breaks from the current OpenQASM2 APIs - https://qiskit.org/documentation/stubs/qiskit.qasm.Qasm.html#qiskit.qasm.Qasm.

I wonder if it might make sense to begin renaming the older API as qasm2 (with module deprecation paths) and to extend the QuantumCircuit methods for OpenQASM3 support?

This could be follow-up work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

While I was on holiday, I actually wrote a whole new OpenQASM 2 parser for Qiskit, with the majority in Rust. It was mostly for my own learning experience, but since it's like 10x faster than the current Qiskit one (with no external Python dependencies), I might make a PR changing it.

With or without though, though, I think we should move to a new qasm2 module with the names load, loads, dump and dumps. I'm not keen to add an extra method to QuantumCircuit because the class is already massively overloaded - I'd rather we keep things in separate packages. The load/dump terminology is the standard Python names (e.g. pickle.dump, json.dump, etc), and it's consistent with the currently existing qasm3.dump and qpy.dump as well - Qiskit is already semi-inconsistent, but really it's the old OQ2 handling that's inconsistent with the rest of the package direction.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, Jake, I'd be hesitant to add more work and potential breaks to the existing QASM2 parser for those that are using it given we're planning on migrating away from it but the pathway suggested for the qasm2 module seems good.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, agree that there's no reason to break users' workflows. My rough intent was to keep QuantumCircuit.from_qasm_{str,file}, and just cause them to internally call qasm2.load and qasm2.loads as appropriate (assuming I do actually publish that parser and it's suitable for Terra).

@mtreinish mtreinish added the Changelog: Added Add an "Added" entry in the GitHub Release changelog. label Jan 6, 2023
Copy link
Copy Markdown
Member

@kdk kdk left a comment

Choose a reason for hiding this comment

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

The overall approach here LGTM, and leaving as an optional import to save requirements churn seems reasonable. One comment on noting the limited scope of OpenQASM3 in the release note.

Comment thread releasenotes/notes/qasm3-import-0e7e01cb75aa6251.yaml
Copy link
Copy Markdown
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, I just had 2 inline comments/questions one about docs and one about the requirements entry. But other than those I think we're ready to move forward here.

Comment thread qiskit/qasm3/__init__.py
Comment thread setup.py Outdated
mtreinish
mtreinish previously approved these changes Jan 18, 2023
Copy link
Copy Markdown
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.

LGTM now, thanks for the quick updates.

Comment thread qiskit/qasm3/__init__.py
Copy link
Copy Markdown
Contributor

@jlapeyre jlapeyre left a comment

Choose a reason for hiding this comment

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

LGTM. I only found a minor suggested edit.

Comment thread qiskit/qasm3/__init__.py Outdated
@jakelishman
Copy link
Copy Markdown
Member Author

@jlapeyre: you'll need to give this your tick to override the "request changes" review you put on it, otherwise PR protection rules will prevent it from merging.

jlapeyre
jlapeyre previously approved these changes Jan 18, 2023
Copy link
Copy Markdown
Contributor

@jlapeyre jlapeyre left a comment

Choose a reason for hiding this comment

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

A OK

kdk
kdk previously requested changes Jan 18, 2023
Comment thread qiskit/utils/optionals.py Outdated
@mergify mergify Bot merged commit b2311c4 into Qiskit:main Jan 19, 2023
@jakelishman jakelishman deleted the add-qasm3-import branch January 19, 2023 19:44
@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
ElePT pushed a commit to ElePT/qiskit-algorithms that referenced this pull request Jul 27, 2023
* Add importer for OpenQASM 3

This adds an interface for importing OpenQASM 3 into Qiskit Terra.
Right now, this is provided only as an optional dependency (the
`qasm3-import` optional), using the package `qiskit_qasm3_import`, which
in turn depends on the ANTLR runtime `antlr4-python3-runtime`.

The importer code may in the future be vendored into Terra, and will
likely be replaced completely by a more efficient importer in the
future, once a more concrete strategy has been decided for handling of
classical components in quantum programs.

* Expand capabilities section of release note

* Expand OQ3 import documentation

* Relax exact pin in optional requirement

* Remove superfluous word

* Update link to Qiskit org

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Added Add an "Added" entry in the GitHub Release changelog. mod: qasm3 Related to OpenQASM 3 import or export

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants