-
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 importer for OpenQASM 3 #9347
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9184649
Add importer for OpenQASM 3
jakelishman 131909b
Expand capabilities section of release note
jakelishman cc8548c
Merge remote-tracking branch 'ibm/main' into add-qasm3-import
jakelishman 45f996a
Merge branch 'main' into add-qasm3-import
jakelishman 531b7ff
Expand OQ3 import documentation
jakelishman e5b409a
Relax exact pin in optional requirement
jakelishman 66e3810
Merge remote-tracking branch 'ibm/main' into add-qasm3-import
jakelishman fc9931a
Remove superfluous word
jakelishman 85e0f5a
Merge remote-tracking branch 'ibm/main' into add-qasm3-import
jakelishman baf9af6
Update link to Qiskit org
jakelishman f121fa4
Merge remote-tracking branch 'ibm/main' into add-qasm3-import
jakelishman 95366fb
Merge branch 'main' into add-qasm3-import
mtreinish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
features: | ||
- | | ||
Support for importing OpenQASM 3 programs into Qiskit has been added. This can most easily be | ||
accessed using the functions :func:`.qasm3.loads` and :func:`.qasm3.load`, to load a program | ||
directly from a string and indirectly from a filename, respectively. For example, one can now | ||
do:: | ||
|
||
from qiskit import qasm3 | ||
|
||
circuit = qasm3.loads(""" | ||
OPENQASM 3.0; | ||
include "stdgates.inc"; | ||
|
||
qubit q; | ||
qubit[5] qr; | ||
bit c; | ||
bit[5] cr; | ||
|
||
h q; | ||
c = measure q; | ||
|
||
if (c) { | ||
h qr[0]; | ||
cx qr[0], qr[1]; | ||
cx qr[0], qr[2]; | ||
cx qr[0], qr[3]; | ||
cx qr[0], qr[4]; | ||
} else { | ||
h qr[4]; | ||
cx qr[4], qr[3]; | ||
cx qr[4], qr[2]; | ||
cx qr[4], qr[1]; | ||
cx qr[4], qr[0]; | ||
} | ||
cr = measure qr; | ||
""") | ||
|
||
This will load the program into a :class:`.QuantumCircuit` instance in the variable ``circuit``. | ||
jakelishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Not all OpenQASM 3 features are supported at first, because Qiskit does not yet have a way to | ||
represent advanced classical data processing. The capabilities of the importer will increase | ||
along with the capabilities of the rest of Qiskit. The initial feature set of the importer is | ||
approximately the same set of features that would be output by the exporter (:func:`.qasm3.dump` | ||
and :func:`.qasm3.dumps`). | ||
|
||
Note that Qiskit's support of OpenQASM 3 is not meant to provide a totally lossless | ||
representation of :class:`.QuantumCircuit`\ s. For that, consider using :mod:`qiskit.qpy`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
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.
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 namesload
,loads
,dump
anddumps
. I'm not keen to add an extra method toQuantumCircuit
because the class is already massively overloaded - I'd rather we keep things in separate packages. Theload
/dump
terminology is the standard Python names (e.g.pickle.dump
,json.dump
, etc), and it's consistent with the currently existingqasm3.dump
andqpy.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.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.
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.
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.
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 callqasm2.load
andqasm2.loads
as appropriate (assuming I do actually publish that parser and it's suitable for Terra).