示例#1
0
def test_program_pop():
    prog = Program(X(0), X(1))
    instruction = prog.pop()
    assert prog.out() == "X 0\n"
    assert Program(instruction).out() == "X 1\n"
示例#2
0
def test_reset():
    p = Program()
    p.reset(0)
    p.reset()
    assert p.out() == "RESET 0\nRESET\n"
示例#3
0
def test_inst_tuple():
    p = Program()
    p.inst(("Y", 0),
           ("X", 1))
    assert len(p) == 2
    assert p.out() == "Y 0\nX 1\n"
示例#4
0
def test_implicit_declare():
    program = Program(MEASURE(0, 0))
    assert program.out() == ('DECLARE ro BIT[1]\n'
                             'MEASURE 0 ro[0]\n')
示例#5
0
def test_controlled_gates():
    p = Program(CNOT(0, 1), CCNOT(0, 1, 2))
    assert p.out() == 'CNOT 0 1\nCCNOT 0 1 2\n'
示例#6
0
def test_kraus():
    pq = Program(X(0))
    pq.define_noisy_gate("X", (0,), [
        [[0., 1.],
         [1., 0.]],
        [[0., 0.],
         [0., 0.]]
    ])
    pq.inst(X(1))
    pq.define_noisy_gate("X", (1,), [
        [[0., 1.],
         [1., 0.]],
        [[0., 0.],
         [0., 0.]]
    ])

    ret = pq.out()
    assert ret == """X 0
PRAGMA ADD-KRAUS X 0 "(0.0 1.0 1.0 0.0)"
PRAGMA ADD-KRAUS X 0 "(0.0 0.0 0.0 0.0)"
X 1
PRAGMA ADD-KRAUS X 1 "(0.0 1.0 1.0 0.0)"
PRAGMA ADD-KRAUS X 1 "(0.0 0.0 0.0 0.0)"
"""
    # test error due to bad normalization
    with pytest.raises(ValueError):
        pq.define_noisy_gate("X", (0,), [
            [[0., 1.],
             [1., 0.]],
            [[0., 1.],
             [1., 0.]]
        ])
    # test error due to bad shape of kraus op
    with pytest.raises(ValueError):
        pq.define_noisy_gate("X", (0,), [
            [[0., 1., 0.],
             [1., 0., 0.]],
            [[0., 1.],
             [1., 0.]]
        ])

    pq1 = Program(X(0))
    pq1.define_noisy_gate("X", (0,), [
        [[0., 1.],
         [1., 0.]],
        [[0., 0.],
         [0., 0.]]
    ])
    pq2 = Program(X(1))
    pq2.define_noisy_gate("X", (1,), [
        [[0., 1.],
         [1., 0.]],
        [[0., 0.],
         [0., 0.]]
    ])

    assert pq1 + pq2 == pq

    pq_nn = Program(X(0))
    pq_nn.no_noise()
    pq_nn.inst(X(1))

    assert pq_nn.out() == """X 0
示例#7
0
def test_classical_regs():
    p = Program()
    p.inst(X(0)).measure(0, 1)
    assert p.out() == 'X 0\nMEASURE 0 [1]\n'
示例#8
0
def test_singles():
    p = Program(I(0), X(0), Y(1), Z(1), H(2), T(2), S(1))
    assert p.out() == 'I 0\nX 0\nY 1\nZ 1\nH 2\nT 2\nS 1\n'
示例#9
0
def test_program_tuple():
    ig = Program()
    ig.inst(("Y", 0), ("X", 1))
    assert len(ig.actions) == 2
    assert ig.out() == "Y 0\nX 1\n"
示例#10
0
def test_prog_init():
    p = Program()
    p.inst(X(0)).measure(0, 0)
    assert p.out() == 'X 0\nMEASURE 0 [0]\n'
示例#11
0
 def native_quil_to_executable(self, nq_program: Program):
     return PyQuilExecutableResponse(
         program=nq_program.out(),
         attributes=_extract_attribute_dictionary_from_program(nq_program))
示例#12
0
def test_phases():
    p = Program(PHASE(np.pi, 1), CPHASE00(np.pi, 0, 1), CPHASE01(np.pi, 0, 1),
                CPHASE10(np.pi, 0, 1), CPHASE(np.pi, 0, 1))
    assert p.out() == 'PHASE(pi) 1\nCPHASE00(pi) 0 1\n' \
                      'CPHASE01(pi) 0 1\nCPHASE10(pi) 0 1\n' \
                      'CPHASE(pi) 0 1\n'
示例#13
0
def test_measurement_calls():
    p = Program()
    p.inst(MEASURE(0, 1), MEASURE(0, Addr(1)))
    assert p.out() == ('DECLARE ro BIT[2]\n'
                       'MEASURE 0 ro[1]\n'
                       'MEASURE 0 ro[1]\n')
示例#14
0
def test_classical_regs():
    p = Program()
    p.inst(Declare('ro', 'BIT', 2), X(0)).measure(0, MemoryReference("ro", 1))
    assert p.out() == ('DECLARE ro BIT[2]\n' 'X 0\n' 'MEASURE 0 ro[1]\n')
示例#15
0
def test_unary_classicals():
    p = Program()
    p.inst(TRUE(0), FALSE(Addr(1)), NOT(2))
    assert p.out() == 'TRUE [0]\n' \
                      'FALSE [1]\n' \
                      'NOT [2]\n'
示例#16
0
def test_simple_instructions():
    p = Program().inst(HALT, WAIT, RESET(), NOP)
    assert p.out() == 'HALT\nWAIT\nRESET\nNOP\n'
示例#17
0
def test_measurement_calls():
    p = Program()
    p.inst(MEASURE(0, 1), MEASURE(0, Addr(1)))
    assert p.out() == 'MEASURE 0 [1]\n' * 2
示例#18
0
def test_rotations():
    p = Program(RX(0.5, 0), RY(0.1, 1), RZ(1.4, 2))
    assert p.out() == 'RX(0.5) 0\nRY(0.1) 1\nRZ(1.4) 2\n'
示例#19
0
def test_measure_all():
    p = Program()
    p.measure_all((0, 0), (1, 1), (2, 3))
    assert p.out() == 'MEASURE 0 [0]\n' \
                      'MEASURE 1 [1]\n' \
                      'MEASURE 2 [3]\n'
示例#20
0
def test_swaps():
    p = Program(SWAP(0, 1), CSWAP(0, 1, 2), ISWAP(0, 1), PSWAP(np.pi, 0, 1))
    assert p.out() == 'SWAP 0 1\nCSWAP 0 1 2\nISWAP 0 1\nPSWAP(pi) 0 1\n'
示例#21
0
def test_swaps():
    p = Program(SWAP(0, 1), CSWAP(0, 1, 2), ISWAP(0, 1), PSWAP(np.pi)(0, 1))
    assert p.out(
    ) == 'SWAP 0 1\nCSWAP 0 1 2\nISWAP 0 1\nPSWAP(3.141592653589793) 0 1\n'
示例#22
0
def test_inst_gates():
    p = Program()
    p.inst(H(0), X(1))
    assert len(p) == 2
    assert p.out() == "H 0\nX 1\n"
示例#23
0
def test_if_option():
    reset_label_counter()
    p = Program(X(0)).measure(0, 0).if_then(0, Program(X(1)))
    assert p.out() == 'X 0\nMEASURE 0 [0]\nJUMP-WHEN @THEN1 [0]\nJUMP @END2\n' \
                      'LABEL @THEN1\nX 1\nLABEL @END2\n'
示例#24
0
def test_inst_string():
    p = Program()
    p.inst("Y 0",
           "X 1", )
    assert len(p) == 2
    assert p.out() == "Y 0\nX 1\n"
示例#25
0
def test_program_gates():
    ig = Program()
    ig.inst(H(0), X(1))
    assert len(ig.actions) == 2
    assert ig.out() == "H 0\nX 1\n"
示例#26
0
def test_no_implicit_declare():
    program = Program(
        Declare("read_out", "BIT", 5),
        MEASURE(0, MemoryReference("read_out", 4)))
    assert program.out() == ('DECLARE read_out BIT[5]\n'
                             'MEASURE 0 read_out[4]\n')
示例#27
0
def test_prog_init():
    p = Program()
    p.inst(X(0)).measure(0, 0)
    assert p.out() == ('DECLARE ro BIT[1]\n'
                       'X 0\n'
                       'MEASURE 0 ro[0]\n')
示例#28
0
def test_no_implicit_declare_2():
    program = Program(
        MEASURE(0, MemoryReference("asdf", 4)))
    assert program.out() == 'MEASURE 0 asdf[4]\n'
示例#29
0
def test_classical_regs():
    p = Program()
    p.inst(X(0)).measure(0, 1)
    assert p.out() == ('DECLARE ro BIT[2]\n'
                       'X 0\n'
                       'MEASURE 0 ro[1]\n')
示例#30
0
def test_plus_operator():
    p = Program()
    p += H(0)
    p += [X(0), Y(0), Z(0)]
    assert len(p) == 4
    assert p.out() == "H 0\nX 0\nY 0\nZ 0\n"
示例#31
0
p = Program()

"""
Gate definitions
"""

theta = Parameter('theta')
crx = np.array([[1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, cos(theta / 2), -1j * sin(theta / 2)],
                [0, 0, -1j * sin(theta / 2), cos(theta / 2)]])

p = Program().defgate("CRX", crx, [theta])

cry = np.array([[1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, cos(theta / 2), -sin(theta / 2)],
                [0, 0, sin(theta / 2), cos(theta / 2)]])

p = Program().defgate("CRY", cry, [theta])


crz = np.array([[1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, exp(-1j*(theta/2)), 0],
                [0, 0, 0, exp(1j*(theta/2))]])

#p = Program().defgate("CRZ", crz, [theta])

print(p.out())