-
Notifications
You must be signed in to change notification settings - Fork 4
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 #41 from qBraid/kh_qrc-refactor
refactor + add base class examples as starting point
- Loading branch information
Showing
8 changed files
with
185 additions
and
6 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
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,53 @@ | ||
"""Base class for quantum dynamics implementations.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Optional, Union | ||
|
||
import numpy as np | ||
|
||
|
||
class DynamicsBase(ABC): | ||
"""Abstract base class for quantum dynamics implementations. | ||
This class defines the interface that all quantum dynamics classes must implement. | ||
It provides a standard way to evolve quantum states according to different | ||
evolution schemes (Magnus expansion, analog evolution, etc.). | ||
""" | ||
|
||
def __init__(self, num_sites: int, **kwargs): | ||
"""Initialize dynamics parameters. | ||
Args: | ||
num_sites: Number of sites in the quantum system | ||
**kwargs: Additional parameters specific to the dynamics implementation | ||
""" | ||
self.num_sites = num_sites | ||
self._validate_parameters(**kwargs) | ||
|
||
@abstractmethod | ||
def evolve( | ||
self, | ||
initial_state: np.ndarray, | ||
time: Union[float, np.ndarray], | ||
backend: Optional[str] = None | ||
) -> np.ndarray: | ||
"""Evolve the quantum state according to the dynamics. | ||
Args: | ||
initial_state: Initial quantum state | ||
time: Evolution time(s) | ||
backend: Optional backend specification for computation | ||
Returns: | ||
Evolved quantum state | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def _validate_parameters(self, **kwargs) -> None: | ||
"""Validate the parameters passed to the dynamics implementation.""" | ||
pass | ||
|
||
def reset(self) -> None: | ||
"""Reset any internal state of the dynamics.""" | ||
pass |
File renamed without changes.
File renamed without changes.
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,56 @@ | ||
"""Base class for encoding classical data into quantum states.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Optional | ||
|
||
import numpy as np | ||
|
||
|
||
class EncoderBase(ABC): | ||
"""Abstract base class for encoding classical data into quantum states. | ||
This class defines the interface for transforming classical data into a format | ||
suitable for quantum evolution, with methods for both encoding and decoding. | ||
""" | ||
|
||
def __init__(self, input_dim: int, output_dim: int, **kwargs): | ||
"""Initialize encoder parameters. | ||
Args: | ||
input_dim: Dimension of input classical data | ||
output_dim: Dimension of encoded quantum data | ||
**kwargs: Additional parameters specific to the encoding method | ||
""" | ||
self.input_dim = input_dim | ||
self.output_dim = output_dim | ||
self._validate_dimensions() | ||
|
||
@abstractmethod | ||
def encode(self, data: np.ndarray, normalize: bool = True) -> np.ndarray: | ||
"""Transform classical data into quantum encoding. | ||
Args: | ||
data: Classical data to encode | ||
normalize: Whether to normalize the encoded data | ||
Returns: | ||
Encoded quantum data | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def decode(self, encoded_data: np.ndarray) -> np.ndarray: | ||
"""Transform encoded quantum data back to classical form. | ||
Args: | ||
encoded_data: Encoded quantum data | ||
Returns: | ||
Decoded classical data | ||
""" | ||
pass | ||
|
||
def _validate_dimensions(self) -> None: | ||
"""Validate input and output dimensions.""" | ||
if self.input_dim <= 0 or self.output_dim <= 0: | ||
raise ValueError("Dimensions must be positive integers") |
File renamed without changes.
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,70 @@ | ||
"""Base class for quantum reservoir computing models.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Optional, Tuple | ||
|
||
import numpy as np | ||
|
||
from ..dynamics.base import DynamicsBase | ||
from ..encoding.base import EncoderBase | ||
|
||
|
||
class QRCModelBase(ABC): | ||
"""Abstract base class for quantum reservoir computing models. | ||
This class combines encoding and dynamics components to create a complete | ||
quantum reservoir computing model. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
encoder: EncoderBase, | ||
dynamics: DynamicsBase, | ||
random_state: Optional[int] = None | ||
): | ||
"""Initialize the QRC model. | ||
Args: | ||
encoder: Encoder for transforming classical data | ||
dynamics: Quantum dynamics implementation | ||
random_state: Random seed for reproducibility | ||
""" | ||
self.encoder = encoder | ||
self.dynamics = dynamics | ||
self._set_random_state(random_state) | ||
|
||
@abstractmethod | ||
def fit( | ||
self, | ||
X: np.ndarray, | ||
y: np.ndarray, | ||
**kwargs | ||
) -> "QRCModelBase": | ||
"""Train the model on given data. | ||
Args: | ||
X: Training input data | ||
y: Training target values | ||
**kwargs: Additional training parameters | ||
Returns: | ||
self: The trained model | ||
""" | ||
pass | ||
|
||
@abstractmethod | ||
def predict(self, X: np.ndarray) -> np.ndarray: | ||
"""Make predictions using the trained model. | ||
Args: | ||
X: Input data for prediction | ||
Returns: | ||
Model predictions | ||
""" | ||
pass | ||
|
||
def _set_random_state(self, random_state: Optional[int]) -> None: | ||
"""Set random state for reproducibility.""" | ||
if random_state is not None: | ||
np.random.seed(random_state) |
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