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

Update to retrieve results with v0.3 #96

Merged
merged 6 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://arxiv.org/pdf/2301.07233.pdf>
[(#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)
Expand Down
19 changes: 12 additions & 7 deletions pennylane_ionq/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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.
Expand All @@ -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:
Expand All @@ -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):
"""
Expand All @@ -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.
Expand All @@ -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())
lillian542 marked this conversation as resolved.
Show resolved Hide resolved

if hasattr(self.resource, "refresh_data"):
self.resource.refresh_data()

Expand Down Expand Up @@ -449,7 +455,6 @@ def __init__(self, client=None, api_key=None):
"""
self.fields = (
Field("id", str),
Field("type", str),
timmysilv marked this conversation as resolved.
Show resolved Hide resolved
Field("status", str),
Field("request", dateutil.parser.parse),
Field("response", dateutil.parser.parse),
Expand Down
2 changes: 1 addition & 1 deletion pennylane_ionq/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
# 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

Check notice on line 234 in pennylane_ionq/device.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane_ionq/device.py#L234

Attribute 'histogram' defined outside __init__ (attribute-defined-outside-init)
timmysilv marked this conversation as resolved.
Show resolved Hide resolved

@property
def prob(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading