Exemple #1
0
def test_eval():
    x = Parameter('x')
    assert substitute(x, {x: 5}) == 5

    y = Parameter('y')
    assert substitute(x + y, {x: 5, y: 6}) == 11
    assert substitute(x + y, {x: 5}) == 5 + y
    assert substitute(quil_exp(x), {y: 5}) != np.exp(5)
    assert substitute(quil_exp(x), {x: 5}) == np.exp(5)

    assert np.isclose(substitute(quil_sin(x * x**2 / y), {
        x: 5.0,
        y: 10.0
    }), np.sin(12.5))
    assert np.isclose(substitute(quil_sqrt(x), {
        x: 5.0,
        y: 10.0
    }), np.sqrt(5.0))
    assert np.isclose(substitute(quil_cis(x), {
        x: 5.0,
        y: 10.0
    }), np.exp(1j * 5.0))
    assert np.isclose(substitute(x - y, {x: 5.0, y: 10.0}), -5.)

    assert substitute(quil_cis(x), {y: 5}) == quil_cis(x)
    assert np.allclose(substitute_array([quil_sin(x), quil_cos(x)], {x: 5}),
                       [np.sin(5), np.cos(5)])
def create_CRZ():
    """
    Defining control RX pyquil Gate

    :return: instruction for CRZ
    """

    theta = Parameter('theta')
    crz = np.array([[1, 0, 0, 0],
                    [0, 1, 0, 0],
                    [0, 0, quil_exp(-1j*theta / 2), 0],
                    [0, 0, 0, quil_exp(1j*theta / 2)]])

    dg = DefGate('CRZ', crz, [theta])
    return dg
Exemple #3
0
def _apply_function(func, arg):
    # type: (QuilParser.FunctionContext, Any) -> Any
    if isinstance(arg, Expression):
        if func.SIN():
            return parameters.quil_sin(arg)
        elif func.COS():
            return parameters.quil_cos(arg)
        elif func.SQRT():
            return parameters.quil_sqrt(arg)
        elif func.EXP():
            return parameters.quil_exp(arg)
        elif func.CIS():
            return parameters.quil_cis(arg)
        else:
            raise RuntimeError("Unexpected function to apply: " + func.getText())
    else:
        if func.SIN():
            return sin(arg)
        elif func.COS():
            return cos(arg)
        elif func.SQRT():
            return sqrt(arg)
        elif func.EXP():
            return exp(arg)
        elif func.CIS():
            return cos(arg) + complex(0, 1) * sin(arg)
        else:
            raise RuntimeError("Unexpected function to apply: " + func.getText())
        return x % m


qvm = QVMConnection()

k = Parameter('k')
ccrk = np.array([
    [1, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 0, 0,
     quil_exp((2 * np.pi * 1j) / (2**k))],
])
ccrk_gate_def = DefGate('CCRK', ccrk, [k])
CCRK = ccrk_gate_def.get_constructor()

crk = np.array([
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, quil_exp((2 * np.pi * 1j) / (2**k))],
])
crk_gate_def = DefGate('CRK', crk, [k])
CRK = crk_gate_def.get_constructor()

rk = np.array([
    [1, 0],
from pyquil.parameters import Parameter, quil_sin, quil_cos, quil_exp
from pyquil.latex import to_latex
from math import pi, log2, sqrt
import itertools
import numpy as np
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

t1 = [x for x in np.linspace(-np.pi / 2, 3 * np.pi / 2, 32)]  #phi
t2 = [x for x in np.linspace(0, np.pi / 2, 32)]  #alpha

phi = Parameter('phi')
u_1 = np.array([[1, 0], [0, quil_exp(phi)]])

# Get the Quil definition for the new gate
u_1_definition = DefGate('U1', u_1, [phi])
# Get the gate constructor
U1 = u_1_definition.get_constructor()

alpha = Parameter('alpha')
u_2 = np.array([[quil_cos(alpha), 0], [0, quil_sin(alpha)]])

# Get the Quil definition for the new gate
u_2_definition = DefGate('U2', u_2, [alpha])
# Get the gate constructor
U2 = u_2_definition.get_constructor()

ch = np.array([[1, 0, 0, 0], [0, 1, 0, 0],