Beispiel #1
0
def test_submit_proto_problem(testprotosolver):
    problem = Problem(name="proto_test", content_type=ContentType.protobuf)
    problem.terms = [Term(c=3, indices=[1, 0]), Term(c=5, indices=[2, 0])]
    with patch("azure.quantum.job.base_job.upload_blob") as mock_upload:
        job = testprotosolver.submit(problem)
    mock_upload.assert_called_once()
    testprotosolver.workspace.submit_job.assert_called_once()
Beispiel #2
0
def test_throw_exception_proto_problem(testprotosolver):
    testprotosolver.name = "SimulatedAnnealing"
    problem = Problem(name="proto_test_exception",
                      content_type=ContentType.protobuf)
    problem.terms = [Term(c=3, indices=[1, 0]), Term(c=5, indices=[2, 0])]
    with patch("azure.quantum.job.base_job.upload_blob") as mock_upload:
        pytest.raises(ValueError, testprotosolver.submit, problem)
Beispiel #3
0
 def test_serialzie_proto_problem(self):
     problem = Problem(name="test_proto",
                       problem_type=ProblemType.ising,
                       content_type=ContentType.protobuf)
     problem.terms = [
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
     ]
     problem_msgs = problem.serialize()
     self.assertEqual(len(problem_msgs), 1)
     proto_problem = ProtoProblem()
     proto_problem.ParseFromString(problem_msgs[0])
     self.assertEqual(proto_problem.cost_function.type,
                      ProtoProblem.ProblemType.ISING)
     self.assertEqual(len(proto_problem.cost_function.terms), 12)
Beispiel #4
0
def test_submit_large_proto_problem(testprotosolver):

    problem = Problem(name="proto_test", content_type=ContentType.protobuf)
    terms = []
    for i in range(0, 3000):
        terms.append(Term(c=i, indices=[i, i + 1]))
    problem.terms = terms
    with patch("azure.quantum.job.base_job.upload_blob") as mock_upload:
        job = testprotosolver.submit(problem)
    mock_upload.assert_called_once()
    testprotosolver.workspace.submit_job.assert_called_once()
Beispiel #5
0
 def test_grouped_type(self):
     problem = Problem(name="test_pubo_grouped",
                       problem_type=ProblemType.pubo)
     problem.terms = [
         Term(c=3, indices=[1, 0, 1]),
         Term(c=5, indices=[2, 0, 0]),
         Term(c=-1, indices=[1, 0, 0]),
         Term(c=4, indices=[0, 2, 1])
     ]
     assert problem.problem_type is ProblemType.pubo
     problem.add_slc_term([(3, 0), (2, 1), (-1, None)])
     assert problem.problem_type is ProblemType.pubo_grouped
Beispiel #6
0
    def test_problem_name_serialization(self):
        problem_names = ["test", "my_problem"]
        for problem_name in problem_names:
            problem = Problem(name=problem_name)
            problem.terms = [
                Term(c=3, indices=[1, 0]),
                Term(c=5, indices=[2, 0]),
            ]
            serialized_problem = problem.serialize()

            # name is in the serialized string
            assert re.search(f'"name"\\s*:\\s*"{problem_name}"',
                             serialized_problem,
                             flags=re.RegexFlag.MULTILINE)

            # name is in the correct place in the json structure
            problem_json = json.loads(serialized_problem)
            assert problem_json["metadata"]["name"] == problem_name

            # deserializes name
            deserialized_problem = Problem.deserialize(
                input_problem=serialized_problem)
            assert problem_name == deserialized_problem.name

            new_problem_name = "new_problem_name"
            # use the name passed in the parameter
            deserialized_problem = Problem.deserialize(
                input_problem=serialized_problem, name=new_problem_name)
            assert new_problem_name == deserialized_problem.name

        # test deserializing a problem that does not have a name in the json
        # and leaving the name as None
        serialized_problem_without_name = '{"cost_function": {"version": "1.0", "type": "ising", "terms": [{"c": 3, "ids": [1, 0]}, {"c": 5, "ids": [2, 0]}]}}'
        deserialized_problem = Problem.deserialize(
            input_problem=serialized_problem_without_name)
        assert deserialized_problem.name == "Optimization problem"

        # test deserializing a problem that does not have a name in the json
        # and using the name parameter
        new_problem_name = "new_problem_name"
        deserialized_problem = Problem.deserialize(
            input_problem=serialized_problem_without_name,
            name=new_problem_name)
        assert new_problem_name == deserialized_problem.name

        # test deserializing a problem that does not have a name but have a metadata in the json
        # and leaving the name as None
        serialized_problem_without_name = '{"metadata":{"somemetadata":123}, "cost_function": {"version": "1.0", "type": "ising", "terms": [{"c": 3, "ids": [1, 0]}, {"c": 5, "ids": [2, 0]}]}}'
        deserialized_problem = Problem.deserialize(
            input_problem=serialized_problem_without_name)
        assert deserialized_problem.name == "Optimization problem"
Beispiel #7
0
 def test_deserialize_proto_problem(self):
     problem = Problem(name="test_proto",
                       problem_type=ProblemType.pubo,
                       content_type=ContentType.protobuf)
     problem.terms = [
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
         Term(c=3, indices=[1, 0]),
         Term(c=5, indices=[2, 0]),
     ]
     problem_msgs = problem.serialize()
     deserialized_problem = Problem.deserialize(problem_msgs)
     self.assertEqual(len(deserialized_problem.terms), 12)
     self.assertEqual(deserialized_problem.problem_type, ProblemType.pubo)
     self.assertEqual(deserialized_problem.name, problem.name)