Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix initial config formatting in StreamingProblem #342

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _upload_start(self, terms):
def _get_initial_config_string(self):
if self.problem.init_config:
return (
f'{"initial_configuration":}'
'"initial_configuration":'
+ json.dumps(self.problem.init_config)
+ ","
)
Expand Down
61 changes: 61 additions & 0 deletions azure-quantum/tests/unit/test_streaming_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,42 @@ def __test_upload_problem(
local = json.loads(rProblem.serialize())
self.assertEqual(uploaded, local)

def __test_upload_problem_with_init_config(
self,
problem_type: ProblemType = ProblemType.ising,
initial_terms: List[Term] = [],
initial_config: dict = None,
**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_workspace()

sProblem = StreamingProblem(
ws, name="test", problem_type=problem_type, terms=initial_terms
)
cProblem = StreamingProblem(
"test", problem_type=problem_type, terms=initial_terms, init_config=initial_config
)

self.assertEqual(problem_type, sProblem.problem_type)
self.assertEqual(problem_type, cProblem.problem_type)
self.assertEqual(problem_type.name, sProblem.stats["type"])
self.assertEqual(problem_type.name, cProblem.stats["type"])
self.assertEqual(len(initial_terms), sProblem.stats["num_terms"])
self.assertEqual(len(initial_terms), cProblem.stats["num_terms"])

uri = cProblem.upload(ws)
uploaded = json.loads(cProblem.download().serialize())
local = json.loads(cProblem.serialize())
self.assertEqual(uploaded, local)

def __kwarg_or_value(self, kwarg, name, default):
if name in kwarg:
return kwarg[name]
Expand Down Expand Up @@ -124,13 +160,38 @@ def test_streaming_problem_initial_terms(self):
max_coupling=3,
)

@pytest.mark.live_test
def test_upload_streaming_problem_with_initial_config(self):
self.__test_upload_problem_with_init_config(
initial_terms=[
Term(c=-9, indices=[0]),
Term(c=-3, indices=[1,0]),
Term(c=5, indices=[2,0])
],
initial_config={'0': -1, '1': 1, '2': 1}
)

@pytest.mark.live_test
def test_upload_streaming_problem_with_initial_config_pubo(self):
self.__test_upload_problem_with_init_config(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, tests marked by @pytest.mark.live_test need corresponding .yaml recordings file.

problem_type=ProblemType.pubo,
initial_terms=[
Term(c=-9, indices=[0]),
Term(c=-3, indices=[1,0]),
Term(c=5, indices=[2,0])
],
initial_config={'0': 1, '1': 1, '2': 0}
)

def check_all(self):
self.test_streaming_problem_small_chunks()
self.test_streaming_problem_large_chunks()
self.test_streaming_problem_small_chunks_compressed()
self.test_streaming_problem_large_chunks_compressed()
self.test_streaming_problem_pubo()
self.test_streaming_problem_initial_terms()
self.test_compare_streaming_problem_initial_config()
self.test_compare_streaming_problem_initial_config_pubo()


if __name__ == "__main__":
Expand Down