diff --git a/qiskit_ibm_runtime/qiskit_runtime_service.py b/qiskit_ibm_runtime/qiskit_runtime_service.py index 85a2c1190..17df8ec86 100644 --- a/qiskit_ibm_runtime/qiskit_runtime_service.py +++ b/qiskit_ibm_runtime/qiskit_runtime_service.py @@ -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]) @@ -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( diff --git a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml new file mode 100644 index 000000000..b23198477 --- /dev/null +++ b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml @@ -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. diff --git a/test/integration/test_backend.py b/test/integration/test_backend.py index 7c726b492..865a3659d 100644 --- a/test/integration/test_backend.py +++ b/test/integration/test_backend.py @@ -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):