qek.kernel
source package qek.kernel
The Quantum Evolution Kernel itself, for use in a machine-learning pipeline.
Classes
-
QuantumEvolutionKernel — Implementation of the Quantum Evolution Kernel.
source class QuantumEvolutionKernel(mu: float, size_max: int | None = None)
Implementation of the Quantum Evolution Kernel.
Initialize the QuantumEvolutionKernel.
Attributes
-
- params (dict) — Dictionary of training parameters. As of this writing, the only training parameter is "mu", the scaling factor for the Jensen-Shannon divergence.
-
- X (Sequence[ProcessedData]) — Training data used for fitting the kernel.
-
- kernel_matrix (np.ndarray) — Kernel matrix. This is assigned in the
fit()method
Parameters
-
mu : float — Scaling factor for the Jensen-Shannon divergence
-
size_max : int, optional — If specified, only consider the first
size_maxqubits of bitstrings. Otherwise, consider all qubits. You may use this to trade precision in favor of speed.
Methods
-
similarity — Compute the similarity between two graphs using Jensen-Shannon divergence.
-
fit — Fit the kernel to the training dataset by storing the dataset.
-
transform — Transform the dataset into the kernel space with respect to the training dataset.
-
fit_transform — Fit the kernel to the training dataset and transform it.
-
create_train_kernel_matrix — Compute a kernel matrix for a given training dataset.
-
create_test_kernel_matrix — Compute a kernel matrix for a given testing dataset and training set.
-
set_params — Set multiple parameters for the kernel.
-
get_params — Retrieve the value of all parameters.
source method QuantumEvolutionKernel.similarity(graph_1: ProcessedData, graph_2: ProcessedData) → float
Compute the similarity between two graphs using Jensen-Shannon divergence.
This method computes the square of the Jensen-Shannon divergence (JSD) between two probability distributions over bitstrings. The JSD is a measure of the difference between two probability distributions, and it can be used as a kernel for machine learning algorithms that require a similarity function.
The input graphs are assumed to have been processed using the
ProcessedData class from qek_os.data_io.dataset. Parameter size_max
controls the maximum length of the bitstrings considered in the
computation.
Args:
graph_1 (ProcessedData): First graph.
graph_2 (ProcessedData): Second graph.
size_max (int, optional): Maximum length of bitstrings to
consider. Defaults to all.
Returns
-
float — Similarity between the two graphs, scaled by a factor that depends on mu.
Notes
The JSD is computed using the jensenshannon function from
scipy.spatial.distance, and it is squared because scipy function
jensenshannon outputs the distance instead of the divergence.
source method QuantumEvolutionKernel.fit(X: Sequence[ProcessedData], y: list | None = None) → None
Fit the kernel to the training dataset by storing the dataset.
Parameters
-
X : Sequence[ProcessedData] — The training dataset.
-
y : list | None — list: Target variable for the dataset sequence. This argument is ignored, provided only for compatibility with machine-learning libraries.
source method QuantumEvolutionKernel.transform(X_test: Sequence[ProcessedData], y_test: list | None = None) → np.ndarray
Transform the dataset into the kernel space with respect to the training dataset.
Parameters
-
X_test : Sequence[ProcessedData] — The dataset to transform. y_test: list: Target variable for the dataset sequence. This argument is ignored, provided only for compatibility with machine-learning libraries.
-
Returns —
np.ndarray: Kernel matrix where each entry represents the similarity between the given dataset and the training dataset.
Raises
-
ValueError
source method QuantumEvolutionKernel.fit_transform(X: Sequence[ProcessedData], y: list | None = None) → np.ndarray
Fit the kernel to the training dataset and transform it.
Parameters
-
X : Sequence[ProcessedData] — The dataset to fit and transform. y: list: Target variable for the dataset sequence. This argument is ignored, provided only for compatibility with machine-learning libraries.
-
Returns —
np.ndarray: Kernel matrix for the training dataset.
source method QuantumEvolutionKernel.create_train_kernel_matrix(train_dataset: Sequence[ProcessedData]) → np.ndarray
Compute a kernel matrix for a given training dataset.
This method computes a symmetric N x N kernel matrix from the Jensen-Shannon divergences between all pairs of graphs in the input dataset. The resulting matrix can be used as a similarity metric for machine learning algorithms. Args: train_dataset (Sequence[ProcessedData]): A list of ProcessedData objects to compute the kernel matrix from. Returns: np.ndarray: An N x N symmetric matrix where the entry at row i and column j represents the similarity between the graphs in positions i and j of the input dataset.
source method QuantumEvolutionKernel.create_test_kernel_matrix(test_dataset: Sequence[ProcessedData], train_dataset: Sequence[ProcessedData]) → np.ndarray
Compute a kernel matrix for a given testing dataset and training set.
This method computes an N x M kernel matrix from the Jensen-Shannon divergences between all pairs of graphs in the input testing dataset and the training dataset. The resulting matrix can be used as a similarity metric for machine learning algorithms, particularly when evaluating the performance on the test dataset using a trained model. Args: test_dataset (Sequence[ProcessedData]): A list of ProcessedData objects representing the testing dataset. train_dataset (Sequence[ProcessedData]): A list of ProcessedData objects representing the training set. Returns: np.ndarray: An M x N matrix where the entry at row i and column j represents the similarity between the graph in position i of the test dataset and the graph in position j of the training set.
source method QuantumEvolutionKernel.set_params(**kwargs: dict[str, Any]) → None
Set multiple parameters for the kernel.
Parameters
-
**kwargs : dict[str, Any] — Arbitrary keyword dictionary where keys are attribute names
-
and values are their respective values
source method QuantumEvolutionKernel.get_params(deep: bool = True) → dict
Retrieve the value of all parameters.
Parameters
-
deep : bool — Ignored for the time being. Added for compatibility with various machine learning libraries, such as scikit-learn.
Returns dict: A dictionary of parameters and their respective values. Note that this method always performs a copy of the dictionary.