qxmt.kernels.base module

qxmt.kernels.base module#

class qxmt.kernels.base.BaseKernel(device, feature_map)

Bases: ABC

Base kernel class for quantum kernel computation. This class is used to compute the kernel value between two samples. If defining a custom kernel, inherit this class and implement the compute() method. The feature map used to compute the kernel value is defined in the constructor. It is possible to use a feature map instance or a function that defines the feature map circuit.

Examples

>>> import numpy as np
>>> from typing import Callable
>>> from qxmt.kernels.base import BaseKernel
>>> from qxmt.feature_maps.pennylane.defaults import ZZFeatureMap
>>> from qxmt.configs import DeviceConfig
>>> from qxmt.devices.base import BaseDevice
>>> from qxmt.devices.builder import DeviceBuilder
>>> config = DeviceConfig(
...     platform="pennylane",
...     name="default.qubit",
...     n_qubits=2,
...     shots=1000,
>>> )
>>> device = DeviceBuilder(config).build()
>>> feature_map = ZZFeatureMap(2, 2)
>>> class CustomKernel(BaseKernel):
...     def __init__(self, device: BaseDevice, feature_map: Callable[[np.ndarray], None]) -> None:
...         super().__init__(device, feature_map)
...
...     def compute(self, x1: np.ndarray, x2: np.ndarray) -> float:
...         return np.dot(x1, x2)
>>> kernel = CustomKernel(device, feature_map)
>>> x1 = np.random.rand(2)
>>> x2 = np.random.rand(2)
>>> kernel.compute(x1, x2)
0.28
Parameters:
  • device (BaseDevice)

  • feature_map (BaseFeatureMap | Callable[[ndarray], None])

__init__(device, feature_map)

Initialize the kernel class.

Parameters:
  • device (BaseDevice) – device instance for quantum computation

  • feature_map (BaseFeatureMap | Callable[[np.ndarray], None]) – feature map instance or function

Return type:

None

abstract compute(x1, x2)

Compute kernel value between two samples.

Parameters:
  • x1 (np.ndarray) – array of one sample

  • x2 (np.ndarray) – array of one sample

Returns:

kernel value and probability distribution

Return type:

tuple[float, np.ndarray]

compute_matrix(x_array_1, x_array_2, return_shots_resutls=False, n_jobs=3)

Default implementation of kernel matrix computation.

Parameters:
  • x_array_1 (np.ndarray) – array of samples (ex: training data)

  • x_array_2 (np.ndarray) – array of samples (ex: test data)

  • return_shots_resutls (bool, optional) – return the shot results. Defaults to False.

  • n_jobs (int, optional) – number of jobs for parallel computation. Defaults to DEFAULT_N_JOBS.

Returns:

computed kernel matrix

Return type:

np.ndarray

plot_matrix(x_array_1, x_array_2, save_path=None, n_jobs=3)

Plot kernel matrix for given samples. Caluculation of kernel values is performed in parallel.

Parameters:
  • x_array_1 (np.ndarray) – array of samples (ex: training data)

  • x_array_2 (np.ndarray) – array of samples (ex: test data)

  • save_path (Optional[str | Path], optional) – save path for the plot. Defaults to None.

  • n_jobs (int, optional) – number of jobs for parallel computation. Defaults to DEFAULT_N_JOBS.

Return type:

None

plot_train_test_matrix(x_train, x_test, save_path=None, n_jobs=3)

Plot kernel matrix for training and testing data. Caluculation of kernel values is performed in parallel.

Parameters:
  • x_train (np.ndarray) – array of training samples

  • x_test (np.ndarray) – array of testing samples

  • save_path (Optional[str | Path], optional) – save path for the plot. Defaults to None.

  • n_jobs (int, optional) – number of jobs for parallel computation. Defaults to DEFAULT_N_JOBS.

Return type:

None

save_shots_results(probs_matrix, save_path)

Save the shot results to a file. probs_matrix contains the probability distribution of the observable states for each sample. expected shape of probs_matrix: (n_samples_1, n_samples_2, num_state)

Parameters:
  • probs_matrix (np.ndarray) – probability distribution of the observable states

  • save_path (str | Path) – save path for the shot results

Raises:
  • DeviceSettingError – not sampling mode

  • ValueError – save path extension not “.h5”

  • ValueError – state labels and probs length mismatch

Return type:

None