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

Commit

Permalink
Reset backend options to default in get_backend (#705)
Browse files Browse the repository at this point in the history
* Reset backend.options to default values in IBMProvider.get_backend()

* Reset backend.options to default values in IBMProvider.get_backend()

* Minor changes

* Update releasenotes/notes/reset_backend_options-f7e02c9bfff58213.yaml

Co-authored-by: Kevin Tian <[email protected]>

* Update releasenotes/notes/reset_backend_options-f7e02c9bfff58213.yaml

Co-authored-by: Kevin Tian <[email protected]>

---------

Co-authored-by: Kevin Tian <[email protected]>
  • Loading branch information
merav-aharoni and kt474 authored Aug 22, 2023
1 parent 4c4baa1 commit d724b5d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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

0 comments on commit d724b5d

Please sign in to comment.