def test_pramga(): p = """ #pragma {verbatim;} #pragma {my_statement1; my_statement2;} end; """.strip() program = parse(p) assert program == Program(statements=[ Pragma(statements=[ExpressionStatement(Identifier("verbatim"))]), Pragma(statements=[ ExpressionStatement(Identifier("my_statement1")), ExpressionStatement(Identifier("my_statement2")), ]), EndStatement(), ]) SpanGuard().visit(program)
def test_unary_expression(): p = """ ~b; !b; -i; """.strip() program = parse(p) assert program == Program(statements=[ ExpressionStatement(expression=UnaryExpression( op=UnaryOperator["~"], expression=Identifier(name="b"), )), ExpressionStatement(expression=UnaryExpression( op=UnaryOperator["!"], expression=Identifier(name="b"), )), ExpressionStatement(expression=UnaryExpression( op=UnaryOperator["-"], expression=Identifier(name="i"), )), ])
def test_durationof(): p = """ durationof({x $0;}) """.strip() program = parse(p) assert program == Program(statements=[ ExpressionStatement(expression=DurationOf(target=[ QuantumGate( modifiers=[], name=Identifier("x"), arguments=[], qubits=[Identifier("$0")], ), ])) ]) SpanGuard().visit(program)
def visitExpressionStatement(self, ctx: qasm3Parser.ExpressionStatementContext): return ExpressionStatement(self.visit(ctx.expression()))
def test_binary_expression_precedence(): p = """ b1 || b2 && b3; b1 | b2 ^ b3; b1 != b2 + b3; i1 >= i2 + i3; i1 - i2 << i3; i1 - i2 / i3; i1[i2] + -i1[i2]; -i1 ** i2; """.strip() program = parse(p) assert program == Program(statements=[ ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["||"], lhs=Identifier(name="b1"), rhs=BinaryExpression( op=BinaryOperator["&&"], lhs=Identifier(name="b2"), rhs=Identifier(name="b3"), ), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["|"], lhs=Identifier(name="b1"), rhs=BinaryExpression( op=BinaryOperator["^"], lhs=Identifier(name="b2"), rhs=Identifier(name="b3"), ), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["!="], lhs=Identifier(name="b1"), rhs=BinaryExpression( op=BinaryOperator["+"], lhs=Identifier(name="b2"), rhs=Identifier(name="b3"), ), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator[">="], lhs=Identifier(name="i1"), rhs=BinaryExpression( op=BinaryOperator["+"], lhs=Identifier(name="i2"), rhs=Identifier(name="i3"), ), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["<<"], lhs=BinaryExpression( op=BinaryOperator["-"], lhs=Identifier(name="i1"), rhs=Identifier(name="i2"), ), rhs=Identifier(name="i3"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["-"], lhs=Identifier(name="i1"), rhs=BinaryExpression( op=BinaryOperator["/"], lhs=Identifier(name="i2"), rhs=Identifier(name="i3"), ), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["+"], lhs=IndexExpression(collection=Identifier("i1"), index=[Identifier("i2")]), rhs=UnaryExpression( op=UnaryOperator["-"], expression=IndexExpression( collection=Identifier("i1"), index=[Identifier("i2")], ), ), ), ), ExpressionStatement(expression=UnaryExpression( op=UnaryOperator["-"], expression=BinaryExpression( op=BinaryOperator["**"], lhs=Identifier("i1"), rhs=Identifier("i2"), ), ), ), ])
def test_binary_expression(): p = """ b1 || b2; b1 && b2; b1 | b2; b1 ^ b2; b1 & b2; b1 != b2; i1 >= i2; i1 << i2; i1 - i2; i1 / i2; """.strip() program = parse(p) assert program == Program(statements=[ ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["||"], lhs=Identifier(name="b1"), rhs=Identifier(name="b2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["&&"], lhs=Identifier(name="b1"), rhs=Identifier(name="b2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["|"], lhs=Identifier(name="b1"), rhs=Identifier(name="b2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["^"], lhs=Identifier(name="b1"), rhs=Identifier(name="b2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["&"], lhs=Identifier(name="b1"), rhs=Identifier(name="b2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["!="], lhs=Identifier(name="b1"), rhs=Identifier(name="b2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator[">="], lhs=Identifier(name="i1"), rhs=Identifier(name="i2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["<<"], lhs=Identifier(name="i1"), rhs=Identifier(name="i2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["-"], lhs=Identifier(name="i1"), rhs=Identifier(name="i2"), )), ExpressionStatement(expression=BinaryExpression( op=BinaryOperator["/"], lhs=Identifier(name="i1"), rhs=Identifier(name="i2"), )), ])
def test_primary_expression(): p = """ π; pi; 5; 2.0; true; false; a; "openqasm"; sin(0.0); foo(x); 1.1ns; 0.3µs; 1E-4us; (x); q[1]; int[1](x); bool(x); sizeof(a); sizeof(a, 1); """.strip() program = parse(p) assert program == Program(statements=[ ExpressionStatement(expression=Constant(name=ConstantName.pi)), ExpressionStatement(expression=Constant(name=ConstantName.pi)), ExpressionStatement(expression=IntegerLiteral(5)), ExpressionStatement(expression=RealLiteral(2.0)), ExpressionStatement(expression=BooleanLiteral(True)), ExpressionStatement(expression=BooleanLiteral(False)), ExpressionStatement(expression=Identifier("a")), ExpressionStatement(expression=StringLiteral("openqasm")), ExpressionStatement( expression=FunctionCall(Identifier("sin"), [RealLiteral(0.0)])), ExpressionStatement( expression=FunctionCall(Identifier("foo"), [Identifier("x")])), ExpressionStatement(expression=DurationLiteral(1.1, TimeUnit.ns)), ExpressionStatement(expression=DurationLiteral(0.3, TimeUnit.us)), ExpressionStatement(expression=DurationLiteral(1e-4, TimeUnit.us)), ExpressionStatement(expression=Identifier("x")), ExpressionStatement( expression=IndexExpression(Identifier("q"), [IntegerLiteral(1)])), ExpressionStatement(expression=Cast( IntType(size=IntegerLiteral(1)), [Identifier("x")], )), ExpressionStatement(expression=Cast( BoolType(), [Identifier("x")], )), ExpressionStatement( expression=FunctionCall(Identifier("sizeof"), [Identifier("a")])), ExpressionStatement(expression=FunctionCall(Identifier( "sizeof"), [Identifier("a"), IntegerLiteral(1)]), ), ])