Skip to content

Commit

Permalink
Filter backends by instance when given in QiskitRuntimeService constr…
Browse files Browse the repository at this point in the history
…uctor (#979)

* Added filtering of backends depending on the instance defined in init of QiskitRuntimeService

* Change warning to error when input instance is not supported. Removed instance 'h/g/p' from tests

* Added new exception to documentation

* Release note

* Removed instance from test

* Syntax changes

* black

---------

Co-authored-by: Kevin Tian <[email protected]>
  • Loading branch information
merav-aharoni and kt474 authored Aug 1, 2023
1 parent 1fa790d commit 928184e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
17 changes: 9 additions & 8 deletions qiskit_ibm_runtime/qiskit_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ def _initialize_hgps(
Raises:
IBMInputValueError: If the URL specified is not a valid IBM Quantum authentication URL.
IBMAccountError: If no hub/group/project could be found for this account.
IBMInputValueError: If instance parameter is not found in hgps.
Returns:
The hub/group/projects for this account.
Expand Down Expand Up @@ -409,9 +410,8 @@ def _initialize_hgps(
# Move user selected hgp to front of the list
hgps.move_to_end(default_hgp, last=False)
else:
warnings.warn(
f"Default hub/group/project {default_hgp} not "
"found for the account and is ignored."
raise IBMInputValueError(
f"Hub/group/project {default_hgp} could not be found for this account."
)
return hgps

Expand Down Expand Up @@ -538,6 +538,7 @@ def backends(
"""
# TODO filter out input_allowed not having runtime
backends: List[IBMBackend] = []
instance_filter = instance if instance else self._account.instance
if self._channel == "ibm_quantum":
if name:
if name not in self._backends:
Expand All @@ -550,16 +551,16 @@ def backends(
)
if self._backends[name]:
backends.append(self._backends[name])
elif instance:
hgp = self._get_hgp(instance=instance)
elif instance_filter:
hgp = self._get_hgp(instance=instance_filter)
for backend_name in hgp.backends:
if (
not self._backends[backend_name]
or instance != self._backends[backend_name]._instance
or instance_filter != self._backends[backend_name]._instance
):
self._set_backend_config(backend_name, instance)
self._set_backend_config(backend_name, instance_filter)
self._backends[backend_name] = self._create_backend_obj(
self._backend_configs[backend_name], instance
self._backend_configs[backend_name], instance_filter
)
if self._backends[backend_name]:
backends.append(self._backends[backend_name])
Expand Down
8 changes: 8 additions & 0 deletions releasenotes/notes/filter_instance-a7203041b5ab85d9.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
features:
- |
If ''instance'' is provided as parameter to
:meth:`QiskitRuntimeService.__init__`,
then this is used as a filter in :meth:`QiskitRuntimeService.backends()`.
If ''instance'' is not recognized as one of the provider instances,
an exception will be raised. Previously, we only issued a warning.
8 changes: 4 additions & 4 deletions test/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def run_quantum_and_cloud_fake(func):

@wraps(func)
def _wrapper(self, *args, **kwargs):
ibm_quantum_service = FakeRuntimeService(
channel="ibm_quantum", token="my_token", instance="h/g/p"
)
ibm_quantum_service = FakeRuntimeService(channel="ibm_quantum", token="my_token")
cloud_service = FakeRuntimeService(
channel="ibm_cloud",
token="my_token",
Expand Down Expand Up @@ -129,7 +127,9 @@ def _wrapper(self, *args, **kwargs):
service = None
if init_service:
service = QiskitRuntimeService(
channel=channel, token=token, url=url, instance=instance
channel=channel,
token=token,
url=url,
)
dependencies = IntegrationTestDependencies(
channel=channel,
Expand Down
12 changes: 11 additions & 1 deletion test/unit/test_backend_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ def test_filter_by_instance_ibm_quantum(self):
for back in backends:
self.assertEqual(back._instance, hgp)

def test_filter_by_service_instance_ibm_quantum(self):
"""Test filtering by QiskitRuntimeService._account.instance (works only on ibm_quantum)."""
for hgp in FakeRuntimeService.DEFAULT_HGPS:
service = FakeRuntimeService(channel="ibm_quantum", token="my_token", instance=hgp)
with self.subTest(hgp=hgp):
backends = service.backends()
backend_name = [back.name for back in backends]
self.assertEqual(len(backend_name), 2)
for back in backends:
self.assertEqual(back._instance, hgp)

def test_filter_config_properties(self):
"""Test filtering by configuration properties."""
n_qubits = 5
Expand Down Expand Up @@ -192,7 +203,6 @@ def _get_services(self, fake_backend_specs):
ibm_quantum_service = FakeRuntimeService(
channel="ibm_quantum",
token="my_token",
instance="h/g/p",
backend_specs=fake_backend_specs,
)
cloud_service = FakeRuntimeService(
Expand Down

0 comments on commit 928184e

Please sign in to comment.