def available_quantum_device(): # Returns first available quantum device devices_dict = get_devices(as_dict=True) print('devices_dict: ', devices_dict) for device in devices_dict.values(): if (device.is_online()): return device # If we got here, there weren't any devices online return None
def with_error(n, s, runs): acorn = get_devices( as_dict=True)['19Q-Acorn'] # simulate noise with 19Q-Acorn qvm = QVMConnection(acorn, use_queue=True) # put in queue in case too long p = grover(n, s) # stores grover program job_id = qvm.run_async(p, list(range(n)), trials=runs) # stores result job = qvm.get_job(job_id) # stores job while not job.is_done(): # while the job isn't finished time.sleep(.1) # wait .1 seconds job = qvm.get_job(job_id) # update job status result = job.result() # store job result print(accuracy(result, s)) # prints accuracy rate
def test_qpu_run(forest: ForestConnection): devices = get_devices(async_endpoint=forest.async_endpoint, api_key=forest.api_key, user_id=forest.user_id, as_dict=True) for name, dev in devices.items(): if not dev.is_online: continue # TODO: gh-372. No way to query whether a device is available for running pytest.xfail("Please fix after gh-372") qpu = QPU(connection=forest, device_name=name) bitstrings = qpu.run( quil_program=Program(X(0), MEASURE(0, 0)), classical_addresses=[0], trials=1000, ) assert bitstrings.shape == (1000, 1) assert np.mean(bitstrings) > 0.8
def compiletoquil(myprogram): devices = get_devices(as_dict=True) agave = devices['8Q-Agave'] compiler = CompilerConnection(agave) print('\n# Original pyQuil program,\n\n', myprogram) job_id = compiler.compile_async(myprogram) job = compiler.wait_for_job(job_id) print('\n# Compiled quil code,\n\n', 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()) print('\n# End of compiling info\n') return #myprogram, job.compiled_quil()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # =========================================== # wavelet.py # # Doing the wavelet transform in PyQuil. # # written by Ryan LaRose <*****@*****.**> # at Michigan State University 05-17-18 # =========================================== from pyquil.quil import Program import pyquil.gates as gates from pyquil import api devices = api.get_devices(as_dict=True) acorn = devices['8Q-Agave'] compiler = api.CompilerConnection(acorn) qprog = Program() qprog.inst(gates.H(0), gates.H(1), gates.H(2), gates.CNOT(0, 2), gates.CNOT(1, 2)) job_id = compiler.compile_async(qprog) job = compiler.wait_for_job(job_id) print(job.compiled_quil()) qvm = api.QVMConnection()
if (x[0] == 0 and x[1] == 0): sum = sum + 1 return (float(sum) / float(seq_trials)) print(sum) gate_list = [gen_X, gen_Z] qubit_list = [13] seq_trials = 500 #how many times to test each sequence num_of_seqs = 20 #number of sequences of a given length to try range_of_seqs = range(0, 200) #every 10 up to 200 interval = 5 acorn = get_devices(as_dict=True)['19Q-Acorn'] qvm = QVMConnection(acorn) data = [] len_list = [] ave_fid = [] for length in range_of_seqs[::interval]: print(length) data = [] for j in range(0, num_of_seqs): data.append(Fidelity(length, seq_trials, gate_list, qubit_list, qvm)) len_list.append(length) print(sum(data) / float(len(data))) ave_fid.append(sum(data) / float(len(data)))
""" Spyder Editor This is a temporary script file. """ import pyquil.quil as pq from pyquil.quil import Program from pyquil import api from pyquil.gates import * from pyquil.api import CompilerConnection, get_devices from pyquil.quil import Pragma from qutip import * import numpy as np devices = get_devices(as_dict=True) acorn = devices['19Q-Acorn'] compiler = CompilerConnection(acorn) qvm = api.QVMConnection() qpu = api.QPUConnection('19Q-Acorn') qubits = [[10, 16, 11, 17, 12], [4, 9, 14, 19, 13]] def postselect_had(data): newdata = [] for item in data: if (item[0] + item[2] + item[3]) % 2 == 0 and (item[1] + item[2]) % 2 == 0: newdata.append([item[-1]])
from pyquil.api import CompilerConnection, get_devices from pyquil.gates import X, H, CNOT, MEASURE from pyquil.quil import Program program = Program( # put your program here... X(0), ) agave = get_devices(as_dict=True)['8Q-Agave'] compiler = CompilerConnection(device=agave) compiled = compiler.compile(program) print(compiled)
def main(): qvm = QVMConnection() agave = get_devices(as_dict=True)['8Q-Agave'] qvm_noisy = QVMConnection(agave) print( "Timestamp, Singlet (Wavefunction), Triplet (Wavefunction), Singlet (QVM), Triplet (QVM)," "Singlet (Noise), Triplet (Noise), 00 (Noise), 11 (Noise)," "Singlet (Compiled on QVM), Triplet (Compiled on QVM), 00 (Compiled on QVM), 11 (Compiled on QVM)," ) # Truncate file with compiled code open(FILENAME, "w").close() # Rotation for t in range(0, 50): # 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 on a perfect QVM (no noise) data = qvm.run(p, trials=1000) # simulate physical noise on QVM data_noisy = qvm_noisy.run(p, trials=1000) noisy_data_distr = distribution(data_noisy) agave = get_devices(as_dict=True)['8Q-Agave'] compiler = CompilerConnection(agave) job_id = compiler.compile_async(p) # wait_for_job has print statement # using this workaround to suppress it import sys, os _old_stdout = sys.stdout with open(os.devnull, 'w') as fp: sys.stdout = fp job = compiler.wait_for_job( job_id) # This is the only line that matters sys.stdout = _old_stdout # Run code compiled for 8Q-Agave on a noisy QVM # Per example on https://github.com/rigetticomputing/pyquil/blob/master/examples/run_quil.py p_compiled = Program(job.compiled_quil()) with open(FILENAME, "a") as fp: fp.write("Timestep: %s\n" % t) fp.write("%s" % job.compiled_quil()) fp.write("\n") data_compiled = qvm_noisy.run(p_compiled, trials=1000) compiled_data_distr = distribution(data_compiled) print("%s, %s, %s, %s, %s, %s, %s, %s ,%s, %s, %s, %s, %s" % ( t, probs['01'], probs['10'], distribution(data).get((0, 1), 0), distribution(data).get((1, 0), 0), noisy_data_distr.get((0, 1), 0), noisy_data_distr.get((1, 0), 0), noisy_data_distr.get((0, 0), 0), noisy_data_distr.get((1, 1), 0), compiled_data_distr.get((0, 1), 0), compiled_data_distr.get((1, 0), 0), compiled_data_distr.get((0, 0), 0), compiled_data_distr.get((1, 1), 0), ))
#!/usr/bin/env python # Author Dario Clavijo 2018 from pyquil.quilatom import QubitPlaceholder from pyquil.quil import Program, address_qubits from pyquil.api import QVMConnection from pyquil.gates import CNOT, H, X, Z, MEASURE from pyquil.api import get_devices, QPUConnection for device in get_devices(): if device.is_online(): print('Device {} is online'.format(device.name)) # this is the secret s = int(b'001100110011001100', 2) # 1110 class_readouts = [17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] qregs = QubitPlaceholder().register(18) tmp = QubitPlaceholder().register(1) prog = Program(X(tmp[0])) prog += Program(H(tmp[0])) for i in range(0, len(qregs)): prog += Program(H(qregs[i])) # this is the oracle, think of it as a blackbo for i in range(0, len(qregs)): if s & (1 << i): prog += Program(CNOT(qregs[i], tmp[0]))
def get_qc(name: str, *, as_qvm: bool = None, noisy: bool = None, connection: ForestConnection = None): """ Get a quantum computer. A quantum computer is an object of type :py:class:`QuantumComputer` and can be backed either by a QVM simulator ("Quantum/Quil Virtual Machine") or a physical Rigetti QPU ("Quantum Processing Unit") made of superconducting qubits. You can choose the quantum computer to target through a combination of its name and optional flags. There are multiple ways to get the same quantum computer. The following are equivalent:: >>> qc = get_qc("8Q-Agave-noisy-qvm") >>> qc = get_qc("8Q-Agave", as_qvm=True, noisy=True) and will construct a simulator of the 8q-agave chip with a noise model based on device characteristics. We also provide a means for constructing generic quantum simulators that are not related to a given piece of Rigetti hardware:: >>> qc = get_qc("9q-generic-qvm") >>> qc = get_qc("9q-generic", as_qvm=True) Redundant flags are acceptable, but conflicting flags will raise an exception:: >>> qc = get_qc("9q-generic-qvm") # qc is fully specified by its name >>> qc = get_qc("9q-generic-qvm", as_qvm=True) # redundant, but ok >>> qc = get_qc("9q-generic-qvm", as_qvm=False) # Error! Use :py:func:`list_quantum_computers` to retrieve a list of known qc names. This method is provided as a convenience to quickly construct and use QVM's and QPU's. Power users may wish to have more control over the specification of a quantum computer (e.g. custom noise models, bespoke topologies, etc.). This is possible by constructing a :py:class:`QuantumComputer` object by hand. Please refer to the documentation on :py:class:`QuantumComputer` for more information. :param name: The name of the desired quantum computer. This should correspond to a name returned by :py:func:`list_quantum_computers`. Names ending in "-qvm" will return a QVM. Names ending in "-noisy-qvm" will return a QVM with a noise model. Otherwise, we will return a QPU with the given name. :param as_qvm: An optional flag to force construction of a QVM (instead of a QPU). If specified and set to ``True``, a QVM-backed quantum computer will be returned regardless of the name's suffix :param noisy: An optional flag to force inclusion of a noise model. If specified and set to ``True``, a quantum computer with a noise model will be returned regardless of the name's suffix. The noise model for QVM's based on a real QPU is an empirically parameterized model based on real device noise characteristics. The generic QVM noise model is simple T1 and T2 noise plus readout error. See :py:func:`decoherance_noise_with_asymmetric_ro`. :param connection: An optional :py:class:ForestConnection` object. If not specified, the default values for URL endpoints, ping time, and status time will be used. Your user id and API key will be read from ~/.pyquil_config. If you deign to change any of these parameters, pass your own :py:class:`ForestConnection` object. :return: """ if connection is None: connection = ForestConnection() name, as_qvm, noisy = _parse_name(name, as_qvm, noisy) if name == '9q-generic': if not as_qvm: raise ValueError( "The device '9q-generic' is only available as a QVM") nineq_square = nx.convert_node_labels_to_integers( nx.grid_2d_graph(3, 3)) nineq_device = NxDevice(topology=nineq_square) if noisy: noise_model = decoherance_noise_with_asymmetric_ro( nineq_device.get_isa()) else: noise_model = None return QuantumComputer(name='9q-generic-qvm', qam=QVM(connection=connection, noise_model=noise_model), device=nineq_device) # At least based off a real device. device = get_devices(as_dict=True)[name] if not as_qvm: if noisy is not None and noisy: warnings.warn( "You have specified `noisy=True`, but you're getting a QPU. This flag " "is meant for controling noise models on QVMs.") return QuantumComputer(name=name, qam=QPU(device_name=name, connection=connection), device=device) if noisy: noise_model = device.noise_model name = "{name}-noisy-qvm".format(name=name) else: noise_model = None name = "{name}-qvm".format(name=name) return QuantumComputer(name=name, qam=QVM(connection=connection, noise_model=noise_model), device=device)
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), ))