Example #1
0
def test_indexing():
    program = Program(H(0), Y(1), CNOT(0, 1)).measure(0, 0).if_then(0, Program(X(0)), Program())
    assert program[0] == (0, H(0))
    for ii, action in enumerate(program.actions):
        assert program[ii] == action
Example #2
0
def test_qvm__default_client(client_configuration: QCSClientConfiguration):
    qvm = QVM(client_configuration=client_configuration)
    p = Program(Declare("ro", "BIT"), X(0), MEASURE(0, MemoryReference("ro")))
    result = qvm.run(p.wrap_in_numshots_loop(1000))
    bitstrings = result.readout_data.get("ro")
    assert bitstrings.shape == (1000, 1)
Example #3
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
Example #4
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"
Example #5
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'
Example #6
0
def test_get_qubit_placeholders():
    qs = QubitPlaceholder.register(8)
    pq = Program(X(qs[0]), CNOT(qs[0], qs[4]), MEASURE(qs[5], 5))
    assert pq.get_qubits() == {qs[i] for i in [0, 4, 5]}
Example #7
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"
Example #8
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')
Example #9
0
def test_prog_merge():
    prog_0 = Program(X(0))
    prog_1 = Program(Y(0))
    assert merge_programs([prog_0, prog_1]).out() == (prog_0 + prog_1).out()
Example #10
0
def test_instruction_group_gates():
    ig = InstructionGroup()
    ig.inst(H(0), X(1))
    assert len(ig.actions) == 2
    assert ig.out() == "H 0\nX 1\n"
Example #11
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'
Example #12
0
def test_classical_regs():
    p = Program()
    p.inst(X(0)).measure(0, 1)
    assert p.out() == 'X 0\nMEASURE 0 [1]\n'
Example #13
0
def test_prog_init():
    p = Program()
    p.inst(X(0)).measure(0, 0)
    assert p.out() == 'X 0\nMEASURE 0 [0]\n'
Example #14
0
def test_qvm_run_region_not_declared_not_measured(
        client_configuration: QCSClientConfiguration):
    qvm = QVM(client_configuration=client_configuration)
    p = Program(X(0))
    result = qvm.run(p.wrap_in_numshots_loop(100))
    assert result.readout_data.get("ro") is None
Example #15
0
def test():
    qvm = QVMConnection()
    p = Program(X(1), H(0), CNOT(0, 1))
    wf = qvm.wavefunction(p)
    wfo = str(wf)
    output.writetooutput()
Example #16
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"
Example #17
0
def test_qc_run(qvm, compiler):
    qc = get_qc("9q-square-noisy-qvm")
    bs = qc.run_and_measure(Program(X(0)), trials=3)
    assert len(bs) == 9
    for _, bits in bs.items():
        assert bits.shape == (3,)
Example #18
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')
Example #19
0
def test_get_qvm_with_topology_2(forest):
    topo = nx.from_edgelist([(5, 6), (6, 7)])
    qc = _get_qvm_with_topology(name="test-qvm", topology=topo)
    results = qc.run_and_measure(Program(X(5)), trials=5)
    assert sorted(results.keys()) == [5, 6, 7]
    assert all(x == 1 for x in results[5])
Example #20
0
def test_merge_with_pauli_noise():
    p = Program(X(0)).inst(Z(0))
    probs = [0., 1., 0., 0.]
    merged = merge_with_pauli_noise(p, probs, [0])
    assert merged.out() == """DEFGATE pauli_noise:
Example #21
0
def test_noisy(forest):
    # https://github.com/rigetti/pyquil/issues/764
    p = Program(X(0))
    qc = get_qc("1q-qvm", noisy=True)
    result = qc.run_and_measure(p, trials=10000)
    assert result[0].mean() < 1.0
Example #22
0
def test_get_qubits_not_as_indices():
    pq = Program(X(0), CNOT(0, 4), MEASURE(5, 5))
    assert pq.get_qubits(indices=False) == {Qubit(i) for i in [0, 4, 5]}
Example #23
0
def test_tomo_experiment_empty():
    suite = TomographyExperiment([], program=Program(X(0)))
    assert len(suite) == 0
    assert str(suite.program) == 'X 0\n'
Example #24
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"
Example #25
0
def test_remove_reset_from_program():
    p = Program(DEFGATE_X)
    p += RESET()
    p += X(0)
    new_p = _remove_reset_from_program(p)
    assert '\n' + new_p.out() == TRIMMED_PROG
Example #26
0
def test_len_one():
    prog = Program(X(0))
    assert len(prog) == 1
Example #27
0
    # measure the results and store them in classical registers [0] and [1]
    program.measure(start_index, ro[0])
    program.measure(ancilla_index, ro[1])

    program.if_then(ro[1], X(2))
    program.if_then(ro[0], Z(2))

    program.measure(end_index, ro[2])

    print(program)
    return program


if __name__ == "__main__":
    qvm = api.QVMConnection()

    # initialize qubit 0 in |1>
    teleport_demo = Program(X(0))
    teleport_demo += teleport(0, 2, 1)
    print("Teleporting |1> state: {}".format(qvm.run(teleport_demo, [2])))

    # initialize qubit 0 in |0>
    teleport_demo = Program()
    teleport_demo += teleport(0, 2, 1)
    print("Teleporting |0> state: {}".format(qvm.run(teleport_demo, [2])))

    # initialize qubit 0 in |+>
    teleport_demo = Program(H(0))
    teleport_demo += teleport(0, 2, 1)
    print("Teleporting |+> state: {}".format(qvm.run(teleport_demo, [2], 10)))