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"
def download(self): """Downloads the uploaded problem as an instance of `Problem`""" if not self.uploaded_uri: raise Exception( 'StreamingProblem may not be downloaded before it is uploaded') coords = self._get_upload_coords() blob = coords['container_client'].get_blob_client(coords['blob_name']) contents = download_blob(blob.url) return Problem.deserialize(contents, self.name)
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])
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)
def test_problem_upload_download(self): solver_type = functools.partial(microsoft.SimulatedAnnealing, beta_start=0) solver_name = "SimulatedAnnealing" workspace = self.create_workspace() solver = solver_type(workspace) with unittest.mock.patch.object( Job, self.mock_create_job_id_name, return_value=self.get_test_job_id(), ): problem_name = f'Test-{solver_name}-{datetime.now():"%Y%m%d-%H%M%S"}' if self.is_playback: problem_name = f'Test-{solver_name}-"20210101-000000"' problem = self.create_problem(name=problem_name) job = solver.submit(problem) # Check if problem can be successfully downloaded and deserialized problem_as_json = job.download_data(job.details.input_data_uri) downloaded_problem = Problem.deserialize( input_problem=problem_as_json) actual = downloaded_problem.serialize() expected = problem.serialize() self.assertEqual(expected, actual)