def test_regref_transform(self, parser, ctx, monkeypatch): """Test that a Blackbird expression containing register references evaluates""" expr = blackbirdParser.VariableLabelContext(parser, ctx) expr.getText = lambda: "q2" expr.REGREF = lambda: True assert isinstance(_expression(expr), sym.Symbol) assert str(_expression(expr)) == "q2"
def test_variable_invalid(self, parser, ctx, monkeypatch): """Test that an error is raised if the variable does not exist""" expr = blackbirdParser.VariableLabelContext(parser, ctx) expr.getText = lambda: "var2" expr.start = start() with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"var1": 5}) with pytest.raises(BlackbirdSyntaxError, match="name 'var2' is not defined"): _expression(expr)
def test_p_parameter_type_error(self, parser, ctx, monkeypatch): """Test that an error is raised if the variable does not exist""" expr = blackbirdParser.VariableLabelContext(parser, ctx) expr.getText = lambda: "p0" expr.start = start() with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"p0": 13}) m.setattr(blackbird.auxiliary, "_PARAMS", ["p0"]) with pytest.raises(TypeError, match="Invalid type for parameter."): _expression(expr)
def test_variable(self, parser, ctx, monkeypatch): """Test that a Blackbird expression containing variables evaluates""" expr = blackbirdParser.VariableLabelContext(parser, ctx) expr.getText = lambda: "var1" with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"var1": 5}) assert _expression(expr) == 5
def test_unary_plus(self, parser, ctx, n, expected, num): """Test unary plus of an expression""" class DummySignLabel(blackbirdParser.SignLabelContext): """Dummy sign label""" expression = lambda self: num(n) expr = DummySignLabel(parser, ctx) expr.PLUS = lambda: True assert _expression(expr) == expected
def test_minus(self, parser, ctx, n1, n2, num): """Test subtraction of two numbers""" class DummyAddLabel(blackbirdParser.AddLabelContext): """Dummy add label""" expression = lambda self: (num(n1[0]), num(n2[0])) expr = DummyAddLabel(parser, ctx) expr.MINUS = lambda: True assert _expression(expr) == n1[1] - n2[1]
def test_multiply(self, parser, ctx, n1, n2, num): """Test multiplication of two numbers""" class DummyMulLabel(blackbirdParser.MulLabelContext): """Dummy mul label""" expression = lambda self: (num(n1[0]), num(n2[0])) expr = DummyMulLabel(parser, ctx) expr.TIMES = lambda: True assert _expression(expr) == n1[1] * n2[1]
def test_power(self, parser, ctx, n1, n2, num): """Test power of two numbers""" class DummyPowerLabel(blackbirdParser.PowerLabelContext): """Dummy power label""" expression = lambda self: (num(n1[0], num_type='complex'), num(n2[0], num_type='float')) expr = DummyPowerLabel(parser, ctx) assert np.allclose(_expression(expr), n1[1]**n2[1])
def test_negation(self, parser, ctx, n, expected, num): """Test negation of an expression""" class DummySignLabel(blackbirdParser.SignLabelContext): """Dummy sign label""" expression = lambda self: num(n) expr = DummySignLabel(parser, ctx) expr.MINUS = lambda: True assert _expression(expr) == -expected
def test_plus(self, parser, ctx, n1, n2, num): """Test addition of two numbers""" class DummyAddLabel(blackbirdParser.AddLabelContext): """Dummy add label""" expression = lambda self: (num(n1[0]), num(n2[0])) expr = DummyAddLabel(parser, ctx) expr.PLUS = lambda: True assert np.isclose(_expression(expr), n1[1] + n2[1])
def test_divide(self, parser, ctx, n1, n2, num): """Test division of two numbers""" class DummyMulLabel(blackbirdParser.MulLabelContext): """Dummy mul label""" expression = lambda self: (num(n1[0], num_type='complex'), num(n2[0], num_type='float')) expr = DummyMulLabel(parser, ctx) expr.DIVIDE = lambda: True assert np.allclose(_expression(expr), n1[1] / n2[1])
def test_pow_scalar_array(self, parser, ctx, n1, num, var, monkeypatch): """Test power of a number and an array""" class DummyPowLabel(blackbirdParser.PowerLabelContext): """Dummy power label""" expression = lambda self: (num(n1[0]), var("U")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"U": U.tolist()}) expr = DummyPowLabel(parser, ctx) assert np.allclose(_expression(expr), n1[1]**U) class DummyPowLabel(blackbirdParser.PowerLabelContext): """Dummy power label""" expression = lambda self: (var("U"), num(n1[0])) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"U": U.tolist()}) expr = DummyPowLabel(parser, ctx) assert np.allclose(_expression(expr), U**n1[1])
def test_divide_scalar_array(self, parser, ctx, n1, num, var, monkeypatch): """Test division of a number and an array""" class DummyMulLabel(blackbirdParser.MulLabelContext): """Dummy mul label""" expression = lambda self: (num(n1[0]), var("U")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"U": U}) expr = DummyMulLabel(parser, ctx) expr.DIVIDE = lambda: True assert np.all(_expression(expr) == n1[1] / U)
def test_divide_by_integer(self, parser, ctx, n1, n2, num): """Test division of a number by an integer""" if n2[1] == 0: pytest.skip("Cannot divide by zero") class DummyMulLabel(blackbirdParser.MulLabelContext): """Dummy mul label""" expression = lambda self: (num(n1[0]), num(n2[0], num_type='int')) expr = DummyMulLabel(parser, ctx) expr.DIVIDE = lambda: True assert np.allclose(_expression(expr), n1[1] / n2[1])
def test_minus_scalar_array(self, parser, ctx, n1, num, var, monkeypatch): """Test subtraction of a number and an array""" class DummyAddLabel(blackbirdParser.AddLabelContext): """Dummy add label""" expression = lambda self: (num(n1[0]), var("U")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"U": U}) expr = DummyAddLabel(parser, ctx) expr.MINUS = lambda: True assert np.all(_expression(expr) == n1[1] - U)
def test_minus_array(self, parser, ctx, var, monkeypatch): """Test subtraction of two arrays""" class DummyAddLabel(blackbirdParser.AddLabelContext): """Dummy add label""" expression = lambda self: (var("U1"), var("U2")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"U1": U*5, "U2": np.cos(U)}) expr = DummyAddLabel(parser, ctx) expr.MINUS = lambda: True assert np.allclose(_expression(expr), U*5-np.cos(U))
def test_function(self, parser, ctx, n1, num): """Test function is properly called""" class DummySin(blackbirdParser.FunctionContext): """Dummy sin label""" SIN = lambda self: True class DummyFunctionLabel(blackbirdParser.FunctionLabelContext): """Dummy function label""" expression = lambda self: num(n1[0]) function = lambda self: DummySin(parser, ctx) expr = DummyFunctionLabel(parser, ctx) assert np.allclose(_expression(expr), np.sin(n1[1]))
def test_multiply_scalar_array(self, parser, ctx, n1, num, var, monkeypatch): """Test multiplication of a number and an array""" class DummyMulLabel(blackbirdParser.MulLabelContext): """Dummy mul label""" expression = lambda self: (num(n1[0]), var("U")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"U": U}) expr = DummyMulLabel(parser, ctx) expr.TIMES = lambda: True assert np.allclose(_expression(expr), n1[1]*U)
def test_divide_array_element(self, parser, ctx, var, monkeypatch): """Test division of two arrays""" class DummyMulLabel(blackbirdParser.MulLabelContext): """Dummy mul label""" expression = lambda self: (var("U1"), var("U2")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", {"U1": U*5, "U2": np.cos(U)}) expr = DummyMulLabel(parser, ctx) expr.DIVIDE = lambda: True assert np.allclose(_expression(expr), U*5/np.cos(U))
def test_pow_array_element(self, parser, ctx, var, monkeypatch): """Test power of two arrays""" class DummyPowLabel(blackbirdParser.PowerLabelContext): """Dummy power label""" expression = lambda self: (var("U1"), var("U2")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", { "U1": (U * 5).tolist(), "U2": np.cos(U).tolist() }) expr = DummyPowLabel(parser, ctx) assert np.allclose(_expression(expr), (U * 5)**np.cos(U))
def test_multiply_array_element(self, parser, ctx, var, monkeypatch): """Test multiplication of two arrays""" class DummyMulLabel(blackbirdParser.MulLabelContext): """Dummy mul label""" expression = lambda self: (var("U1"), var("U2")) with monkeypatch.context() as m: m.setattr(blackbird.auxiliary, "_VAR", { "U1": (U * 5).tolist(), "U2": np.cos(U).tolist() }) expr = DummyMulLabel(parser, ctx) expr.TIMES = lambda: True assert np.allclose(_expression(expr), U * 5 * np.cos(U))
def test_brackets(self, parser, num, n1, n2, ctx): """Test that brackets are correctly parsed""" class DummyAddLabel(blackbirdParser.AddLabelContext): """Dummy add label""" expression = lambda self: (num(n1[0]), num(n2[0])) class DummyBracketsLabel(blackbirdParser.BracketsLabelContext): """Dummy brackets""" def expression(self): """Returns a Blackbird abstract syntax tree section corresponding to an expression""" e = DummyAddLabel(parser, ctx) e.PLUS = lambda: True return e class DummyPowerLabel(blackbirdParser.PowerLabelContext): """Dummy power""" expression = lambda self: (DummyBracketsLabel(parser, ctx), num(2)) expr = DummyPowerLabel(parser, ctx) assert _expression(expr) == (n1[1] + n2[1])**2
def test_number(self, n, expected, num): """Test that a Blackbird expression containing numbers evaluates""" expr = num(n) assert _expression(expr) == expected