Skip to content

Commit

Permalink
Defer gathering backends until they are needed (Qiskit#1760)
Browse files Browse the repository at this point in the history
* Defer gathering backends until they are needed

* Disable the not-an-iterable warning

Pylint infers _get_backends to always return None, even if we add type
annotations. Suppress the warning.

* Add @staticmethod to AerProvider._get_backends

---------

Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>
  • Loading branch information
SquidDev and hhorii committed Apr 7, 2023
1 parent fdded76 commit f75f0f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions qiskit_aer/aerprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class AerProvider(Provider):

_BACKENDS = None

def __init__(self):
@staticmethod
def _get_backends():
if AerProvider._BACKENDS is None:
# Populate the list of Aer simulator backends.
methods = AerSimulator().available_methods()
Expand Down Expand Up @@ -60,6 +61,8 @@ def __init__(self):
]
AerProvider._BACKENDS = backends

return AerProvider._BACKENDS

def get_backend(self, name=None, **kwargs):
if name == "pulse_simulator":
warnings.warn(
Expand All @@ -76,7 +79,10 @@ def backends(self, name=None, filters=None, **kwargs):
# Instantiate a new backend instance so if config options
# are set they will only last as long as that backend object exists
backends = []
for backend_name, backend_cls, method, device in self._BACKENDS:

# pylint: disable=not-an-iterable
# pylint infers _get_backends to always return None
for backend_name, backend_cls, method, device in self._get_backends():
opts = {"provider": self}
if method is not None:
opts["method"] = method
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Available devices and methods are no longer queried when importing Aer.

0 comments on commit f75f0f6

Please sign in to comment.