How to use pyquil - 10 common examples

To help you get started, we’ve selected a few pyquil examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rigetti / reference-qvm / tests / test_wavefunction.py View on Github external
def test_belltest(qvm):
    """
    Generate a bell state with fake qvm and qvm and compare
    """
    prog = Program().inst([Hgate(0), CNOTgate(0, 1)])
    bellout, _ = qvm.wavefunction(prog)
    bell = np.zeros((4, 1))
    bell[0, 0] = bell[-1, 0] = 1.0 / np.sqrt(2)
    assert np.allclose(bellout.amplitudes, bell)
github rigetti / reference-qvm / tests / test_unitary_generator.py View on Github external
def test_tensor_gates_single_qubit():
    prog = Program().inst([Hgate(0)])
    test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 1).toarray()
    true_unitary = gate_matrix['H']
    assert np.allclose(test_unitary, true_unitary)

    prog = Program().inst([Hgate(0)])
    test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 5).toarray()
    true_unitary = np.kron(np.eye(2**4), gate_matrix['H'])
    assert np.allclose(test_unitary, true_unitary)

    prog = Program().inst([RXgate(0.2)(3)])
    test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 5).toarray()
    true_unitary = np.kron(np.eye(2**1), np.kron(gate_matrix['RX'](0.2),  np.eye(2**3)))
    assert np.allclose(test_unitary, true_unitary)

    prog = Program().inst([RXgate(0.5)(4)])
    test_unitary = tensor_gates(gate_matrix, {}, prog.actions[0][1], 5).toarray()
    true_unitary = np.kron(np.eye(2**0), np.kron(gate_matrix['RX'](0.5),  np.eye(2**4)))
    assert np.allclose(test_unitary, true_unitary)
github rigetti / reference-qvm / tests / test_wavefunction.py View on Github external
def test_qaoa_circuit(qvm):
    wf_true = [0.00167784 + 1.00210180e-05*1j, 0.50000000 - 4.99997185e-01*1j,
               0.50000000 - 4.99997185e-01*1j, 0.00167784 + 1.00210180e-05*1j]
    wf_true = np.reshape(np.array(wf_true), (4, 1))
    prog = Program()
    prog.inst([RYgate(np.pi/2)(0), RXgate(np.pi)(0),
               RYgate(np.pi/2)(1), RXgate(np.pi)(1),
               CNOTgate(0, 1), RXgate(-np.pi/2)(1), RYgate(4.71572463191)(1),
               RXgate(np.pi/2)(1), CNOTgate(0, 1),
               RXgate(-2*2.74973750579)(0), RXgate(-2*2.74973750579)(1)])
    wf_test, _ = qvm.wavefunction(prog)
    assert np.allclose(wf_test.amplitudes, wf_true)
github rigetti / reference-qvm / tests / test_wavefunction.py View on Github external
def test_larger_qaoa_circuit(qvm):
    square_qaoa_circuit = [Hgate(0), Hgate(1), Hgate(2), Hgate(3),
                           Xgate(0),
                           PHASEgate(0.3928244130249029)(0),
                           Xgate(0),
                           PHASEgate(0.3928244130249029)(0),
                           CNOTgate(0, 1),
                           RZgate(0.78564882604980579)(1),
                           CNOTgate(0, 1),
                           Xgate(0),
                           PHASEgate(0.3928244130249029)(0),
                           Xgate(0),
                           PHASEgate(0.3928244130249029)(0),
                           CNOTgate(0, 3),
                           RZgate(0.78564882604980579)(3),
                           CNOTgate(0, 3),
                           Xgate(0),
                           PHASEgate(0.3928244130249029)(0),
                           Xgate(0),
                           PHASEgate(0.3928244130249029)(0),
                           CNOTgate(1, 2),
                           RZgate(0.78564882604980579)(2),
                           CNOTgate(1, 2),
                           Xgate(0),
github rigetti / reference-qvm / tests / test_wavefunction.py View on Github external
def test_qaoa_circuit(qvm):
    wf_true = [0.00167784 + 1.00210180e-05*1j, 0.50000000 - 4.99997185e-01*1j,
               0.50000000 - 4.99997185e-01*1j, 0.00167784 + 1.00210180e-05*1j]
    wf_true = np.reshape(np.array(wf_true), (4, 1))
    prog = Program()
    prog.inst([RYgate(np.pi/2)(0), RXgate(np.pi)(0),
               RYgate(np.pi/2)(1), RXgate(np.pi)(1),
               CNOTgate(0, 1), RXgate(-np.pi/2)(1), RYgate(4.71572463191)(1),
               RXgate(np.pi/2)(1), CNOTgate(0, 1),
               RXgate(-2*2.74973750579)(0), RXgate(-2*2.74973750579)(1)])
    wf_test, _ = qvm.wavefunction(prog)
    assert np.allclose(wf_test.amplitudes, wf_true)
github entropicalabs / entropica_qaoa / tests / test_optimizer2.py View on Github external
from pyquil.paulis import PauliSum, PauliTerm
from pyquil.api import WavefunctionSimulator, local_qvm, get_qc
from pyquil.quil import Program
from pyquil.gates import RX, CNOT

from forest_qaoa.vqe.optimizer import scipy_optimizer
from forest_qaoa.vqe.cost_function import (PrepareAndMeasureOnWFSim,
                                           PrepareAndMeasureOnQVM)
from forest_qaoa.qaoa.cost_function import QAOACostFunctionOnWFSim
from forest_qaoa.qaoa.parameters import FourierParams

#hamiltonian = PauliSum.from_compact_str("(-1.0)*Z0*Z1 + 0.8*Z0 + (-0.5)*Z1")

hamiltonian = PauliSum([PauliTerm("Z", 0, -1.0) * PauliTerm("Z", 1, 1.0),
                        PauliTerm("Z", 0, 0.8), PauliTerm("Z", 1, -0.5)])
prepare_ansatz = Program()
params = prepare_ansatz.declare("params", memory_type="REAL", memory_size=4)
prepare_ansatz.inst(RX(params[0], 0))
prepare_ansatz.inst(RX(params[1], 1))
prepare_ansatz.inst(CNOT(0, 1))
prepare_ansatz.inst(RX(params[2], 0))
prepare_ansatz.inst(RX(params[3], 1))

p0 = [0, 0, 0, 0]


def test_vqe_on_WFSim():
    sim = WavefunctionSimulator()
    cost_fun = PrepareAndMeasureOnWFSim(prepare_ansatz=prepare_ansatz,
                                        make_memory_map=lambda p: {"params": p},
                                        hamiltonian=hamiltonian,
                                        sim=sim,
github entropicalabs / entropica_qaoa / tests / test_qaoa_cost_function.py View on Github external
from pyquil.api import local_qvm, WavefunctionSimulator
from pyquil import get_qc, Program
from pyquil.paulis import PauliSum, PauliTerm
from pyquil.quil import QubitPlaceholder

from forest_qaoa.qaoa.cost_function import (QAOACostFunctionOnQVM,
                                            QAOACostFunctionOnWFSim)
from forest_qaoa.qaoa.parameters import (AnnealingParams,
                                         StandardWithBiasParams)

# Create a mixed and somehwat more complicated hamiltonian
# TODO fix the whole Qubit Placeholder Business
hamiltonian = PauliSum.from_compact_str("1.0*Z0Z1")
hamiltonian += PauliTerm("Z", 1, 0.5)
next_term = PauliTerm("Z", 0, -2.0)
next_term *= PauliTerm("Z", 1)
hamiltonian += next_term

# TODO verfiy, that the results actually make sense


def test_QAOACostFunctionOnWFSim():
    sim = WavefunctionSimulator()
    params = AnnealingParams.linear_ramp_from_hamiltonian(hamiltonian, n_steps=4)

    with local_qvm():
        cost_function = QAOACostFunctionOnWFSim(hamiltonian,
                                                params=params,
                                                sim=sim,
                                                scalar_cost_function=False,
                                                noisy=True,
github entropicalabs / entropica_qaoa / tests / test_cost_function.py View on Github external
def test_PrepareAndMeasureOnQVM_QubitPlaceholders():
    q1, q2 = QubitPlaceholder(), QubitPlaceholder()
    prepare_ansatz = Program()
    param_register = prepare_ansatz.declare(
        "params", memory_type="REAL", memory_size=2)
    prepare_ansatz.inst(RX(param_register[0], q1))
    prepare_ansatz.inst(RX(param_register[1], q2))

    def make_memory_map(params):
        return {"params": params}

    ham = PauliSum([PauliTerm("Z", q1), PauliTerm("Z",q2)])
    qubit_mapping = get_default_qubit_mapping(prepare_ansatz)
    qvm = get_qc("2q-qvm")
    with local_qvm():
        #        qvm = proc.qvm
        cost_fn = PrepareAndMeasureOnQVM(prepare_ansatz, make_memory_map,
                                         qvm=qvm,
                                         hamiltonian=ham, enable_logging=True,
                                         scalar_cost_function=False,
                                         base_numshots=10,
                                         qubit_mapping=qubit_mapping)
        out = cost_fn([np.pi, np.pi / 2], nshots=10)
        assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.1), rtol=1.1)
        assert np.allclose(out, (-1, 0.1), rtol=1.1)
github entropicalabs / entropica_qaoa / tests / test_qaoa_cost_function_qubit_placeholders.py View on Github external
# gonna need this program and hamiltonian for both tests.
# So define them globally
q0 = QubitPlaceholder()
q1 = QubitPlaceholder()
hamiltonian = PauliTerm("Z", q0, 2.5)
hamiltonian += PauliTerm("Z", q1, 0.5)
hamiltonian += PauliTerm("Z", q1, -1) * PauliTerm("Z", q0)


prepare_ansatz = Program()
params = prepare_ansatz.declare("params", memory_type="REAL", memory_size=4)
prepare_ansatz.inst(RX(params[0], q0))
prepare_ansatz.inst(RX(params[1], q1))
prepare_ansatz.inst(CNOT(q0, q1))
prepare_ansatz.inst(RX(params[2], q0))
prepare_ansatz.inst(RX(params[3], q1))

p0 = [0, 5.2, 0, 0]


def test_vqe_on_WFSim_QubitPlaceholders():
    qubit_mapping = get_default_qubit_mapping(prepare_ansatz)
    sim = WavefunctionSimulator()
    cost_fun = PrepareAndMeasureOnWFSim(prepare_ansatz=prepare_ansatz,
                                        make_memory_map=lambda p: {"params": p},
                                        hamiltonian=hamiltonian,
                                        sim=sim,
                                        return_standard_deviation=True,
                                        noisy=False,
                                        qubit_mapping=qubit_mapping)