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 _apply_function(func, arg): # type: (QuilParser.FunctionContext, Any) -> Any if isinstance(arg, Expression): if func.SIN(): return quil_sin(arg) elif func.COS(): return quil_cos(arg) elif func.SQRT(): return quil_sqrt(arg) elif func.EXP(): return quil_exp(arg) elif func.CIS(): return 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())
def test_substitute_memory_reference(): x_0 = MemoryReference("x", 0, declared_size=2) x_1 = MemoryReference("x", 1, declared_size=2) # complete substitutions assert substitute(x_0, {x_0: 5}) == 5 assert substitute(x_0 + x_1, {x_0: +5, x_1: -5}) == 0 assert substitute(x_0 - x_1, {x_0: +5, x_1: -5}) == 10 assert substitute(x_0 * x_1, {x_0: +5, x_1: -5}) == -25 assert substitute(x_0 / x_1, {x_0: +5, x_1: -5}) == -1 assert substitute(x_0 * x_0**2 / x_1, {x_0: 5, x_1: 10}) == 12.5 assert np.isclose(substitute(quil_exp(x_0), {x_0: 5, x_1: 10}), np.exp(5)) assert np.isclose(substitute(quil_sin(x_0), {x_0: 5, x_1: 10}), np.sin(5)) assert np.isclose(substitute(quil_cos(x_0), {x_0: 5, x_1: 10}), np.cos(5)) assert np.isclose(substitute(quil_sqrt(x_0), { x_0: 5, x_1: 10 }), np.sqrt(5)) assert np.isclose(substitute(quil_cis(x_0), { x_0: 5, x_1: 10 }), np.exp(1j * 5.0)) # incomplete substitutions y = MemoryReference("y", 0, declared_size=1) z = MemoryReference("z", 0, declared_size=1) assert substitute(y + z, {y: 5}) == 5 + z assert substitute(quil_cis(z), {y: 5}) == quil_cis(z) # array substitution pass-through a = MemoryReference("a", 0, declared_size=1) assert np.allclose(substitute_array([quil_sin(a), quil_cos(a)], {a: 5}), [np.sin(5), np.cos(5)])
def apply_fun(self, fun, arg): if fun == "SIN": return quil_sin(arg) if isinstance(arg, Expression) else np.sin(arg) if fun == "COS": return quil_cos(arg) if isinstance(arg, Expression) else np.cos(arg) if fun == "SQRT": return quil_sqrt(arg) if isinstance(arg, Expression) else np.sqrt(arg) if fun == "EXP": return quil_exp(arg) if isinstance(arg, Expression) else np.exp(arg) if fun == "CIS": return quil_cis(arg) if isinstance(arg, Expression) else np.cos(arg) + 1j * np.sin(arg)