-
Notifications
You must be signed in to change notification settings - Fork 1
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 #11 from yasuhito/feature/add-program-and-device-t…
…ype-registry feat: add program and device type registry
- Loading branch information
Showing
8 changed files
with
380 additions
and
24 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Any | ||
|
||
from .tranqu_error import TranquError | ||
|
||
|
||
class DeviceLibNotFoundError(TranquError): | ||
"""Error raised when device library cannot be detected.""" | ||
|
||
|
||
class DeviceTypeManager: | ||
"""Class that manages mapping between device types and library identifiers.""" | ||
|
||
def __init__(self) -> None: | ||
self._type_registry: dict[type, str] = {} | ||
|
||
def register_type(self, device_lib: str, device_type: type) -> None: | ||
"""Register a device type and its library identifier. | ||
Args: | ||
device_lib (str): Library identifier (e.g., "qiskit", "oqtopus") | ||
device_type (Type): Device type class to register | ||
""" | ||
self._type_registry[device_type] = device_lib | ||
|
||
def detect_lib(self, device: Any) -> str | None: # noqa: ANN401 | ||
"""Detect library based on device type. | ||
Args: | ||
device (Any): Device to inspect | ||
Returns: | ||
str | None: Library identifier for registered device type, None otherwise | ||
""" | ||
for device_type, lib in self._type_registry.items(): | ||
if isinstance(device, device_type): | ||
return lib | ||
|
||
return None |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Any | ||
|
||
|
||
class ProgramTypeManager: | ||
"""Class that manages the mapping between program types and library identifiers.""" | ||
|
||
def __init__(self) -> None: | ||
self._type_registry: dict[type, str] = {} | ||
|
||
def register_type(self, program_lib: str, program_type: type) -> None: | ||
"""Register a program type and its library identifier. | ||
Args: | ||
program_lib (str): Library identifier (e.g., "qiskit", "tket") | ||
program_type (Type): Program type class to register | ||
""" | ||
self._type_registry[program_type] = program_lib | ||
|
||
def detect_lib(self, program: Any) -> str | None: # noqa: ANN401 | ||
"""Detect the library based on the program type. | ||
Args: | ||
program (Any): Program to inspect | ||
Returns: | ||
str | None: Library identifier for registered program type, None otherwise | ||
""" | ||
for program_type, lib in self._type_registry.items(): | ||
if isinstance(program, program_type): | ||
return lib | ||
|
||
return None |
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
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from tranqu.device_type_manager import DeviceTypeManager | ||
|
||
|
||
class DummyDevice: | ||
"""Dummy device class for testing""" | ||
|
||
|
||
class TestDeviceTypeManager: | ||
def setup_method(self): | ||
self.manager = DeviceTypeManager() | ||
|
||
def test_register_type(self): | ||
self.manager.register_type("dummy", DummyDevice) | ||
device = DummyDevice() | ||
|
||
result = self.manager.detect_lib(device) | ||
|
||
assert result == "dummy" | ||
|
||
def test_detect_lib_returns_none_for_unregistered_type(self): | ||
device = DummyDevice() | ||
|
||
result = self.manager.detect_lib(device) | ||
|
||
assert result is None | ||
|
||
def test_detect_lib_with_multiple_registrations(self): | ||
class AnotherDummyDevice: | ||
pass | ||
|
||
self.manager.register_type("dummy1", DummyDevice) | ||
self.manager.register_type("dummy2", AnotherDummyDevice) | ||
|
||
device1 = DummyDevice() | ||
device2 = AnotherDummyDevice() | ||
|
||
assert self.manager.detect_lib(device1) == "dummy1" | ||
assert self.manager.detect_lib(device2) == "dummy2" | ||
|
||
def test_register_type_multiple_times(self): | ||
self.manager.register_type("dummy", DummyDevice) | ||
self.manager.register_type("another_dummy", DummyDevice) | ||
|
||
device = DummyDevice() | ||
|
||
# The last registered library identifier is returned | ||
assert self.manager.detect_lib(device) == "another_dummy" |
Oops, something went wrong.