Skip to content

Commit ad4c65d

Browse files
Improve backend module imports and dynamic loading
Refactor backend imports and add dynamic loading for backends.
1 parent 36e281d commit ad4c65d

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

src/qmlhc/backends/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11

22
# Public re-exports for backend adapters.
33

4-
from .qiskit_backend import QiskitBackend
5-
from .pennylane_backend import PennyLaneBackend
6-
from .cpp_backend import CppBackend
7-
8-
__all__ = [
9-
"QiskitBackend",
10-
"PennyLaneBackend",
11-
"CppBackend",
12-
]
4+
__all__ = ["QiskitBackend", "PennyLaneBackend", "CppBackend"]
5+
6+
import importlib
7+
import warnings
8+
9+
def __getattr__(name):
10+
if name == "PennyLaneBackend":
11+
mod = importlib.import_module(".pennylane_backend", __package__)
12+
return getattr(mod, name)
13+
elif name == "QiskitBackend":
14+
try:
15+
mod = importlib.import_module(".qiskit_backend", __package__)
16+
return getattr(mod, name)
17+
except Exception as e:
18+
warnings.warn(f"Qiskit backend not available: {e}")
19+
return None
20+
elif name == "CppBackend":
21+
mod = importlib.import_module(".cpp_backend", __package__)
22+
return getattr(mod, name)
23+
raise AttributeError(f"No backend named {name!r}")

0 commit comments

Comments
 (0)