def get_quantum_machine():
    if USE_REAL_QCOMP:
        from pyquil.api import QVMConnection
        qm = QVMConnection()
    else:
        from pyquil.api import QPUConnection
        # Agave 8-qubit chip should be live and freely available
        qm = QPUConnection('8Q-Agave')
    return qm
Example #2
0
def test_qpu_connection():
    qpu = QPUConnection(device_name='fake_device')

    program = {
        "type": "multishot",
        "addresses": [0, 1],
        "trials": 2,
        "quil-instructions": "H 0\nCNOT 0 1\n"
    }

    def mock_queued_response(request, context):
        assert json.loads(request.text) == {
            "machine": "QPU",
            "program": program,
            "device": "fake_device"
        }
        return json.dumps({"jobId": JOB_ID, "status": "QUEUED"})

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID,
              [{
                  'text': json.dumps({
                      "jobId": JOB_ID,
                      "status": "RUNNING"
                  })
              }, {
                  'text':
                  json.dumps({
                      "jobId": JOB_ID,
                      "status": "FINISHED",
                      "result": [[0, 0], [1, 1]],
                      "program": program
                  })
              }])

        result = qpu.run(BELL_STATE, [0, 1], trials=2)
        assert result == [[0, 0], [1, 1]]
Example #3
0
from pyquil.quil import Program
from pyquil.api import QPUConnection
from pyquil.gates import *

qpu = QPUConnection(device_name='19Q-Acorn')

grover = Program(
    I(0),
    I(1),
    I(4),
    I(5),
    CNOT(5, 2),
    CNOT(0, 2),
    CNOT(4, 2),
    CNOT(1, 2),
)
print("Plaquette Z 0000")
result = qpu.run_and_measure(grover, [2], 100)
print(result)
# -*- coding: utf-8 -*-
from pyquil.quil import Program
from pyquil.api import QVMConnection
from random import choice
import numpy as np
from pyquil.gates import *
from math import pi
from pyquil.paulis import *
from pyquil.api import QVMConnection, QPUConnection, CompilerConnection
import pickle
import json

qvm = QVMConnection()
qpu = QPUConnection('19Q-Acorn')

from pyquil.api import get_devices

acorn = get_devices(as_dict=True)['19Q-Acorn']
qvmn = QVMConnection(acorn)

compiler = CompilerConnection(acorn)
devices = get_devices(as_dict=True)
acorn = devices['19Q-Acorn']
compiler = CompilerConnection(acorn)

comp_gates = {
    'RX(-pi/2) 0': 'RX(pi/2) 0',
    'RZ(-pi/2) 0': 'RZ(pi/2) 0',
    'RX(pi/2) 0': 'RX(-pi/2) 0',
    'RZ(pi/2) 0': 'RZ(-pi/2) 0'
}
Example #5
0
def test_qpu_connection(test_device):
    qpu = QPUConnection(device=test_device)

    run_program = {
        "type": "multishot",
        "addresses": [0, 1],
        "trials": 2,
        "uncompiled-quil": "H 0\nCNOT 0 1\nMEASURE 0 [0]\nMEASURE 1 [1]\n"
    }

    run_and_measure_program = {
        "type": "multishot-measure",
        "qubits": [0, 1],
        "trials": 2,
        "uncompiled-quil": "H 0\nCNOT 0 1\nMEASURE 0 [0]\nMEASURE 1 [1]\n"
    }

    reply_program = {
        "type": "multishot-measure",
        "qubits": [0, 1],
        "trials": 2,
        "uncompiled-quil": "H 0\nCNOT 0 1\nMEASURE 0 [0]\nMEASURE 1 [1]\n",
        "compiled-quil": "H 0\nCNOT 0 1\nMEASURE 0 [0]\nMEASURE 1 [1]\n"
    }

    def mock_queued_response_run(request, context):
        assert json.loads(request.text) == {
            "machine": "QPU",
            "program": run_program,
            "device": "test_device"
        }
        return json.dumps({"jobId": JOB_ID, "status": "QUEUED"})

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response_run)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID, [
            {'text': json.dumps({"jobId": JOB_ID, "status": "RUNNING"})},
            {'text': json.dumps({"jobId": JOB_ID, "status": "FINISHED",
                                 "result": [[0, 0], [1, 1]], "program": reply_program})}
        ])

        result = qpu.run(BELL_STATE_MEASURE, [0, 1], trials=2)
        assert result == [[0, 0], [1, 1]]

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response_run)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID, [
            {'text': json.dumps({"jobId": JOB_ID, "status": "RUNNING"})},
            {'text': json.dumps({"jobId": JOB_ID, "status": "FINISHED",
                                 "result": [[0, 0], [1, 1]], "program": reply_program,
                                 "metadata": {
                                     "compiled_quil": "H 0\nCNOT 0 1\nMEASURE 0 [0]\nMEASURE 1 [1]\n",
                                     "topological_swaps": 0,
                                     "gate_depth": 2
                                 }})}
        ])

        job = qpu.wait_for_job(qpu.run_async(BELL_STATE_MEASURE, [0, 1], trials=2))
        assert job.result().tolist() == [[0, 0], [1, 1]]
        assert job.compiled_quil() == Program(H(0), CNOT(0, 1), MEASURE(0, 0), MEASURE(1, 1))
        assert job.topological_swaps() == 0
        assert job.gate_depth() == 2

    def mock_queued_response_run_and_measure(request, context):
        assert json.loads(request.text) == {
            "machine": "QPU",
            "program": run_and_measure_program,
            "device": "test_device"
        }
        return json.dumps({"jobId": JOB_ID, "status": "QUEUED"})

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response_run_and_measure)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID, [
            {'text': json.dumps({"jobId": JOB_ID, "status": "RUNNING"})},
            {'text': json.dumps({"jobId": JOB_ID, "status": "FINISHED",
                                 "result": [[0, 0], [1, 1]], "program": reply_program})}
        ])

        result = qpu.run_and_measure(BELL_STATE, [0, 1], trials=2)
        assert result == [[0, 0], [1, 1]]

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response_run_and_measure)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID, [
            {'text': json.dumps({"jobId": JOB_ID, "status": "RUNNING"})},
            {'text': json.dumps({"jobId": JOB_ID, "status": "FINISHED",
                                 "result": [[0, 0], [1, 1]],
                                 "program": reply_program,
                                 "metadata": {
                                     "topological_swaps": 0,
                                     "gate_depth": 2
                                 }})}
        ])

        job = qpu.wait_for_job(qpu.run_and_measure_async(BELL_STATE, [0, 1], trials=2))
        assert job.result().tolist() == [[0, 0], [1, 1]]
        assert job.compiled_quil() == Program(H(0), CNOT(0, 1), MEASURE(0, 0), MEASURE(1, 1))
        assert job.topological_swaps() == 0
        assert job.gate_depth() == 2
Example #6
0
from pyquil.quil import Program
from pyquil.api import QPUConnection
from pyquil.gates import *


qpu = QPUConnection(device_name='19Q-Acorn')

grover = Program(
    H(1),
    H(2),
    S(1),
    S(2),
    H(2),
    CNOT(1, 2),
    H(2),
    S(1),
    S(2),
    H(1),
    H(2),
    X(1),
    X(2),
    H(2),
    CNOT(1, 2),
    H(2),
    X(1),
    X(2),
    H(1),
    H(2),

)
print("Grover N=2 A=00")
Example #7
0
def test_qpu_connection():
    qpu = QPUConnection(device_name='fake_device')

    program = {
        "type": "multishot",
        "addresses": [0, 1],
        "trials": 2,
        "quil-instructions": "H 0\nCNOT 0 1\n"
    }

    def mock_queued_response(request, context):
        assert json.loads(request.text) == {
            "machine": "QPU",
            "program": program,
            "device": "fake_device"
        }
        return json.dumps({"jobId": JOB_ID, "status": "QUEUED"})

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID,
              [{
                  'text': json.dumps({
                      "jobId": JOB_ID,
                      "status": "RUNNING"
                  })
              }, {
                  'text':
                  json.dumps({
                      "jobId": JOB_ID,
                      "status": "FINISHED",
                      "result": [[0, 0], [1, 1]],
                      "program": program
                  })
              }])

        result = qpu.run(BELL_STATE, [0, 1], trials=2)
        assert result == [[0, 0], [1, 1]]

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID,
              [{
                  'text': json.dumps({
                      "jobId": JOB_ID,
                      "status": "RUNNING"
                  })
              }, {
                  'text':
                  json.dumps({
                      "jobId": JOB_ID,
                      "status": "FINISHED",
                      "result": [[0, 0], [1, 1]],
                      "program": program,
                      "metadata": {
                          "compiled_quil": "H 0\nCNOT 0 1\n",
                          "topological_swaps": 0,
                          "gate_depth": 2
                      }
                  })
              }])

        job = qpu.wait_for_job(qpu.run_async(BELL_STATE, [0, 1], trials=2))
        assert job.result() == [[0, 0], [1, 1]]
        assert job.compiled_quil() == Program(H(0), CNOT(0, 1))
        assert job.topological_swaps() == 0
        assert job.gate_depth() == 2

    with requests_mock.Mocker() as m:
        m.post('https://job.rigetti.com/beta/job', text=mock_queued_response)
        m.get('https://job.rigetti.com/beta/job/' + JOB_ID,
              [{
                  'text': json.dumps({
                      "jobId": JOB_ID,
                      "status": "RUNNING"
                  })
              }, {
                  'text':
                  json.dumps({
                      "jobId": JOB_ID,
                      "status": "FINISHED",
                      "result": [[0, 0], [1, 1]],
                      "program": program
                  })
              }])

        job = qpu.wait_for_job(qpu.run_async(BELL_STATE, [0, 1], trials=2))
        assert job.result() == [[0, 0], [1, 1]]
        assert job.compiled_quil() is None
        assert job.topological_swaps() is None
        assert job.gate_depth() is None
Example #8
0

def distribution(data):
    """ Distribution of measurement of quantum system """
    combinations = {}
    for result in data:
        result_as_tuple = tuple(result)
        if result_as_tuple not in combinations:
            combinations[result_as_tuple] = 0
        combinations[result_as_tuple] += 1
    return combinations


if __name__ == '__main__':
    agave = get_devices(as_dict=True)['8Q-Agave']
    qpu = QPUConnection(agave)  # Physical QPU
    compiler = CompilerConnection(agave)

    p = Program()

    p.inst(X(0))
    p.inst(X(0))
    p.inst(X(1))
    p.inst(X(2))
    p.inst(X(3))
    p.inst(X(4))
    p.inst(X(5))
    p.measure(0, 0)
    p.measure(1, 1)
    p.measure(2, 2)
    p.measure(3, 3)
Example #9
0
from pyquil.quil import Program
from pyquil.api import QPUConnection
from pyquil.api import QVMConnection
from pyquil.gates import *

qpu = QPUConnection(device_name='19Q-Acorn')
qvm = QVMConnection()

test_run_and_measure_empty = Program()

print(test_run_and_measure_empty)
result = qvm.run_and_measure(Program(), [0], 1)

test_run = Program(H(0), CNOT(0, 1))

print("test_run")
result = qvm.run(test_run, [0], 1)
print(result)

test_run_async = Program(H(0), CNOT(0, 1))

print("test_run_async")
result = qvm.run(test_run_async, [0], 1)
print(result)

test_run_and_measure = Program(H(0), CNOT(0, 1))

print("test_run_and_measure")
result = qvm.run_and_measure(test_run_and_measure, [0], 1)
print(result)
from pyquil.quil import Program
from pyquil.api import QPUConnection, Job
from pyquil.gates import *
import pyquil.paulis as paulis
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

if __name__ == '__main__':
    N_qubit = 10
    for noise_prob in [0., .01, .05, .1]:
        #1% chance of each gate at each timestep
        pauli_channel = [noise_prob] * 3

        qpu = QPUConnection('19Q-Acorn')

        p = Program(H(0))
        for i in range(N_qubit - 1):
            p += Program(CNOT(i, i + 1))

        # print(qpu.wavefunction(p))

        print(p)

        classical_reg = [i for i in range(N_qubit)]
        for i in range(N_qubit):
            p.measure(i, i)

        num_trials = 1000
        result = qpu.run(p, classical_reg, trials=num_trials)
prog = address_qubits(prog)

print(prog)

print("Executing in the QVM")

qvm = QVMConnection()

wf = qvm.wavefunction(prog)
print(wf)
print(wf.amplitudes)

results = qvm.run(prog, classical_addresses=class_readouts, trials=10)

for r in results:
    print r

print("Executing in the QPU")

acorn = get_devices(as_dict=True)['19Q-Acorn']

qpu = QPUConnection(acorn)
# The device name as a string is also acceptable
# qpu = QPUConnection('19Q-Acorn')

results = qpu.run(prog, classical_addresses=class_readouts, trials=10)

for r in results:
    print r
Example #12
0
from pyquil.quil import Program
from pyquil.api import QPUConnection
from pyquil.gates import H, CNOT

qpu = QPUConnection(device_name='19Q-Acorn')

bell_state = Program(H(0), CNOT(0, 1))
result = qpu.run_and_measure(bell_state, [0, 1])
def main():
    agave = get_devices(as_dict=True)['8Q-Agave']
    compiler = CompilerConnection(agave)
    qvm = QVMConnection()  # Perfect QVM
    qvm_noisy = QVMConnection(agave)  # Simulate Noise
    qpu = QPUConnection(agave)  # Physical QPU
    print("Timestamp,"
          "Singlet (Wavefunction), Triplet (Wavefunction), "
          "Singlet (QVM), Triplet (QVM),"
          "Singlet Mean (QVM Noise), Singlet Std (QVM Noise), "
          "Triplet Mean (QVM Noise), Triplet Std (QVM Noise),"
          "00 Mean (QVM Noise), 00 Std (QVM Noise),"
          "11 Mean (QVM Noise), 11 Std (QVM Noise),"
          "Singlet Mean (QPU), Singlet Std (QPU),"
          "Triplet Mean (QPU), Triplet Std (QPU),"
          "00 Mean (QPU), 00 Std (QPU),"
          "11 Mean (QPU), 1 Std (QPU)")
    # Rotation
    fp_raw = open("output.txt", "w")
    for t in range(0, 30):  # ns
        # for t in np.arange(0.0, 30.0, 0.1):  # ns
        p = create_singlet_state()
        add_switch_to_singlet_triplet_basis_gate_to_program(p)
        w_larmor = 0.46  # 4.6e8 1/s as determined in the experiment
        p.inst(PHASE(w_larmor * t, 0))
        p.inst(("SWITCH_TO_SINGLET_TRIPLET_BASIS", 0, 1))
        wavefunction = qvm.wavefunction(p)
        probs = wavefunction.get_outcome_probs()

        p.measure(0, 0)
        p.measure(1, 1)

        # Run the code on a perfect QVM (no noise)
        data = qvm.run(p, trials=1024)

        # simulate physical noise on QVM
        singlet_noisy = []
        triplet_noisy = []
        state11_noisy = []
        state00_noisy = []
        for i in range(0, 3):
            data_noisy = qvm_noisy.run(p, trials=1000)
            noisy_data_distr = distribution(data_noisy)
            singlet_noisy.append(noisy_data_distr[(1, 0)])
            triplet_noisy.append(noisy_data_distr[(0, 1)])
            state11_noisy.append(noisy_data_distr[(1, 1)])
            state00_noisy.append(noisy_data_distr[(0, 0)])

        # Run the code on QPU
        singlet_qpu = []
        triplet_qpu = []
        state11_qpu = []
        state00_qpu = []

        # Suppress print statements
        _old_stdout = sys.stdout
        for i in range(0, 9):
            with open(os.devnull, 'w') as fp:
                sys.stdout = fp
                data_qpu = qpu.run(p, trials=1024)
            qpu_data_distr = distribution(data_qpu)
            singlet_qpu.append(qpu_data_distr[(1, 0)])
            triplet_qpu.append(qpu_data_distr[(0, 1)])
            state11_qpu.append(qpu_data_distr[(1, 1)])
            state00_qpu.append(qpu_data_distr[(0, 0)])

        sys.stdout = _old_stdout
        # print('compiled quil', job.compiled_quil())
        # print('gate volume', job.gate_volume())
        # print('gate depth', job.gate_depth())
        # print('topological swaps', job.topological_swaps())
        # print('program fidelity', job.program_fidelity())
        # print('multiqubit gate depth', job.multiqubit_gate_depth())

        # Note the order of qubit in Rigetti
        # http://pyquil.readthedocs.io/en/latest/qvm.html#multi-qubit-basis-enumeration
        # (1, 0) is singlet, but in string notation it is reversed ('01'), because
        #
        #  "The Rigetti QVM enumerates bitstrings such that qubit 0 is the least significant bit (LSB)
        #   and therefore on the right end of a bitstring"
        #
        print("%s, Noise, Singlet, %s" % (t, singlet_noisy), file=fp_raw)
        print("%s, Noise, Triplet, %s" % (t, triplet_noisy), file=fp_raw)
        print("%s, Noise, 00, %s" % (t, state00_noisy), file=fp_raw)
        print("%s, Noise, 11, %s" % (t, state11_noisy), file=fp_raw)
        print("%s, QPU, Singlet, %s" % (t, singlet_qpu), file=fp_raw)
        print("%s, QPU, Triplet, %s" % (t, triplet_qpu), file=fp_raw)
        print("%s, QPU, 00, %s" % (t, state00_qpu), file=fp_raw)
        print("%s, QPU, 11, %s" % (t, state11_qpu), file=fp_raw)
        print(
            "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s"
            % (
                t,
                probs['01'],
                probs['10'],
                distribution(data).get((1, 0), 0),
                distribution(data).get((0, 1), 0),
                np.mean(singlet_noisy),
                np.std(singlet_noisy),
                np.mean(triplet_noisy),
                np.std(triplet_noisy),
                np.mean(state00_noisy),
                np.std(state00_noisy),
                np.mean(state11_noisy),
                np.std(state11_noisy),
                np.mean(singlet_qpu),
                np.std(singlet_qpu),
                np.mean(triplet_qpu),
                np.std(triplet_qpu),
                np.mean(state00_qpu),
                np.std(state00_qpu),
                np.mean(state11_qpu),
                np.std(state11_qpu),
            ))