Skip to content

Commit

Permalink
feat: Expose the platform locating function
Browse files Browse the repository at this point in the history
  • Loading branch information
alecandido committed Sep 18, 2024
1 parent 8daa663 commit 9a889b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/qibolab/_core/platform/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from .load import create_platform
from .platform import Platform
from . import load, platform
from .load import *
from .platform import *

__all__ = ["Platform", "create_platform"]
__all__ = []
__all__ += load.__all__
__all__ += platform.__all__
19 changes: 18 additions & 1 deletion src/qibolab/_core/platform/load.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import importlib.util
import os
from pathlib import Path
from typing import Optional

from qibo.config import raise_error

from .platform import Platform

__all__ = ["create_platform", "locate_platform"]

PLATFORM = "platform.py"
PLATFORMS = "QIBOLAB_PLATFORMS"

Expand All @@ -24,7 +27,7 @@ def _platforms_paths() -> list[Path]:

def _search(name: str, paths: list[Path]) -> Path:
"""Search paths for given platform name."""
for path in _platforms_paths():
for path in paths:
platform = path / name
if platform.exists():
return platform
Expand All @@ -44,6 +47,20 @@ def _load(platform: Path) -> Platform:
return module.create()


def locate_platform(name: str, paths: Optional[list[Path]] = None) -> Path:
"""Locate platform's path.
The ``name`` corresponds to the name of the folder in which the platform is defined,
i.e. the one containing the ``platform.py`` file.
If ``paths`` are specified, the environment is ignored, and the folder search
happens only in the specified paths.
"""
if paths is None:
paths = _platforms_paths()
return _search(name, paths)


def create_platform(name: str) -> Platform:
"""A platform for executing quantum algorithms.
Expand Down
2 changes: 2 additions & 0 deletions src/qibolab/_core/platform/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from ..sweeper import ParallelSweepers
from ..unrolling import Bounds, batch

__all__ = ["Platform"]

QubitMap = dict[QubitId, Qubit]
QubitPairMap = list[QubitPairId]
InstrumentMap = dict[InstrumentId, Instrument]
Expand Down

0 comments on commit 9a889b4

Please sign in to comment.