Repository navigation
Add OpenQASM 3.0 Support #8
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
Open
GiggleLiu
wants to merge
10
commits into
main
Choose a base branch
from
openqasm-3.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3959514
refactor
GiggleLiu ac7c641
Refactor to use type-based dispatch and cleanup unused code
GiggleLiu 2810acd
Fix expression parsing in QASM 3.0
GiggleLiu febee28
Fix gate modifiers, for loops, and expression parsing
GiggleLiu 4fbfaed
Remove QASMCommon module to avoid type piracy
GiggleLiu 5503590
Add Aqua.jl tests and mitigate type piracy
GiggleLiu 6bf3976
Address PR review feedback and fix CI issues
GiggleLiu 730b6f9
Remove unnecessary QASMToken wrapper, simplify type piracy
GiggleLiu 3f710f5
Drop Julia 1.0 support, require Julia 1.10+
GiggleLiu ce59a53
Add comprehensive test coverage for QASM 3.0 print_qasm functions
GiggleLiu 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |||||
| [](https://github.com/QuantumBFS/OpenQASM.jl/actions/workflows/ci.yml) | ||||||
| [](https://codecov.io/gh/QuantumBFS/OpenQASM.jl) | ||||||
|
|
||||||
| Tools for parsing OpenQASM. | ||||||
| Tools for parsing OpenQASM 2.0 and 3.0. | ||||||
|
|
||||||
| ## Installation | ||||||
|
|
||||||
|
|
@@ -24,16 +24,137 @@ pkg> add OpenQASM | |||||
|
|
||||||
| ## Usage | ||||||
|
|
||||||
| This package provides a simple function `OpenQASM.parse` to parse a QASM string to | ||||||
| its AST according to its BNF specification described in [OpenQASM 2.0](https://github.com/Qiskit/openqasm/tree/OpenQASM2.x). | ||||||
| This package provides a simple function `OpenQASM.parse` to parse QASM programs (both 2.0 and 3.0) to their AST representation. | ||||||
|
|
||||||
| ### OpenQASM 2.0 | ||||||
|
|
||||||
| Parse QASM 2.0 programs according to the [OpenQASM 2.0 specification](https://github.com/Qiskit/openqasm/tree/OpenQASM2.x): | ||||||
|
|
||||||
| ```julia | ||||||
| using OpenQASM | ||||||
|
|
||||||
| qasm_2_0 = """ | ||||||
| OPENQASM 2.0; | ||||||
| include "qelib1.inc"; | ||||||
| qreg q[2]; | ||||||
| creg c[2]; | ||||||
| h q[0]; | ||||||
| cx q[0], q[1]; | ||||||
| measure q -> c; | ||||||
| """ | ||||||
|
|
||||||
| ast = parse(qasm_2_0) # Auto-detects version 2.0 | ||||||
| ``` | ||||||
|
|
||||||
|  | ||||||
|
|
||||||
| ### OpenQASM 3.0 | ||||||
|
|
||||||
| Parse QASM 3.0 programs with support for classical types, control flow, gate modifiers, and input/output parameters: | ||||||
|
|
||||||
| ```julia | ||||||
| using OpenQASM | ||||||
|
|
||||||
| qasm_3_0 = """ | ||||||
| OPENQASM 3.0; | ||||||
| include "stdgates.inc"; | ||||||
|
|
||||||
| input float[64] theta; | ||||||
| qubit[2] q; | ||||||
| bit[2] c; | ||||||
|
|
||||||
| reset q[0]; | ||||||
| reset q[1]; | ||||||
|
|
||||||
| ry(theta) q[0]; | ||||||
| ctrl @ x q[0], q[1]; | ||||||
|
|
||||||
| measure q -> c; | ||||||
|
|
||||||
| if (c[0] == 1) { | ||||||
| x q[0]; | ||||||
| } | ||||||
|
|
||||||
| output bit[2] c; | ||||||
| """ | ||||||
|
|
||||||
| ast = parse(qasm_3_0) # Auto-detects version 3.0 | ||||||
| ``` | ||||||
|
|
||||||
| ### Supported OpenQASM 3.0 Features | ||||||
|
|
||||||
| This package currently supports the following OpenQASM 3.0 features: | ||||||
|
|
||||||
| - **Classical Types**: `int`, `uint`, `float`, `bit`, `angle` with optional bit widths | ||||||
| ```julia | ||||||
| int[32] x = 5; | ||||||
| float[64] y = 3.14; | ||||||
| const angle[20] theta = pi/4; | ||||||
| ``` | ||||||
|
|
||||||
| - **Control Flow**: `if-else`, `while`, `for` loops, `break`, `continue` | ||||||
| ```julia | ||||||
| if (c == 1) { x q; } else { h q; } | ||||||
| while (i < 10) { i = i + 1; } | ||||||
| for int i in [0:10] { ... } | ||||||
| for int i in {1, 5, 10} { ... } | ||||||
| ``` | ||||||
|
|
||||||
| - **Gate Modifiers**: `inv`, `ctrl`, `negctrl`, `pow` | ||||||
| ```julia | ||||||
| inv @ h q[0]; | ||||||
| ctrl @ x q[0], q[1]; | ||||||
| pow(2) @ s q[0]; | ||||||
| ``` | ||||||
|
|
||||||
| - **Input/Output Parameters**: Parameterized circuits | ||||||
| ```julia | ||||||
| input float[64] theta; | ||||||
| output bit[2] c; | ||||||
| ``` | ||||||
|
|
||||||
| - **Qubit Declarations**: New syntax alongside legacy `qreg`/`creg` | ||||||
| ```julia | ||||||
| qubit[2] q; // New QASM 3.0 syntax | ||||||
| qreg q[2]; // Legacy syntax (still supported) | ||||||
| ``` | ||||||
|
|
||||||
| ### API Reference | ||||||
|
|
||||||
| - `parse(src::String; version=:auto)` - Parse QASM program with auto version detection | ||||||
| - `parse_v2(src::String)` - Parse QASM 2.0 program explicitly | ||||||
| - `parse_v3(src::String)` - Parse QASM 3.0 program explicitly | ||||||
| - `detect_version(src::String)` - Detect OpenQASM version from source code | ||||||
| - `parse_gate(src::String)` - Parse a QASM 2.0 gate definition | ||||||
|
|
||||||
| ### Breaking Changes from OpenQASM 2.0 to 3.0 | ||||||
|
|
||||||
| Important differences to be aware of when migrating from QASM 2.0 to 3.0: | ||||||
|
|
||||||
| 1. **Qubit Initialization**: In QASM 3.0, qubits are **NOT** automatically initialized to |0⟩. You must explicitly use `reset`: | ||||||
| ```julia | ||||||
| // QASM 2.0: qreg q[2]; automatically initializes to |00⟩ | ||||||
| // QASM 3.0: qubit[2] q; does NOT initialize | ||||||
| qubit[2] q; | ||||||
| reset q; // Required to ensure |0⟩ state | ||||||
| ``` | ||||||
|
|
||||||
| 2. **New Syntax**: Prefer `qubit[n]` over `qreg`, and `bit[n]` over `creg` in QASM 3.0 (though legacy syntax is still supported) | ||||||
|
|
||||||
| 3. **Enhanced Expressions**: QASM 3.0 supports logical operators (`&&`, `||`), comparison operators (`==`, `!=`, `<`, `>`, `<=`, `>=`), and power operator (`**`) | ||||||
|
||||||
| 3. **Enhanced Expressions**: QASM 3.0 supports logical operators (`&&`, `||`), comparison operators (`==`, `!=`, `<`, `>`, `<=`, `>=`), and power operator (`**`) | |
| 3. **Enhanced Expressions**: QASM 3.0 supports logical operators (`&&`, `||`) and comparison operators (`==`, `!=`, `<`, `>`, `<=`, `>=`) |
This file contains hidden or 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.
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.
The while loop example includes an assignment statement
i = i + 1, but assignment statements are not yet implemented (as noted in the TODO comment at lines 469-485 in test/runtests.jl). This example code will not parse correctly. Either implement assignment statements or update the example to show a working while loop without assignments.