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())
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)])