Skip to content

Commit

Permalink
Merge pull request #15 from itsubaki/antlr
Browse files Browse the repository at this point in the history
ANTLR v4
  • Loading branch information
itsubaki authored Jan 6, 2025
2 parents 85eb5f7 + 822aa1a commit fc2f946
Show file tree
Hide file tree
Showing 68 changed files with 24,009 additions and 7,305 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ jobs:
run: |
make parse
- name: Run testdata
run: |
make testdata
- name: Codecov
uses: codecov/codecov-action@v4
env:
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ coverage.txt

# codespaces
oryxBuildBinary
.vscode
.vscode

# https://marketplace.visualstudio.com/items?itemName=mike-lischke.vscode-antlr4
.antlr
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
39 changes: 11 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
SHELL := /bin/bash

test:
go test -v -cover $(shell go list ./... | grep -v /cmd | grep -v /repl | grep -v -E "qasm$$") -v -coverprofile=coverage.txt -covermode=atomic
antlr:
# https://github.com/openqasm/openqasm/blob/main/source/grammar
pip install antlr4-tools
antlr4 -Dlanguage=Go -visitor -o ./gen/parser -package parser qasm3Lexer.g4 qasm3Parser.g4

.PHONY: repl
repl:
go run main.go repl
update:
go get -u ./...
go mod tidy

lex:
go run main.go lex -f testdata/bell.qasm
go run main.go lex -f testdata/shor_def.qasm
go run cmd/lex/main.go < _testdata/bell.qasm

parse:
go run main.go parse -f testdata/bell.qasm
go run main.go parse -f testdata/shor_def.qasm

.PHONY: testdata
testdata:
go run main.go -verbose -f testdata/bell.qasm
go run main.go -verbose -f testdata/shor.qasm

install:
-rm ${GOPATH}/bin/qasm
go get -u
go mod tidy
go install

vet:
go vet ./...
go run cmd/parse/main.go < _testdata/bell.qasm

bench:
go test -bench . ./... --benchmem

doc:
godoc -http=:6060
test:
go test -v -cover $(shell go list ./... | grep -v /cmd | grep -v /gen) -v -coverprofile=coverage.txt -covermode=atomic
94 changes: 57 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,14 @@
[![codecov](https://codecov.io/gh/itsubaki/qasm/branch/main/graph/badge.svg?token=94KAQTK9KT)](https://codecov.io/gh/itsubaki/qasm)

- Quantum Computation Simulator with [OpenQASM 3.0](https://openqasm.com)
- Currently rewriting using ANTLR4 [WIP](https://github.com/itsubaki/qasm/tree/antlr)

## Install

```shell
go install github.com/itsubaki/qasm@latest
```

## Example

```shell
$ cat testdata/bell.qasm
OPENQASM 3.0;
include "testdata/stdgates.qasm";

qubit[2] q;
reset q;

h q[0];
cx q[0], q[1];
```

```shell
$ qasm -f testdata/bell.qasm
[00][ 0]( 0.7071 0.0000i): 0.5000
[11][ 3]( 0.7071 0.0000i): 0.5000
```

## REPL

```shell
$ qasm repl
% go run cmd/repl/main.go
>> OPENQASM 3.0;
>>
>> gate h q { U(pi/2.0, 0.0, pi) q; }
>> gate x q { U(pi, 0, pi) q; }
>> gate cx q, p { ctrl @ x q, p; }
>>
>> include "_testdata/stdgates.qasm";
>>
>> qubit[2] q;
[00][ 0]( 1.0000 0.0000i): 1.0000
>> h q[0];
Expand All @@ -54,8 +24,58 @@ $ qasm repl
[11][ 3]( 0.7071 0.0000i): 0.5000
```

## built-in
```shell
% go run cmd/repl/main.go
>> OPENQASM 3.0;
>>
>> const float ratio = pi;
>>
>> int n = 2;
>> if (n > 0) { n = n*ratio; }
>>
>> print
const : map[ratio:3.141592653589793]
variable : map[n:6.283185307179586]
bit : map[]
qubit : map[]
gate : []
subroutine: []
```
```shell
% go run cmd/lex/main.go < _testdata/bell.qasm
[@-1,0:7='OPENQASM',<1>,1:0]
[@-1,9:11='3.0',<103>,1:9]
[@-1,12:12=';',<63>,1:12]
[@-1,14:20='include',<2>,2:0]
[@-1,22:46='"_testdata/stdgates.qasm"',<105>,2:8]
[@-1,47:47=';',<63>,2:33]
[@-1,50:54='qubit',<31>,4:0]
[@-1,55:55='[',<56>,4:5]
[@-1,56:56='2',<91>,4:6]
[@-1,57:57=']',<57>,4:7]
[@-1,59:59='q',<93>,4:9]
[@-1,60:60=';',<63>,4:10]
[@-1,63:63='h',<93>,6:0]
[@-1,65:65='q',<93>,6:2]
[@-1,66:66='[',<56>,6:3]
[@-1,67:67='0',<91>,6:4]
[@-1,68:68=']',<57>,6:5]
[@-1,69:69=';',<63>,6:6]
[@-1,71:72='cx',<93>,7:0]
[@-1,74:74='q',<93>,7:3]
[@-1,75:75='[',<56>,7:4]
[@-1,76:76='0',<91>,7:5]
[@-1,77:77=']',<57>,7:6]
[@-1,78:78=',',<65>,7:7]
[@-1,80:80='q',<93>,7:9]
[@-1,81:81='[',<56>,7:10]
[@-1,82:82='1',<91>,7:11]
[@-1,83:83=']',<57>,7:12]
[@-1,84:84=';',<63>,7:13]
```
- `U`, `X`, `Y`, `Z`, `H`, `S`, `T`
- `QFT`, `IQFT`, `CMODEXP2`
- `print`
```shell
go run cmd/parse/main.go < _testdata/bell.qasm
(program (version OPENQASM 3.0 ;) (statementOrScope (statement (includeStatement include "_testdata/stdgates.qasm" ;))) (statementOrScope (statement (quantumDeclarationStatement (qubitType qubit (designator [ (expression 2) ])) q ;))) (statementOrScope (statement (gateCallStatement h (gateOperandList (gateOperand (indexedIdentifier q (indexOperator [ (expression 0) ])))) ;))) (statementOrScope (statement (gateCallStatement cx (gateOperandList (gateOperand (indexedIdentifier q (indexOperator [ (expression 0) ]))) , (gateOperand (indexedIdentifier q (indexOperator [ (expression 1) ])))) ;))) <EOF>)
```
3 changes: 1 addition & 2 deletions testdata/bell.qasm → _testdata/bell.qasm
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
OPENQASM 3.0;
include "testdata/stdgates.qasm";
include "_testdata/stdgates.qasm";

qubit[2] q;
reset q;

h q[0];
cx q[0], q[1];
6 changes: 6 additions & 0 deletions _testdata/stdgates.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
gate i q { U(0, 0, 0) q; }
gate h q { U(pi/2.0, 0, pi) q; }
gate x q { U(pi, 0, pi) q; }
gate y q { U(pi, pi/2.0, pi/2.0) q; }
gate z q { U(0, pi, 0) q; }
gate cx q0, q1 { ctrl @ U(pi, 0, pi) q0, q1;}
66 changes: 0 additions & 66 deletions ast/ast.go

This file was deleted.

85 changes: 0 additions & 85 deletions ast/ast_test.go

This file was deleted.

Loading

0 comments on commit fc2f946

Please sign in to comment.