Ejemplo n.º 1
0
    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"
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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]
Ejemplo n.º 7
0
    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]
Ejemplo n.º 8
0
    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])
Ejemplo n.º 9
0
    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
Ejemplo n.º 10
0
    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])
Ejemplo n.º 11
0
    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])
Ejemplo n.º 12
0
    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])
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    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])
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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))
Ejemplo n.º 17
0
    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]))
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    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))
Ejemplo n.º 20
0
    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))
Ejemplo n.º 21
0
    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))
Ejemplo n.º 22
0
    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
Ejemplo n.º 23
0
 def test_number(self, n, expected, num):
     """Test that a Blackbird expression containing numbers evaluates"""
     expr = num(n)
     assert _expression(expr) == expected