-
QuantumEvolutionKernel — Implementation of the Quantum Evolution Kernel.
qek.kernel.kernel
module
qek.kernel.kernel
The Quantum Evolution Kernel itself, for use in a machine-learning pipeline.
Classes
Functions
-
count_occupation_from_bitstring — Counts the number of '1' bits in a binary string.
-
dist_excitation_and_vec — Calculates the distribution of excitation energies from a dictionary of bitstrings to their respective counts.
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.
method
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.
method
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.
method
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
method
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.
method
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.
method
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.
method
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
method
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.
count_occupation_from_bitstring (bitstring: str) → int
Counts the number of '1' bits in a binary string.
Parameters
-
bitstring : str — A binary string containing only '0's and '1's.
Returns
-
int — The number of '1' bits found in the input string.
dist_excitation_and_vec (count_bitstring: dict[str, int], size_max: int | None = None) → np.ndarray
Calculates the distribution of excitation energies from a dictionary of bitstrings to their respective counts.
Parameters
-
count_bitstring : dict[str, int] — A dictionary mapping binary strings to their counts.
-
size_max : int | None — If specified, only keep
size_maxenergy distributions in the output. Otherwise, keep all values.
Returns
-
np.ndarray — A NumPy array where keys are the number of '1' bits in each binary string and values are the normalized counts.
Raises
-
ValueError
