From 3103e0f697c4d0c0642918c432f8a6ec8a72a422 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Thu, 30 Nov 2023 14:06:57 -0500 Subject: [PATCH 1/9] Error if backend not in instance --- qiskit_ibm_runtime/qiskit_runtime_service.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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( From 7ebf1f0fb961199c7766bfa0fd882b88cc6df3d7 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Thu, 30 Nov 2023 14:42:19 -0500 Subject: [PATCH 2/9] add reno --- .../notes/backend-instance-filter-20d69b3951437f19.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml diff --git a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml new file mode 100644 index 000000000..42b8e8583 --- /dev/null +++ b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + If an instance is passed in at initialization, and a backend that is not in that instance + but a different instance the user has access to is retrieved, an error is raised. From d9a04dc3a163a35c27e808cc402f44642a8b7388 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 4 Dec 2023 13:12:46 -0500 Subject: [PATCH 3/9] update reno --- .../backend-instance-filter-20d69b3951437f19.yaml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml index 42b8e8583..380aa80d4 100644 --- a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml +++ b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml @@ -1,5 +1,14 @@ --- fixes: - | - If an instance is passed in at initialization, and a backend that is not in that instance - but a different instance the user has access to is retrieved, an error is raised. + When a single backend is retrieved with the ``instance`` parameter, + .. code-block:: + service.backend('ibm_torino', instance='ibm-q/open/main') + + 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. + + .. code-block:: + service = QiskitRuntimeService(channel="ibm_quantum", instance="ibm-q/open/main") + service.backend('ibm_torino') # raises error if torino is not in ibm-q/open/main \ No newline at end of file From 85c2ddc4b9c82d13c9be31c93ab6aea8fba41e90 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 4 Dec 2023 13:33:28 -0500 Subject: [PATCH 4/9] docs build --- .../notes/backend-instance-filter-20d69b3951437f19.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml index 380aa80d4..9b029959c 100644 --- a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml +++ b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml @@ -2,6 +2,7 @@ fixes: - | When a single backend is retrieved with the ``instance`` parameter, + .. code-block:: service.backend('ibm_torino', instance='ibm-q/open/main') @@ -11,4 +12,4 @@ fixes: .. code-block:: service = QiskitRuntimeService(channel="ibm_quantum", instance="ibm-q/open/main") - service.backend('ibm_torino') # raises error if torino is not in ibm-q/open/main \ No newline at end of file + service.backend('ibm_torino') # raises error if torino is not in ibm-q/open/main From abf546638f4f3182431bd9f473e366e874b201cb Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 4 Dec 2023 13:37:45 -0500 Subject: [PATCH 5/9] fix docs again --- .../notes/backend-instance-filter-20d69b3951437f19.yaml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml index 9b029959c..89121a98e 100644 --- a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml +++ b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml @@ -5,11 +5,12 @@ fixes: .. 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. - - .. code-block:: - service = QiskitRuntimeService(channel="ibm_quantum", instance="ibm-q/open/main") - service.backend('ibm_torino') # raises error if torino is not in ibm-q/open/main From d66669ea931d2b355ffe0738aa20f0887ff54d85 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 4 Dec 2023 13:42:11 -0500 Subject: [PATCH 6/9] docs formatting :/ --- .../notes/backend-instance-filter-20d69b3951437f19.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml index 89121a98e..b23198477 100644 --- a/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml +++ b/releasenotes/notes/backend-instance-filter-20d69b3951437f19.yaml @@ -4,10 +4,10 @@ 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 From d34c78891c9928d9a379b8f3b79cf88c22f39387 Mon Sep 17 00:00:00 2001 From: kevin-tian Date: Mon, 4 Dec 2023 18:27:26 -0500 Subject: [PATCH 7/9] add integration test --- test/integration/test_backend.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/integration/test_backend.py b/test/integration/test_backend.py index 7c726b492..ac7465341 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: + unqiue_backned = unique_hgp[0] + with self.assertRaises(QiskitBackendNotFoundError): + service.backend(unqiue_backned.name, instance=hgp_1) + @run_integration_test @quantum_only def test_backends_no_config(self, service): From 3ab432425766d1e204a9dba80b06669e8ee8ff58 Mon Sep 17 00:00:00 2001 From: Kevin Tian Date: Tue, 5 Dec 2023 10:41:59 -0500 Subject: [PATCH 8/9] Update test/integration/test_backend.py Co-authored-by: merav-aharoni --- test/integration/test_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/test_backend.py b/test/integration/test_backend.py index ac7465341..3625fda5f 100644 --- a/test/integration/test_backend.py +++ b/test/integration/test_backend.py @@ -60,7 +60,7 @@ def test_backend_wrong_instance(self, service): set(hgp_2_backends) - set(hgp_1_backends) ) # get differences between the two lists if unique_hgp: - unqiue_backned = unique_hgp[0] + unique_backend = unique_hgp[0] with self.assertRaises(QiskitBackendNotFoundError): service.backend(unqiue_backned.name, instance=hgp_1) From 69e69b4d04b3226a241d10da26076f3f07eaa8d0 Mon Sep 17 00:00:00 2001 From: Kevin Tian Date: Tue, 5 Dec 2023 10:42:07 -0500 Subject: [PATCH 9/9] Update test/integration/test_backend.py Co-authored-by: merav-aharoni --- test/integration/test_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/test_backend.py b/test/integration/test_backend.py index 3625fda5f..865a3659d 100644 --- a/test/integration/test_backend.py +++ b/test/integration/test_backend.py @@ -62,7 +62,7 @@ def test_backend_wrong_instance(self, service): if unique_hgp: unique_backend = unique_hgp[0] with self.assertRaises(QiskitBackendNotFoundError): - service.backend(unqiue_backned.name, instance=hgp_1) + service.backend(unique_backend.name, instance=hgp_1) @run_integration_test @quantum_only