|
| 1 | +from abc import abstractmethod |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from .base import Base, Property |
| 6 | +from .types.array import StateVectors |
| 7 | +from .types.state import State |
| 8 | + |
| 9 | + |
| 10 | +class Kernel(Base): |
| 11 | + """Kernel base type |
| 12 | +
|
| 13 | + A Kernel provides a means to translate state space or measurement space into kernel space. |
| 14 | + """ |
| 15 | + |
| 16 | + @abstractmethod |
| 17 | + def __call__(self, state1, state2=None): |
| 18 | + r""" |
| 19 | + Compute the kernel state of a pair of :class:`~.State` objects |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + state1 : :class:`~.State` |
| 24 | + state2 : :class:`~.State` |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + StateVectors |
| 29 | + kernel state of a pair of input :class:`~.State` objects |
| 30 | +
|
| 31 | + """ |
| 32 | + raise NotImplementedError |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def _get_state_vectors(state1, state2): |
| 36 | + if isinstance(state1, State): |
| 37 | + state_vector1 = state1.state_vector |
| 38 | + else: |
| 39 | + state_vector1 = state1 |
| 40 | + if state2 is None: |
| 41 | + state_vector2 = state_vector1 |
| 42 | + else: |
| 43 | + if isinstance(state2, State): |
| 44 | + state_vector2 = state2.state_vector |
| 45 | + else: |
| 46 | + state_vector2 = state2 |
| 47 | + return state_vector1, state_vector2 |
| 48 | + |
| 49 | + |
| 50 | +class QuadraticKernel(Kernel): |
| 51 | + r"""Quadratic Kernel type |
| 52 | +
|
| 53 | + This kernel returns the quadratic kernel state vector from a pair of |
| 54 | + :class:`~.KernelParticleState` state vectors. |
| 55 | +
|
| 56 | + The Quadratic kernel of state vectors :math:`\mathbf{x}` and |
| 57 | + :math:`\mathbf{x}'` is defined as: |
| 58 | +
|
| 59 | + .. math:: |
| 60 | + \mathtt{k}\left(\mathbf{x}, \mathbf{x}'\right) = |
| 61 | + \left(\alpha \langle \mathbf{x}, \mathbf{x}' \rangle + c\right)^2 |
| 62 | + """ |
| 63 | + c: float = Property( |
| 64 | + default=1, |
| 65 | + doc="Free parameter trading off the influence of higher-order versus lower-order " |
| 66 | + "terms in the polynomial. Default is 1.") |
| 67 | + ialpha: float = Property(default=1e1, doc="Slope. Range is [1e0, 1e4].") |
| 68 | + |
| 69 | + def __call__(self, state1, state2=None): |
| 70 | + r"""Calculate the Quadratic Kernel transformation for a pair of state vectors |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + state1 : :class:`~.KernelParticleState` |
| 75 | + state2 : :class:`~.KernelParticleState` |
| 76 | +
|
| 77 | + Returns |
| 78 | + ------- |
| 79 | + StateVectors |
| 80 | + Transformed state vector in kernel space. |
| 81 | + """ |
| 82 | + state_vector1, state_vector2 = self._get_state_vectors(state1, state2) |
| 83 | + return (state_vector1.T@state_vector2/self.ialpha + self.c) ** 2 |
| 84 | + |
| 85 | + |
| 86 | +class QuarticKernel(Kernel): |
| 87 | + r"""Quartic Kernel |
| 88 | +
|
| 89 | + This kernel returns the quartic kernel state from a pair of |
| 90 | + :class:`~.KernelParticleState` objects. |
| 91 | +
|
| 92 | + The Quartic kernel of state vectors :math:`\mathbf{x}` and |
| 93 | + :math:`\mathbf{x}'` is defined as: |
| 94 | +
|
| 95 | + .. math:: |
| 96 | + \mathtt{k}(\mathbf{x}, \mathbf{x}') = |
| 97 | + \left(\alpha \langle \mathbf{x}, \mathbf{x}' \rangle + c\right)^4 |
| 98 | + """ |
| 99 | + c: float = Property( |
| 100 | + default=1, |
| 101 | + doc="Free parameter trading off the influence of higher-order versus lower-order " |
| 102 | + "terms in the polynomial. Default is 1.") |
| 103 | + ialpha: float = Property(default=1e1, doc="Slope. Range is [1e0, 1e4].") |
| 104 | + |
| 105 | + def __call__(self, state1, state2=None): |
| 106 | + r"""Calculate the Quartic Kernel transformation for a pair of state vectors |
| 107 | +
|
| 108 | + Parameters |
| 109 | + ---------- |
| 110 | + state1 : :class:`~.KernelParticleState` |
| 111 | + state2 : :class:`~.KernelParticleState` |
| 112 | +
|
| 113 | + Returns |
| 114 | + ------- |
| 115 | + StateVectors |
| 116 | + Transformed state in kernel space. |
| 117 | + """ |
| 118 | + state_vector1, state_vector2 = self._get_state_vectors(state1, state2) |
| 119 | + return (state_vector1.T@state_vector2/self.ialpha + self.c) ** 4 |
| 120 | + |
| 121 | + |
| 122 | +class GaussianKernel(Kernel): |
| 123 | + r"""Gaussian Kernel |
| 124 | +
|
| 125 | + This kernel returns the Gaussian kernel state vector from a pair of |
| 126 | + :class:`~.KernelParticleState` state vectors. |
| 127 | +
|
| 128 | + The Gaussian kernel of state vectors :math:`\mathbf{x}` and |
| 129 | + :math:`\mathbf{x}'` is defined as: |
| 130 | +
|
| 131 | + .. math:: |
| 132 | + \mathtt{k}(\mathbf{x}, \mathbf{x}') = |
| 133 | + \mathrm{exp}\left(-\frac{||\mathbf{x} - \mathbf{x}'||^{2}}{2\pi\sigma^2}\right) |
| 134 | + """ |
| 135 | + variance: float = Property( |
| 136 | + default=1e1, |
| 137 | + doc=r"Denoted as :math:`\sigma^2` in the equation above. Determines the width of the " |
| 138 | + r"Gaussian kernel. Range is [1e0, 1e2].") |
| 139 | + |
| 140 | + def __call__(self, state1, state2=None): |
| 141 | + r"""Calculate the Gaussian Kernel transformation for a pair of state vectors |
| 142 | +
|
| 143 | + Parameters |
| 144 | + ---------- |
| 145 | + state1 : :class:`~.KernelParticleState` |
| 146 | + state2 : :class:`~.KernelParticleState` |
| 147 | +
|
| 148 | + Returns |
| 149 | + ------- |
| 150 | + StateVectors |
| 151 | + Transformed state vector in kernel space. |
| 152 | + """ |
| 153 | + state_vector1, state_vector2 = self._get_state_vectors(state1, state2) |
| 154 | + diff_tilde_x = (state_vector1.T[:, :, None] - state_vector2.T[:, None, :]) ** 2 |
| 155 | + diff_tilde_x_sum = np.sum(diff_tilde_x, axis=0) |
| 156 | + |
| 157 | + k_tilde_x = np.exp(-diff_tilde_x_sum/(2*self.variance)) / np.sqrt(2*np.pi*self.variance) |
| 158 | + |
| 159 | + return StateVectors(k_tilde_x) |
0 commit comments