Skip to content

Commit

Permalink
Raise error if backend retrieved not in current instance (#1249)
Browse files Browse the repository at this point in the history
* Error if backend not in instance

* add reno

* update reno

* docs build

* fix docs again

* docs formatting :/

* add integration test

* Update test/integration/test_backend.py

Co-authored-by: merav-aharoni <[email protected]>

* Update test/integration/test_backend.py

Co-authored-by: merav-aharoni <[email protected]>

---------

Co-authored-by: merav-aharoni <[email protected]>
  • Loading branch information
kt474 and merav-aharoni authored Dec 5, 2023
1 parent a618de6 commit 5010416
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
14 changes: 11 additions & 3 deletions qiskit_ibm_runtime/qiskit_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,11 @@ def backends(
if name:
if name not in self._backends:
raise QiskitBackendNotFoundError("No backend matches the criteria.")
if not self._backends[name] or instance != self._backends[name]._instance:
if not self._backends[name] or instance_filter != self._backends[name]._instance:
self._set_backend_config(name)
self._backends[name] = self._create_backend_obj(
self._backend_configs[name],
instance,
instance_filter,
)
if self._backends[name]:
backends.append(self._backends[name])
Expand Down Expand Up @@ -664,9 +664,17 @@ def _create_backend_obj(
break

elif config.backend_name not in self._get_hgp(instance=instance).backends:
hgps_with_backend = []
for hgp in list(self._hgps.values()):
if config.backend_name in hgp.backends:
hgps_with_backend.append(
to_instance_format(hgp._hub, hgp._group, hgp._project)
)
raise QiskitBackendNotFoundError(
f"Backend {config.backend_name} is not in "
f"{instance}: please try a different hub/group/project."
f"{instance}. Please try a different instance. "
f"{config.backend_name} is in the following instances you have access to: "
f"{hgps_with_backend}"
)

return ibm_backend.IBMBackend(
Expand Down
16 changes: 16 additions & 0 deletions releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
fixes:
- |
When a single backend is retrieved with the ``instance`` parameter,
.. code-block::
service.backend('ibm_torino', instance='ibm-q/open/main')
# raises error if torino is not in ibm-q/open/main but in a different instance
# the user has access to
service = QiskitRuntimeService(channel="ibm_quantum", instance="ibm-q/open/main")
service.backend('ibm_torino') # raises the same error
if the backend is not in the instance but in a different one the user has access to, an error
will be raised. The same error will now be raised if an instance is passed in at initialization
and then a backend not in that instance is retrieved.
19 changes: 19 additions & 0 deletions test/integration/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def test_backends(self, service):
f"backend_names={backend_names}",
)

@run_integration_test
def test_backend_wrong_instance(self, service):
"""Test getting a backend with wrong instance."""
hgps = list(service._hgps.keys())
if len(hgps) < 2:
raise SkipTest("Skipping test, not enough instances")

hgp_1 = hgps[0]
hgp_2 = hgps[1]
hgp_1_backends = service.backends(instance=hgp_1)
hgp_2_backends = service.backends(instance=hgp_2)
unique_hgp = list(
set(hgp_2_backends) - set(hgp_1_backends)
) # get differences between the two lists
if unique_hgp:
unique_backend = unique_hgp[0]
with self.assertRaises(QiskitBackendNotFoundError):
service.backend(unique_backend.name, instance=hgp_1)

@run_integration_test
@quantum_only
def test_backends_no_config(self, service):
Expand Down

0 comments on commit 5010416

Please sign in to comment.