Source code for mercurial.core.state_space

"""Hilbert space representation of the neutral substrate."""

from dataclasses import dataclass
from typing import Optional

import numpy as np


[docs] @dataclass class HilbertSpace: """Represents the substrate state space X.""" dimension: int basis: Optional[np.ndarray] = None def __post_init__(self): if self.basis is None: self.basis = np.eye(self.dimension, dtype=complex)
[docs] def inner_product(self, x: np.ndarray, y: np.ndarray) -> float: """Compute inner product ⟨x|y⟩.""" return float(np.real(np.dot(x.conj(), y)))
[docs] def norm(self, x: np.ndarray) -> float: """Compute norm ||x||.""" return np.sqrt(self.inner_product(x, x))
[docs] def distance(self, x: np.ndarray, y: np.ndarray) -> float: """Compute distance d_X(x, y).""" return self.norm(x - y)
[docs] class StateVector: """State vector x(t) in Hilbert space.""" def __init__(self, space: HilbertSpace, components: np.ndarray, t: float = 0.0): self.space = space self.components = components.astype(complex) self.time = t self._validate() def _validate(self): if self.components.shape[0] != self.space.dimension: raise ValueError( f"Component dimension {self.components.shape[0]} != " f"space dimension {self.space.dimension}" )
[docs] def norm(self) -> float: return self.space.norm(self.components)
[docs] def normalize(self) -> "StateVector": return StateVector(self.space, self.components / self.norm(), self.time)
[docs] def copy(self) -> "StateVector": return StateVector(self.space, self.components.copy(), self.time)