Skip to content

Commit 97901fe

Browse files
committed
Restrict qasm2 parser so it rejects multiple expressions in if body
1 parent e33e6af commit 97901fe

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

src/bloqade/qasm2/parse/qasm2.lark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version: INT "." INT
99
// stmts
1010
include: "include" STRING ";"
1111
ifstmt: "if" "(" expr "==" expr ")" ifbody
12-
ifbody: qop | "{" qop* "}" // allow multiple qops
12+
ifbody: qop
1313
opaque: "opaque" IDENTIFIER ["(" [params] ")"] qubits ";"
1414
barrier: "barrier" qubits ";"
1515
qreg: "qreg" IDENTIFIER "[" INT "]" ";"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
OPENQASM 2.0;
2+
include "qelib1.inc";
3+
qreg q[1];
4+
creg c[1];
5+
6+
if(c == 1) {
7+
x q[0];
8+
y q[0];
9+
}

test/qasm2/parse/programs/main.qasm

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,3 @@ measure q -> c;
2727
measure q[1] -> c[1];
2828

2929
if (c == 1) x q[0];
30-
if (c == 1) {
31-
x q[0];
32-
x q[0];
33-
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
OPENQASM 2.0;
2+
include "qelib1.inc";
3+
qreg q[1];
4+
creg c[1];
5+
6+
if(c == 1) x q[0];
7+
if(c == 1) y q[0];

test/qasm2/parse/test_roundtrip.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
import os
22
import pathlib
33

4+
import pytest
5+
from lark.exceptions import UnexpectedToken
6+
47
from bloqade.qasm2.parse import loads, spprint, loadfile
5-
from bloqade.qasm2.parse.parser import qasm2_parser as lark_parser
68

79

8-
def roundtrip(file):
9-
ast1 = loadfile(os.path.join(os.path.dirname(__file__), "programs", file))
10+
def roundtrip(file, dirname):
11+
ast1 = loadfile(os.path.join(os.path.dirname(__file__), dirname, file))
1012
ast2 = loads(spprint(ast1))
1113
return ast1 == ast2
1214

1315

1416
def test_roundtrip():
15-
path = pathlib.Path(__file__).parent / "programs"
17+
dirname = "programs"
18+
path = pathlib.Path(__file__).parent / dirname
1619
for file in path.glob("*.qasm"):
17-
assert roundtrip(file.name), f"Failed roundtrip for {file}"
18-
20+
assert roundtrip(file.name, dirname), f"Failed roundtrip for {file}"
1921

20-
if __name__ == "__main__":
21-
filepath = os.path.join(os.path.dirname(__file__), "programs", "global.qasm")
22-
print(filepath)
23-
with open(filepath) as f:
24-
qasm_str = f.read()
2522

26-
parse_tree = lark_parser.parse(qasm_str) # raw parsing seems to be fine
27-
print(parse_tree.pretty())
28-
29-
ast = loads(qasm_str)
30-
print(spprint(ast))
23+
def test_invalids():
24+
dirname = "invalid_programs"
25+
path = pathlib.Path(__file__).parent / dirname
26+
for file in path.glob("*.qasm"):
27+
with pytest.raises(UnexpectedToken):
28+
roundtrip(file.name, dirname)

0 commit comments

Comments
 (0)