Quantum KNN Classification With K Value Selection and Neighbor Selection

The K-nearest neighbors (KNNs) algorithm is one of Top-10 data mining algorithms and is widely used in various fields of artificial intelligence. This leads to that quantum KNN (QKNN) algorithms have developed and achieved certain speed improvements, denoted as Q- KNN. However, these Q- KNN methods must face two key problems as follows. The first one is that they are mainly focused on neighbor selection without paying attention to the influence of <inline-formula> <tex-math notation="LaTeX">${K}$ </tex-math></inline-formula> value on the algorithm. The second is that only the neighbor selection process is quantized, and the selection of <inline-formula> <tex-math notation="LaTeX">${K}$ </tex-math></inline-formula> value is not quantized. To solve these problems, this article designs a novel quantum circuit for KNN classification, so as to simultaneously quantumize the neighbor selection and <inline-formula> <tex-math notation="LaTeX">${K}$ </tex-math></inline-formula> value selection process. Specifically, the least squares loss and sparse regularization term are first used to construct the objective function of the proposed quantum KNN, so that it can simultaneously obtain the optimal <inline-formula> <tex-math notation="LaTeX">${K}$ </tex-math></inline-formula> value and <inline-formula> <tex-math notation="LaTeX">${K}$ </tex-math></inline-formula> nearest neighbors of the testing data. And then, a new quantum circuit is proposed to quantumize the process through quantum phase estimation, controlled rotation, and inverse phase estimation techniques. Finally, experiments are conducted with qiskit and MATLAB to output the quantum and classical results of the algorithm, verifying that the proposed algorithm can output the optimal <inline-formula> <tex-math notation="LaTeX">${K}$ </tex-math></inline-formula> value and <inline-formula> <tex-math notation="LaTeX">${K}$ </tex-math></inline-formula> nearest neighbors for each testing data.

K nearest neighbors in the training data.The main challenges for K-nearest neighbor (KNN) include: 1) K value selection and 2) neighbor selection, where neighbor selection includes distance measurement and neighbor search.
Since its introduction by Cover and Hart [1], many researchers have proposed improved models for KNN.In recent years, with the development of quantum technology, researchers have proposed several quantum KNN (QKNN) algorithms.For example, Wiebe et al. [2] used quantum technology to improve KNN by calculating inner products and Euclidean distances.Ruan et al. [3] designed a quantum circuit for computing the Hamming distance between testing data and training data for KNN.Feng et al. [4] designed a new distance metric function (i.e., Polar distance), and quantumized it for KNN classification.Although these QKNN algorithms have improved KNN in terms of distance measurement, they all ignore the search for near neighbors (i.e., finding the K smallest distances between the testing data and all training data).Subsequently, Basheer et al. [5] used the quantum K maxima algorithm to perform neighbor search for KNN, thus addressing the challenge of neighbor search.It can be seen that the above QKNN algorithms only optimize one challenge in distance measurement or neighbor search.
Besides the above-mentioned QKNN algorithms, some researchers have proposed new ones.For example, Dang et al. [6] used controlled swap gates and quantum minimum search algorithm to obtain neighbors.Zhang et al. [7] proposed a quantum weighted KNN algorithm.Tian and Baskiyar used Ruan and Baskiyar's [8] quantum Hamming distance calculation method and Grover search algorithm to construct QKNN.Gao et al. [9] used quantum Mahalanobis distance and quantum minimum search algorithm to construct QKNN.Li et al. [10] proposed a quantum K-fold crossvalidation method to improve KNN.These QKNN algorithms improve KNN by addressing both distance measurement and neighbor search challenges, which is a significant improvement.
In summary, although current QKNN algorithms have made a series of breakthroughs, they have not addressed the challenge of K value selection.Different K values will produce different prediction results.If a smaller K value is selected, it is equivalent to using a smaller neighborhood to predict the testing data.If noise appears in the neighborhood, it will lead to incorrect predictions.If a larger K value is selected, although it can avoid the influence of noise, it will cause a large number of dissimilar samples to enter the neighborhood, leading to incorrect predictions.Fig. 1 shows the position distribution of a testing data and training data.From Fig. 1, we can see that for the testing data, if different K values are chosen, the predicted labels will be different, possibly any one of class1, class2, and class3.This indicates that the choice of K value is crucial.
This article proposes a novel QKNN algorithm that can simultaneously handle the challenges of K value selection and neighbor selection (including distance measurement and neighbor search).It can obtain the optimal K value and corresponding K nearest neighbors for each testing data through matrix computation.Specifically, the initial state (|0 |Yb 1 |0 |Yb 2 , . . ., |0 |Yb m )(|0 ⊗n 0 )(|0 m ) of the entire quantum system is first prepared.And then, phase estimation and controlled rotation techniques are used to obtain the required quantum parameter values of the model (including phase of the eigenvalues and angle of the controlled rotation).Finally, combined with inverse phase estimation, a quantum circuit is constructed, which outputs the model W (recording the optimal K value and K nearest neighbors for each testing data).In addition, this article validates the efficiency of the proposed algorithm on datasets using qiskit and MATLAB.
The main contributions of this article are as follows.
1) This article is the first to utilize quantum technology to optimize both K value selection and neighbor selection in KNN simultaneously.2) This article designs a new quantum circuit to quantization KNN.This circuit leverages the advantages of quantum computing to rapidly output the quantum state of the optimal K value and the indices of K nearest neighbor samples for each testing data.This further reduces the time complexity of the KNN algorithm.A series of experiments were conducted to validate the efficiency of the proposed algorithm using IBM's qiskit and MATLAB.The experiments demonstrate that the proposed algorithm can output the quantum state of the optimal K value and the indices of K nearest neighbor samples for each testing data.In addition, compared with advanced comparative algorithms, the proposed algorithm also achieves better classification performance on public dataset.
The remainder of this article is organized as follows.Section II briefly reviews previous related work.Section III describes our proposed method in detail, quantum circuit, and time complexity analysis.Section IV shows the results of algorithm on dataset.Section V summarizes the full paper.

II. RELATED WORK
In this section, we introduce some work of classical KNN and QKNN.

A. Classic KNN
KNN, as one of the top ten data mining algorithms, is widely used in classification tasks.It is a well-known representative of "lazy learning" and does not require any training process.Given a testing data, KNN can predict its label by calculating its K nearest neighbors based on the measurement function.KNN has been further studied and explored by researchers due to its simple concept, ease of implementation, and practicality.These studies mainly focus on the improvement of K value selection and neighbor selection.
K Value Selection: In KNN, the selection of K value is crucial as different K values can lead to different prediction results.If a smaller value of K is chosen, the algorithm may struggle to generalize effectively, potentially leading to overfitting.Conversely, if a larger value of K is selected, the model might become excessively generalized, resulting in less accurate predictions of data class labels.Therefore, selecting an appropriate K value is the goal of researchers.Li et al. [11] set different K values for different categories of data.Nair and Kashyap [12] choose the best K value based on accuracy.Saputra et al. [13] determined the optimal K value based on the local structure of the data and accuracy.Laksono et al. [14] optimized the K value in KNN using frequency distribution clustering.Although these methods have improved the KNN algorithm, their K values are fixed (i.e., the optimal K value for different testing data or different categories of data is the same).Therefore, to further improve the performance of KNN, some researchers have learned different optimal K values for different testing data or different categories of data.For example, Li used a tree structure to obtain an adaptive K value [15].Zhang et al. [16] learnt the optimal K value of the testing data using sparse techniques.Gong and Liu dynamically obtained the optimal K value using similarity calculation [17].Ling et al. [18] proposed a new decision strategy based on probability and iterative K values to improve the KNN algorithm.Gallego et al. [19] proposed an adaptive K value strategy to improve KNN search performance.García-Pedrajas et al. [20] set the optimal K value based on the local and global impact of different K values.
In conclusion, the enhancement of KNN from the perspective of selecting the K value has undergone a process of transitioning from a fixed optimal K value to adapting different optimal K values for distinct data points.However, in the process of researchers using models or algorithms to learn the optimal K value, KNN no longer has the lazy learning characteristic, i.e., KNN has a training process (to obtain the optimal K value during training).Moreover, the process of obtaining the optimal K value is often time consuming.Therefore, in recent years, Zhang et al. [21] proposed the ktree and k*tree algorithms, whose core is to use the leaf nodes in the tree structure to save the optimal K value of the training data and the data subset of the Authorized licensed use limited to the terms of the applicable license agreement with IEEE.Restrictions apply.
potential K nearest neighbors of the testing data.In this way, the optimal K value and class label of each testing data can be obtained quickly (because it narrows down the search space of K nearest neighbors).In addition, Zhang and Li also proposed the one-step KNN algorithm [22], which combines K value selection and neighbor search into one-step matrix computation, thereby reducing the running cost of the algorithm.
Neighbor Selection: In KNN, when the K value is provided as input, it only specifies the number of neighbors without determining which training data points are neighbors of the test data.Therefore, it is usually necessary to use a distance metric function to calculate the K nearest neighbors that are similar to the testing data [23].Traditional distance metric functions include Euclidean distance, Manhattan distance, Chebyshev distance, Minkowski distance, cosine distance, Hamming distance, etc.They can all be used to calculate the distance between the testing data and the training data, thus obtaining the K nearest neighbors of the testing data.However, they all have some shortcomings.For example, Euclidean distance and Manhattan distance become ineffective as the data dimension increases.The scope of the Chebyshev distance is relatively small, and it only applies to specific scenarios.The Minkowski distance is a set of the first three distances, so its shortcomings include their set and the additional parameter p.The Cosine distance only considers the directional characteristics of the data and does not consider their magnitude.The Hamming distance cannot be applied to situations where the dimensions of two data sets are unequal.
Due to the limitations of traditional distance metrics in KNN, some researchers have proposed new distance functions to improve KNN.Rastin et al. [24] proposed a generalized prototype-weighted distance function to obtain neighbors, which makes the algorithm perform stably in class-imbalanced scenarios.Gou et al. [25] proposed a distance-weighted function to obtain neighbors.Liao et al. [26] used convolutional neural network to learn appropriate distance metrics and prototype reduction to find the nearest neighbors of testing data.Zhang et al. [27] proposed a mixed distance metric to improve the separability of samples.Syaliman et al. [28] proposed a new distance function using local means and distance weights to improve the majority rule.Liu improved the general distance metric using vertical shallow cuttings to improve KNN [29].Lubis et al. [30] compared and optimized different distance functions in different situations, making KNN more effective.Gou et al. [31] designed a generalized average distance based on the local mean vector of each category to obtain neighbors.
For the neighbor selection, current work mainly focus on learning and constructing distance metrics.They mainly address issues, such as class imbalance, local structure of data, and accuracy.However, there are still some challenges to be addressed, such as the "time" dimension of data, where neighbors often change over time.For a testing data, the K nearest neighbors at one moment may not be so at the next moment.To address this challenge, a series of distance metrics considering the time dimension need to be proposed.

B. Quantum KNN
In recent years, with the development of quantum computing technology, quantum machine learning can further optimize traditional machine learning by leveraging the high parallelism of quantum computing [32] [33].Many machine learning algorithms have been improved by combining with quantum computing technology, such as quantum principal components analysis, quantum support vector machine, QKNN, quantum feature selection, and quantum search algorithms [34].Compared with traditional machine learning algorithms, they have achieved exponential or quadratic speedup [35].It is worth mentioning the Grover search algorithm, which has achieved a qualitative leap compared to traditional search algorithms.This undoubtedly brings hope to many challenging problems that require searching in very large spaces and encourages researchers.
For the KNN algorithm, researchers have proposed some work on QKNN by combining quantum computing technology.For example, Wiebe et al. [2] proposed a quantum nearest-neighbor algorithm that uses a quantum technology to calculate the distance between testing data and training data.Compared with classical algorithms, in the worst case, it achieves polynomial acceleration.In some specific cases, it even achieves exponential acceleration.This method fully utilizes the advantages of quantum computing, but only improves the process of finding neighbors and does not consider the impact of K values on the algorithm.Similarly, Ruan et al. [3] proposed a quantum circuit for Hamming distance to improve KNN.This method uses a quantum circuit to calculate the Hamming distance between testing data and training data, and sets a threshold to obtain K nearest neighbors of the testing data.Subsequently, Dang et al. [6] proposed a QKNN algorithm and applied it to image classification.Specifically, it first uses classical methods to extract the feature vector of the image, which is nonquantum.Then, the feature vector obtained in the previous step is transformed into a quantum state and inputted into a quantum circuit.Finally, the quantum minimum search algorithm is used to obtain K nearest neighbors of the testing data.This method can be seen as a combination of classical algorithms and quantum technology, and only quantizes a part of the algorithm.
Subsequently, Basheer et al. [5] proposed a QKNN algorithm, which encodes fidelity information between the testing state and all sequence states into the amplitudes of a quantum state, thereby performing measurement to obtain K nearest neighbors.In recent years, several new QKNN algorithms have been proposed.For example, Zhang et al. [7] proposed an improved QKNN algorithm, which uses efficient encoding methods and amplitude estimation techniques to construct a weighted QKNN circuit, thereby improving similarity calculation.Tian and Baskiyar also proposed a QKNN method and applied it to fake news detection [8].Gao et al. [9] proposed a QKNN algorithm, which uses a quantum subalgorithm to search for the minimum value in an unordered dataset, thereby obtaining K nearest neighbors of the testing data.Li et al. [36] used quantum computing to obtain the Hamming distance between testing data and training data for KNN classification.
Authorized licensed use limited to the terms of the applicable license agreement with IEEE.Restrictions apply.
Li et al. [10] proposed a quantum k-fold cross-validation method to improve KNN.Specifically, it first calculates the Hamming distance between the testing data and training data.And then, it obtains the threshold t of the nearest neighbor distance through quantum technology, thereby getting the K nearest neighbors of the testing data.Finally, it uses the majority rule to classify the testing data.Feng et al. [4] proposed a QKNN algorithm based on the polar distance.The core of this method is to propose a new similarity measure function (i.e., polar distance).The K nearest neighbors of the testing data are further found and classified them based on this distance measure.
In summary, we can see that in QKNN, the core breakthrough lies in obtaining the neighbor selection (i.e., distance measurement or neighbor search).Although they have achieved acceleration compared to classical KNN, it is mainly only a partial quantumization of the process, and they have not considered the influence of the K value on the algorithm.In addition, they have not studied the selection of K value using a quantum technology.

A. Notation
In this article, we use X ∈ R n×d to represent the training data and Y ∈ R m×d to represent the testing data, where n and m, respectively, denote the number of training data and testing data, and d represents the number of features.W ∈ R n×m represents the relationship matrix between the testing data and the training data, which records the optimal K value and K nearest training data for all testing data.Y j ∈ R 1×d represents the jth testing data.In addition,

B. KNN With K Value Selection and Neighbor Selection
The conceptual similarity between the proposed objective function and the ideas presented in [21], [37], and [38] is that they are all based on the least squares loss function.
The key distinction lies in the core of the proposed KNN objective function, which uniquely acquires both the K value and K nearest neighbors of the testing data through matrix calculations, a feature not shared by other methods.We first utilize the training data to reconstruct the testing data, in order to establish the relationship between the testing data and training data.This is shown in the following equation: where X ∈ R n×d represents the training data matrix, Y ∈ R m×d represents the testing data matrix, where n and m, respectively, denote the number of training data and testing data, and d represents the number of data features.W ∈ R n×m represents the reconstruction coefficient matrix between the testing data and training data.Equation ( 1) can be utilized to sequentially reconstruct each testing data by the training data, thereby obtaining the reconstruction coefficient matrix.However, two issues still remain.
1) During the reconstruction process, there may be fitting errors that can affect the accuracy of the obtained reconstruction matrix.2) The reconstruction matrix is nonsparse, which prevents us from obtaining the K value and corresponding K nearest neighbors for each testing data.Therefore, we further introduce a bias term to obtain the following equation: where e ∈ R d×1 is a vector with all elements equal to 1 and b ∈ R 1×m is the bias term which can be used to reduce errors during the reconstruction of training data for testing data.Clearly, (2) addresses only the issue of existing fitting errors, without resolving the challenge of obtaining K values and K nearest neighbors.Therefore, we further introduce the W 1 and W 2,p regularization terms to achieve a dual sparsity effect on the matrix W. This can lead to a significant number of zero elements in the reconstruction coefficient matrix, thereby revealing the K values and corresponding K nearest neighbors for testing data.The objective function of our proposed KNN is shown as follows: For (3), we use the alternating iteration method to optimize it [39].When W is fixed, we can obtain the following equation: For (4), we can further rewrite it in the form of a trace, i.e., min b tr((W T X + b T e T − Y)(X T W + eb − Y T )), and take the derivative of b to obtain the following equation: According to (5), the solution of b can be further obtained as follows: Similarly, when b is fixed, (3) can be further written as follows: min By making and taking the derivative of each column W j of W, the following equation can be obtained: Furthermore, according to (8), we can obtain the solution for each column W j of W as follows: where D j and Q are both diagonal matrices, and their diagonal elements are where ς is a small enough constant.It should be noted that the above solving process is an alternating iteration process.i.e., When solving the b of the (t + 1)th iteration, the W value of the t-th iteration will be used.When solving W for the (t + 1)th iteration, the value of b in the t-th iteration needs to be used.The final solution for the model W is obtained only when the algorithm converges.The variable W records the optimal K value for all testing data and the indices of its K nearest neighbor samples.
To facilitate the reader's understanding, we introduce an example.Suppose the final value of the variable W that we obtain is As shown in (12), W has five rows and four columns, indicating that it records the relationships between four testing data and five training data.For the first column of W, it corresponds to the relationship between the first testing data and the five training data.We can observe that in the first column of W, the elements corresponding to the 1st and 4th training data are nonzero, indicating that the optimal K value for the first testing data is 2, with the two nearest neighbors being the 1st and 4th training data.Similarly, we can determine that the optimal K value for the second testing data is 3, with the corresponding 3 nearest neighbors being the 2nd, 4th, and 5th training data.For the third testing data, its optimal K value is 4, and the corresponding four nearest neighbors are the 1st, 3rd, 4th, and 5th training data.Finally, for the fourth testing data, its optimal K value is 1, with the nearest neighbor being the 2nd training data.

C. Quantum KNN With K Value Selection and Neighbor Selection
In the previous section, we obtained the final model W through (3) and alternating iterations.However, the time complexity of this process is relatively high.Therefore, in this section, we propose its quantum version.First, we perform reduced singular value decomposition on the training data matrix X, as shown in the following equation: where r ≤ min(n, d) represents the rank of matrix X, composed of left and right singular value vectors, and = diag(σ 1 , σ 2 , . . ., σ r ).In order to facilitate the quantization of the algorithm, we convert (13) into the dirac symbolic form, i.e., X = r i=1 σ i |u i v i |.According to ( 9) and ( 13), we can obtain the following theorem.
Theorem 1: Given X = r i=1 σ i |u i v i |, the quantum state of model W j can be obtained as follows: where Yb j = (Y T ) j − eb j = r i=1 β i Yb j |v i is the linear combination of Yb j on the basis vector |v i .
Proof: According to the dirac symbolic form of (13), i.e., Furthermore, the following equation can be obtained: Because Yb j can be written as a linear combination on the basis vector |v i , i.e., Yb j = r i=1 β i Yb j |v i .Therefore, the following equation can be obtained: By combining ( 9), (15), and ( 16), the following equation can be further obtained: Authorized licensed use limited to the terms of the applicable license agreement with IEEE.Restrictions apply.
Next, inspired by the HHL algorithm [40], the proposed QKNN algorithm is divided into five steps.The detailed process is shown in the following.
1) Preparation of quantum initial states.First, we prepare three types of quantum registers, as shown in Fig. 3. From bottom to top, they are the first, second, and third types of quantum registers, corresponding to Yb j , eigenvalues, and auxiliary quantum bits, respectively.We set their initial state to . ., |0 |Yb m can refer to the method in [41], where n 0 represents the number of quantum bits to store the eigenvalues, and m represents the number of quantum auxiliary bits, corresponding to the m columns of W where 2) We convert the Hermitian matrix X into a unitary operation e iXt .If matrix X is not Hermitian, we restructure it through the following formula: At this point, the corresponding W j and Yb j are filled with the corresponding zero elements, namely, Wj = 0 W j and Ỹ bj = Yb j 0 .For the preparation of e iXt , if X is a sparse matrix, it can be simulated by the method in [40].Its time complexity is O(s 4 κε −1 log 2 (n)), where κ is the condition number of X, s is the sparsity, and ε is the error.If X is not a sparse matrix, we can use the methods in [42] and [43] to effectively simulate.As this part of the work is not the focus of this article, we will not provide a detailed introduction.3) Phase Estimation: We use phase estimation to transfer all the integer forms of the eigenvalues of X to the basis vector of the stored bits.At this point, the quantum state of the entire system is Since our model W is a matrix, we perform quantum calculations to obtain the quantum states of each column.
There are a total of m columns, so m phase estimation operations are required.4) Controlled Rotation: This process is the core of the algorithm, and each rotation angle affects the solution of W j .Specifically, we control the qubits of the third register through |(±σ i )/(n + d) and rotate them from At this point, the quantum state of the entire system is where the controlled rotation angle is At this stage, all eigenvalues associated with quantum states are acquired, and these eigenvalues are converted from the stored bit basis vector to amplitudes.5) Inverse Phase Estimation and Measurement: At this stage, we perform inverse phase estimation and auxiliary bit measurement, i.e., the eigenvalues on the amplitude of the stored bit are merged onto the Yb j bit, and when the ancillary qubit is measured to be in the specific state (i.e., |1 ), the quantum state of the solution can be obtained on the Yb j bit.At this stage, the quantum state of the entire system is At this point, we only need to remove |1 to obtain the quantum state |W j of each column of W (Here, represents the jth column, out of a total of m columns.).To facilitate the understanding of the algorithm's process, we present the pseudocode of Algorithm 1.The input of the algorithm includes the training data X, testing data Y, and tunable hyperparameters λ 1 and λ 2 .The output is each column of W and its corresponding quantum state |W j .The first

D. Time Complexity Analysis
As described in Section I, the time complexity of current KNN algorithms mainly focuses on K value selection and neighbor selection.In classical KNN algorithms, their time complexity is O(mnd), where m, n, and d represent the number of testing data, the number of training data, and the number of features in the data, respectively.Assuming d = O(n), the time complexity of classical KNN can be further simplified to O(mn 2 ).The time complexity of the proposed QKNN is mainly composed of phase estimation, amplitude amplification, and the number of iterations.According to [44], we can obtain that the time complexity of phase  6); The quantum state of W j is obtained by Fig. 3 i.e., Eq. ( 14); max poly log (n + d)κ 2 /ε 3 ).By adding the time complexity of amplitude amplification and iteration, we can know that the time complexity of where t is the number Authorized licensed use limited to the terms of the applicable license agreement with IEEE.Restrictions apply.

TABLE I TIME COMPLEXITY COMPARISON BETWEEN
PROPOSED QKNN AND NON-QKNN of iterations and κ is the number of conditions (i.e., the ratio of the maximum singular value of X). ε is the error term.1), and 1/ε = O(poly log n), the time complexity of the proposed algorithm can be further written as O(poly log(n)κ 3 m).When κ satisfies different situations, the time complexity comparison between the proposed QKNN algorithm and the non-QKNN algorithm is shown in Table I.
As shown in Table I, we can observe that the proposed algorithm can further achieve exponential acceleration on the basis of non-QKNN.

IV. EXPERIMENT
In this section, we conducted a series of experiments using qiskit and MATLAB programming to, respectively, output the quantum and classical results of the proposed algorithm, thereby verifying the efficiency of the proposed algorithm.

A. Experimental Setup
Due to the limitations of quantum computer hardware, we use the dataset in [45] and the public dataset Banknote1 to verify the performance of the proposed quantum KNN.Specifically, we set training data X =  After completing these settings, we conducted the following four parts of experiments.
1) For each testing data, we output the quantum and nonquantum results for the first eight iterations of the algorithm.From these results, we determine the optimal K value and the corresponding K nearest neighbors for each testing data.2) We conduct convergence experiments to verify the convergence of the algorithm.3) We output the quantum circuit diagram for the algorithm in the final iteration.Additionally, we also output the controlled rotation angles for each iteration of the algorithm to aid readers in understanding.4) Based on the output results from the quantum circuit and classical computation, we obtain the label for each testing data.In addition, we compared the proposed algorithm with the QKNN in [46] on the Banknote thereby verifying the classification performance of the algorithm.It is worth noting that the proposed algorithm had already converged by the 8th iteration.Therefore, all of our experiments only present the results from the first eight iterations.

B. Analysis of Quantum and Classical Output Results
Figs. 4-7 show the quantum output results of the first to fourth testing data for the first eight iterations, respectively.Fig. 9 shows the classical results of all testing data for the first eight iterations.As we only measured three quantum bits, only the results corresponding to these three qubits are shown in Figs.4-7, where two quantum bits correspond to each testing data and the other qubit is an ancillary qubit.In addition, it is only considered when the auxiliary bit is |1 .From Fig. 4, in the first iteration, we can see that the ratio of the measurement probability is |00 y 1 |1 a : |01 y 1 |1 a : |10 y 1 |1 a : |11 y 1 |1 a = 0.145 : 0.120 : 0.079 : 0.086.In the classic output result (i.e., Fig. 9), the output of the first iteration is [2.3361; −0.2541; −0.9744; −1.6464].This indicates that for the first testing data, both quantum and nonquantum results were given the maximum weight to the first training data in the first iteration.Unfortunately, due to the noise present in the quantum circuit and errors in the Hamiltonian simulation process, there is a slight deviation between the quantum results and classical results.However, this does not affect our ability to obtain the K nearest neighbors of the data and the prediction accuracy.Similarly, for the first testing data, after the fifth iteration, its optimal K value was obtained to be 1, and the nearest neighbor data was the first training data.From Fig. 4, it can also be seen that after the fifth iteration, the probability of |00 y 1 |1 a is the highest, relative to |01 y 1 |1 a , |10 y 1 |1 a , and |11 y 1 |1 a .From Fig. 5, for the second testing data, in the first iteration, we can see that the ratio of the measurement probability is |00   K value for the third testing data can be obtained as 1, with the third training data as its nearest neighbor.Similarly, according to Figs. 7 and 9, the optimal K value for the fourth testing data is 1, with the fourth training data as its nearest neighbor.
Table VI shows a comparison between the proposed algorithm and the state-of-the-art QKNN algorithms.From Authorized licensed use limited to the terms of the applicable license agreement with IEEE.Restrictions apply.data in the final iteration of the proposed algorithm.Since the quantum circuits differ only slightly in each iteration, with the main differences being the input quantum states |Yb 1 , |Yb 2 , |Yb 3 , and |Yb 4 , as well as the controlled rotation angles, we only show the circuit diagram for one iteration.

C. Convergence Analysis and Quantum Circuit
In Fig. 8, from bottom to top, there are three quantum registers.The first quantum register is initialized to the corresponding quantum state of the testing data.The second quantum register mainly stores the eigenvalues of the training data, and we use four qubits with their initial states being |0 , |0 , |0 , and |0 .The third register is an auxiliary quantum register, and its initial state is |0 .After the initial state preparation, the corresponding unitary operation of the training data is obtained by the Hamiltonian simulation.Then, phase estimation, controlled rotation, and inverse phase estimation are performed sequentially.Finally, the auxiliary qubit is measured, and when |1 is measured, the quantum state of the final output W can be obtained.

D. Controlled Rotation Angle
The core of the proposed algorithm is to obtain quantum results for each iteration based on different controlled rotation angles.It is equivalent to RY(θ ) rotation, as follows: where Due to the fact that the training data has four eigenvalues, we need to perform four RY(θ ) rotations for each testing data.Additionally, each testing data needs to correspond to an auxiliary qubit, so in one iteration, we require 16 controlled rotations.The controlled rotation angles for each testing data in each iteration are shown in Tables II-V.From Tables II-V, it can be observed that for the first testing data, starting from the 6th iteration, the controlled rotation angles for each qubit remain almost unchanged, with the first qubit's rotation angle ranging from 0.177±0.004.The controlled rotation angles for the remaining three qubits remain almost constant.This indicates that after the 6th iteration, the controlled rotation angles remain almost unchanged, which is consistent with the convergence of the algorithm.For the second testing data, starting from the 6th iteration, the controlled rotation angles change relatively little.The controlled rotation angles for the first, third, and fourth qubits remain almost constant, while the second qubit's rotation angle ranges from 0.192 ± 0.003.Similarly, for the third and fourth testing data, starting from the 6th iteration, the controlled rotation angles change relatively little.For example, the rotation angle range for the third qubit corresponding to the third testing data is 0.165±0.001.The rotation angle range for the fourth qubit corresponding to the fourth testing data is 0.1664 ± 0.0001.

E. Classification Result Analysis
For the dataset in [45], based on the quantum results (Figs. 4-7) and the classical results (Fig. 9), we can obtain the Authorized licensed use limited to the terms of the applicable license agreement with IEEE.Restrictions apply.

TABLE VI
CHARACTERISTICS COMPARISON OF THE QKNN ALGORITHMS optimal K value and K nearest neighbors for each testing data.Specifically, the optimal K value for the first testing data is 1, and its nearest neighbor is the first training data.Similarly, the nearest neighbors for the second, third, and fourth testing data are the second, third, and fourth training data, respectively.Consequently, we can deduce that the class labels for these four testing data are 1, 1, 2, and 2, respectively.This achieves a 100% accuracy.
In addition, classification experiments were conducted on the publicly available Banknote dataset.The comparative algorithm QKNN only achieved a classification accuracy of 70.83%, while the proposed algorithm achieved a significantly higher accuracy of 87.50%.This outcome suggests that the proposed algorithm exhibits superior classification performance.This phenomenon can be attributed to the proposed algorithm's ability to learn the optimal K value for each testing data, thereby mitigating the influence of inappropriate K values on the classification performance of the algorithm.

V. CONCLUSION
This article introduces a novel QKNN algorithm that determines the optimal K value and its corresponding K nearest neighbors for each testing data by manipulating rotation angles.Specifically, the KNN objective function is formulated using least square loss, l 2,p , and l 1 regularization terms.Subsequently, a new quantum circuit is proposed to solve this objective function and acquire the quantum state of variables storing the optimal K value and the indices of the corresponding K nearest neighbors for the testing data.Finally, a series of experiments are conducted to demonstrate the efficiency of the proposed algorithm.
The core of the proposed method lies in leveraging quantum technology to address the issues of K value selection and neighbor selection in KNN.To some extent, this enhances the QKNN algorithm.In addition, the proposed method can handle objective functions with sparse regularization terms, which is of great significance in quantum computing.As far as we know, a current quantum technology can only solve problems in the form of Xw = y, where X is a known data matrix and y is the corresponding class label.Clearly, w = X −1 y.The core of our proposed quantum algorithm can solve objective functions containing sparse regularization terms, as shown in (3).This capability can be very helpful for quantum computing to solve objective functions based on sparse learning.
However, due to the impact of noise, the outcomes of the proposed algorithm have exhibited deviations in quantum outputs.Hence, for future investigations, we have identified two primary research directions.
1) We intend to delve into the development of an anti-noise QKNN algorithm, aiming to ensure that the algorithm's outcomes remain unaffected by noise.2) Employing insights from the structure and functioning of quantum circuits, we will construct novel quantum denoising algorithms to address this challenge.

Fig. 1 .
Fig. 1.Position distribution of a testing data and training data.

Fig. 3 .
Fig. 3.Quantum circuit for solving |W 1 , |W 2 , . . ., |W m .From bottom to top, a total of three types of quantum registers are involved.The initial quantum states of the first type of registers are denoted as |0 |Yb 1 |0 |Yb 2 , . . ., |0 |Yb m .The initial quantum states of the second type of quantum registers are denoted as |0 ⊗n 0 .The initial quantum states of the third type of quantum registers are denoted as |0 m .After inputting the quantum initial states of the three quantum registers, from left to right, the phases estimation, controlled rotation, inverse phase estimation, and measurement operations are sequentially performed.When the auxiliary qubits in the third type of quantum registers are measured as |1 , quantum states |W 1 , |W 2 , . . ., |W m can be obtained.Here, H represents the Hadamard gate.R 1 , R 2 , . . ., R m is the controlled rotation operations corresponding to |W 1 , |W 2 , . . ., |W m , respectively.FT and FT −1 represent the quantum Fourier transform and its inverse, respectively.n 0 represents the number of quantum bits required to store eigenvalues.

Algorithm 1 :repeat 4
Pseudo Code for Proposed Method Input: Training data X ∈ R n×d , labels l x of the training data, testing data Y ∈ R m×d , adjustable parameters λ 1 and λ 2 ;; Output: W and |W 1 , |W 2 , . . ., |W m ; 1 Initialize t = 0; 2 Random initialization matrix W; 3 Update b according to Eq. ( l x = [1; 1; 2; 2] for training data, and testing data Y = label for testing data is the same as the label for training data, i.e., l y = [1; 1; 2; 2].Parameters λ 1 = 1 and λ 2 = 1.Due to the fact that the eigenvalues of training data X are 1, 2, 4, and 8.The eigenvectors corresponding to the eigenvalues are , we set each y register to contain two qubits with initial quantum states of |Yb 1 , |Yb 2 , |Yb 3 , and |Yb 4 .The intermediate quantum register requires four qubits with initial quantum states of |0 |0 |0 |0 .In addition, we also need an auxiliary register with a quantum initial state of |0 .

Fig. 10
Fig. 10 displays the value of the objective function at each iteration.As can be seen from Fig. 10, the algorithm reached convergence after five iterations, indicating its fast convergence.Combining Figs.4-7, we also observe that the results from both quantum circuit outputs and classical computation outputs stabilize from the fifth iteration onward.Fig. 8 displays the circuit diagram for the first testing y 1 |1 a : |01 y 1 |1 a : |10 y 1 |1 a : |11 y 1 |1 a = 0.041 : 0.065 : 0.024 : 0.046.In classical calculations, the first output result of the second testing data is [0.1470; 1.6382; −0.5731; −1.1893].Starting from the sixth iteration, it is determined that the optimal K value of the second testing data is 1, and the nearest neighbor is the second Authorized licensed use limited to the terms of the applicable license agreement with IEEE.Restrictions apply.

TABLE III CONTROLLED
ROTATION ANGLE CORRESPONDING TO THE SECOND TESTING DATATable VI, we can see that the proposed quantum algorithm can simultaneously perform K value selection and neighbor selection.Unlike existing QKNN algorithms, which use quantum search algorithms or distance metric quantization for neighbor selection, our proposed QKNN algorithm includes both K value selection and neighbor selection in one-step quantum computation, achieving exponential speedup.Although Wiebe et al.'s algorithm can also achieve exponential speedup under certain conditions, its acceleration is only relative to the neighbor selection process and does not include K value selection.Therefore, our proposed QKNN algorithm is a significant improvement as it combines both K value selection and neighbor selection and achieves exponential speedup.