-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from qiboteam/list_backends
MetaBackend
- Loading branch information
Showing
4 changed files
with
60 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import importlib.metadata as im | ||
|
||
from qibojit.backends import MetaBackend | ||
|
||
__version__ = im.version(__package__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,46 @@ | ||
from typing import Union | ||
|
||
from qibojit.backends.cpu import NumbaBackend | ||
from qibojit.backends.gpu import CupyBackend, CuQuantumBackend | ||
|
||
QibojitBackend = Union[NumbaBackend, CupyBackend, CuQuantumBackend] | ||
|
||
PLATFORMS = ("numba", "cupy", "cuquantum") | ||
|
||
|
||
class MetaBackend: | ||
"""Meta-backend class which takes care of loading the qibojit backends.""" | ||
|
||
@staticmethod | ||
def load(platform: str = None) -> QibojitBackend: | ||
"""Loads the backend. | ||
Args: | ||
platform (str): Name of the backend to load: either `numba`, `cupy` or `cuquantum`. | ||
Returns: | ||
qibo.backends.abstract.Backend: The loaded backend. | ||
""" | ||
|
||
if platform == "numba": | ||
return NumbaBackend() | ||
elif platform == "cupy": | ||
return CupyBackend() | ||
elif platform == "cuquantum": | ||
return CuQuantumBackend() | ||
else: # pragma: no cover | ||
try: | ||
return CupyBackend() | ||
except (ModuleNotFoundError, ImportError): | ||
return NumbaBackend() | ||
|
||
def list_available(self) -> dict: | ||
"""Lists all the available qibojit backends.""" | ||
available_backends = {} | ||
for platform in PLATFORMS: | ||
try: | ||
MetaBackend.load(platform=platform) | ||
available = True | ||
except: | ||
available = False | ||
available_backends[platform] = available | ||
return available_backends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters