def test_alias_statement(): p = """ let a = b; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement(target=Identifier(name="a"), value=Identifier(name="b")) ]) SpanGuard().visit(program) alias_statement = program.statements[0] assert alias_statement.span == Span(1, 0, 1, 9) assert alias_statement.target.span == Span(1, 4, 1, 4) assert alias_statement.value.span == Span(1, 8, 1, 8)
def test_gate_definition2(): p = """ gate majority a, b, c { cx c, b; cx c, a; ccx a, b, c; }""".strip() program = parse(p) assert program == Program(statements=[ QuantumGateDefinition( name=Identifier("majority"), arguments=[], qubits=[ Identifier(name="a"), Identifier(name="b"), Identifier(name="c"), ], body=[ QuantumGate( modifiers=[], name=Identifier("cx"), arguments=[], qubits=[Identifier(name="c"), Identifier(name="b")], ), QuantumGate( modifiers=[], name=Identifier("cx"), arguments=[], qubits=[Identifier(name="c"), Identifier(name="a")], ), QuantumGate( modifiers=[], name=Identifier("ccx"), arguments=[], qubits=[ Identifier(name="a"), Identifier(name="b"), Identifier(name="c"), ], ), ], ) ], ) SpanGuard().visit(program) gate_declaration = program.statements[0] assert gate_declaration.span == Span(1, 0, 5, 0) assert gate_declaration.qubits[0].span == Span(1, 14, 1, 14)
def test_single_gatecall(): p = """ h q; """.strip() program = parse(p) assert program == Program(statements=[ QuantumGate(modifiers=[], name=Identifier("h"), arguments=[], qubits=[Identifier(name="q")]) ]) SpanGuard().visit(program) quantum_gate = program.statements[0] assert quantum_gate.span == Span(1, 0, 1, 3) assert quantum_gate.qubits[0].span == Span(1, 2, 1, 2)
def test_subscript(): p = """ let a = b[10]; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement( target=Identifier(name="a"), value=Subscript(name="b", index=IntegerLiteral(value=10)), ) ]) SpanGuard().visit(program) subscript = program.statements[0] assert subscript.span == Span(1, 0, 1, 13) assert subscript.target.span == Span(1, 4, 1, 4) assert subscript.value.span == Span(1, 8, 1, 12)
def test_qubit_declaration(): p = """ qubit q; qubit[4] a; """.strip() program = parse(p) assert program == Program(statements=[ QubitDeclaration(qubit=Identifier(name="q"), size=None), QubitDeclaration( qubit=Identifier(name="a"), size=IntegerLiteral(4), ), ]) SpanGuard().visit(program) qubit_declaration = program.statements[0] assert qubit_declaration.span == Span(1, 0, 1, 7) assert qubit_declaration.qubit.span == Span(1, 6, 1, 6)
def test_selection(): p = """ let a = b[1, 2]; """.strip() program = parse(p) assert program == Program(statements=[ AliasStatement( target=Identifier(name="a"), value=Selection( name="b", indices=[IntegerLiteral( value=1), IntegerLiteral(value=2)]), ) ]) SpanGuard().visit(program) selection = program.statements[0] assert selection.span == Span(1, 0, 1, 15) assert selection.target.span == Span(1, 4, 1, 4) assert selection.value.span == Span(1, 8, 1, 14)
def test_bit_declaration(): p = """ bit c; """.strip() program = parse(p) assert program == Program(statements=[ ClassicalDeclaration(BitType(None), Identifier("c"), None) ]) SpanGuard().visit(program) classical_declaration = program.statements[0] assert classical_declaration.span == Span(1, 0, 1, 5)
def test_gate_definition3(): p = """ gate rz(λ) a { gphase(-λ/2); U(0, 0, λ) a; } """.strip() program = parse(p) assert program == Program(statements=[ QuantumGateDefinition( name=Identifier("rz"), arguments=[Identifier(name="λ")], qubits=[Identifier(name="a")], body=[ QuantumPhase( quantum_gate_modifiers=[], argument=BinaryExpression( op=BinaryOperator["/"], lhs=UnaryExpression(op=UnaryOperator["-"], expression=Identifier(name="λ")), rhs=IntegerLiteral(value=2), ), qubits=[], ), QuantumGate( modifiers=[], name=Identifier("U"), arguments=[ IntegerLiteral(value=0), IntegerLiteral(value=0), Identifier(name="λ"), ], qubits=[Identifier(name="a")], ), ], ) ]) SpanGuard().visit(program) gate_declaration = program.statements[0] assert gate_declaration.span == Span(1, 0, 1, 43) assert gate_declaration.arguments[0].span == Span(1, 8, 1, 8) assert gate_declaration.qubits[0].span == Span(1, 11, 1, 11)
def test_complex_declaration(): p = """ complex[int[24]] iq; """.strip() program = parse(p) assert program == Program(statements=[ ClassicalDeclaration( ComplexType(base_type=IntType(IntegerLiteral(24))), Identifier("iq"), None, ), ]) SpanGuard().visit(program) context_declaration = program.statements[0] assert context_declaration.span == Span(1, 0, 1, 19)