def test_seeded_qvm(test_device): def mock_response(request, context): assert json.loads(request.text) == { "type": "multishot-measure", "qubits": [0, 1], "trials": 2, "compiled-quil": "H 0\nCNOT 0 1\n" } return '[[0,0],[1,1]]' with patch.object(CompilerConnection, "compile") as m_compile,\ patch('pyquil.api.qvm.apply_noise_model') as m_anm,\ requests_mock.Mocker() as m: m.post('https://api.rigetti.com/qvm', text=mock_response) m_compile.side_effect = [BELL_STATE] m_anm.side_effect = [BELL_STATE] qvm = QVMConnection(test_device) assert qvm.noise_model == test_device.noise_model qvm.run_and_measure(BELL_STATE, qubits=[0, 1], trials=2) assert m_compile.call_count == 1 assert m_anm.call_count == 1 test_device.noise_model = None qvm = QVMConnection(test_device) assert qvm.noise_model is None qvm.run_and_measure(BELL_STATE, qubits=[0, 1], trials=2) assert m_compile.call_count == 1 assert m_anm.call_count == 1 qvm = QVMConnection() assert qvm.noise_model is None qvm.run_and_measure(BELL_STATE, qubits=[0, 1], trials=2) assert m_compile.call_count == 1 assert m_anm.call_count == 1
from pyquil.gates import * qvm = QVMConnection() ins = Program() ins.inst(H(1), CNOT(1, 2)) # Creating B00 ins.inst(CNOT(0, 1), H(0)) ins.measure(0, 0).measure(1, 1).if_then(1, X(2)).if_then(0, Z(2)) wvf = qvm.wavefunction(ins, [0, 1]) #print( wvf) ins = Program( H(0), H(1), CNOT(1, 2), CNOT(0, 1), H(0), ) ins.measure(0, 0).measure(1, 1).if_then(1, X(2)).if_then(1, Z(2)) wvf = qvm.wavefunction(ins) print(ins) result = qvm.run_and_measure(ins, [2]) print(result) print(wvf)
# https://www.reddit.com/r/QuantumComputing/comments/7tfl74/what_does_this_circuit_do/?st=jcxukv2c&sh=f19ff94e from pyquil.quil import Program from pyquil.api import QVMConnection from pyquil.gates import * qvm = QVMConnection() inqnet = Program( H(1), CNOT(0, 1), CNOT(1, 2), CNOT(0, 1), H(0), CNOT(1, 2), CZ(2, 0), H(0), H(0), ) print("inqnet") result = qvm.run_and_measure(inqnet, [0, 1, 2], 5) print(result)
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() # f0 zero_one = Program( # Put qubit 0 and qubit 1 into superposition H(0), CNOT(0, 1), X(0), Z(0), CNOT(0, 1), H(0), ) wvf = qvm.wavefunction(zero_one) print(wvf) print("0 1") result = qvm.run_and_measure(zero_one, [0, 1], 100) print(result)
#from pyquil.api import QPUConnection from pyquil.api import QVMConnection from pyquil.gates import * qvm = QVMConnection() ins = Program() ins.inst(H(1), CNOT(1, 2)) # Creating B00 ins.inst(CNOT(0, 1), H(0)) ins.measure(0, 0).measure(1, 1).if_then(1, X(2)).if_then(0, Z(2)) wvf = qvm.wavefunction(ins, [0, 1]) #print( wvf) ins = Program( H(0), H(1), CNOT(1, 2), CNOT(0, 1), H(0), ) ins.measure(0, 0).measure(1, 1).if_then(1, X(2)).if_then(1, Z(2)) wvf = qvm.wavefunction(ins) print(ins) result = qvm.run_and_measure(ins, [2]) print(result) print(wvf)
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)
# unfinished: https://www.nature.com/articles/s41467-017-01904-7 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() grover = Program(X(3), H(0), H(1), H(2), H(3), X(0) CNOT(0, 1), CCNOT(1, 2, 3), H(0), H(1), H(2), H(3), X(0), X(1), X(2)) print("3 Qubit Grover") result = qvm.run_and_measure(grover, [0, 1, 2], 100) print(result)
else: num_ones += 1 return [num_zeros, num_ones] def get_Re(result): dist = measurement_distribution(result) return dist[0] - dist[1] hadamard_test0 = Program( X(0), H(0), CNOT(0, 1), H(0), ) print("Expected value will be closer to 0 than 100 or -100") result = qvm.run_and_measure(hadamard_test0, [0], 100) print(get_Re(result)) hadamard_test1 = Program( H(1), H(0), CNOT(0, 1), H(0), ) print("Expected value will 1") result = qvm.run_and_measure(hadamard_test1, [0], 1) print(get_Re(result))
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() phase_estimation_0 = Program( H(0), H(1), CNOT(0, 1), # CNOT is another name for cX which is cU in this example H(0), ) print("Expected answer is 0") result = qvm.run_and_measure(phase_estimation_0, [0], 1) print(result) phase_estimation_1 = Program( H(0), Z(0), H(1), CNOT(0, 1), # CNOT is another name for CX which is CU in this example H(0), ) print("Expected answer is 1") result = qvm.run_and_measure(phase_estimation_1, [0], 1) print(result)
else: num_ones += 1 return [num_zeros, num_ones] def get_Re(result): dist = measurement_distribution(result) return dist[0] - dist[1] hadamard_test0 = Program( X(0), H(0), CNOT(0, 1), H(0), ) print("Expected value will be closer to 0 than 100 or -100") result = qvm.run_and_measure(hadamard_test0, [0], 100) print(get_Re(result)) hadamard_test1 = Program( H(1), H(0), CNOT(0, 1), H(0), ) print("Expected value will 1") result = qvm.run_and_measure(hadamard_test1, [0], 1) print(get_Re(result))
from pyquil.quil import Program #from pyquil.api import QPUConnection from pyquil.api import QVMConnection from pyquil.gates import * from numpy import pi #qpu = QPUConnection(device_name='19Q-Acorn') qvm = QVMConnection() exp = Program(RX(pi/2, 0), I(0), RZ(-pi/2, 0), RX(-pi/2, 0), I(0), RZ(pi/2, 0)) print("EXPERIMENT 1") result = qvm.run_and_measure(exp, [0], 10) print(result)
from pyquil import Program from pyquil.gates import RY from pyquil.api import QVMConnection from matplotlib import pyplot as plt import numpy as np cxn = QVMConnection() thetas = np.linspace(0, np.pi, 20) bitstrings = [ np.asarray( cxn.run_and_measure(Program(RY(theta, 0)), qubits=[0], trials=1000)) for theta in thetas ] averages = [np.mean(bs[:, 0]) for bs in bitstrings] plt.plot(thetas, averages, 'o-') plt.show()
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() ghz_state = Program(H(0), H(1), X(2), CNOT(1, 2), CNOT(0, 2), H(0), H(1), H(2)) ghz_state_efficient = Program(H(0), CNOT(0, 1), CNOT(1, 2)) print("3Q GHZ State QVM") result = qvm.run_and_measure(ghz_state, [0, 1, 2], 100) print(result)
h_cost = -0.5 * sum(sI(nodes[0]) - sZ(i) * sZ(j) for i, j in graph) # The driver Hamiltonian is the sum of the application of \sigma_x^i for all qubits i. h_driver = -1. * sum(sX(i) for i in nodes) def qaoa_ansatz(gammas, betas): """ Function that returns a QAOA ansatz program for a list of angles betas and gammas. len(betas) == len(gammas) == P for a QAOA program of order P. :param list(float) gammas: Angles over which to parameterize the cost Hamiltonian. :param list(float) betas: Angles over which to parameterize the driver Hamiltonian. :return: The QAOA ansatz program. :rtype: Program. """ return Program([exponentiate_commuting_pauli_sum(h_cost)(g) + exponentiate_commuting_pauli_sum(h_driver)(b) \ for g, b in zip(gammas, betas)]) # Create a program, the state initialization plus a QAOA ansatz program, for P = 2. program = init_state_prog + qaoa_ansatz([0., 0.5], [0.75, 1.]) # Initialize the QVM and run the program. qvm = QVMConnection() from mock import patch with patch("pyquil.api.QVMConnection") as qvm: qvm.run_and_measure.return_value = [[1, 0, 1, 0], [1, 0, 1, 0]] results = qvm.run_and_measure(program, qubits=nodes, trials=2)
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)
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() # f0 zero_one = Program( # Put qubit 0 and qubit 1 into superposition H(0), CNOT(0, 1), X(0), Z(0), CNOT(0, 1), H(0), ) wvf = qvm.wavefunction(zero_one) print(wvf) print("0 1") result = qvm.run_and_measure(zero_one, [0, 1], 100) print(result)