Skip to content

Commit

Permalink
[TEST] Add test for get_available_result and update existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuMoreau0 committed Sep 18, 2024
1 parent c9552a3 commit 1332859
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pulser-core/pulser/backend/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def get_available_results(self, submission_id: str) -> dict[str, Result]:
some jobs associated to the submission do not have results.
Returns:
dict[str, Result]: A dictionary mapping the job ID to their results.
dict[str, Result]: A dictionary mapping the job ID to their results.
Jobs with no result are omitted.
"""

Expand All @@ -145,6 +145,7 @@ def __getattr__(self, name: str) -> Any:
self._submission_id, self._job_ids
)
)
return self._results
except RemoteResultsError as e:
raise RemoteResultsError(
"Results are not available for all jobs. Use the "
Expand Down
18 changes: 13 additions & 5 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_emulator_config_type_errors(param, msg):
class _MockConnection(RemoteConnection):
def __init__(self):
self._status_calls = 0
self._progress_calls = 0
self.result = SampledResult(
("q0", "q1"),
meas_basis="ground-rydberg",
Expand All @@ -102,6 +103,10 @@ def submit(self, sequence, wait: bool = False, **kwargs) -> RemoteResults:
def _fetch_result(
self, submission_id: str, job_ids: list[str] | None = None
) -> typing.Sequence[Result]:
self._progress_calls += 1
if self._progress_calls == 1:
raise RemoteResultsError("Results not available")

return (self.result,)

def _query_job_progress(
Expand All @@ -110,9 +115,6 @@ def _query_job_progress(
return {"abcd": (JobStatus.DONE, self.result)}

def _get_submission_status(self, submission_id: str) -> SubmissionStatus:
self._status_calls += 1
if self._status_calls == 1:
return SubmissionStatus.RUNNING
return SubmissionStatus.DONE


Expand Down Expand Up @@ -181,10 +183,16 @@ def test_qpu_backend(sequence):

with pytest.raises(
RemoteResultsError,
match="The results are not available. The submission's status is"
" SubmissionStatus.RUNNING",
match=(
"Results are not available for all jobs. "
"Use the `get_available_results` method to retrieve partial "
"results."
),
):
remote_results.results

results = remote_results.results
assert results[0].sampling_dist == {"00": 1.0}

available_results = remote_results.get_available_results("id")
assert available_results == {"abcd": connection.result}
13 changes: 13 additions & 0 deletions tests/test_pasqal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import pulser_pasqal
from pulser.backend.config import EmulatorConfig
from pulser.backend.remote import (
JobStatus,
RemoteConnection,
RemoteResults,
SubmissionStatus,
Expand Down Expand Up @@ -82,6 +83,7 @@ def __init__(
self.variables = variables
self.result = result
self.id = str(np.random.randint(10000))
self.status = JobStatus.DONE.name


@pytest.fixture
Expand Down Expand Up @@ -190,6 +192,17 @@ def test_remote_results(fixt, mock_batch, with_job_id):

assert hasattr(remote_results, "_results")

fixt.mock_cloud_sdk.get_batch.reset_mock()
available_results = remote_results.get_available_results("id")
assert available_results == {
job.id: SampledResult(
atom_order=("q0", "q1", "q2", "q3"),
meas_basis="ground-rydberg",
bitstring_counts=job.result,
)
for job in select_jobs
}


@pytest.mark.parametrize("mimic_qpu", [False, True])
@pytest.mark.parametrize(
Expand Down

0 comments on commit 1332859

Please sign in to comment.