예제 #1
0
    def test_serialize_operation_array_kwarg(self):
        """Test serialization of an operation with an array kwarg"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        U = np.int64(np.identity(2))

        bb._operations.append({
            "op": "Interferometer",
            "modes": [0],
            "args": [],
            "kwargs": {
                "U": U
            }
        })

        res = bb.serialize()
        expected = dedent("""\
            name prog
            version 0.0

            int array A0[2, 2] =
                1, 0
                0, 1

            Interferometer(U=A0) | 0
            """)
        assert res == expected
예제 #2
0
    def test_use_template(self):
        """Test templates can be initialized"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        x = sym.Symbol('x')
        hi = sym.Symbol('hi')
        bb._parameters = [str(x), str(hi)]
        bb._operations.append({
            "op": "Dgate",
            "modes": [0],
            "args": [0.54 / x**2],
            "kwargs": {
                'test': hi**4
            }
        })
        bb._operations.append({
            "op": "Sgate",
            "modes": [0],
            "args": [0.543],
            "kwargs": {}
        })
        bb._operations.append({"op": "Sgate", "modes": [0]})

        assert bb.parameters == {'x', 'hi'}
        assert bb.is_template()

        bb2 = bb(x=5, hi=2)
        assert not bb2.parameters
        assert not bb2.is_template()
        assert bb2.operations[0]["args"] == [0.54 / 5**2]
        assert bb2.operations[0]["kwargs"] == {'test': 2**4}
        assert bb.operations[1] == bb2.operations[1]
예제 #3
0
    def test_serialize_invalid_operation_kwargs(self):
        """Test serialization of an operation with invalid arg raises error"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._operations.append({"op": "Dgate", "modes": [0], "args": [], "kwargs": {"U": np.array([0.5])}})

        with pytest.raises(ValueError, match="Unknown argument type"):
            res = bb.serialize()
예제 #4
0
    def test_serialize_operation_multiple_arrays(self):
        """Test serialization of an operation with multiple array args"""
        bb = BlackbirdProgram(name="prog", version=1.0)
        U = np.int64(np.identity(2))
        U2 = np.array([[1, 2j], [-2j, 3]])

        bb._operations.extend(
            [
                {"op": "Interferometer", "modes": [0, 2], "args": [U], "kwargs": {}},
                {"op": "BSgate", "modes": [0, 1], "args": [0.543, -0.1231], "kwargs": {}},
                {"op": "GaussianTransform", "modes": [0, 2], "args": [U2], "kwargs": {}},
            ]
        )

        res = bb.serialize()
        expected = dedent(
            """\
            name prog
            version 1.0

            int array A0[2, 2] =
                1, 0
                0, 1

            complex array A1[2, 2] =
                1.0+0.0j, 0.0+2.0j
                -0.0-2.0j, 3.0+0.0j

            Interferometer(A0) | [0, 2]
            BSgate(0.543, -0.1231) | [0, 1]
            GaussianTransform(A1) | [0, 2]
            """
        )
        assert res == expected
예제 #5
0
    def test_serialize_operation_no_args(self):
        """Test serialization of an operation with no args"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._operations.append({"op": "Vac", "modes": [0]})
        res = bb.serialize()
        expected = dedent("""\
            name prog
            version 0.0

            Vac | 0
            """)
        assert res == expected
예제 #6
0
    def test_serialize_operation_multimode(self):
        """Test serialization of an operation on multiple modes"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._operations.append({"op": "Vac", "modes": [0, 1, 2]})
        res = bb.serialize()
        expected = dedent("""\
            name prog
            version 0.0

            Vac | [0, 1, 2]
            """)
        assert res == expected
예제 #7
0
    def test_invalid_template_call(self):
        """Test templates raise exception if parameter values not passed"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        x = sym.Symbol('x')
        hi = sym.Symbol('hi')
        bb._parameters = [x, hi]
        bb._operations.append({"op": "Dgate", "modes": [0], "args": [0.54/x**2], "kwargs": {'test': hi**4}})

        with pytest.raises(ValueError, match="Invalid value for free parameter provided"):
            bb(hi=4)

        with pytest.raises(ValueError, match="Invalid value for free parameter provided"):
            bb(x=4)
예제 #8
0
    def test_serialize_operation_args(self):
        """Test serialization of an operation with 1 arg"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._operations.append({"op": "Dgate", "modes": [0], "args": [0.43 - 0.543j], "kwargs": {}})
        res = bb.serialize()
        expected = dedent(
            """\
            name prog
            version 0.0

            Dgate(0.43-0.543j) | 0
            """
        )
        assert res == expected
예제 #9
0
    def test_serialize_operation_multiple_args(self):
        """Test serialization of an operation with many args"""
        bb = BlackbirdProgram(name="prog", version=1.0)
        bb._operations.append({"op": "Sgate", "modes": [0], "args": [0.43, 0.5432], "kwargs": {}})
        res = bb.serialize()
        expected = dedent(
            """\
            name prog
            version 1.0

            Sgate(0.43, 0.5432) | 0
            """
        )
        assert res == expected
예제 #10
0
    def test_serialize_operation_string_kwarg(self):
        """Test serialization of an operation with a string kwarg"""
        bb = BlackbirdProgram(name="prog", version=1.0)
        bb._operations.append({"op": "Dgate", "modes": [0], "args": [], "kwargs": {"key": "val"}})
        res = bb.serialize()
        expected = dedent(
            """\
            name prog
            version 1.0

            Dgate(key="val") | 0
            """
        )
        assert res == expected
예제 #11
0
    def test_serialize_free_params(self):
        """Test serialization of an operation with a string kwarg"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        x = sym.Symbol('x')
        bb._operations.append({"op": "Dgate", "modes": [0], "args": [0.54/x**2], "kwargs": {}})
        res = bb.serialize()
        expected = dedent(
            """\
            name prog
            version 0.0

            Dgate(0.54/{x}**2) | 0
            """
        )
        assert res == expected
예제 #12
0
    def test_serialize_operation_kwargs(self):
        """Test serialization of an operation with a kwarg"""
        bb = BlackbirdProgram(name="prog", version=1.0)
        bb._operations.append(
            {"op": "MeasureFock", "modes": [0], "args": [], "kwargs": {"select": 2}}
        )
        res = bb.serialize()
        expected = dedent(
            """\
            name prog
            version 1.0

            MeasureFock(select=2) | 0
            """
        )
        assert res == expected
예제 #13
0
    def test_invalid_template_call_variables(self):
        """Test templates raise exception if parameter variable values not passed"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        y = sym.Symbol('y')
        bye = sym.Symbol('bye')

        bb._parameters = [y, bye]
        bb._var.update({"y": y, "bye": np.array([[1, 2, bye]])})

        with pytest.raises(ValueError,
                           match="Invalid value for free parameter provided"):
            bb(bye=2)

        with pytest.raises(ValueError,
                           match="Invalid value for free parameter provided"):
            bb(y=2)
예제 #14
0
    def test_not_template(self):
        """Test initializing a template fails if program is not a template"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._operations.append({"op": "Dgate", "modes": [0], "args": [0.54/2**2], "kwargs": {'test': 1.0}})

        with pytest.raises(ValueError, match="Program is not a template"):
            bb()
예제 #15
0
    def test_initialization(self):
        """Test all attributes correctly initialized"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        assert not bb._var

        assert bb.name == "prog"
        assert bb.version == 0.0
        assert not bb.modes

        assert bb.target["name"] is None
        assert not bb.target["options"]
        assert not bb.operations

        assert not len(bb)

        assert not bb.is_template()
        assert not bb.parameters
예제 #16
0
    def test_serialize_tdmprogram(self):
        """Test serialization of a tdm program"""
        bb = BlackbirdProgram(name="tdm", version=1.0)

        bb._type = OrderedDict([('name', 'tdm'),
                                ('options',
                                 OrderedDict([('temporal_modes', 2),
                                              ('copies', 3)]))])
        bb._var = OrderedDict([('p0', np.array([[1, 2]])),
                               ('p1', np.array([[3, 4]]))])
        bb._operations.extend([{
            'kwargs': {},
            'args': [0.7, 0],
            'op': 'Sgate',
            'modes': [1]
        }, {
            'kwargs': {},
            'args': ['p0', 0.0],
            'op': 'BSgate',
            'modes': [0, 1]
        }, {
            'kwargs': {
                'phi': 'p1'
            },
            'args': [],
            'op': 'MeasureHomodyne',
            'modes': [0]
        }])

        res = bb.serialize()
        expected = dedent("""\
            name tdm
            version 1.0
            type tdm (temporal_modes=2, copies=3)

            int array p0 =
                1, 2
            int array p1 =
                3, 4

            Sgate(0.7, 0) | 1
            BSgate(p0, 0.0) | [0, 1]
            MeasureHomodyne(phi=p1) | 0
            """)
        assert res == expected
예제 #17
0
    def test_serialize_operation_multiple_kwargs(self):
        """Test serialization of an operation with many kwargs"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._operations.append({
            "op":
            "Dgate",
            "modes": [0],
            "args": [],
            "kwargs":
            OrderedDict([("alpha", 0.43 - 0.543j), ("phi", 0.54)]),
        })
        res = bb.serialize()
        expected = dedent("""\
            name prog
            version 0.0

            Dgate(alpha=0.43-0.543j, phi=0.54) | 0
            """)
        assert res == expected
예제 #18
0
    def test_serialize_operation_args_kwargs(self):
        """Test serialization of an operation with args and kwargs"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._operations.append({
            "op":
            "MeasureHomodyne",
            "modes": [0],
            "args": [0.54, -1],
            "kwargs":
            OrderedDict([("select", 5.43), ("phi", 0.54)]),
        })

        res = bb.serialize()
        expected = dedent("""\
            name prog
            version 0.0

            MeasureHomodyne(0.54, -1, select=5.43, phi=0.54) | 0
            """)
        assert res == expected
예제 #19
0
    def test_serialize_target(self):
        """Test target serialization"""
        bb = BlackbirdProgram(name="prog", version=0.0)
        bb._target["name"] = "chip0"
        res = bb.serialize()
        assert res == "name prog\nversion 0.0\ntarget chip0\n"

        bb._target["options"] = OrderedDict([("shots", 100), ("hbar", 0.2),
                                             ("real", True), ("str", "hi")])
        res = bb.serialize()
        assert res == dedent("""\
            name prog
            version 0.0
            target chip0 (shots=100, hbar=0.2, real=True, str="hi")
            """)
예제 #20
0
 def test_serialize_empty(self):
     """Test serialization of an empty program"""
     bb = BlackbirdProgram(name="prog", version=1.0)
     res = bb.serialize()
     assert res == "name prog\nversion 1.0\n"