diff --git a/CHANGELOG.md b/CHANGELOG.md index adcc5cd..225d546 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,11 +14,13 @@ For more details, see the [IonQ Guide on sharpening and debiasing](https://ionq.com/resources/debiasing-and-sharpening), or refer to the publication [(#75)](https://github.com/PennyLaneAI/PennyLane-IonQ/pull/75) + [(#96)](https://github.com/PennyLaneAI/PennyLane-IonQ/pull/96) ### Improvements 🛠 * The IonQ API version accessed via the plugin is updated from 0.1 to 0.3 [(#75)](https://github.com/PennyLaneAI/PennyLane-IonQ/pull/75) + [(#96)](https://github.com/PennyLaneAI/PennyLane-IonQ/pull/96) * Use new `backend` field to specify `qpu`. [(#81)](https://github.com/PennyLaneAI/PennyLane-IonQ/pull/81) diff --git a/pennylane_ionq/api_client.py b/pennylane_ionq/api_client.py index 4f6b19d..363ca39 100644 --- a/pennylane_ionq/api_client.py +++ b/pennylane_ionq/api_client.py @@ -270,7 +270,9 @@ def get(self, resource_id=None, params=None): response = self.client.get(self.join_path(str(resource_id)), params=params) else: response = self.client.get(self.resource.PATH, params=params) - self.handle_response(response) + + # we need params later, unfortuantely + self.handle_response(response, params) def create(self, **params): """ @@ -290,7 +292,7 @@ def create(self, **params): self.handle_response(response) - def handle_response(self, response): + def handle_response(self, response, params=None): """ Store the status code on the manager object and handle the response based on the status code. @@ -303,7 +305,7 @@ def handle_response(self, response): self.http_response_status_code = response.status_code if response.status_code in (200, 201): - self.handle_success_response(response) + self.handle_success_response(response, params=params) else: self.handle_error_response(response) else: @@ -315,14 +317,14 @@ def handle_no_response(self): """ warnings.warn("Your request could not be completed") - def handle_success_response(self, response): + def handle_success_response(self, response, params=None): """ Handles a successful response by refreshing the instance fields. Args: response (requests.Response): a response object to be parsed """ - self.refresh_data(response.json()) + self.refresh_data(response.json(), params=params) def handle_error_response(self, response): """ @@ -339,7 +341,7 @@ def handle_error_response(self, response): except Exception as e: raise Exception(response.text) from e - def refresh_data(self, data): + def refresh_data(self, data, params=None): """ Refreshes the instance's attributes with the provided data and converts it to the correct type. @@ -350,6 +352,10 @@ def refresh_data(self, data): for field in self.resource.fields: field.set(data.get(field.name, None)) + if "results_url" in data.keys(): + result = self.client.get(self.join_path(data["results_url"]), params=params) + self.resource.fields[-1].set(result.json()) + if hasattr(self.resource, "refresh_data"): self.resource.refresh_data() @@ -449,10 +455,11 @@ def __init__(self, client=None, api_key=None): """ self.fields = ( Field("id", str), - Field("type", str), Field("status", str), Field("request", dateutil.parser.parse), Field("response", dateutil.parser.parse), + # it is important that data remain the final item in + # this tuple to ensure storing results in the correct entry Field("data"), ) diff --git a/pennylane_ionq/device.py b/pennylane_ionq/device.py index 026f699..5d19c08 100644 --- a/pennylane_ionq/device.py +++ b/pennylane_ionq/device.py @@ -231,7 +231,7 @@ def _submit_job(self): # state (as a base-10 integer string) to the probability # as a floating point value between 0 and 1. # e.g., {"0": 0.413, "9": 0.111, "17": 0.476} - self.histogram = job.data.value["histogram"] + self.histogram = job.data.value @property def prob(self): diff --git a/tests/test_api_client.py b/tests/test_api_client.py index e95e720..c465f01 100755 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -188,7 +188,7 @@ def test_get(self, monkeypatch, resource_id, params): # TODO test that this is called with correct path mock_client.get.assert_called_once() - manager.handle_response.assert_called_once_with(mock_response) + manager.handle_response.assert_called_once_with(mock_response, params) def test_create_unsupported(self): """ @@ -259,7 +259,7 @@ def test_handle_response(self, monkeypatch): mock_response.status_code = 200 manager.handle_response(mock_response) - mock_handle_success_response.assert_called_once_with(mock_response) + mock_handle_success_response.assert_called_once_with(mock_response, params=None) def test_handle_refresh_data(self): """ diff --git a/tests/test_device.py b/tests/test_device.py index 847a768..6333fe6 100755 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -116,7 +116,7 @@ def test_shots(self, shots, monkeypatch, mocker, tol): def fake_response(self, resource_id=None, params=None): """Return fake response data""" - fake_json = {"histogram": {"0": 1}} + fake_json = {"0": 1} setattr(self.resource, "data", type("data", tuple(), {"value": fake_json})()) monkeypatch.setattr(ResourceManager, "get", fake_response) @@ -145,7 +145,7 @@ def test_error_mitigation(self, error_mitigation, monkeypatch, mocker): def fake_response(self, resource_id=None, params=None): """Return fake response data""" - fake_json = {"histogram": {"0": 1}} + fake_json = {"0": 1} setattr(self.resource, "data", type("data", tuple(), {"value": fake_json})()) monkeypatch.setattr(ResourceManager, "get", fake_response)