Exemple #1
0
def test_memory_commands():
    parse_equals(
        "DECLARE mem OCTET[32] SHARING mem2 OFFSET 16 REAL OFFSET 32 REAL",
        Declare("mem", "OCTET", 32, shared_region="mem2", offsets=[(16, "REAL"), (32, "REAL")]),
    )
    parse_equals("STORE mem ro[2] ro[0]", STORE("mem", MemoryReference("ro", 2), MemoryReference("ro", 0)))
    parse_equals("STORE mem ro[2] 7", STORE("mem", MemoryReference("ro", 2), 7))
    parse_equals("LOAD ro[8] mem mem[4]", LOAD(MemoryReference("ro", 8), "mem", MemoryReference("mem", 4)))
    parse_equals("CONVERT ro[1] ro[2]", CONVERT(MemoryReference("ro", 1), MemoryReference("ro", 2)))
    parse_equals("EXCHANGE ro[0] ro[1]", EXCHANGE(MemoryReference("ro", 0), MemoryReference("ro", 1)))
    parse_equals("MOVE mem[2] 4", MOVE(MemoryReference("mem", 2), 4))
    parse_equals("MOVE mem[2] -4", MOVE(MemoryReference("mem", 2), -4))
    parse_equals("MOVE mem[2] -4.1", MOVE(MemoryReference("mem", 2), -4.1))
Exemple #2
0
def test_ternary_classicals():
    p = Program()
    p.inst(LOAD(MemoryReference("ro", 0), "ro", MemoryReference("n", 0)),
           STORE("ro", MemoryReference("n", 0), MemoryReference("ro", 0)),
           EQ(MemoryReference("ro", 0), MemoryReference("ro", 1), MemoryReference("ro", 2)),
           GT(MemoryReference("ro", 0), MemoryReference("ro", 1), MemoryReference("ro", 2)),
           GE(MemoryReference("ro", 0), MemoryReference("ro", 1), MemoryReference("ro", 2)),
           LE(MemoryReference("ro", 0), MemoryReference("ro", 1), MemoryReference("ro", 2)),
           LT(MemoryReference("ro", 0), MemoryReference("ro", 1), MemoryReference("ro", 2)))
    assert p.out() == 'LOAD ro[0] ro n[0]\n' \
                      'STORE ro n[0] ro[0]\n' \
                      'EQ ro[0] ro[1] ro[2]\n' \
                      'GT ro[0] ro[1] ro[2]\n' \
                      'GE ro[0] ro[1] ro[2]\n' \
                      'LE ro[0] ro[1] ro[2]\n' \
                      'LT ro[0] ro[1] ro[2]\n'
Exemple #3
0
def test_ternary_classicals():
    p = Program()
    p.inst(
        LOAD(MemoryReference("ro", 0), "ro", MemoryReference("n", 0)),
        STORE("ro", MemoryReference("n", 0), MemoryReference("ro", 0)),
        STORE("ro", MemoryReference("n", 0), 0),
        STORE("ro", MemoryReference("n", 0), 0.1),
        EQ(MemoryReference("ro", 0), MemoryReference("ro", 1), 0),
        EQ(MemoryReference("ro", 0), MemoryReference("ro", 1), 0.0),
        EQ(MemoryReference("ro", 0), MemoryReference("ro", 1),
           MemoryReference("ro", 0)),
        GE(MemoryReference("ro", 0), MemoryReference("ro", 1), 1),
        GE(MemoryReference("ro", 0), MemoryReference("ro", 1), 1.1),
        GE(MemoryReference("ro", 0), MemoryReference("ro", 1),
           MemoryReference("ro", 1)),
        GT(MemoryReference("ro", 0), MemoryReference("ro", 1), 2),
        GT(MemoryReference("ro", 0), MemoryReference("ro", 1), 2.2),
        GT(MemoryReference("ro", 0), MemoryReference("ro", 1),
           MemoryReference("ro", 2)),
        LE(MemoryReference("ro", 0), MemoryReference("ro", 1), 3),
        LE(MemoryReference("ro", 0), MemoryReference("ro", 1), 3.3),
        LE(MemoryReference("ro", 0), MemoryReference("ro", 1),
           MemoryReference("ro", 3)),
        LT(MemoryReference("ro", 0), MemoryReference("ro", 1), 4),
        LT(MemoryReference("ro", 0), MemoryReference("ro", 1), 4.4),
        LT(MemoryReference("ro", 0), MemoryReference("ro", 1),
           MemoryReference("ro", 4)),
    )
    assert (p.out() == "LOAD ro[0] ro n[0]\n"
            "STORE ro n[0] ro[0]\n"
            "STORE ro n[0] 0\n"
            "STORE ro n[0] 0.1\n"
            "EQ ro[0] ro[1] 0\n"
            "EQ ro[0] ro[1] 0.0\n"
            "EQ ro[0] ro[1] ro[0]\n"
            "GE ro[0] ro[1] 1\n"
            "GE ro[0] ro[1] 1.1\n"
            "GE ro[0] ro[1] ro[1]\n"
            "GT ro[0] ro[1] 2\n"
            "GT ro[0] ro[1] 2.2\n"
            "GT ro[0] ro[1] ro[2]\n"
            "LE ro[0] ro[1] 3\n"
            "LE ro[0] ro[1] 3.3\n"
            "LE ro[0] ro[1] ro[3]\n"
            "LT ro[0] ro[1] 4\n"
            "LT ro[0] ro[1] 4.4\n"
            "LT ro[0] ro[1] ro[4]\n")
def test_all_instructions():
    pq = Program(H(0), X(1), RX(1.2, 2), CNOT(0, 1), CCNOT(0, 1, 2))
    ro = pq.declare("ro")
    pq.measure(0, ro)
    pq.defgate("mygate", [[1, 0], [0, 1]])
    pq.inst(("mygate", 0))
    pq.reset(0)
    pq += Program(NEG(ro), AND(ro, ro), ADD(ro, 1), EQ(ro, ro, ro))
    pq += Program(EXCHANGE(ro, ro), CONVERT(ro, ro))
    pq += Program(LOAD(ro, ro, ro), STORE(ro, ro, ro))

    G = QuilControlFlowGraph(pq)

    assert len(G.blocks) == 1
    assert set(G.nodes) == set([0])
    assert set(G.edges) == set()
    assert G.is_dag()