Exemplo n.º 1
0
def test_n_qubit_control_name_bad():
    """
    Checks that the n qubit control object needs a
    list of control qubits
    """
    with pytest.raises(AssertionError):
        n_qubit_control([0, 1, 2], 4, np.array([[1, 0], [0, 1]]), "")
Exemplo n.º 2
0
def test_n_qubit_control_target_none():
    """
    Checks that the n qubit control object needs a
    list of control qubits
    """
    with pytest.raises(AssertionError):
        n_qubit_control([0, 1, 2], -1, np.array([[1, 0], [0, 1]]), "IDENT")
Exemplo n.º 3
0
def test_n_qubit_control_unitary_none():
    """
    Checks that the n qubit control object needs a
    unitary as a numpy matrix
    """
    with pytest.raises(AssertionError):
        n_qubit_control([0, 1, 2], 3, "not an array", "BAD")
Exemplo n.º 4
0
def basis_selector_oracle(bitstring, qubits):
    """
    Defines an oracle that selects the ith element of the computational basis.

    Defines a phase filp rather than bit flip oracle to eliminate need
    for extra qubit. Flips the sign of the state :math:`\\vert x\\rangle>`
    if and only if x==bitstring and does nothing otherwise.

    :param bitstring: The desired bitstring,
                      given as a string of ones and zeros. e.g. "101"
    :param qubits: The qubits the oracle is called on.
                   The qubits are assumed to be ordered from most
                   significant qubit to least significant qubit.
    :return: A program representing this oracle.
    """
    if len(qubits) != len(bitstring):
        raise ValueError(
            "The bitstring should be the same length as the number of qubits.")
    if not (isinstance(bitstring, str)
            and all([num in ('0', '1') for num in bitstring])):
        raise ValueError("The bitstring must be a string of ones and zeros.")
    prog = pq.Program()
    for i, qubit in enumerate(qubits):
        if bitstring[i] == '0':
            prog.inst(X(qubit))

    prog += amp.n_qubit_control(qubits[:-1], qubits[-1],
                                np.array([[1, 0], [0, -1]]), 'Z')

    for i, qubit in enumerate(qubits):
        if bitstring[i] == '0':
            prog.inst(X(qubit))
    return prog
Exemplo n.º 5
0
def test_qubit_control():
    """
    Tests the n_qubit_control on a generic number of qubits
    """

    # Creates a controlled Z gate from index 0 to index 1
    created = n_qubit_control([0], 1, np.array([[1, 0], [0, -1]]), "CZ")
    assert np.array_equal(
        np.array(created.defined_gates[0].matrix),
        np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]]))
Exemplo n.º 6
0
import numpy as np
import pyquil.quil as pq
import pytest
from pyquil.gates import *

from grove.amplification.amplification import amplify, n_qubit_control, \
    diffusion_operator
from grove.pyqaoa.utils import compare_progs

# Normal operation

# Setup some variables to reuse
A = pq.Program().inst(H(0)).inst(H(1)).inst(H(2))
A_inv = pq.Program().inst(H(0)).inst(H(1)).inst(H(2))
cz_gate = n_qubit_control([1], 2, np.array([[1, 0], [0, -1]]), "CZ")
oracle = pq.Program().inst()
qubits = [0, 1, 2]
iters = 2


def test_qubit_control():
    """
    Tests the n_qubit_control on a generic number of qubits
    """

    # Creates a controlled Z gate from index 0 to index 1
    created = n_qubit_control([0], 1, np.array([[1, 0], [0, -1]]), "CZ")
    assert np.array_equal(
        np.array(created.defined_gates[0].matrix),
        np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]]))