From f75f0f6563452814da16791ab59af4fed255cb0c Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Fri, 7 Apr 2023 00:11:50 +0100 Subject: [PATCH] Defer gathering backends until they are needed (#1760) * 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 --- qiskit_aer/aerprovider.py | 10 ++++++++-- .../defer-backend-gathering-773d0ed8092c24d9.yaml | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/defer-backend-gathering-773d0ed8092c24d9.yaml diff --git a/qiskit_aer/aerprovider.py b/qiskit_aer/aerprovider.py index 8fca28f521..5ad1956495 100644 --- a/qiskit_aer/aerprovider.py +++ b/qiskit_aer/aerprovider.py @@ -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() @@ -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( @@ -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 diff --git a/releasenotes/notes/defer-backend-gathering-773d0ed8092c24d9.yaml b/releasenotes/notes/defer-backend-gathering-773d0ed8092c24d9.yaml new file mode 100644 index 0000000000..1045d675f3 --- /dev/null +++ b/releasenotes/notes/defer-backend-gathering-773d0ed8092c24d9.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Available devices and methods are no longer queried when importing Aer.