squlearn.kernel.loss.ODELoss
- class squlearn.kernel.loss.ODELoss(ode_functional: Expr | Callable, initial_values: Sequence | ndarray, symbols_involved_in_ode: Sequence[Basic] | None = None, eta: float = 1.0, ode_order: int | None = None)
ODE loss function for Quantum Kernels.
This class implements the ODE loss function for Quantum Kernels. The ODE loss function is defined as the sum of the squared residuals of the ODE functional and the initial conditions.
Implements an ODE Loss based on the mixed model regression algorithm of Ref. [1].
It uses the pinned method for handling the boundary conditions: An extra term is added to the loss function to enforce the initial values of the ODE. This term is pinned by the
etaparameter. The loss function is given by:\[L = \sum_{i=0}^{n} L_{\alpha_i}\left( \dot{f}, f, x \right) + \eta \cdot (f(x_0) - f_0)^2, \text{with} f(x) = \sum \alpha_i k(x_i, x)\]- Parameters:
ode_functional (
sympy.core.expr.Expror callable) – Functional representation of the ODE. If asympy.core.expr.Expris passed, then symbols_involved_in_ode is required. If a callable is passed, symbols_involved_in_ode is optional (can be used for sanity checks) and ode_order must be provided if symbols are not given.initial_values (sequence or np.ndarray) – Initial values for the ODE (length must equal ODE order).
symbols_involved_in_ode (sequence of
sympy.core.symbol.Symbol, optional) – Symbols in the order [x, f, f_d1, f_d2, …]. Required forsympy.core.expr.Expr.eta (float, default=1.0) – Weight for the pinned initial-value penalty.
ode_order (int, optional) – Order of the ODE. If provided it is used to set/validate order_of_ode. When passing a
sympy.core.expr.Exprthis must match len(symbols_involved_in_ode)-2.
Example
1. Implements a loss function for the ODE \(\cos(t) y + \frac{dy(t)}{dt} = 0\) with initial value \(y(0) = 0.1\).
t, y, dydt, = sp.symbols("t y dydt") eq = sp.cos(t)*y + dydt initial_values = [0.1] loss_ode = ODELoss( ode_functional=eq, initial_values=initial_values, symbols_involved_in_ode=[t, y, dydt], )
2. Implements a loss function for the ODE \(\left(df(x)/dx\right) - cos(f(x)) = 0\) with initial values \(f(0) = 0.\).
x, f, dfdx = sp.symbols("x f dfdx") eq = dfdx - sp.cos(f) initial_values = [0] loss_ode = ODELoss( ode_functional=eq, initial_values=initial_values, symbols_involved_in_ode=[x, f, dfdx], eta=1.2, )
See also
squlearn.kernel.QKODEQuantum Kernel for ODE solving.
References
[1]: A. Paine et al., “Quantum kernel methods for solving regression problems and differential equations”, Phys. Rev. A 107, 032428
Methods:
- compute(parameter_values: ndarray, data: ndarray, **kwargs) float
Calculates the squared loss of the loss function for the ODE as
\[\begin{align} \mathcal{L}_{\vec{\alpha}} [ \ddot f, \dot f, f, x] &= \sum_j^N \left(F\left( \ddot f_{\vec{\alpha}}, \dot f_{\vec{\alpha}}, f_{\vec{\alpha}}, x\right)_j\right)^2 + \eta\left(f_{\vec{\alpha}}(0) - u_0\right)^2 + \eta\left(\dot f_{\vec{\alpha}}(0) - \dot u_0\right)^2 \end{align}\]with the ansatz \(f_{\vec{\alpha}} = \alpha_0 + \sum_{i=1}^{n} \alpha_i k(x_i, x)\). Importantly, the optimized parameters act as the coefficients of the kernel matrix and do not directly correspond parameterized rotations in the quantum circuit.
- Parameters:
parameter_values (np.ndarray) – The parameters \(\vec{\alpha}\) of the ansatz to be optimized.
data (np.ndarray) – The training data to be used for the kernel matrix.
kwargs –
Additional arguments for specific loss functions.
- kernel_tensor (np.ndarray):
A tensor containing the kernel matrix and its derivatives. The shapes of each element in the array are (n_samples, n_samples).
- Returns:
The loss function value.
- Return type:
float
- set_quantum_kernel(quantum_kernel: KernelMatrixBase) None
Set the quantum kernel matrix to be used in the loss.
- Parameters:
quantum_kernel (KernelMatrixBase) – The quantum kernel matrix to be used in the loss.