Skip to content

Commit 19611dc

Browse files
add extra tests, fix type
1 parent 6ca3161 commit 19611dc

2 files changed

Lines changed: 36 additions & 54 deletions

File tree

source/openpulse/openpulse/ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from __future__ import annotations
1313

1414
from dataclasses import dataclass
15-
from typing import List, Optional
15+
from typing import List, Optional, Union
1616

1717
# Re-export the existing AST classes from openqasm3
1818
# pylint: disable=unused-import
@@ -72,7 +72,7 @@ class CalibrationStatement(Statement):
7272
}
7373
"""
7474

75-
body: List[Statement]
75+
body: List[Union[Statement, Pragma]]
7676

7777

7878
# Override the class from openqasm3

source/openpulse/tests/test_openpulse_parser.py

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,19 @@
11
import dataclasses
22

33
import pytest
4-
5-
from openqasm3.visitor import QASMVisitor
6-
4+
from openpulse.ast import (AngleType, Annotation, ArrayLiteral, ArrayType,
5+
AssignmentOperator, CalibrationDefinition,
6+
CalibrationStatement, ClassicalArgument,
7+
ClassicalAssignment, ClassicalDeclaration,
8+
ComplexType, DurationType, ExpressionStatement,
9+
ExternArgument, ExternDeclaration, FloatLiteral,
10+
FloatType, ForInLoop, FrameType, FunctionCall,
11+
Identifier, IntegerLiteral, IntType, PortType,
12+
Pragma, Program, QASMNode, QuantumBarrier,
13+
RangeDefinition, ReturnStatement, UnaryExpression,
14+
UnaryOperator, WaveformType)
715
from openpulse.parser import parse
8-
from openpulse.ast import (
9-
AngleType,
10-
Annotation,
11-
ArrayLiteral,
12-
ArrayType,
13-
CalibrationDefinition,
14-
CalibrationStatement,
15-
ClassicalArgument,
16-
ClassicalDeclaration,
17-
ComplexType,
18-
DurationType,
19-
ExpressionStatement,
20-
ExternArgument,
21-
ExternDeclaration,
22-
FloatLiteral,
23-
FloatType,
24-
ForInLoop,
25-
FunctionCall,
26-
Identifier,
27-
IntegerLiteral,
28-
IntType,
29-
Pragma,
30-
Program,
31-
QASMNode,
32-
QuantumBarrier,
33-
RangeDefinition,
34-
ReturnStatement,
35-
UnaryExpression,
36-
UnaryOperator,
37-
FrameType,
38-
PortType,
39-
WaveformType,
40-
)
16+
from openqasm3.visitor import QASMVisitor
4117

4218

4319
class SpanGuard(QASMVisitor):
@@ -273,27 +249,33 @@ def test_array():
273249
def test_annotation_in_cal_block():
274250
p = """
275251
cal {
276-
@foo
252+
@word1
277253
int x = 10;
254+
255+
@word1 command1
256+
@word2 command2 32f%^&
257+
x = 0;
258+
259+
@word1 @not_a_separate_annotation uint x;
260+
z();
278261
}
279262
"""
280263

281-
program = parse(p)
282-
expected = Program(
283-
statements=[
284-
CalibrationStatement(
285-
body=[
286-
ClassicalDeclaration(
287-
type=IntType(),
288-
identifier=Identifier("x"),
289-
init_expression=IntegerLiteral(10),
290-
)
291-
]
292-
)
293-
]
264+
xdecl = ClassicalDeclaration(
265+
type=IntType(),
266+
identifier=Identifier("x"),
267+
init_expression=IntegerLiteral(10),
294268
)
295-
# annotate the declaration
296-
expected.statements[0].body[0].annotations = [Annotation("foo")]
269+
xdecl.annotations = [Annotation("word1")]
270+
271+
xassign = ClassicalAssignment(Identifier("x"), AssignmentOperator["="], IntegerLiteral(0))
272+
xassign.annotations = [Annotation("word1", "command1"), Annotation("word2", "command2 32f%^&")]
273+
274+
zcall = ExpressionStatement(FunctionCall(Identifier("z"), []))
275+
zcall.annotations = [Annotation("word1", "@not_a_separate_annotation uint x;")]
276+
program = parse(p)
277+
expected = Program(statements=[CalibrationStatement(body=[xdecl, xassign, zcall])])
278+
297279
assert _remove_spans(program) == expected
298280

299281

0 commit comments

Comments
 (0)