This repository contains a small experimental implementation of a local quantum kernel for quantum machine learning workflows. It provides a localized alternative to standard global quantum kernels, designed to preserve expressiveness when dealing with high-dimensional datasets.
KernelGenerator.py: implements the patched local kernel computation.QuantumLocalKernelExperiments.ipynb: notebook with experiments, plots, and a comparison between local and global kernels.
The main idea behind this implementation is to mitigate the vanishing similarity phenomenon, a common issue in quantum machine learning where global kernels exponentially concentrate and lose their data-dependent expressiveness as feature dimensionality grows. Instead of evaluating a global feature map on the full high-dimensional input vector at once, the kernel is computed locally on reduced-dimensional subspaces using overlapping sliding windows, and then averaged. This approach leverages the methodology introduced in our paper, "Mitigating vanishing similarity in quantum kernels for DDoS attack detection".
The main function in this repository is:
kernelsubcomputation(x, y=None, feature_map=None, cyclic=True)
- If
yis omitted, the kernel is computed againstxitself. - If
feature_mapis omitted, the function uses a defaultZZfeature map with 3 qubits. - The function evaluates the kernel on sliding windows of the same size as the number of qubits in the feature map.
- If
cyclic=True, it also includes wrap-around windows at the end of the vector. - The final kernel matrix is the average of all evaluated local subkernels.
The notebook demonstrates the generator with a small, balanced subset of the breast_cancer dataset from scikit-learn. It includes:
- a handcrafted feature map,
- a built-in
Zfeature map, - a built-in
Paulifeature map, - a comparison between a global kernel and the patched local kernel.
The project uses Python 3.12+. All core package dependencies (such as qiskit, scikit-learn, numpy, etc.) are listed directly inside the requirements.txt file.
pip install -r requirements.txt You can import the kernel generator from Python:
import numpy as np
from qiskit.circuit.library import zz_feature_map
from KernelGenerator import kernelsubcomputation
# Generate dummy data and a feature map
x = np.random.rand(5, 6)
y = np.random.rand(5, 6)
feature_map = zz_feature_map(feature_dimension=3, reps=1, entanglement="linear")
# Compute the local patched kernel matrix
kernel_matrix = kernelsubcomputation(x, y, feature_map=feature_map, cyclic=True)You can also open QuantumLocalKernelExperiments.ipynb and run the cells to see the complete usage, benchmarks, and data visualization.
- The implementation is intended for experimentation and explanation, not for production workloads.
- Quantum kernel simulations can be slow as the number of samples or qubits increases, so the notebook keeps the dataset slices deliberately small.
This work has been partially supported by the research and development project "Quantum Shield: Seguridad postcuántica y aprendizaje automático cuántico para la ciberseguridad de en- tornos de fabricación inteligente en la Industria 4.0; Post-quantum Security and Quantum Machine Learning for Cybersecurity in Smart Manufacturing Environments in Industry 4.0" (Reference: CCTT5/23/SA/0002), funded by the Instituto de Competitividad Empresarial of the Junta de Castilla y León (ICECyL) and the European Regional Development Fund (ERDF).