Skip to content

Commit

Permalink
Merge pull request #41 from qBraid/kh_qrc-refactor
Browse files Browse the repository at this point in the history
refactor + add base class examples as starting point
  • Loading branch information
Kenny-Heitritter authored Jan 7, 2025
2 parents 1029a4c + d226c0b commit 81d9c91
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 6 deletions.
8 changes: 4 additions & 4 deletions qbraid_algorithms/qrc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
"""

from .encoding import PCA, one_hot_encoding
from .magnus_expansion import MagnusExpansion
from .qrc_model import DetuningLayer, QRCModel
from .time_evolution import AnalogProgramEvolver
from .encoding.encoding import PCA, one_hot_encoding
from .dynamics.magnus_expansion import MagnusExpansion
from .model.qrc_model import DetuningLayer, QRCModel
from .dynamics.time_evolution import AnalogProgramEvolver

__all__ = [
"QRCModel",
Expand Down
53 changes: 53 additions & 0 deletions qbraid_algorithms/qrc/dynamics/base.py
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.
56 changes: 56 additions & 0 deletions qbraid_algorithms/qrc/encoding/base.py
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.
70 changes: 70 additions & 0 deletions qbraid_algorithms/qrc/model/base.py
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)
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from bloqade.emulate.ir.state_vector import StateVector
from bloqade.ir.location import Chain

from .encoding import PCA
from .time_evolution import AnalogProgramEvolver
from ..encoding.encoding import PCA
from ..dynamics.time_evolution import AnalogProgramEvolver


@dataclass
Expand Down

0 comments on commit 81d9c91

Please sign in to comment.