Skip to content

Commit

Permalink
Implementing get_my_targets() (#1111)
Browse files Browse the repository at this point in the history
Clientside changes for the new `get_my_targets()` endpoint that returns
only `ss_unconstrained_simulator` if it is a FREE_TRIAL user otherwise
filters targets with `accessible= True`.
  • Loading branch information
natibek authored Dec 11, 2024
1 parent 63c080d commit 0a455f1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions general-superstaq/general_superstaq/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ def get_targets(
)
return self._client.get_targets(**filters)

def get_my_targets(self) -> list[gss.Target]:
"""Gets a list of Superstaq targets that the user can submit to and are available along
with their status information.
Returns:
A list of Superstaq targets that the user can currently submit to.
"""
return self._client.get_my_targets()

@overload
def get_user_info(self) -> dict[str, str | float]: ...

Expand Down
32 changes: 32 additions & 0 deletions general-superstaq/general_superstaq/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,38 @@ def test_service_get_targets(_mock_get_request: mock.MagicMock) -> None:
assert service.get_targets() == RETURNED_TARGETS


@mock.patch(
"general_superstaq.superstaq_client._SuperstaqClient.post_request",
return_value={
"superstaq_targets": {
"ss_unconstrained_simulator": {
"supports_submit": True,
"supports_submit_qubo": True,
"supports_compile": True,
"available": True,
"retired": False,
"accessible": True,
}
}
},
)
def test_service_get_my_targets(_mock_post_request: mock.MagicMock) -> None:
service = gss.service.Service(api_key="key", remote_host="http://example.com")
assert service.get_my_targets() == [
gss.typing.Target(
target="ss_unconstrained_simulator",
**{
"supports_submit": True,
"supports_submit_qubo": True,
"supports_compile": True,
"available": True,
"retired": False,
"accessible": True,
},
)
]


@mock.patch(
"general_superstaq.superstaq_client._SuperstaqClient.aqt_get_configs",
return_value={"pulses": "Hello", "variables": "World"},
Expand Down
17 changes: 17 additions & 0 deletions general-superstaq/general_superstaq/superstaq_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ def get_targets(self, **kwargs: bool | None) -> list[gss.Target]:
]
return target_list

def get_my_targets(self) -> list[gss.Target]:
"""Makes a GET request to retrieve targets from the Superstaq API.
Returns:
A list of Superstaq targets matching all provided criteria.
"""
json_dict: dict[str, str | bool] = {"accessible": True}
if self.client_kwargs:
json_dict["options"] = json.dumps(self.client_kwargs)

superstaq_targets = self.post_request("/targets", json_dict)["superstaq_targets"]
target_list = [
gss.Target(target=target_name, **properties)
for target_name, properties in superstaq_targets.items()
]
return target_list

def add_new_user(self, json_dict: dict[str, str]) -> str:
"""Makes a POST request to Superstaq API to add a new user.
Expand Down
35 changes: 35 additions & 0 deletions general-superstaq/general_superstaq/superstaq_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,41 @@ def test_superstaq_client_get_targets(mock_post: mock.MagicMock) -> None:
)


@mock.patch("requests.Session.post")
def test_superstaq_client_get_my_targets(mock_post: mock.MagicMock) -> None:
mock_post.return_value.ok = True
target = {
"ss_unconstrained_simulator": {
"supports_submit": True,
"supports_submit_qubo": True,
"supports_compile": True,
"available": True,
"retired": False,
"accessible": True,
}
}
mock_post.return_value.json.return_value = {"superstaq_targets": target}
client = gss.superstaq_client._SuperstaqClient(
client_name="general-superstaq",
remote_host="http://example.com",
api_key="to_my_heart",
ibmq_token="token",
)
response = client.get_my_targets()
assert response == [
gss.typing.Target(
target="ss_unconstrained_simulator",
**target["ss_unconstrained_simulator"],
)
]
mock_post.assert_called_once_with(
f"http://example.com/{API_VERSION}/targets",
headers=EXPECTED_HEADERS,
verify=False,
json={"accessible": True, "options": json.dumps({"ibmq_token": "token"})},
)


@mock.patch("requests.Session.post")
def test_superstaq_client_fetch_jobs_unauthorized(mock_post: mock.MagicMock) -> None:
mock_post.return_value.ok = False
Expand Down
1 change: 1 addition & 0 deletions general-superstaq/general_superstaq/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ class Target(pydantic.BaseModel):
supports_compile: bool = False
available: bool = False
retired: bool = False
accessible: bool = False

0 comments on commit 0a455f1

Please sign in to comment.