Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
 def visitLoopStatement(self, ctx: qasm3Parser.LoopStatementContext):
     if ctx.loopSignature().getChild(0).getText() == "while":
         return WhileLoop(
             while_condition=self.visit(ctx.loopSignature().expression()),
             block=self.visit(ctx.programBlock()),
         )
     else:  # For In Loop
         return ForInLoop(
             loop_variable=add_span(
                 Identifier(ctx.loopSignature().Identifier().getText()),
                 get_span(ctx.loopSignature().Identifier()),
             ),
             set_declaration=self.visit(
                 ctx.loopSignature().setDeclaration()),
             block=self.visit(ctx.programBlock()),
         )