Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Reset backend options to default in get_backend #705

Merged
merged 7 commits into from
Aug 22, 2023
5 changes: 5 additions & 0 deletions qiskit_ibm_provider/ibm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class IBMProvider(Provider):

simulator_backend = provider.get_backend('ibmq_qasm_simulator')

IBMBackend's are uniquely identified by their name. If you invoke :meth:`get_backend()` twice,
you will get the same IBMBackend instance, and any previously updated options will be reset
to the default values.

It is also possible to use the ``backend`` attribute to reference a backend.
As an example, to retrieve the same backend from the example above::

Expand Down Expand Up @@ -660,6 +664,7 @@ def get_backend(
)
if not backends:
raise QiskitBackendNotFoundError("No backend matches the criteria")
backends[0]._options = IBMBackend._default_options()
return backends[0]

def __repr__(self) -> str:
Expand Down
12 changes: 12 additions & 0 deletions releasenotes/notes/reset_backend_options-f7e02c9bfff58213.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
fixes:
- |
``IBMProvider.get_backend()`` returns the backend with its default options. At the end of this
.. code-block::

backend1 = provider.get_backend("xxx")
backend1.options.shots = 100
backend2 = provider.get_backend("xxx")

``backend2.options.shots`` has the default value (4000). ``backend1.options.shots``
also has the default value, because backend1 and backend2 are the same instance.
13 changes: 13 additions & 0 deletions test/integration/test_ibm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ def test_get_backend(self):
backend = self.dependencies.provider.get_backend(name=self.backend_name)
self.assertEqual(backend.name, self.backend_name)

def test_get_backend_options(self):
"""Test resetting backend options when calling get_backend."""
default_shots = 4000
backend1 = self.dependencies.provider.get_backend(name=self.backend_name)
self.assertEqual(backend1.options.shots, default_shots)
backend1.options.shots = 100
self.assertEqual(backend1.options.shots, 100)
backend2 = self.dependencies.provider.get_backend(name=self.backend_name)
# When getting a backend, it has the default options.
# backend1 and backend2 are the same instance.
self.assertEqual(backend2.options.shots, default_shots)
self.assertEqual(backend1.options.shots, default_shots)

def test_jobs_filter(self):
"""Test limit filters when accessing jobs from the provider."""
num_jobs = PAGE_SIZE + 1
Expand Down
Loading