def test_def_gate_with_variables(): # Note that technically the RX gate includes -i instead of just i but this messes a bit with the test since # it's not smart enough to figure out that -1*i == -i theta = Parameter('theta') rx = np.array([[cos(theta / 2), 1j * sin(theta / 2)], [1j * sin(theta / 2), cos(theta / 2)]]) defgate = 'DEFGATE RX(%theta):\n' \ ' cos(%theta/2), i*sin(%theta/2)\n' \ ' i*sin(%theta/2), cos(%theta/2)\n\n' _test(defgate, DefGate('RX', rx, [theta]))
def test_def_gate_with_parameters(): theta = Parameter('theta') rx = np.array([[cos(theta / 2), -1j * sin(theta / 2)], [-1j * sin(theta / 2), cos(theta / 2)]]) p = Program().defgate("RX", rx, [theta]) assert p.out() == 'DEFGATE RX(%theta):\n' \ ' cos(%theta/2), -i*sin(%theta/2)\n' \ ' -i*sin(%theta/2), cos(%theta/2)\n\n' dg = DefGate('MY_RX', rx, [theta]) MY_RX = dg.get_constructor() p = Program().inst(MY_RX(np.pi)(0)) assert p.out() == 'MY_RX(pi) 0\n'
def _apply_function(func, arg): # type: (QuilParser.FunctionContext, Any) -> Any if isinstance(arg, Expression): if func.SIN(): return parameters.sin(arg) elif func.COS(): return parameters.cos(arg) elif func.SQRT(): return parameters.sqrt(arg) elif func.EXP(): return parameters.exp(arg) elif func.CIS(): return parameters.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())
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(exp(x), {y: 5}) != exp(5) assert substitute(exp(x), {x: 5}) == np.exp(5) assert np.isclose(substitute(sin(x * x ** 2 / y), {x: 5.0, y: 10.0}), np.sin(12.5)) assert np.isclose(substitute(sqrt(x), {x: 5.0, y: 10.0}), np.sqrt(5.0)) assert np.isclose(substitute(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(cis(x), {y: 5}) == cis(x) assert np.allclose(substitute_array([sin(x), cos(x)], {x: 5}), [np.sin(5), np.cos(5)])
def test_contained_parameters(): x = Parameter('x') assert _contained_parameters(x) == {x} y = Parameter('y') assert _contained_parameters(x + y) == {x, y} assert _contained_parameters(x ** y ** sin(x * y * 4)) == {x, y}
def test_expression_to_string(): x = Parameter('x') assert str(x) == '%x' y = Parameter('y') assert str(y) == '%y' assert str(x + y) == '%x+%y' assert str(3 * x + y) == '3*%x+%y' assert str(3 * (x + y)) == '3*(%x+%y)' assert str(x + y + 2) == '%x+%y+2' assert str(x - y - 2) == '%x-%y-2' assert str(x - (y - 2)) == '%x-(%y-2)' assert str((x + y) - 2) == '%x+%y-2' assert str(x + (y - 2)) == '%x+%y-2' assert str(x ** y ** 2) == '%x^%y^2' assert str(x ** (y ** 2)) == '%x^%y^2' assert str((x ** y) ** 2) == '(%x^%y)^2' assert str(sin(x)) == 'sin(%x)' assert str(3 * sin(x + y)) == '3*sin(%x+%y)'
from pyquil.quil import Program from pyquil.parameters import Parameter, cos, sin, exp import numpy as np p = Program() """ Gate definitions """ theta = Parameter('theta') crx = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, cos(theta / 2), -1j * sin(theta / 2)], [0, 0, -1j * sin(theta / 2), cos(theta / 2)]]) p = Program().defgate("CRX", crx, [theta]) cry = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, cos(theta / 2), -sin(theta / 2)], [0, 0, sin(theta / 2), cos(theta / 2)]]) p = Program().defgate("CRY", cry, [theta]) crz = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, exp(-1j * (theta / 2)), 0], [0, 0, 0, exp(1j * (theta / 2))]]) #p = Program().defgate("CRZ", crz, [theta]) print(p.out())
from pyquil.quil import Program from pyquil.parameters import Parameter, cos, sin, exp import numpy as np p = Program() """ Gate definitions """ theta = Parameter('theta') crx = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, cos(theta / 2), -1j * sin(theta / 2)], [0, 0, -1j * sin(theta / 2), cos(theta / 2)]]) p = Program().defgate("CRX", crx, [theta]) cry = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, cos(theta / 2), -sin(theta / 2)], [0, 0, sin(theta / 2), cos(theta / 2)]]) p = Program().defgate("CRY", cry, [theta]) crz = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, exp(-1j*(theta/2)), 0], [0, 0, 0, exp(1j*(theta/2))]])