Skip to content

Commit

Permalink
Only return channel strategy supported backends (#1095)
Browse files Browse the repository at this point in the history
* Q-CTRL Backend filters

* add reno

* fix unit tests
  • Loading branch information
kt474 authored Sep 21, 2023
1 parent e924296 commit 40a5217
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
10 changes: 8 additions & 2 deletions qiskit_ibm_runtime/api/clients/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,19 @@ def close_session(self, session_id: str) -> None:
"""
self._api.runtime_session(session_id=session_id).close()

def list_backends(self, hgp: Optional[str] = None) -> List[str]:
def list_backends(
self, hgp: Optional[str] = None, channel_strategy: Optional[str] = None
) -> List[str]:
"""Return IBM backends available for this service instance.
Args:
hgp: Filter by hub/group/project.
channel_strategy: Filter by channel strategy.
Returns:
IBM backends available for this service instance.
"""
return self._api.backends(hgp=hgp)["devices"]
return self._api.backends(hgp=hgp, channel_strategy=channel_strategy)["devices"]

def backend_configuration(self, backend_name: str) -> Dict[str, Any]:
"""Return the configuration of the IBM backend.
Expand Down
14 changes: 11 additions & 3 deletions qiskit_ibm_runtime/api/rest/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,25 @@ def backend(self, backend_name: str) -> CloudBackend:
return CloudBackend(self.session, backend_name)

def backends(
self, hgp: Optional[str] = None, timeout: Optional[float] = None
self,
hgp: Optional[str] = None,
timeout: Optional[float] = None,
channel_strategy: Optional[str] = None,
) -> Dict[str, List[str]]:
"""Return a list of IBM backends.
Args:
hgp: The service instance to use, only for ``ibm_quantum`` channel, in h/g/p format.
timeout: Number of seconds to wait for the request.
channel_strategy: Error mitigation strategy.
Returns:
JSON response.
"""
url = self.get_url("backends")
params = {}
if hgp:
return self.session.get(url, params={"provider": hgp}).json()
return self.session.get(url, timeout=timeout).json()
params["provider"] = hgp
if channel_strategy:
params["channel_strategy"] = channel_strategy
return self.session.get(url, params=params, timeout=timeout).json()
2 changes: 1 addition & 1 deletion qiskit_ibm_runtime/qiskit_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def _discover_cloud_backends(self) -> Dict[str, "ibm_backend.IBMBackend"]:
A dict of the remote backend instances, keyed by backend name.
"""
ret = OrderedDict() # type: ignore[var-annotated]
backends_list = self._api_client.list_backends()
backends_list = self._api_client.list_backends(channel_strategy=self._channel_strategy)
for backend_name in backends_list:
raw_config = self._api_client.backend_configuration(backend_name=backend_name)
config = configuration_from_server_data(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
If using a ``channel_strategy``, only backends that support that ``channel_strategy``
will be accessible to the user.
5 changes: 4 additions & 1 deletion test/unit/mock/fake_runtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,10 @@ def _get_job(self, job_id: str, exclude_params: bool = None) -> Any:
raise RequestsApiError("Job not found", status_code=404)
return self._jobs[job_id]

def list_backends(self, hgp: Optional[str] = None) -> List[str]:
# pylint: disable=unused-argument
def list_backends(
self, hgp: Optional[str] = None, channel_strategy: Optional[str] = None
) -> List[str]:
"""Return IBM backends available for this service instance."""
return [back.name for back in self._backends if back.has_access(hgp)]

Expand Down

0 comments on commit 40a5217

Please sign in to comment.