def test_gate_definition1(): p = """ gate xy q { x q; y q; } """.strip() program = parse(p) assert program == Program(statements=[ QuantumGateDefinition( Identifier("xy"), [], [Identifier("q")], [ QuantumGate( modifiers=[], name=Identifier("x"), arguments=[], qubits=[Identifier(name="q")], ), QuantumGate( modifiers=[], name=Identifier("y"), arguments=[], qubits=[Identifier(name="q")], ), ], ) ], ) SpanGuard().visit(program) gate_declaration = program.statements[0] assert gate_declaration.span == Span(1, 0, 4, 0) assert gate_declaration.qubits[0].span == Span(1, 8, 1, 8)
def test_subroutine_definition(): p = """ def ymeasure(qubit q) -> bit { s q; h q; return measure q; } """.strip() program = parse(p) assert program == Program(statements=[ SubroutineDefinition( name=Identifier("ymeasure"), arguments=[QuantumArgument(qubit=Identifier("q"), size=None)], return_type=BitType(None), body=[ QuantumGate( modifiers=[], name=Identifier("s"), arguments=[], qubits=[Identifier(name="q")], ), QuantumGate( modifiers=[], name=Identifier("h"), arguments=[], qubits=[Identifier(name="q")], ), ReturnStatement(expression=QuantumMeasurement(qubit=Identifier( name="q"))), ], ) ]) SpanGuard().visit(program)
def test_gate_calls(): p = """ qubit q; qubit r; h q; cx q, r; inv @ h q; """.strip() # TODO Add "ctrl @ pow(power) @ phase(theta) q, r;" after we complete expressions program = parse(p) assert program == Program(statements=[ QubitDeclaration(qubit=Identifier(name="q"), size=None), QubitDeclaration(qubit=Identifier(name="r"), size=None), QuantumGate(modifiers=[], name=Identifier("h"), arguments=[], qubits=[Identifier(name="q")]), QuantumGate( modifiers=[], name=Identifier("cx"), arguments=[], qubits=[Identifier(name="q"), Identifier(name="r")], ), QuantumGate( modifiers=[ QuantumGateModifier(modifier=GateModifierName["inv"], argument=None) ], name=Identifier("h"), arguments=[], qubits=[Identifier(name="q")], ), ], ) SpanGuard().visit(program)
def test_quantumloop(): p = """ box [maxdur] { delay[start_stretch] $0; for i in [1:2]{ h $0; cx $0, $1; } x $0; } """.strip() program = parse(p) print(parse(p)) assert program == Program(statements=[ Box( duration=Identifier("maxdur"), body=[ DelayInstruction( arguments=[], duration=Identifier("start_stretch"), qubits=[Identifier("$0")], ), ForInLoop( loop_variable=Identifier(name="i"), set_declaration=RangeDefinition( start=IntegerLiteral(value=1), end=IntegerLiteral(value=2), step=None, ), block=[ QuantumGate( modifiers=[], name=Identifier("h"), arguments=[], qubits=[Identifier(name="$0")], ), QuantumGate( modifiers=[], name=Identifier("cx"), arguments=[], qubits=[ Identifier(name="$0"), Identifier(name="$1") ], ), ], ), QuantumGate(modifiers=[], name=Identifier("x"), arguments=[], qubits=[Identifier("$0")]), ], ) ]) SpanGuard().visit(program)
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_branch_statement(): p = """ if(temp == 1) { ry(pi / 2) q; } else continue; """.strip() program = parse(p) assert program == Program(statements=[ BranchingStatement( condition=BinaryExpression( op=BinaryOperator["=="], lhs=Identifier("temp"), rhs=IntegerLiteral(1), ), if_block=[ QuantumGate( modifiers=[], name=Identifier("ry"), arguments=[ BinaryExpression( op=BinaryOperator["/"], lhs=Constant(ConstantName.pi), rhs=IntegerLiteral(2), ) ], qubits=[Identifier("q")], ), ], else_block=[ContinueStatement()], ) ]) SpanGuard().visit(program)
def test_box(): p = """ box [maxdur] { delay[start_stretch] $0; x $0; } """.strip() program = parse(p) assert program == Program(statements=[ Box( duration=Identifier("maxdur"), body=[ DelayInstruction( arguments=[], duration=Identifier("start_stretch"), qubits=[Identifier("$0")], ), QuantumGate(modifiers=[], name=Identifier("x"), arguments=[], qubits=[Identifier("$0")]), ], ) ]) SpanGuard().visit(program)
def test_gate_defs(): p = """ gate xyz q { x q; y q; z q; } """.strip() program = parse(p) assert program == Program(statements=[ QuantumGateDefinition( name=Identifier("xyz"), arguments=[], qubits=[Identifier(name="q")], body=[ QuantumGate( modifiers=[], name=Identifier("x"), arguments=[], qubits=[Identifier(name="q")], ), QuantumGate( modifiers=[], name=Identifier("y"), arguments=[], qubits=[Identifier(name="q")], ), QuantumGate( modifiers=[], name=Identifier("z"), arguments=[], qubits=[Identifier(name="q")], ), ], ) ], ) SpanGuard().visit(program)
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_for_in_loop(): p = """ for i in [0: 2] { majority a[i], b[i + 1], a[i + 1]; } """.strip() program = parse(p) assert program == Program(statements=[ ForInLoop( loop_variable=Identifier("i"), set_declaration=RangeDefinition( start=IntegerLiteral(0), end=IntegerLiteral(2), step=None), block=[ QuantumGate( modifiers=[], name=Identifier("majority"), arguments=[], qubits=[ IndexedIdentifier( name=Identifier(name="a"), indices=[[Identifier("i")]], ), IndexedIdentifier( name=Identifier("b"), indices=[[ BinaryExpression( op=BinaryOperator["+"], lhs=Identifier("i"), rhs=IntegerLiteral(1), ), ]], ), IndexedIdentifier( name=Identifier(name="a"), indices=[ [ BinaryExpression( op=BinaryOperator["+"], lhs=Identifier("i"), rhs=IntegerLiteral(1), ), ], ], ), ], ), ], ) ]) SpanGuard().visit(program)
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 visitQuantumGateCall(self, ctx: qasm3Parser.QuantumGateCallContext): modifiers = [ self.visit(modifier) for modifier in ctx.quantumGateModifier() ] gate_name = self.visit(ctx.quantumGateName()) expression_list = ctx.expressionList() if expression_list: arguments = [ self.visit(expression) for expression in expression_list.expression() ] else: arguments = [] qubits = [self.visit(qubit) for qubit in ctx.indexedIdentifier()] gate = QuantumGate(modifiers=modifiers, name=gate_name, arguments=arguments, qubits=qubits) return gate
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)