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

Raise error if backend retrieved not in current instance #1249

Merged
merged 11 commits into from
Dec 5, 2023
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:
unqiue_backned = unique_hgp[0]
kt474 marked this conversation as resolved.
Show resolved Hide resolved
with self.assertRaises(QiskitBackendNotFoundError):
service.backend(unqiue_backned.name, instance=hgp_1)
kt474 marked this conversation as resolved.
Show resolved Hide resolved

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