Quantum K-nearest neighbor classification algorithm based on Hamming distance

Jing Li, Song Lin, Kai Yu, Gongde GuoarXiv 2021 · DOI

Abstract

K-nearest neighbor classification algorithm is one of the most basic algorithms in machine learning, which determines the sample's category by the similarity between samples. In this paper, we propose a quantum K-nearest neighbor classification algorithm with Hamming distance. In this algorithm, quantum computation is firstly utilized to obtain Hamming distance in parallel. Then, a core sub-algorithm for searching the minimum of unordered integer sequence is presented to find out the minimum distance. Based on these two sub-algorithms, the whole quantum frame of K-nearest neighbor classification algorithm is presented. At last, it is shown that the proposed algorithm can achieve a quadratical speedup by analyzing its time complexity briefly.

Introduction: The KNN Bottleneck

The classical K-nearest neighbor (KNN) algorithm is a staple of machine learning due to its simplicity: to classify a new data point, one identifies the $K$ most similar samples in a training set and performs a majority vote. However, its runtime scales linearly with the number of training samples ($M$). For massive datasets, this becomes a significant computational bottleneck.

The basic principle of the K-nearest neighbor (KNN) algorithm, where a new sample is classified based on its proximity to existing data points.

While previous quantum KNN approaches exist, they often rely on Euclidean distance or require hard-to-tune "threshold" parameters. This paper addresses these issues by focusing on Hamming distance—ideal for categorical or binary data—and providing a framework that directly finds the $K$ nearest neighbors without arbitrary thresholds.

Methodology

The proposed algorithm is structured around two primary quantum sub-algorithms that leverage quantum parallelism and amplitude amplification.

Sub-algorithm A: Parallel Hamming Distance Calculation

Using Quantum Random Access Memory (QRAM), the algorithm prepares a superposition of all training samples. It employs CNOT gates to compare the bits of the test sample with the training samples. To aggregate these differences into a distance value, the authors use a "controlled incrementing circuit." This allows the Hamming distances for all $M$ samples to be calculated simultaneously in a single quantum register.

Quantum circuit for the parallel calculation of Hamming distances between a test sample and training samples.

Standard quantum search algorithms like Grover’s are often adapted for minimization. However, the authors propose a more efficient method specifically for unordered integer sequences. By leveraging the integer nature of Hamming distances, they use a binary-search-inspired approach. They compare distances to a "key" value, use amplitude amplification to isolate elements smaller than that key, and iteratively narrow the range until the minimum is found.

The Integrated Framework

The algorithm repeats Sub-algorithm B $K$ times to identify the $K$ nearest neighbors. Instead of a classical voting step, a quantum measurement technique is used: a state representing the categories of the $K$ neighbors is prepared, and measuring this state naturally yields the most frequent category with the highest probability.

Key Insights

  • Integer-Specific Optimization: By restricting the domain to integers, the authors utilize a binary search logic more suited to current quantum architectural constraints than general numeric minimization algorithms.
  • Eliminating Thresholds: This version finds the actual $K$ nearest neighbors, making it a true quantum analog of the classical algorithm without requiring a pre-set distance threshold.
  • Quantum Voting: Using measurement probability to determine the majority class avoids the "input/output bottleneck" common in quantum-classical hybrid systems.

Results and Complexity Analysis

The theoretical analysis demonstrates a quadratic speedup. For a dataset with $M$ samples, the quantum algorithm runs in approximately $O(\sqrt{M} \log_2 M)$ time, compared to the classical $O(M)$. The total complexity is expressed as $O(K \log_2 N \sqrt{M}(\log_2 M + n^3))$, where $N$ is the vector dimension. The speedup is most significant in low-dimensional feature spaces where $N \ll M$.

Limitations

The algorithm's performance is tied to the existence of QRAM, which remains a developing technology. Additionally, while it scales well with the number of samples, a very large feature dimension ($N$) can impact the overall acceleration. Future work may address the optimal selection of $K$ within this quantum framework.

Summary updated

BibTeX
@misc{li2021quantum,
  author = {Jing Li and Song Lin and Kai Yu and Gongde Guo},
  title = {{Quantum K-nearest neighbor classification algorithm based on Hamming distance}},
  year = {2021},
  eprint = {2103.04253},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2103.04253},
}
Figure