squlearn.kernel.lowlevel_kernel.KernelOptimizer

class squlearn.kernel.lowlevel_kernel.KernelOptimizer(quantum_kernel: KernelMatrixBase, loss: KernelLossBase = None, optimizer: OptimizerBase = None, initial_parameters: Sequence[float] | None = None)

Quantum kernel optimizer. This class can be used to optimize the variational parameters of a quantum kernel.

Parameters:
  • loss (KernelLossBase) – The loss function to be minimized.

  • optimizer (OptimizerBase) – The optimizer to be used.

  • quantum_kernel (KernelMatrixBase) – The quantum kernel to be optimized.

  • initial_parameters (Optional[Sequence[float]]) – Initial parameters for the optimizer.

assign_parameters(parameters: ndarray)

Set the training parameters of the encoding circuit to numerical values

Parameters:

parameters (np.ndarray) – Array containing numerical values to be assigned to the trainable parameters of the encoding circuit

evaluate(x: ndarray, y: ndarray = None) ndarray

Evaluate the kernel matrix using the current parameters.

Parameters:
  • x (np.ndarray) – Vector of training or test data.

  • y (np.ndarray, optional) – Vector of training or test data.

Returns:

The evaluated kernel matrix.

Return type:

np.ndarray

evaluate_derivatives(x: ndarray, y: ndarray = None, values: str = 'K') ndarray

Computes the derivatives of the quantum kernel matrix.

Parameters:
  • x (np.ndarray) – Vector of training or test data for which the kernel matrix is evaluated

  • y (np.ndarray, default=None) – Vector of training or test data for which the kernel matrix is evaluated

  • values (str) – String that specifies which derivatives of the kernel matrix should be computed. Possible values are K, dKdx, dKdy, dKdxdx, dKdp. FQKs also support dKdxdy and dKdydx and jacobian

Returns:

Returns the derivatives of the quantum kernel matrix as 2D numpy array.

evaluate_pairwise(x: ndarray, y: ndarray = None) float

Computes the quantum kernel matrix.

Parameters:
  • x (np.ndarray) – Vector of training or test data for which the kernel matrix is evaluated

  • y (np.ndarray, default=None) – Vector of training or test data for which the kernel matrix is evaluated

evaluate_with_parameters(x: ndarray, y: ndarray, parameters: ndarray) ndarray

Computes the quantum kernel matrix with assigned parameters

Parameters:
  • x (np.ndarray) – Vector of training or test data for which the kernel matrix is evaluated

  • y (np.ndarray) – Vector of training or test data for which the kernel matrix is evaluated

  • parameters (np.ndarray) – Array containing numerical values to be assigned to the trainable parameters of the encoding circuit

get_optimal_parameters() ndarray

Get the optimal parameters.

Returns:

The optimal parameters.

Return type:

np.ndarray

get_params(deep=True) dict

Returns hyper-parameters and their values of the fidelity kernel.

Parameters:

deep (bool) – If True, also the parameters for contained objects are returned (default=True).

Returns:

Dictionary with hyper-parameters and values.

run_optimization(X: ndarray, y: ndarray = None)

Run the optimization and return the result.

Parameters:
  • X (np.ndarray) – The input data.

  • y (np.ndarray) – The labels.

Returns:

The optimization result.

Return type:

OptimizeResult

set_params(**params)

Sets value of the kernel optimizer hyper-parameters.

Parameters:

params – Hyper-parameters and their values, e.g. num_qubits=2