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

✨ OpenQASM 3.0 support #309

Merged
merged 116 commits into from
Dec 13, 2023
Merged

✨ OpenQASM 3.0 support #309

merged 116 commits into from
Dec 13, 2023

Conversation

martin-fink
Copy link
Collaborator

@martin-fink martin-fink commented May 17, 2023

This PR replaces the existing OpenQASM 2.0 parser with a new OpenQASM 3.0 parser.

The new parser now builds a syntax tree, where type checking, constant evaluation, and translation to the Quantum circuit.

The parser can handle the following new features:

New Syntax

New syntax for declaring bits, qubit, measure operations. The old syntax (creg, qreg) is still supported.

qubit[8] q;
bit[8] c;

c[0] = measure q[0];
measure q[1] -> c[1];

if (c[0] == 1) {
  x q[0];
}

Gate modifiers

Gate modifiers (inv, ctrl, and negctrl) are now supported. This replaces the c prefix.
See the OpenQASM 3.0 specification for more information: https://openqasm.com/language/gates.html#quantum-gate-modifiers

ctrl @ x q[0], q[1]; // Equivalent to cx q
ctrl(2) @ x q[0], q[1], q[2]; // Equivalent to ccx q;

Classical constant values

The parser now supports classical computation with constant values. This can be used to e.g. define the number of quantum registers.

const uint N = 4;
qubit[N * 2];
x qubit[N * 2 - 1];

Additionally, all features of the previous parser are still supported.

The big features from OpenQASM 3.0 still missing are:

Copy link
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

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

Just some very early comments on the early draft.
It would be great to separate the generation of the parser from the actual library. Ideally the underlying grammar hardly changes and the Lexer and syntax tree construction remains rather constant.
As such, the parser generation should probably be hidden behind a CMake option (just as we hide the tests behind some CMake option).

The generation itself only needs to work on one particular system (probably Ubuntu, because it's easiest and the fastest).
It would be nice to have a separate CI check that can be used for checking whether everything still works. Maybe there even is some GitHub action for properly setting up ANTLr. Wouldn't be surprised. That CI check should probably also only run on changes to the respective files.

Of course, the generated parser should work under all systems. Maybe this setup becomes obsolete if we were to adopt QIR and the corresponding OQ3 parser. We'll have to see.

src/QuantumComputation.cpp Outdated Show resolved Hide resolved
src/parsers/qasm3_parser/cmake/FindANTLR.cmake Outdated Show resolved Hide resolved
@burgholzer burgholzer added this to the OpenQASM 3.0 Support milestone Jun 15, 2023
@burgholzer burgholzer added feature New feature or request Core Anything related to the Core library and IR labels Jun 15, 2023
@martin-fink
Copy link
Collaborator Author

There remains some refactoring and cleanup to do; currently, stdgates.inc is parsed from a string every time it is included.
Additionally, computing the inverse and controls is done on the parser level and will be moved to the QuantumComputation level.

@burgholzer
Copy link
Member

Really just a comment since I briefly looked at the PR: I think we don't really need the explicit UTF-8 support via an external library. std::string should be just fine for our purposes.

Another comment: try to factor out as much functionality as possible from the parser and try to put it into the Core library. Most importantly the modifiers. These can all happen in their own PR. Then this PR can be rebased and use the newly created functions 🙂

Copy link
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

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

Hey 👋🏼

I know you are still working on this, but I just had some time to kill at the airport and thought I'd give you some feedback. You should be able to use GitHub's batch commit feature (go to files changed and then add suggestions to commit batch) to resolve most of the suggestions/comments. Hope all of that makes sense.

README.md Outdated Show resolved Hide resolved
docs/index.md Outdated Show resolved Hide resolved
include/QuantumComputation.hpp Outdated Show resolved Hide resolved
include/operations/ClassicControlledOperation.hpp Outdated Show resolved Hide resolved
include/parsers/qasm3_parser/Parser.hpp Outdated Show resolved Hide resolved
src/operations/NonUnitaryOperation.cpp Outdated Show resolved Hide resolved
src/operations/StandardOperation.cpp Outdated Show resolved Hide resolved
src/operations/SymbolicOperation.cpp Outdated Show resolved Hide resolved
src/parsers/qasm3_parser/Scanner.cpp Outdated Show resolved Hide resolved
test/unittests/test_qfr_functionality.cpp Outdated Show resolved Hide resolved
@burgholzer burgholzer changed the title [WIP] OpenQASM 3.0 support ✨ OpenQASM 3.0 support Dec 11, 2023
@burgholzer burgholzer marked this pull request as ready for review December 11, 2023 17:30
@burgholzer
Copy link
Member

From my side I'm happy -- I will address the CodeQL warnings though. Regarding your questions which issues will be closed

Great, many thanks. Updated the list of linked issues accordingly.
I also took the liberty of adjusting the title from WIP to a proper title and update with the latest changes from main.
As a last step before merging: Could you please update the PR description so that it is a little more complete and elaborative on what is actually available now (and maybe also that the OQ2 parser is completely replaced by the new parser).
Afterwards, you can request a (final) review and I should be able to merge 🎉

@burgholzer burgholzer added the minor Minor version update label Dec 11, 2023
@martin-fink
Copy link
Collaborator Author

I've updated the description. Thanks!

Copy link
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

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

I now also briefly looked over the parser code itself and it is looking good ✨

I just left one last comment where I would like to get your opinion on a couple of things. Those things need not necessarily happen as part of this PR if they turn out to be to complicated.

@burgholzer
Copy link
Member

And I seem to have screwed up one of the tests with one of the changes above. Could you maybe fix that? 😅

martin-fink and others added 12 commits December 13, 2023 11:01
If the parser detects an `OpenQASM 2.0` version declaration,
`c` prefixes are treated as control modifiers.

This allows parsing the following gate:
```qasm
cccc x q[0], q[1], q[2], q[3], q[4];
// equivalent to
ctrl(4) @ x q[0], q[1], q[2], q[3], q[4];
```
The corresponding member was hardly maintained and even less frequently used. Might as well not bother keeping it.
…ason for not assuming these are supported natively in either of the two standards.
Copy link

codecov bot commented Dec 13, 2023

Codecov Report

Merging #309 (eb7a097) into main (d181d45) will increase coverage by 0.3%.
The diff coverage is 89.5%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main    #309     +/-   ##
=======================================
+ Coverage   90.4%   90.8%   +0.3%     
=======================================
  Files        114     127     +13     
  Lines      12033   13850   +1817     
  Branches    2058    2176    +118     
=======================================
+ Hits       10889   12583   +1694     
- Misses      1144    1267    +123     
Flag Coverage Δ
cpp 90.5% <89.5%> (+0.4%) ⬆️
python 99.7% <ø> (ø)
Files Coverage Δ
include/Definitions.hpp 86.6% <ø> (ø)
include/QuantumComputation.hpp 91.0% <100.0%> (+0.1%) ⬆️
include/operations/ClassicControlledOperation.hpp 96.8% <100.0%> (+8.9%) ⬆️
include/operations/CompoundOperation.hpp 93.2% <100.0%> (ø)
include/operations/NonUnitaryOperation.hpp 91.6% <ø> (ø)
include/operations/Operation.hpp 95.9% <ø> (ø)
include/operations/StandardOperation.hpp 100.0% <ø> (ø)
include/operations/SymbolicOperation.hpp 100.0% <ø> (ø)
include/parsers/qasm3_parser/Gate.hpp 100.0% <100.0%> (ø)
include/parsers/qasm3_parser/NestedEnvironment.hpp 100.0% <100.0%> (ø)
... and 25 more

... and 2 files with indirect coverage changes

Copy link
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

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

Many many thanks again for this nice contribution! Great to see OpenQASM 3 support within the MQT finally happening 🎉
Looking forward to some future additions to enhance the support even further.

@burgholzer burgholzer merged commit e193ddb into cda-tum:main Dec 13, 2023
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Anything related to the Core library and IR feature New feature or request minor Minor version update
Projects
Status: Done
Status: Done
2 participants