Ejemplo n.º 1
0
    def test_serialization_cterms(self):
        count = 2
        terms = []
        for i in range(count):
            terms.append(Term(c=i, indices=[i, i + 1]))
        problem = Problem(name="test", terms=terms)

        expected = json.dumps({
            "metadata": {
                "name": "test"
            },
            "cost_function": {
                "version": "1.0",
                "type": "ising",
                "terms": [
                    {
                        "c": 0,
                        "ids": [0, 1]
                    },
                    {
                        "c": 1,
                        "ids": [1, 2]
                    },
                ],
            }
        })
        print(problem.serialize())
        actual = problem.serialize()
        self.assertEqual(expected, actual)
Ejemplo n.º 2
0
    def test_serialization_init_config(self):
        count = 2
        terms = []
        for i in range(count):
            terms.append(Term(c=i, indices=[i, i + 1]))
        init_config = {"0": -1, "1": 1, "2": -1}
        problem = Problem(name="test", terms=terms, init_config=init_config)

        expected = json.dumps({
            "metadata": {
                "name": "test"
            },
            "cost_function": {
                "version": "1.1",
                "type": "ising",
                "terms": [
                    {
                        "c": 0,
                        "ids": [0, 1]
                    },
                    {
                        "c": 1,
                        "ids": [1, 2]
                    },
                ],
                "initial_configuration": {
                    "0": -1,
                    "1": 1,
                    "2": -1
                },
            }
        })
        actual = problem.serialize()
        self.assertEqual(expected, actual)
    async def __test_upload_problem(
            self,
            count: int,
            terms_thresh: int,
            size_thresh: int,
            compress: bool,
            problem_type: ProblemType = ProblemType.ising,
            initial_terms: List[Term] = [],
            **kwargs):
        if not (self.in_recording or self.is_live):
            # Temporarily disabling this test in playback mode
            # due to multiple calls to the storage API
            # that need to have a request id to distinguish
            # them while playing back
            print("Skipping this test in playback mode")
            return

        ws = self.create_async_workspace()

        sProblem = await StreamingProblem.create(ws,
                                                 name="test",
                                                 problem_type=problem_type,
                                                 terms=initial_terms)
        rProblem = Problem("test",
                           problem_type=problem_type,
                           terms=initial_terms)
        sProblem.upload_terms_threshold = terms_thresh
        sProblem.upload_size_threshold = size_thresh
        sProblem.compress = compress

        for i in range(count):
            await sProblem.add_term(c=i, indices=[i, i + 1])
            rProblem.add_term(c=i, indices=[i, i + 1])

        self.assertEqual(problem_type, sProblem.problem_type)
        self.assertEqual(problem_type.name, sProblem.stats["type"])
        self.assertEqual(count + len(initial_terms),
                         sProblem.stats["num_terms"])
        self.assertEqual(
            self.__kwarg_or_value(kwargs, "avg_coupling", 2),
            sProblem.stats["avg_coupling"],
        )
        self.assertEqual(
            self.__kwarg_or_value(kwargs, "max_coupling", 2),
            sProblem.stats["max_coupling"],
        )
        self.assertEqual(
            self.__kwarg_or_value(kwargs, "min_coupling", 2),
            sProblem.stats["min_coupling"],
        )

        uri = await sProblem.upload(ws)
        data = await sProblem.download()
        uploaded = json.loads(data.serialize())
        local = json.loads(rProblem.serialize())
        self.assertEqual(uploaded, local)
Ejemplo n.º 4
0
 def test_deserialize(self):
     count = 2
     terms = []
     for i in range(count):
         terms.append(Term(c=i, indices=[i, i + 1]))
     problem = Problem(name="test", terms=terms)
     deserialized = Problem.deserialize(problem.serialize(), problem.name)
     self.assertEqual(problem.name, deserialized.name)
     self.assertEqual(problem.problem_type, deserialized.problem_type)
     self.assertEqual(count, len(deserialized.terms))
     self.assertEqual(problem.init_config, deserialized.init_config)
     self.assertEqual(Term(c=0, indices=[0, 1]), problem.terms[0])
     self.assertEqual(Term(c=1, indices=[1, 2]), problem.terms[1])