def test_serialisation(self):
        """Serialize/deserialize a graph with a node with a PyMigenBody.

        Notes
        -----
        The content of the bodies depends on the environment, i.e. how the test
        is executed. For this reason we just compare the structure of the graph
        here.
        """
        s = StateSaver(int)

        example_migen = TestMigen(tb_num_iter=2000,
                                  name='counter',
                                  lvl=logging.INFO,
                                  vcd_name="/workdir/TestMigen.vcd")

        with DeltaGraph() as graph:
            example_migen_out = example_migen.call(in1=40, in2=2)
            s.save_and_exit(
                adder(example_migen_out.out1,
                      multiplier(example_migen_out.out2)))

        data, _ = serialize_graph(graph)
        self.assertEqual(type(data), bytes)
        g_capnp = deserialize_graph(data).to_dict()
        assert_capnp_content_types(self, g_capnp)

        with open(os.path.join(self.datapath, 'graph_with_migen_capnp.json'),
                  'r') as file:
            self.assertEqual(g_capnp, json.load(file))
Exemple #2
0
    def test_qiskit_serialisation(self):
        """Test Qiskit nodes serialization/deserialization.
        """

        with DeltaGraph() as test_graph:
            HardwareAbstractionLayerNode(
                QiskitQuantumSimulator(register_size=2)).accept_command(
                    hal_command=0x4000000)

        data, _ = serialize_graph(test_graph)
        g_capnp = deserialize_graph(data)

        # Checking that we are investigating the right node.
        self.assertEqual(g_capnp.nodes[1].name.split("_")[0], "accept")
        body = g_capnp.bodies[1].python.dillImpl
        node = dill.loads(body)
        node.eval(hal_command=0x4000000)
Exemple #3
0
    def test_serialisation(self):
        """Serialize/deserialize a graph.

        .. note::
            The content of the bodies depends on the environment, i.e. how
            the test is executed.
            For this reason we just compare the structure of the graph here.
        """
        with DeltaGraph() as graph:
            self.func(40, 2)

        data, _ = serialize_graph(graph)
        self.assertEqual(type(data), bytes)
        g_capnp = deserialize_graph(data).to_dict()
        assert_capnp_content_types(self, g_capnp)

        with open(os.path.join(self.datapath, 'graph_capnp.json'),
                  'r') as file:
            self.assertEqual(g_capnp, json.load(file))
Exemple #4
0
    def test_template_node_capnp(self):
        """Test serialisation of nodes with no body.
        """
        template = NodeTemplate(inputs=[('a', int), ('b', int)],
                                outputs=int,
                                name="temp-test")

        with DeltaGraph() as test_graph:
            template.call(a=1, b=2)

        data, _ = serialize_graph(test_graph)
        g_capnp = deserialize_graph(data)

        for n in g_capnp.nodes:
            if n.name.split("_")[1] == 'temp-test':
                node = n
                break
        self.assertEqual(node.name.split("_")[0], 'template')
        self.assertEqual(len(node.bodies), 0)
Exemple #5
0
    def test_multi_body_serialisation(self):
        """Tests a graph with a multi-body node is serialised and matches
        a target capnp file.
        """
        with DeltaGraph() as graph:
            n1 = self.func(40, 2)

        @DeltaBlock(allow_const=False)
        def simple_add_2(a: DOptional(int), b: DOptional(int)):
            raise DeltaRuntimeExit

        n1.add_body(simple_add_2)

        data, _ = serialize_graph(graph)
        self.assertEqual(type(data), bytes)
        g_capnp = deserialize_graph(data).to_dict()
        assert_capnp_content_types(self, g_capnp)

        with open(os.path.join(self.datapath, 'graph_multibody_capnp.json'),
                  'r') as file:
            self.assertEqual(g_capnp, json.load(file))
Exemple #6
0
    def test_projectQ_serialisation(self):
        """Test ProjectQ nodes serialization/deserialization.

        ProjectQ can't be fully serialized, we need to exclude from dill the
        engine (C++ libraries). This test is to guarantee that when we
        deserialize everything works as expected.
        """

        with DeltaGraph() as test_graph:
            HardwareAbstractionLayerNode(
                ProjectqQuantumSimulator(register_size=2)).accept_command(
                    hal_command=0x4000000)

        data, _ = serialize_graph(test_graph)
        g_capnp = deserialize_graph(data)

        # Checking that we are investigating the right node.
        self.assertEqual(g_capnp.nodes[1].name.split("_")[0], "accept")
        body = g_capnp.bodies[1].python.dillImpl

        node = dill.loads(body)
        node.eval(hal_command=0x4000000)