Skip to content

Commit

Permalink
Use jobs order provided by the cloud (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri authored Sep 22, 2023
1 parent 5270944 commit 5256cf8
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 57 deletions.
18 changes: 3 additions & 15 deletions pulser-core/pulser/backend/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,12 @@ class RemoteResults(Results):
the results.
connection: The remote connection over which to get the submission's
status and fetch the results.
jobs_order: An optional list of job IDs (as stored by the connection)
used to order the results.
"""

def __init__(
self,
submission_id: str,
connection: RemoteConnection,
jobs_order: list[str] | None = None,
):
def __init__(self, submission_id: str, connection: RemoteConnection):
"""Instantiates a new collection of remote results."""
self._submission_id = submission_id
self._connection = connection
self._jobs_order = jobs_order

@property
def results(self) -> tuple[Result, ...]:
Expand All @@ -87,9 +79,7 @@ def __getattr__(self, name: str) -> Any:
status = self.get_status()
if status == SubmissionStatus.DONE:
self._results = tuple(
self._connection._fetch_result(
self._submission_id, self._jobs_order
)
self._connection._fetch_result(self._submission_id)
)
return self._results
raise RemoteResultsError(
Expand All @@ -112,9 +102,7 @@ def submit(
pass

@abstractmethod
def _fetch_result(
self, submission_id: str, jobs_order: list[str] | None
) -> typing.Sequence[Result]:
def _fetch_result(self, submission_id: str) -> typing.Sequence[Result]:
"""Fetches the results of a completed submission."""
pass

Expand Down
31 changes: 4 additions & 27 deletions pulser-pasqal/pulser_pasqal/pasqal_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,8 @@ def submit(self, sequence: Sequence, **kwargs: Any) -> RemoteResults:
configuration=configuration,
wait=False,
)
jobs_order = []
if job_params:
for job_dict in job_params:
for job in batch.jobs.values():
if (
job.id not in jobs_order
and job_dict["runs"] == job.runs
and job_dict.get("variables", None) == job.variables
):
jobs_order.append(job.id)
break
else:
raise RuntimeError(
f"Failed to find job ID for {job_dict}."
)

return RemoteResults(batch.id, self, jobs_order or None)

return RemoteResults(batch.id, self)

@backoff_decorator
def fetch_available_devices(self) -> dict[str, Device]:
Expand All @@ -170,9 +155,7 @@ def fetch_available_devices(self) -> dict[str, Device]:
for name, dev_str in abstract_devices.items()
}

def _fetch_result(
self, submission_id: str, jobs_order: list[str] | None
) -> tuple[Result, ...]:
def _fetch_result(self, submission_id: str) -> tuple[Result, ...]:
# For now, the results are always sampled results
get_batch_fn = backoff_decorator(self._sdk_connection.get_batch)
batch = get_batch_fn(id=submission_id)
Expand All @@ -182,13 +165,7 @@ def _fetch_result(
meas_basis = seq_builder.get_measurement_basis()

results = []

jobs = (
(batch.jobs[job_id] for job_id in jobs_order)
if jobs_order
else batch.jobs.values()
)
for job in jobs:
for job in batch.ordered_jobs:
vars = job.variables
size: int | None = None
if vars and "qubits" in vars:
Expand Down
2 changes: 1 addition & 1 deletion pulser-pasqal/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pasqal-cloud ~= 0.3.3
pasqal-cloud ~= 0.3.5
backoff ~= 2.2
4 changes: 1 addition & 3 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ def __init__(self):
def submit(self, sequence, **kwargs) -> RemoteResults:
return RemoteResults("abcd", self)

def _fetch_result(
self, submission_id: str, jobs_order: list[str] | None
) -> typing.Sequence[Result]:
def _fetch_result(self, submission_id: str) -> typing.Sequence[Result]:
return (
SampledResult(
("q0", "q1"),
Expand Down
12 changes: 1 addition & 11 deletions tests/test_pasqal.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def mock_batch(mock_job, seq):
class MockBatch:
id = "abcd"
status = "DONE"
jobs = {mock_job.id: mock_job}
ordered_jobs = [mock_job]
sequence_builder = seq_.to_abstract_repr()

return MockBatch()
Expand Down Expand Up @@ -208,16 +208,6 @@ def test_submit(fixt, parametrized, emulator, seq, mock_job):
)
)

job_params[0]["runs"] = 1
with pytest.raises(RuntimeError, match="Failed to find job ID"):
# Job runs don't match MockJob
fixt.pasqal_cloud.submit(
seq,
job_params=job_params,
emulator=emulator,
config=config,
)

job_params[0]["runs"] = {10}
with pytest.raises(
TypeError, match="Object of type set is not JSON serializable"
Expand Down

0 comments on commit 5256cf8

Please sign in to comment.