|
1 | 1 | import dataclasses |
2 | 2 |
|
3 | 3 | import pytest |
4 | | - |
5 | | -from openqasm3.visitor import QASMVisitor |
6 | | - |
7 | | -from openpulse.parser import parse |
8 | 4 | from openpulse.ast import ( |
9 | 5 | AngleType, |
| 6 | + Annotation, |
10 | 7 | ArrayLiteral, |
11 | 8 | ArrayType, |
| 9 | + AssignmentOperator, |
12 | 10 | CalibrationDefinition, |
13 | 11 | CalibrationStatement, |
14 | 12 | ClassicalArgument, |
| 13 | + ClassicalAssignment, |
15 | 14 | ClassicalDeclaration, |
16 | 15 | ComplexType, |
17 | 16 | DurationType, |
|
21 | 20 | FloatLiteral, |
22 | 21 | FloatType, |
23 | 22 | ForInLoop, |
| 23 | + FrameType, |
24 | 24 | FunctionCall, |
25 | 25 | Identifier, |
26 | 26 | IntegerLiteral, |
27 | 27 | IntType, |
| 28 | + PortType, |
| 29 | + Pragma, |
28 | 30 | Program, |
29 | 31 | QASMNode, |
30 | 32 | QuantumBarrier, |
31 | 33 | RangeDefinition, |
32 | 34 | ReturnStatement, |
33 | 35 | UnaryExpression, |
34 | 36 | UnaryOperator, |
35 | | - FrameType, |
36 | | - PortType, |
37 | 37 | WaveformType, |
38 | 38 | ) |
| 39 | +from openpulse.parser import parse |
| 40 | +from openqasm3.visitor import QASMVisitor |
39 | 41 |
|
40 | 42 |
|
41 | 43 | class SpanGuard(QASMVisitor): |
@@ -268,6 +270,65 @@ def test_array(): |
268 | 270 | SpanGuard().visit(program) |
269 | 271 |
|
270 | 272 |
|
| 273 | +def test_annotation_in_cal_block(): |
| 274 | + p = """ |
| 275 | + cal { |
| 276 | + @word1 |
| 277 | + int x = 10; |
| 278 | +
|
| 279 | + @word1 command1 |
| 280 | + @word2 command2 32f%^& |
| 281 | + x = 0; |
| 282 | +
|
| 283 | + @word1 @not_a_separate_annotation uint x; |
| 284 | + z(); |
| 285 | + } |
| 286 | + """ |
| 287 | + |
| 288 | + xdecl = ClassicalDeclaration( |
| 289 | + type=IntType(), |
| 290 | + identifier=Identifier("x"), |
| 291 | + init_expression=IntegerLiteral(10), |
| 292 | + ) |
| 293 | + xdecl.annotations = [Annotation("word1")] |
| 294 | + |
| 295 | + xassign = ClassicalAssignment(Identifier("x"), AssignmentOperator["="], IntegerLiteral(0)) |
| 296 | + xassign.annotations = [Annotation("word1", "command1"), Annotation("word2", "command2 32f%^&")] |
| 297 | + |
| 298 | + zcall = ExpressionStatement(FunctionCall(Identifier("z"), [])) |
| 299 | + zcall.annotations = [Annotation("word1", "@not_a_separate_annotation uint x;")] |
| 300 | + program = parse(p) |
| 301 | + expected = Program(statements=[CalibrationStatement(body=[xdecl, xassign, zcall])]) |
| 302 | + |
| 303 | + assert _remove_spans(program) == expected |
| 304 | + |
| 305 | + |
| 306 | +def test_pragma_in_cal_block(): |
| 307 | + p = """ |
| 308 | + cal { |
| 309 | + #pragma foo bar |
| 310 | + int x = 10; |
| 311 | + } |
| 312 | + """ |
| 313 | + |
| 314 | + program = parse(p) |
| 315 | + expected = Program( |
| 316 | + statements=[ |
| 317 | + CalibrationStatement( |
| 318 | + body=[ |
| 319 | + Pragma(command="foo bar"), |
| 320 | + ClassicalDeclaration( |
| 321 | + type=IntType(), |
| 322 | + identifier=Identifier("x"), |
| 323 | + init_expression=IntegerLiteral(10), |
| 324 | + ), |
| 325 | + ] |
| 326 | + ) |
| 327 | + ] |
| 328 | + ) |
| 329 | + assert _remove_spans(program) == expected |
| 330 | + |
| 331 | + |
271 | 332 | @pytest.mark.parametrize( |
272 | 333 | "p", |
273 | 334 | [ |
|
0 commit comments