arxiv:2003.09187v1 [quant-ph] 20 mar 2020

21
Quantum k nearest neighbors algorithm Afrad Basheer 1,2 , A. Afham 1,3 , and Sandeep K. Goyal 3 1 Centre for Quantum Software and Information, University of Technology, Sydney, Australia 2 Chennai Mathematical Institute, H1 SIPCOT IT Park, Kelambakkam, Tamil Nadu 603103, India 3 Department of Physical Sciences, Indian Institute of Science Education & Research (IISER) Mohali, Sector 81 SAS Nagar, Manauli PO 140306 Punjab India. One of the simplest and most effective classical machine learning algorithms is the k nearest neighbors algorithm (kNN) which classifies an unknown test state by finding the k nearest neighbors from a set of M train states. Here we present a quantum analog of classical kNN – quan- tum kNN (QkNN) – based on fidelity as the similarity measure. We show that the QkNN algorithm can be reduced to an instance of the quantum k maxima al- gorithm; hence the query complexity of QkNN is O( kM ). The non-trivial task in this reduction is to encode the fidelity in- formation between the test state and all the train states as amplitudes of a quan- tum state. Converting this amplitude en- coded information to a digital format en- ables us to compare them efficiently, thus completing the reduction. Unlike classi- cal kNN and existing quantum kNN algo- rithms, the proposed algorithm can be di- rectly used on quantum data, thereby by- passing expensive processes such as quan- tum state tomography. As an example, we show the applicability of this algorithm in entanglement classification and quantum state discrimination. 1 Introduction Quantum machine learning (QML) [1, 2, 3, 4] is a recent offspring of quantum computing and ma- chine learning (ML). One of the proposed appli- cations of QML is using quantum computers to speed up ML tasks [5, 6]. In a converse man- ner, ML has proven itself adept at problems in Afrad Basheer: [email protected] A. Afham: [email protected] Sandeep K. Goyal: [email protected] physics [7, 8, 9, 10]. We have also seen the real- isation of quantum versions of several (classical) ML algorithms [11, 12, 13]. Along the same vein, we propose a quantum version of the k nearest neighbor algorithm [14] by reducing it to an in- stance of the quantum k maxima finding algo- rithm [15, 16] and providing an explicit construc- tion of the oracle required. The algorithm is ca- pable of classifying states without their explicit classical description. We then present two prob- lems of interest as applications of our algorithm, namely entanglement classification and quantum state discrimination. kNN is a simple supervised ML algorithm used extensively for pattern recognition and classifica- tion. This algorithm rests on the assumption that two points close to each other are more likely to be of the same type. The algorithm is provided with a set of train states (vectors) whose class la- bels are known. The test state (vector) with the unknown label is compared with the train states, and the k number of nearest neighbors of the train states are identified for the given test state. Fi- nally, the label of the test state is determined upon majority voting. The expensive step in the kNN algorithm is to determine the distance between the test state and all the train states. Each state (train or test) is represented by a vector of real (or complex) num- bers. As the number of train states and the size of the state vectors increases, kNN becomes more expensive. To classify vectors of dimension N by comparing it to a set of train vectors of cardinal- ity M , we need to carry out O(MN ) operations. In this article, we propose a novel quantum k nearest neighbor (QkNN) algorithm, a quantum analog of the classical kNN algorithm. Utilising existing algorithms such as the Swap test [17], k minima finding algorithm [15], quantum phase estimation, and the recent quantum analog-to- 1 arXiv:2003.09187v3 [quant-ph] 17 Jun 2021

Upload: others

Post on 23-Mar-2022

0 views

Category:

Documents


0 download

TRANSCRIPT

Quantum k nearest neighbors algorithmAfrad Basheer1,2, A. Afham1,3, and Sandeep K. Goyal3

1Centre for Quantum Software and Information, University of Technology, Sydney, Australia2Chennai Mathematical Institute, H1 SIPCOT IT Park, Kelambakkam, Tamil Nadu 603103, India3Department of Physical Sciences, Indian Institute of Science Education & Research (IISER) Mohali, Sector 81 SAS Nagar,Manauli PO 140306 Punjab India.

One of the simplest and most effectiveclassical machine learning algorithms isthe k nearest neighbors algorithm (kNN)which classifies an unknown test state byfinding the k nearest neighbors from a setof M train states. Here we present aquantum analog of classical kNN – quan-tum kNN (QkNN) – based on fidelity asthe similarity measure. We show thatthe QkNN algorithm can be reduced toan instance of the quantum k maxima al-gorithm; hence the query complexity ofQkNN is O(

√kM). The non-trivial task in

this reduction is to encode the fidelity in-formation between the test state and allthe train states as amplitudes of a quan-tum state. Converting this amplitude en-coded information to a digital format en-ables us to compare them efficiently, thuscompleting the reduction. Unlike classi-cal kNN and existing quantum kNN algo-rithms, the proposed algorithm can be di-rectly used on quantum data, thereby by-passing expensive processes such as quan-tum state tomography. As an example, weshow the applicability of this algorithm inentanglement classification and quantumstate discrimination.

1 IntroductionQuantum machine learning (QML) [1, 2, 3, 4] is arecent offspring of quantum computing and ma-chine learning (ML). One of the proposed appli-cations of QML is using quantum computers tospeed up ML tasks [5, 6]. In a converse man-ner, ML has proven itself adept at problems inAfrad Basheer: [email protected]. Afham: [email protected] K. Goyal: [email protected]

physics [7, 8, 9, 10]. We have also seen the real-isation of quantum versions of several (classical)ML algorithms [11, 12, 13]. Along the same vein,we propose a quantum version of the k nearestneighbor algorithm [14] by reducing it to an in-stance of the quantum k maxima finding algo-rithm [15, 16] and providing an explicit construc-tion of the oracle required. The algorithm is ca-pable of classifying states without their explicitclassical description. We then present two prob-lems of interest as applications of our algorithm,namely entanglement classification and quantumstate discrimination.

kNN is a simple supervised ML algorithm usedextensively for pattern recognition and classifica-tion. This algorithm rests on the assumption thattwo points close to each other are more likely tobe of the same type. The algorithm is providedwith a set of train states (vectors) whose class la-bels are known. The test state (vector) with theunknown label is compared with the train states,and the k number of nearest neighbors of the trainstates are identified for the given test state. Fi-nally, the label of the test state is determinedupon majority voting.

The expensive step in the kNN algorithm is todetermine the distance between the test state andall the train states. Each state (train or test) isrepresented by a vector of real (or complex) num-bers. As the number of train states and the sizeof the state vectors increases, kNN becomes moreexpensive. To classify vectors of dimension N bycomparing it to a set of train vectors of cardinal-ity M , we need to carry out O(MN) operations.

In this article, we propose a novel quantum knearest neighbor (QkNN) algorithm, a quantumanalog of the classical kNN algorithm. Utilisingexisting algorithms such as the Swap test [17],k minima finding algorithm [15], quantum phaseestimation, and the recent quantum analog-to-

1

arX

iv:2

003.

0918

7v3

[qu

ant-

ph]

17

Jun

2021

1. Initialize setwith randomlychosen indices.

2a. Choose anindex from theindices featuringin

2b. Using quantumsearch algorithmfind an indexsuch that

& .

2c. Replacewith in the set

.

Encode fidelityas amplitudes(analog encoding).

Analog to digitalconversion.

Classical circuitsto comparefidelities andmembership in

Structure of the oracle required to implement the Grover operator which can be used forthe quantum search subroutine. This is the only quantum subroutine in the algorithm.

Repeat untilnearest neighboursare found.

Quantum -maxima finding algorithm

Figure 1: An overview of the QkNN algorithm. The idea is to use the quantum k maxima finding algorithm ona table F = [F0, . . . , FM−1] to find the k nearest neighbors of the test state. Here Fj = |〈ψ|φj〉|2 is the fidelityof the test state |ψ〉 with the jth train state |φj〉. We start with a set A of randomly chosen k indices and then,using quantum search algorithm (Grover’s search when the number of targets are unknown), we replace indices inA with new indices (that are not in A) until the top k neighbors are found. The crucial step here is to prepare anoracle capable of performing the required Grover operator in the quantum search subroutine. The oracle should becapable of making comparisons of the form Fy′ > Fy and ensure that y′ /∈ A. Such an oracle is constructed by firstextracting the fidelity using Swap test and encoding it as amplitudes of a quantum state. Then, an analog to digitalconversion of the amplitudes is carried out which results in the fidelity being encoded as digital (bit-string) states.Once this is done, we perform a series of classical operations to compare the fidelities and check membership in A.

digital conversion algorithm [18], we constructan algorithm capable of classifying states with-out the requirement of having their classical de-scription (writing out the amplitudes in some ba-sis). This is particularly useful in cases wherethe data to be classified is inherently quantum,and thereby we can bypass expensive processessuch as tomography [19] to learn the descriptionof the state in question. We also provide two ap-plications of our algorithm, namely entanglementclassification of pure states and quantum statediscrimination. Moreover, since we use quantumk maxima finding algorithm to find the k nearestneighbors, the query complexity of our algorithmis O(

√kM).

Previous work in quantum versions of kNN in-clude [20, 21, 22, 23, 24]. Ruan et al. [21] usesHamming distance as the metric to estimate the knearest neighbors. Dang et al. [23] uses quantumk nearest neighbors algorithm proposed in [22] forimage classification. Of all the previous papersmentioned, the work by Chen et al. [22] seems tobe the closest to ours with the same overall querycomplexity of O(

√kM).

However, there are a few features that distin-guish our work from the above-mentioned works.Firstly, we envision our algorithm to be used di-rectly on quantum data (though it can also beused on ‘classical data’), which allows us to clas-sify states without having their explicit classicaldescription. Instead, we require the circuits ca-pable of preparing the test state. Also, we usefidelity and dot product as a measure of similar-ity, and we demonstrate problems where fidelitycan be used to carry out classification.

In Section 2, we present the necessary back-ground, and in Section 3, we present our quantumk nearest neighbors algorithm, which uses fidelityas a measure of closeness. Then, in Section 4,we present a variation of the algorithm that usesdot product instead of fidelity for classification.Both of these algorithms build upon generalisa-tions of algorithms from [18]. Section 5 comprisesthe query complexity of our algorithm which weshow to be O(

√kM). We then present in Section

6 two problems where our QkNN algorithm canbe utilised - the problem of entanglement classifi-cation and a problem analogous to quantum state

2

discrimination.Finally, we conclude in Section 7 while also dis-

cussing the kind of problems our algorithm wouldbe adept at solving.

2 BackgroundIn this section, we provide the relevant back-ground for the Quantum kNN algorithm. Webegin with the introduction of the classical kNNalgorithm

2.1 Classical kNN algorithmLet {un} be a collection of vectors of unknown la-bels, which we call test states. The aim is to accu-rately assign these test states labels. For this, thek nearest neighbors (kNN) algorithm requires acollection of vectors {vm} of the same dimensionwhose labels are known to us. We shall call thesestates train states. kNN assigns labels to eachtest state by first computing the k nearest neigh-bors of the test state. Then a majority votingis carried out among these k nearest neighbors.Ties are resolved in different ways, such as as-signing the label of the nearest training point orthe label of a random training point among thek nearest neighbors. Successful applications ofkNN include [25, 26]. Being a simple algorithm,kNN also allows us to reason about the structureof the data we are working with.

Let the test states and the trains states are r-dimensional real or complex vectors. Any bonafide definition of a distance measure can be usedfor the purpose of kNN algorithm. Most com-mon distance measures include Euclidean dis-tance d(u, v) =

√∑ri |ui − vi|2 and cosine simi-

larity c(u, v) = 〈u|v〉‖u‖·‖v‖ (which reduces to inner

product for normalised states), where u and v arer-dimensional complex vectors.

In quantum information, the fidelity function,though not a metric, F(·, ·) can be used to as-sign a notion of nearness between two quan-tum states beloning to the same space. Forarbitrary quantum states ρ, σ belonging to thesame space,the fidelity between them is F(ρ, σ) =(Tr(√√

ρσ√ρ))2

. For pure quantum states ρ =|u〉〈u|, σ = |v〉〈v|, the fidelity function simplfiesto F(u, v) = |〈u|v〉|2.

Though fidelity is not a metric, the metric Bu-res distance B(u, v) =

√2− 2

√F(u, v), defined

Figure 2: Choosing a k = 3 neighborhood. Here cir-cle and square represents two different classes and starrepresents the unknown state whose label is to be deter-mined. On choosing k = 3, we classify it as a ‘square’point.

over quantum states, is a monotonous functionof the fidelity. Therefore, finding the k -nearestneighbors of any quantum state with respect tothe Bures distance is the same as finding the kstates with the largest fidelity to the chosen quan-tum state.

The rationale behind kNN is that data pointsthat are close together, with respect to some dis-tance measure, must be similar. Formally, thekNN algorithm consists of the following steps (seeFig. 2):

1. For each test state (whose label is to be de-termined), compute its distance to the trainstates whose labels are known.

2. Choose the k number of neighbors which arenearest to the test state.

3. Assign the label using majority voting.

Although the kNN algorithm is simple to un-derstand and easy to implement, the algorithmhas its drawbacks. As the number of train datapoints and the dimension of the state vectorsgrows, kNN can quickly turn intractable. Classi-fication of an N dimensional test state by com-paring withM train states requires O(MN) mul-tiplication operations. Furthermore, there is nogeneral way of choosing k, and usually, hyperpa-rameter tuning is done to choose the best possiblek [27].

2.2 Quantum k maxima finding algorithmDurr and Hoyer describe, based on Grover searchalgorithm, an algorithm in [28] which can be used

3

to find the minimum of an unsorted list of sizeM with complexity O(

√M). Through a sim-

ple modification, one can use the same algorithmto find the maximum instead of the minimum.A generalisation of the algorithm can be foundin [15], which can be used to find the k smallestelements in a table T = [T0, . . . , TM−1] of M el-ements in time O(

√kM). A simple explanation

of the algorithm can be found in [16].The general idea of the algorithm is to start

with randomly chosen k indices and use the quan-tum search algorithm [29] to find and replace thechosen indices with ones that have a higher ta-ble value. This process is repeated until we endup with the k highest values in the table. Thealgorithm is as follows:

1. Initialise a set A = {i1, . . . , ik} with ran-domly chosen k indices from the list of Mindices.

2. Repeat the following forever:

(a) Select threshold index y from A ran-domly.

(b) Using quantum search algorithm pro-posed in [29], find index y′ which is notpresent in A, such that Ty′ > Ty. Thiscan be seen as using quantum search onthe Boolean function

fy,A(j) ={

1 : Tj > Ty and j /∈ A,0 : otherwise.

(1)

(c) Replace y with y′ in the set A.

The step 2b is the only quantum subroutine of thealgorithm. Essentially, we use quantum searchto find an index not present in A and has itstable value greater than the table value of thethreshold index. The quantum search algorithmis a slightly modified Grover’s search algorithm,which is used in cases where the number of solu-tions to the boolean function is unknown. Notethat this algorithm also uses the same Grover op-erator as in normal Grover’s search. Also, insteadof randomly sampling an index from A in step 2a,one can choose the index

y = argmini∈A

Ti.

0/1

n

n

|0〉 H H

|ψ〉

|φ〉

Figure 3: Circuit for Swap test.

This method would ensure a stopping criterion forthe algorithm, that is, repeat step 2 until step 2bcannot be carried out. This is because if we can-not find an index, which is not in A and has ahigher table value than the index with the min-imum table value in A, then we should have allthe k indices with the largest table values alreadypresent in A.

2.3 Swap TestThe swap test [17] is a quantum algorithm thatcan be used to statistically estimate the fidelityF(ψ, φ) = |〈ψ|φ〉|2 between two arbitrary n qubitpure states |ψ〉 and |φ〉. The three register gatein Fig. 3 is the controlled swap (CSWAP) gatewhose action is defined by

CSWAP|0〉|ψ〉|φ〉 = |0〉|ψ〉|φ〉,CSWAP|1〉|ψ〉|φ〉 = |1〉|φ〉|ψ〉.

(2)

To implement the swap test between states |ψ〉and |φ〉, we need three registers prepared in states|0〉, |ψ〉, and |φ〉, respectively. The initial com-bined state of the three registers is |0〉|ψ〉|φ〉. Wethen implement circuit in Fig. 3.

At the end of the circuit, the measurementprobabilities of the first register are

Pr(0) = 12 + 1

2 |〈ψ|φ〉|2, (3)

Pr(1) = 12 −

12 |〈ψ|φ〉|

2. (4)

The quantity Pr(0) − Pr(1) gives us the desiredfidelity.

2.4 Quantum Analog to Digital Conversion(QADC) algorithmMitarai et al. describe a set of algorithms in [18]to carry out analog to digital conversions withina quantum circuit. We provide a description of

4

what the algorithm does here and refer to Ap-pendix (A) for details for the sake of brevity.

Let∑d−1i=0 ci|i〉 be an arbitrary quantum state.

Let {r0, . . . , rd−1} be bitstrings that denote thebest b-bit approximation of {|c0|, . . . , |cd−1|} re-spectively. An m-bit abs-QADC algorithm cantransform the analog encoded state

∑d−1i=0 ci|i〉 to

the digital encoded state 1√M

∑d−1i=0 |i〉|ri〉.

Let {r0, . . . , rd−1} be bitstrings thatdenote the best b-bit approximation of{Re(ci), . . . ,Re(cd−1)} respectively. An m-bit real-QADC algorithm transforms the analogencoded state

∑d−1i=0 ci|i〉 and to the digital

encoded state 1√M

∑d−1i=0 |i〉|ri〉.

In the coming sections, we show that variationsto the circuits of these algorithms give rise to cir-cuits capable of carrying out a quantum kNN al-gorithm.

3 Quantum k Nearest neighbors algo-rithm using fidelityWe now present the Quantum k Nearest neigh-bors algorithm. Refer Fig. 1 for an overview ofthe algorithm. LetH be the n-qubit Hilbert spaceof dimension N = 2n and let |ψ〉 ∈ H be the un-known test state whose label is to be determined.Let

{|φj〉 : j ∈ {0, . . . ,M − 1}} ⊂ H (5)

be a collection of M train states whose labelsare known to us. For the sake of convenience,we assume M = 2m for some positive integer m.The idea is to find the k nearest neighbors of |ψ〉from the train states and then through majorityvoting, assign |ψ〉 a label. Let Fj ≡ F(ψ, φj) =|〈ψ|φj〉|2 be the fidelity between the test state |ψ〉and the jth train state |φj〉 and define

F = [F0, . . . , FM−1] (6)

to be a table of lengthM containing the fidelitieswith the test state |ψ〉 and all the train states{|φj〉}.

3.1 A summary of the algorithm

Note that the problem of finding the k nearestneighbors of a test state |ψ〉 can be reduced to aninstance of k maximum finding algorithm carriedout on the table F given in Eq. (6). The only stepthat requires a quantum circuit in the quantum

k maximum finding algorithm is the quantumsearch subroutine given in step 2b. To achievethis, we should be able to prepare a circuit thatcarries out the oracle transformation

Oy,A|j〉|0〉 = |j〉|fy,A(j)〉. (7)

where fy,A is the Boolean function defined as

fy,A(j) ={

1 : Fj > Fy and j /∈ A,0 : otherwise.

(8)

That is, the fidelity Fj must be greater than Fyand j should not feature in the threshold indexset A. QkNN algorithm using fidelity as the sim-ilarity measure is then:

1. Using Oy,A as the required oracle for theGrover operator in quantum search in step2b, use k maxima finding algorithm tofind the k indices {j1, . . . , jk} whose states{|φj1〉, . . . , |φjk〉} have the maximum fidelitywith the test state.

2. Conduct a majority voting among the kstates and assign |ψ〉 the label of the ma-jority.

The non-trivial part in the above steps is the re-alisation of the oracle Oy,A. We briefly discussthis oracle in the next subsection and provide anexplicit construction of it after that.

3.2 On the oracle Oy,A

The construction of this oracle is based on theabs-QADC circuit from [18]. In that work, tocompute the absolute values of the amplitudes ofthe state, we apply Swap test in superpositionwith the state and computational basis vectors.In the quantum kNN setting, we apply Swap testin superposition with the test state and the trainstates. Roughly speaking, the way to constructthe oracle Oy,A is as follows:

1. Construct an operator F capable of thetransformation

F|j〉|0〉 = |j〉|Fj〉 (9)

for arbitrary j ∈ {0, . . . ,M − 1}. Here |Fj〉is the computational basis state which is thebinary representation of Fj . This step canbe broken down into two.

5

(a) First, we perform the transformation

Eamp|j〉|0〉 = |j〉|Ψj〉, (10)

where |Ψj〉 is a state with informationregarding Fj encoded in its amplitudes.We achieve this using the Swap test al-gorithm.

(b) We now perform the transformation

Edig|j〉|Ψj〉 = |j〉|Fj〉. (11)

This can be thought of as an analogto digital conversion as we are convert-ing the fidelity information from ampli-tudes of |Ψj〉 to a digital format |Fj〉.We use a slightly modified version of theabs-QADC algorithm to achieve this.

Thus, F = EdigEamp.

2. Consider two pairs of registers, index,fidelity; index′, fidelity′ initialised as|j〉in|0〉fid|y〉in′ |0〉fid′ .

Apply F (step 1) on each of the two pairs ofregisters.

Fin,fidFin′,fid′−−−−−−−−→ |j〉in|Fj〉fid|y〉in′ |Fy〉fid′ . (12)

where Fin,fid denotes the gate F applied onthe index and fidelity registers.

3. Now that we have our information in digitalformat, we may use a series of classical gatesto realise the function fy,A (8). Note thatany classical operation can be simulated ina quantum setting using Toffoli gates (referpage 29 of [30]). Let C denote the operatorachieving (8). Uncomputing the irrelevantregisters, we obtain

C,uncompute−−−−−−−−→ |j〉|fy,A(j)〉. (13)

For the sake of brevity, we do not furtherexpand upon the form of C, just like withother gates, here.

We now provide an explicit construction of theoracle Oy,A.

Eamp

Eamp

Edig

Edig

C

|j〉 |fy,A(j)〉

F

F

|y〉 |0〉

|j〉 |0〉

|0〉

Figure 4: A breakdown of the oracle Oy,A. In two pairsof registers correspoding to index j and y, apply F sep-arately to obtain digital encoding of Fj and Fy. Usingclassical circuits (through Toffoli gates) compare Fj andFy, and flip an ancilla qubit from |0〉 to |1〉 if Fj > Fy.Uncomputing irrelevant registers, we obtain |j〉|fy,A(j)〉.Note that in the explicit construction of the oracle, thereis an uncomputation procedure of one F coming in be-tween the required classical operations. But for simplic-ity of explaining the circuit, we have avoided it in thisdiagram.

3.3 Constructing the oracle Oy,AWe begin with the assumption that we are pro-vided with efficient circuits of state preparationoracles V,W of the form

V|0n〉 = |ψ〉, (14)W|j〉|0n〉 = |j〉|φj〉, (15)

for j ∈ {0, . . . ,M−1}. That is, we do not requirethe classical description of the test state. Insteadwe require the circuits. Efficient implementationof oracles like W is discussed in [31]. Assumingsuch oracles are provided, we now describe theconstruction of the oracle Oy,A.

1. Initialise four registers named index, train,test, B of sizes m,n, n, 1 respectively, wheren = logN and m = logM .

|j〉in|0⊗n〉tr|0⊗n〉tst|0〉B. (16)

2. Apply W on train register

Win,tr−−−−→ |j〉in|φj〉tr|0〉tst|0〉B. (17)

3. Now apply V on test register to obtain

Vtst−−→ |j〉in|φj〉tr|ψ〉tst|0〉B. (18)

6

U

m

n

n

index: |j〉W

train: |0〉

test: |0〉 V

B: |0〉 H H

Figure 5: Circuit for steps 1-4.

4. Apply the swap test circuit (sans measure-ment) between train register and test regis-ter with B as the control qubit. The state isthen

SwapTest−−−−−−→12 |j〉in

[(|φj〉tr|ψ〉tst + |ψ〉tr|φj〉tst

)|0〉B

+(|φj〉tr|ψ〉tst − |ψ〉tr|φj〉tst

)|1〉B

]≡|j〉in|Ψj〉tr,tst,B,

(19)

where we have defined

|Ψj〉 = 12[(|φj〉tr|ψ〉tst + |ψ〉tr|φj〉tst

)|0〉B

+(|φj〉tr|ψ〉tst − |ψ〉tr|φj〉tst

)|1〉B

].

(20)

Define U to be the combined unitary trans-formations of steps 3 and 4 (refer Fig. 5). Ifone now measures the register B, one wouldsee the probabilities as

Pr(B = 0) = 1 + Fj2 , (21)

Pr(B = 1) = 1− Fj2 . (22)

The information regarding fidelity is now en-coded in the amplitudes. Therefore gatesfrom steps 2-4 makes up the Eamp operatorgiven in Eq. (10). We must now convert itinto a ‘digital’ format which can be furtherutilised.

5. To this end, construct a gate

G = Utr,tst,BWin,trS0tr,tst,BW†in,trU

†tr,tst,BZB,

(23)

m

n

n

index: |j〉W† W

train: |0〉

U† S0 Utest: |0〉

B: |0〉 ZB

Figure 6: Constructing the operator G as defined inEq. (23).

where ZB denotes the application of the Zgate on register B and S0 = 1− 2|0〉〈0| (re-fer Fig. 6). This operator can be seen asthe operator G used in the abs-QADC algo-rithm [18], with the CNOT gates replacedby the train data preparation oracleW. Theaction of G on the current state can be writ-ten as controlled action of operatorsGj (referappendix F):

G|j〉in|Ψj〉tr,tst,B = |j〉in(Gj |Ψj〉tr,tst,B

),

(24)

where

Gj = Utr,tst,BSjU†tr,tst,BZB, (25)

Sj = 1− 2(|φj〉〈φj |tr ⊗ |0〉〈0|tst,B

). (26)

6. |Ψj〉tr,tst,B can be decomposed into twoeigenstates of Gj , namely |Ψj+〉 and |Ψj−〉,corresponding to the eigenvalues e±i2πθj , re-spectively. Here, sin(πθj) =

√12(1 + Fj) and

θ ∈ [1/4, 1/2) (refer Appendix G). The de-composition is given as

|Ψj〉 = −i√2

(eiπθj |Ψj+〉 − e−iπθj |Ψj−〉), (27)

7. We now have an operator G which has thefidelity value Fj stored in its eigenvalues. Toget a b-bit binary representation of θj , wenow run the quantum phase estimation al-gorithm (refer Appendix B for more detailson quantum phase estimation) on G. To thisend, we bring the phase register containingb qubits and run the phase estimation algo-rithm:

PhaseEst.−−−−−−→−i√2|j〉in

[eiπθj |θj〉ph|Ψj+〉tr,tst,B

− e−iπθj |1− θj〉ph|Ψj−〉tr,tst,B]

≡ |j〉in|Ψj,AE〉ph,tr,tst,B.(28)

7

W

U PhaseEst

on G

QA

Uncomp-

utation

|j〉

|0〉

|0〉

|0〉

|0〉

|0〉

m

n

n

b

b

Index

Test

Train

B

Phase

Fidelity

Eamp Edig

|j〉in |Fj〉fid

Figure 7: Detailed construction of the operator F defined in Eq. (9). We first use Eamp to encode the fidelity in theamplitudes. See Fig. (5) for details on U . Having the fidelity Fj encoded as amplitudes, we want to convert theminto a digital format. To this end we use Edig, which comprises of phase estimation on the operator G (Eq. (23))(refer Fig. 8 for an explicit construction), which returns a state that must undergo further arithmetics (realised usingquantum arithmetics) before it has a digital representation of Fj stored in the Fidelity register. We represent thesecircuits as QA. Finally, uncomputing every register except the Index and Fidelity registers, we have the required state.

where we have defined the combined state ofall registers except index register after esti-mation to be

|ΨjAE〉ph,tr,tst,B = −i√2

(eiπθj |θj〉ph|Ψj+〉tr,tst,B

−e−iπθj |1− θj〉ph|Ψj−〉tr,tst,B).

(29)

Here, |θj〉ph and |1− θj〉ph are b-qubit statesstoring b-bit binary representation of θj and1− θj respectively.

8. Introducing a separate register, named fid,compute Fj = 2 sin2(πθj) − 1 using quan-tum arithmetic (Appendix C). Note thatsin(πθj) = sin(π(1− θj)), and Fj is uniquelyrecovered. Then our total state is

q. arithmetics−−−−−−−−−→ |j〉in|Fj〉fid|Ψj,AE〉ph,tr,tst,B.(30)

9. Uncompute everything in registers phase,train, test and B to get

uncompute ph, tr, tst, B−−−−−−−−−−−−−−−→ |j〉in|Fj〉fid. (31)

Now, we have successfully converted the fi-delity values from amplitudes to digital for-mat. Therefore steps 5-9 makes up the oper-ator Edig given in Eq. (11). 2-9 gives the con-struction of the gate F given in Eq. (9). We

now have an operator capable of the transfor-mation |j〉|0〉 F−→ |j〉|Fj〉 for arbitrary indexj.

10. On separate registers, named index′ andfidelity′, initialised as |y〉in′ |0〉fid′ , apply F ,to obtain

F−→ |j〉in|Fj〉fid|y〉in′ |Fy〉fid′ . (32)

11. Add an extra qubit Q1 and apply the clas-sical comparison gate (refer Appendix J formore details)

J |a〉|b〉|0〉 ={|a〉|b〉|1〉 : a > b,

|a〉|b〉|0〉 : a ≤ b,(33)

on registers fid and fid′ to get the state

J−→ |j〉in|Fj〉fid|y〉in’|Fy〉fid′ |g(j)〉Q1(34)

where

g(j) ={

1 : Fj > Fy,

0 : Fj ≤ Fy,(35)

The qubit Q1 will mark all indices j suchthat Fj > Fy.

12. Uncompute the registers in′ and fid′ to ob-tain the state

uncompute in′, fid′−−−−−−−−−−−→ |j〉in|Fj〉fid|g(j)〉Q1 . (36)

8

13. Add an extra qubit Q2 and for every il ∈ A,apply the gate D(il) of the form

D(il)|j〉|0〉 ={|j〉|1〉 : j = il,

|j〉|0〉 : j 6= il.(37)

on registers index and Q2 to get the state

(D(i1)···D(ik))in,Q1−−−−−−−−−−−−→|j〉in|Fj〉fid|g(j)〉Q1 |χA(j)〉Q2 ,

(38)

where χA(j) = 1 if j ∈ A and 0 otherwise,is the indicator function of the set A. Thatis, the sequence of operators D(i1) · · ·D(ik)

marks all the indices that are already in thethreshold index set A as we would like toavoid these indices so as to not have repe-tition in our top k neighbors. These gatescan be realized using classical gates (referAppendix J for more details) by preparingthe state |il〉 in an additional m-qubit regis-ter, which we uncomputed and recycled fromstep 12, and using comparison gates.

14. Add an extra qubit Q3. Then apply an Xgate on Q2 and a Toffoli gate with controlsQ1, Q2 and target Q3. This results in thestateX,Toffoli−−−−−→|j〉in|Fj〉fid|g(j)〉Q1 |χA(j)〉Q2 |fy,A(j)〉Q3

(39)

Note that ultimately we are trying to con-struct an oracle capable of marking indicesj which have g(j) = 1 as well as χA(j) = 0.An X gate combined with a Toffoli gate willflip the target qubit if one of the input qubitsis 0 and the other is 1.

15. Uncomputing every register except indexand Q3, we have

uncompute−−−−−−−→ |j〉in|fy,A(j)〉Q3 . (40)

Since the construction of this circuit does notdepend on j, we now have an operator thatdoes the aforementioned transformation.

Oy,A|j〉|0〉 = |j〉|fy,A(j)〉. (41)

This completes the construction of the oracleOy,A (7). We may now use this oracle in thek maxima finding algorithm to find the k nearestneighbors of a test state based on fidelity.

4 Quantum k Nearest neighbors algo-rithm using dot product

We briefly discuss how one can construct a QkNNalgorithm that utilises dot product X(u, v) ≡〈u|v〉 instead of fidelity. For real-valued vectors,which is usually the case with real-world prac-tical applications, dot product is more useful.Note that for QkNN using fidelity, we are per-forming Swap test between the test state andall train states in superposition to analog-encodethe fidelity information Fj , and we then use abs-QADC algorithm to digitise this information. ForQkNN using dot product, we replace Swap testwith Hadamard test (refer Appendix D), i.e., weperform Hadamard test between the test stateand all the train states in superposition to analog-encode the dot product information Xj , and thenuse a similar modification to the real-QADC al-gorithm to digitise this information. Note thateven for complex-valued vectors |ψ〉, |φ〉, real-QADC returns the real part of the inner productRe〈ψ|φ〉, and thereby can be of use if the situa-tion agrees.

The steps of this procedure are similar toQkNN using fidelity. Therefore we refrain frompresenting it here and instead present it in fulldetail in Appendix E.

5 Complexity analysis

The fidelity based QkNN requires O(√kM) calls

to the oracle Oy,A. The oracle contains two usesof the F circuit, which is a slight modificationof the abs-QADC algorithm. Therefore the com-plexity of executing the oracle is similar to theabs-QADC algorithm. For a precision parameterε = 2−b, the circuit of the oracle contains O(1/ε)controlled V and W gates and O((log2N)/ε) sin-gle and 2 qubit gates. So, one can executethe whole fidelity based QkNN procedure withO(√kM)/ε) calls to the data preparation oracles

V and W and O(√kM(log2N)/ε) single and 2

qubit gates, O(k√kM) D(il) gates, each of which

can be realized in O(log(M)) with an extra reg-ister of logM qubits and O(

√kM) J gates, each

of which can be realized in O(b).When N >> 1/ε, QkNN algorithm will be a

better choice than other kNN algorithms that re-quire the description of the test state since read-ing the description of the state will have com-

9

plexity O(N), regardless of how their complexityis related to ε. Another approach that can by-pass the process of reading the description of thestate is by estimating the fidelity through sam-pling from measurements. For example, one cancarry out swap test between the test state andeach train state separately, estimate fidelity ineach case, and then find the k nearest neighbours.Now, in this method, since the fidelity values areestimated by sampling, to achieve an error boundof ε, the test state preparation circuit V has tobe executed O(1/ε2) times. Then, the methodwill have a query complexity which is at leastO(M/ε2). The proposed QkNN has a query com-plexity of O(

√kM/ε). So, in this scenario also,

QkNN has a potential advantage.The QkNN algorithm can be executed in

O(logM)+O(logN)+O(log 1/ε) qubits. To per-form F once, we need logM + b qubits and an-other 2 logN + b + 1 qubits which are uncom-puted at step 9. Considering the fact that theuncomputed qubits can be recycled, applying Fon two separate registers will require 2(logM +b)+2 logN+b+1 qubits. The classical operationswould require 3 qubits as well as O(logM) qubitsfor theD(il) gates and O(b) qubits for the J gates.Let kJ be the number of qubits required for the Jgates and let kD be the number of qubits requiredfor the D(il) gates. Since the 2 logN+b+1 qubitscan be recycled and after using the J gates, thestates prepared in registers in′, fid′ can be un-computed, to execute the QkNN algorithm usingfidelity measure, we require 2 logM + 2b + kJ +1+max(kD +2− logM − b, 2 logN + b−kJ). Forexample, to carry out QkNN for 30 qubit states(dimension ≈ 1 billion) with 210 train states, wewould require around 84 + 3b qubits, along withany ancilliary qubits required kD and kJ .

The dot product based QkNN has the samequery complexity as the fidleity based QkNN.To perform this operation, one would require2 logM + 2b + kJ + 1 + max(kD + 2 − logM −b, logN + b− kJ) qubits.

6 Applications

We present two scenarios where our quantumkNN algorithm can be applied. In both scenar-ios, we utilize the fact that we can classify anunknown test state.

6.1 Entanglement classification6.1.1 Entanglement classes

In this section, we discuss the entanglementclasses in pure n-partite quantum states. For sim-plicity, we restrict ourselves to n-qubit systems.We begin with n = 2 case. A pure two-qubitquantum state |Φ〉 is called separable or productstate if and only if it can be written as a ten-sor product of two pure states corresponding toindividual subsystems,

|Φ〉 = |φ1〉 ⊗ |φ2〉. (42)

If the state |Φ〉 is not of the form (42) thenits an entangled state. A pure n-qubit quantumstate |Ψ〉 is separable only if it can be written asthe tensor product of n quantum states as

|Ψ〉 = |ψ1〉 ⊗ · · · ⊗ |ψn〉. (43)

Such states are also called n-separable states [32].Equivalently, a pure state |Ψ〉 is an n-separablestate if it is separable across all the possible bi-partitions of the n qubits. If this condition isviolated, then the state is no longer n-separable.Some states can be entangled in certain biparti-tions and separable in others. Some states areentangled across all bipartitions. This motivatesa classification of n-partite quantum states on thebasis of entanglement. For two-qubit systems,there are only two classes – separable and entan-gled states.

For three-qubit systems, we have more. Let A,B, and C represent the three qubits, and let us useA-B to denote that subsystems A and B are sep-arable and AB to denote that subsystems A andB are entangled. Then the entanglement classescan be written as {A-B-C, AB-C, A-BC, AC-B, ABC}. Note that we do not distinguish be-tween W states and GHZ states defined in [33]and keep them in the same class ABC.

The same classification of the entanglement canbe extended to n number of qubits. The questionthat is relevant to us is the following: given a cir-cuit that prepares an n-qubit arbitrary state, isthere a way to label it according to its entangle-ment class. We show that the classical kNN algo-rithm can classify these states for n = 2, 3 withhigh accuracy. Furthermore, similar accuracycan be achieved by our QkNN algorithm with-out the classical description of the given quantumstate, establishing the advantage of QkNN over

10

the other kNN algorithms in settings where theinput is a circuit which prepares the test state,rather than the description of the state itself.

6.1.2 Simulation results

We present the results of simulation of estimat-ing the entanglement class of a test state |ψ〉 usingclassical kNN in Table 1 1. We use this numericalexperiment to demonstrate that quantum statesin Hilbert space have a nearest-neighbor struc-ture when it comes to entanglement. That is, thecloser the states are, the similar their entangle-ment is.

For two-qubit states, we demonstrate this forboth separable (entanglement entropy = 0) vsentangled (entanglement entropy 6= 0) and sep-arable vs maximally entangled (entanglement en-tropy = 1). We also demonstrate it for three-qubit states with a classification among the fivedifferent classes mentioned above.

6.2 Quantum state discrimination

Another application that we propose for QkNNis a problem analogous to quantum state dis-crimination [34]. The problem of quantum statediscrimination is originally formulated for arbi-trary (mixed) states and in terms of measure-ment. Consider a collection of states S ={ρ0, . . . , ρM−1} with associated probabilities p =(p0, . . . , pM−1). A state is drawn from S accord-ing to p and is prepared. The aim is to find ameasurement which maximizes the probability ofcorrectly identifying the prepared state.

We are concerned with an analogous problem.Suppose we are given a circuit that produces anunknown (pure) state |ψ〉. We are guaranteedthat |ψ〉 is one of the M known (pure) states{|φ0〉, . . . , |φM−1〉}. We also assume that we aregiven circuitW to prepare the known states. Thetask is to correctly deduce the identity of |ψ〉.

In such a scenario, one can run the QkNN al-gorithm with k = 1 to obtain the j such that|ψ〉 = |φj〉 with O(

√M) oracle calls, as the

largest value fidelity can take is 1 and F(u, v) = 1if and only if u = v.

1The simulation code can be found at https://github.com/afradnyf/QKNN.

7 ConclusionIn this paper, we have presented a novel QkNN al-gorithm, which is a quantum analog of the classi-cal kNN algorithm. We use Swap test and gener-alizations of quantum analog to digital conversionalgorithms to construct an oracle which enablesus to reduce the problem of quantum kNN to aninstance of quantum k maxima finding algorithm.We assume that state preparations circuits areprovided, and the algorithm uses fidelity as a sim-ilarity measure which is widely used in problemswhere the data is inherently quantum. The al-gorithm requires O(

√kM) calls to these circuits

to obtain the identities of the k nearest neighborsof the test out of M train states. Since the met-ric Bures distance is a monotonous function offidelity, kNN done using fidelity is in agreementwith kNN carried out using Bures distance. Wealso present a variant of the algorithm which usesdot product as distance measure.

An advantage of the proposed algorithm is itsability to classify quantum states without theirexplicit classical description in some basis. In-stead, we require circuits capable of preparingthese states. Furthermore, while dealing withquantum data, the algorithm is able to classifywithout the requirement of quantum state to-mography, which is essential if one were to useany other, quantum or classical, kNN algorithmwhich requires the classical description of states.Along with the calls to the state preparation cir-cuits being O(

√kM), all the other parts of the

circuit are efficiently preparable. The number ofqubits required is also poly-logarithmic in the di-mension of the states involved and the number oftrain states.

As an example, we show the effectiveness ofkNN method in identifying the type of entangle-ment in quantum states. For the problem of en-tanglement classification, preparing train statesof different types of entanglement is much eas-ier when we’re working with their circuits ratherthan working with their classical descriptions.Furthermore, we discuss the applicability of thealgorithm in an analogous version of quantumstate discrimination.

A particular future direction regarding the ap-plications of QkNN is to study its capability inan analogous version of quantum gate discrimi-nation [35]. This is due to the ability of QkNNto work with quantum circuits which could be

11

No. of Qubits No. of classes Entanglement classes Accuracy2 2 Separable, Entangled 99%2 2 Separable, Maximally entangled 100%3 5 1-2-3, 12-3, 1-23, 13-2, 123 89%

Table 1: Entanglement classification using classical kNN classifier. Cardinality of the set of train states is M =(number of classes)×(class size). In each case, the total number of train states used for each class is 105.

efficient representations of unitary matrices. Theproblem of identifying a test circuit among a finiteset of train circuits can potentially be addressedusing a column-wise QkNN.

8 Acknowledgements

We thank Yuan Feng, Sanjiang Li, and Christo-pher Ferrie for fruitful discussions. SKG acknowl-edges the financial support from SERB-DST (FileNo. ECR/2017/002404) and InterdisciplinaryCyber Physical Systems(ICPS) programme ofthe Department of Science and Technology,India (Grant No.:DST/ICPS/QuST/Theme-1/2019/12). The quantum circuits weregenerated using the Quantikz package [36] andMathcha [37].

References

[1] J. Biamonte, P. Wittek, N. Pancotti, P.Rebentrost, N. Wiebe, and S. Lloyd, Quan-tum machine learning - Nature 549, 195–202(2017).

[2] P. Wittek, Quantum machine learning -What quantum computing means to datamining (2017).

[3] M. Schuld, I. Sinayskiy, and F. Petruccione„An introduction to quantum machine learn-ing (2014).

[4] S. Arunachalam and R. de Wolf , ASurvey of Quantum Learning Theory -arXiv:1701.06806 (2017).

[5] A. Harrow, A. Hassidim, and S. Lloyd,Quantum Algorithm for Linear Systems ofEquations - Phys. Rev. Lett. 103, 150502(2009).

[6] N. Wiebe, D. Braun, and Seth Lloyd, Quan-tum Algorithm for Data Fitting - Phys. Rev.Lett. 109, 050505 (2012).

[7] J. Carrasquilla and R. Melko, Machine learn-ing phases of matter - Nature Phys 13,431–434 (2017).

[8] B. Wang, Learning to Detect Entanglement- arXiv:1709.03617 (2017).

[9] S. Lu, S. Huang, K. Li, J. Li, JianxinChen, Dawei Lu, Zhengfeng Ji, Yi Shen,Duanlu Zhou, and Bei Zeng, Separability-entanglement classifier via machine learning- Phys. Rev. A 98, 012315 (2018).

[10] G. Carleo, I. Cirac, K. Cranmer, L. Daudet,M. Schuld, N. Tishby, L. Vogt-Maranto,and L. Zdeborová, Machine learning andthe physical sciences - Rev. Mod. Phys. 91,045002 (2019).

[11] S. Lloyd, M. Mohseni, P. Rebentrost, Quan-tum principal component analysis - NaturePhysics, 10, 631–633 (2014).

[12] P. Rebentrost, M. Mohseni, and Seth Lloyd,Quantum Support Vector Machine for BigData Classification - Phys. Rev. Lett. 113,130503 (2014).

[13] S. Lloyd, M. Mohseni, and P. Reben-trost, Quantum algorithms for super-vised and unsupervised machine learning -arXiv:1307.0411 (2013).

[14] T. Cover and P. Hart, Nearest neighbor pat-tern classification - IEEE Transactions onInformation Theory, 13, 1, 21-27 (1967).

[15] C. Dürr, M. Heiligman, P. Hoyer, andM. Mhalla, Quantum Query Complexity ofSome Graph Problems, SIAM Journal onComputing 35, 1310-1328 (2006).

[16] K. Miyamoto, M. Iwamura, K. Kise, AQuantum Algorithm for Finding k-Minima- arXiv:1907.03315 (2019).

[17] H. Buhrman, R. Cleve, J. Watrous, and R.de Wolf, Quantum Fingerprinting - Phys.Rev. Lett. 87, 167902 (2001).

12

[18] K. Mitarai, M. Kitagawa, and K. Fujii,Quantum analog-digital conversion - Phys.Rev. A 99, 012301 (2019).

[19] S. Aaronson, The learnability of quantumstates - Proc. R. Soc. A. 4633089–3114(2007).

[20] N. Wiebe, A. Kapoor, and K. M. Svore,Quantum algorithms for nearest-neighbormethods for supervised and unsupervisedlearning - Quantum Information & Compu-tation, 15, 3-4, 316–356 (2015).

[21] Y. Ruan, x. Xue, H. Liu, J. Tan, andX. Li, Quantum Algorithm for K-NearestNeighbors Classification Based on the Metricof Hamming Distance - International Jour-nal of Theoretical Physics, 56, 3496–3507(2017).

[22] H. Chen, Y. Gao, and J. Zhang, Quantum K-nearest neighbor algorithm -Dongnan DaxueXuebao 45, 647 (2015).

[23] Y. Dang, N. Jiang, H. Hu, Z. Ji, and W.Zhang, Image classification based on quan-tum K-Nearest-Neighbor algorithm - Quan-tum Information Processing, 17, 239 (2018).

[24] M. Schuld, I. Sinayskiy, and F. Petruccione,Quantum Computing for Pattern Classifica-tion - PRICAI 2014: Trends in Artificial In-telligence, Lecture Notes in Computer Sci-ence, 8862, Springer (2014).

[25] Y. Liao and V. R. Vemuri, Use of K-NearestNeighbor classifier for intrusion detection- Computers & Security, 21, 5, 439-448(2002).

[26] J.P. Zhang and I. Mani, KNN Approachto Unbalanced Data Distributions: A CaseStudy Involving Information Extraction -Proceeding of International Conference onMachine Learning, Workshop on Learningfrom Imbalanced Data Sets, Washington DC,21 (2003).

[27] R. J. Samworth, Optimal weighted nearestneighbour classifiers - The Annals of Statis-tics, 40, 5, 2733-2763 (2012).

[28] C. Durr and P. Hoyer, A Quantum Algo-rithm for Finding the Minimum - quant-ph/9607014 (1996).

[29] M. Boyer, G. Brassard, P. Hoyer, and A.Tappa, Tight Bounds on Quantum Search-ing - Fortschritte der Physik, 46, 187-199(2005).

[30] M. A. Nielsen and I. L. Chuang, Quan-tum Computation and Quantum Informa-tion: 10th Anniversary Edition - CambridgeUniversity Press (2011).

[31] Iordanis Kerenidis, Anupam Prakash,Quantum Recommendation Systems -arXiv:1603.08675 (2016).

[32] R. Horodecki, P. Horodecki, M. Horodecki,and K. Horodecki, Quantum entanglement -Rev. Mod. Phys. 81, 865 (2009).

[33] W. Dür, G. Vidal, and J. I. Cirac, Threequbits can be entangled in two inequivalentways - Phys. Rev. A 62, 062314 (2000).

[34] J. Bae and L. Kwek, Quantum state dis-crimination and its applications - Journalof Physics A: Mathematical and Theoretical,48, 8 (2015).

[35] G. Chiribella, G. M. D’Ariano, and M. Roet-teler, Identification of a reversible quantumgate: Assessing the resources - New Journalof Physics, 15 (2013).

[36] A. Kay, Tutorial on the Quantikz Package -arXiv:1809.03842 (2018).

[37] Mathcha, Mathcha - online mathematics ed-itor, (2021).

[38] R. Cleve, A. Ekert, C. Macchiavello and M.Mosca, Quantum algorithms revisited - Proc.R. Soc. Lond. A.454339–354 (1998).

[39] L. Ruiz-Perez and J. C. Garcia-Escartin,Quantum arithmetic with the quantumFourier transform - Quantum InformationProcessing, 16, 152 (2017).

[40] D. S. Oliveira, P. B. M. de Sousa and R.V. Ramos, Quantum search algorithm usingquantum bit string comparator - 2006 In-ternational Telecommunications Symposium,582-585 (2006).

13

· · ·...

· · ·

· · ·

· · ·· · ·· · ·· · ·

n

n

m

phase register |0⊗b〉

H

IQFTH

H

B |0〉

G G2 G2b−1Test |0〉Train |0〉Index |j〉

Figure 8: Quantum phase estimation on an operator G. This is the innards of the ‘PhaseEst on G’ operator in Fig. 7.

A Quantum Analog-Digital Conversion algorithms

Mitarai et al. describes a set of algorithms [18] to carry out analog-digital conversions within a quantumcircuit. Let U |0〉 =

∑d−1i=0 ci|i〉 be an arbitrary quantum state.

Definition 1. abs-QADC [18] Let r̃j denote the m-bit string r̃j1, r̃j2, . . . , r̃j

m that best approximates

|cj | bym∑k=1

r̃jk2−k. An m-bit abs-QADC operation transforms analog-encoded state

N∑j=1

cj |j〉|0〉⊗m to

1√N

N∑j=1|j〉|r̃j〉.

Definition 2. real-QADC [18] Let x̃j denote the m-bit string x̃j1, x̃j2, . . . , x̃j

m that best approximatesthe real part of cj by

m∑k=1

x̃jk2−k. An m-bit real-QADC operation transforms analog-encoded state

N∑j=1

cj |j〉|0〉⊗m to 1√N

N∑j=1|j〉|x̃j〉.

Theorem 1. abs-QADC [18] There exists an m-bit abs-QADC algorithm that runs using O(1/ε)controlled-U gates and O((log2N)/ε) single and two qubit gates with output state fidelity (1 −O(poly(ε))), where ε = 2−m.

Theorem 2. real-QADC [18] There exists an m-bit real-QADC algorithm that runs using O(1/ε)controlled-U gates and O((log2N)/ε) single and two qubit gates with output state fidelity (1 −O(poly(ε))), where ε = 2−m.

B Quantum Phase Estimation

Quantum phase estimation is a quantum procedure that can be used to estimate the phase of theeigenvalue of a given eigenvector of a unitary operator. It relies on the quantum Fourier transform andis the engine behind some of the most popular quantum algorithms such as the Shor’s algorithm forfactoring.

Theorem 3. [38] Let U be a unitary operator acting on M-qubit Hilbert space with eigenstates{|ψj〉}2

M

j=1 and corresponding eigenvalues {e2πiφj}2M

j=1 where φj ∈ [0, 1). Let ε = 2−m for some pos-itive integer m. There exists a quantum algorithm, which consists of O(1/ε) controlled-U calls and

14

ndata: |0〉 V V† W

B: |0〉 H H

Figure 9: Circuit for Hadamard test.

O(log2(1/ε)) single and two-qubit gates, that performs transformation2M∑j=1

aj |ψj〉|0〉⊗m → |ψPE〉 =

2M∑j=1

aj |ψj〉|φ̃J〉 where |φ̃J〉 denotes a bitstring φ̃J(1)φ̃J

(2). . . φ̃J

(m) such that∣∣∣ m∑k=1

φ̃J(k)2−k−φJ

∣∣∣ ≤ ε forall j with state fidelity at least 1− poly(ε).

C Quantum arithmeticsWithin a quantum circuit, one can always apply simple arithmetic functions such as additions, multi-plication, trginometric functions, exponentiation, etc. This is explained using the following theorems.

Theorem 4. [39] Let a,b be m-bit strings. There exists a quantum algorithm that performs trans-formation |a〉|b〉 → |a〉|a + b〉 with O(poly(m)) single and two qubit gates.

Using this quantum adder, one can construct a circuit capable of carrying out any basic function ina similar manner in a quantum circuit.

Theorem 5. [18] Some basic functions such as inverse, trigonometric functions, square root, and in-verse trigonometric functions can be calculated to accuracy ε, that is, we can perform a transformation|a〉|0〉 → |a〉| ˜f(a)〉 such that |f(a)− ˜f(a)| ≤ ε where f is the required function using O(poly(log(1/ε)))quantum arithmetics.

D Hadamard TestThe Hadamard test is a quantum circuit that can be used to compute the real part or the imaginarypart of the inner product between two quantum states |u〉, |v〉 ∈ Cn. Since, we are more interestedin the real part of the inner product, we will be using and explaining that particular version of theHadamard test.

Let U |0〉 = |u〉 and V |0〉 = |v〉. The aim is to use U and V in a controlled manner to construct thestate

12[|0〉(|u〉+ |v〉

)+ |1〉

(|u〉 − |v〉

)]. (44)

The detailed circuit is shown in Fig. 9. The real part of the inner product can be estimated frommeasuring the first qubit as

Pr(0) = 12 + 1

2Re(〈u|v〉) and Pr(1) = 12 −

12Re(〈u|v〉). (45)

The quantity Pr(0)− Pr(1) gives us the desired real part of the inner product.

E Quantum k Nearest neighbors algorithm using dot productA more general distance measure used in kNN problems is the dot product. Let H be the n-qubitHilbert space and let |v〉 ∈ H be the real valued test state whose label is to be determined. Let{|ui〉 : i ∈ {0, . . . ,M − 1}} ⊂ H be a collection of M known real valued train states whose labels are

15

known to us. Let Xi ≡ X(v, ui) = 〈v|ui〉 be the dot product between the test state |v〉 and the ith

train state |ui〉 andX = [X0, . . . , XM−1] (46)

be a table of lengthM containing the dot product values with the test state |v〉 and all the train states{|ui〉}. The general approach adopted here is very similar to fidelity based QkNN. One can see thatthe problem of QkNN using dot product is an instance of the quantum k maxima finding algorithmon the set X given in Eq. (46). Similar to QkNN using fidelity, the problem boils down to being ableto perform quantum search on the Boolean function

fy,A(j) ={

1 : Xj > Xy and j /∈ A,0 : otherwise.

(47)

That is, we should be able to prepare the oracle

Oy,A|j〉|0〉 ={|j〉|1〉 : Xj > Xy and j /∈ A,|j〉|0〉 : otherwise.

(48)

The assumptions here are that we are provided with state preparation oracles V,W of the form

|0n〉 V−→ |v〉, (49)

|j〉|0n〉 W−→ |j〉|uj〉, (50)

for j ∈ {0, . . . ,M − 1}. The construction of the oracle is based on the real-QADC circuit from [18]. Inthat circuit, to compute the real values of the coordinates of the state, we apply the Hadamard test insuperposition with the state and standard basis vectors. In the quantum kNN setting, we apply theHadamard test in superposition with the test state and the train states. We now show the correctnessof such a protocol and a method to build the required oracle Oy using it. The explicit construction ofthe oracle is as follows:

1. Initialise three registers named index, data, B of sizes m,n, 1 respectively, where n = logN andm = logM

|j〉in|0n〉data|0〉B. (51)

2. Apply V on the data registerV−→ |j〉in|v〉data|0〉B. (52)

3. Perform Hadamrd test to obtain the state

Hadamard Test−−−−−−−−−→12 |j〉in

[(|v〉data + |uj〉data

)|0〉B +

(|v〉data − |uj〉data

)|1〉B

]≡|j〉in|Ψj〉data,B,

(53)

where,

|Ψj〉data,B = 12[(|v〉data + |uj〉data

)|0〉B +

(|v〉data − |uj〉data

)|1〉B

]. (54)

Define V to be the combined unitary transformations of steps 2 and 3. If one now measures theB register, one would see the probabilities as

Pr(B = 0) = 1 +Xj

2 and Pr(B = 1) = 1−Xj

2 . (55)

The information regarding dot product is now encoded in the amplitudes. We must now convertit into a ‘digital’ format which can be further utilised.

16

4. Construct a gateH = Vin,data,B S0data,B V †in,data,B ZB, (56)

where S0 = 1− 2|0〉〈0|. This H gate can be seen as the H gate in the real QADC algorithm [18],with the controlled CNOT gate replaced by controlled W. The action of H on the current statecan be written as controlled action of operators Hj (refer Appendix H):

H|j〉in|Ψj〉data,B = |j〉in(Hj |Ψj〉data,B

), (57)

whereHj =

(1− 2|Ψj〉〈Ψj |data,B

)ZB. (58)

5. |Ψj〉tr,tst,B can be decomposed into two eigenstates of Hj , namely |Ψj+〉 and |Ψj−〉, correspondingto the eigenvalues e±i2πθj , respectively. Here, sin(πθj) =

√12(1 +Xj) and θ ∈ [1/4, 1/2) (refer

Appendix I). The decomposition is given as

|Ψj〉 = −i√2

(eiπθj |Ψj+〉 − e−iπθj |Ψj−〉), (59)

6. The operator H has the dot product values {Xj} stored in its eigenvalues. To get a b-bit binaryrepresentation of θj , we now run the phase estimation algorithm on H. To this end, we bring thephase register containing b qubits and run the phase estimation algorithm:

PhaseEst.−−−−−−→−i√2|j〉in

[eiπθj |θj〉ph|Ψj+〉data,B − e−iπθj |1− θj〉ph|Ψj−〉data,B

]≡ |j〉in|Ψj,AE〉ph,data,B,

(60)

where we have defined the combined state of all registers except index register after estimation tobe

|Ψj,AE〉ph,data,B = −i√2

(eiπθj |θj〉ph|Ψj+〉data,B − e−iπθj |1− θj〉ph|Ψj−〉data,B

). (61)

Here, |θj〉ph and |1 − θj〉ph are b-qubit states storing b-bit binary representation of θj and 1 − θjrespectively.

7. Introducing a register, dp, compute Xj = 2 sin2(πθj)−1 using quantum arithmetic from AppendixC. Note that sin(πθj) = sin(π(1− θj)), and Xj is uniquely recovered. Then our total state is

quantum arithmetics−−−−−−−−−−−−−→ |j〉in|Xj〉dp|Ψj,AE〉ph,data,B. (62)

8. Uncompute everything in registers ph,data and B to get

uncompute ph, data, B−−−−−−−−−−−−−−→ |j〉in|Xj〉dp. (63)

We have successfully converted the dot product values from amplitudes to digital format.

9. Add more registers and apply X , so that we have

X−→ |j〉in|Xj〉dp|y〉in′ |Xy〉dp′ (64)

10. Add an extra qubit Q1 and apply the gate J defined in Eq. (11) on registers dp and dp′ to getthe state

J−→ |j〉in|Xj〉dp|y〉in′ |Xy〉dp′ |g(j)〉Q1(65)

where

g(j) ={

1 : Xj > Xy,

0 : Xj ≤ Xy,(66)

17

11. Uncompute the registers in′ and dp′ to obtain the state

uncompute in′, dp′−−−−−−−−−−−→ |j〉in|Xj〉dp|g(j)〉Q1 . (67)

12. Add an extra qubit Q2 and for every il ∈ A, apply the gate D(il) defined in Eq. (13) on registersindex and Q2 to get the state

(D(i1)···D(ik))in,Q1−−−−−−−−−−−−→ |j〉in|Xj〉dp|g(j)〉Q1 |χA(j)〉Q2 ,(68)

where χA(j) = 1 if j ∈ A and 0 otherwise, is the indicator function of the set A.

13. Add an extra qubit Q3. Then apply an X gate on Q2 and a Toffoli gate with controls Q1, Q2 andtarget Q3. This results in the state

X,Toffoli−−−−−→ |j〉in|Xj〉dp|g(j)〉Q1 |χA(j)〉Q2 |fy,A(j)〉Q3 , (69)

where fy,A is the Boolean function defined in Eq. (E).

14. Uncomputing every register except index and Q3, we have

uncompute−−−−−−−→ |j〉in|fy,A(j)〉Q3 , (70)

which is the required oracleOy,A|j〉|0〉 = |j〉|fy,A(j)〉. (71)

This completes the construction of the oracle given in (48). We may now use this oracle in the kmaxima finding algorithm to find the k nearest neighbors of a test state based on dot product.

F Action of G as controlled Gj for the Fidelity based QkNN

Recall that

G = Utr,tst,BWin,trS0tr,tst,BW†in,trU

†tr,tst,BZB, (72)

Gj = Utr,tst,BSjU†tr,tst,B (73)

where

S0 = 1− 2|0〉〈0|tst, tr,B, (74)Sj = 1− 2|0〉〈0|tst,B ⊗ |φk〉〈φk|. (75)

To show the equivalence between G and controlled Gk, it suffices to show the equivalence betweenWS0W† and Sk. Recall that we introduced Wind, tr for the preparation of train states. The action ofW is given by

Wind, tr|i〉ind|0〉tr = |i〉ind|φi〉tr. (76)

We have

1in ⊗ S0 = 1− 2M−1∑k=0|k, 0, 0, 0〉〈k, 0, 0, 0|in,tst,tr,B, (77)

18

and therefore

WS0W† =Wind,tr

(1− 2

M−1∑k=0|k, 0, 0, 0〉〈k, 0, 0, 0|in, tst, tr,B

)W†ind,tr

= 1− 2M−1∑k=0W|k, 0, 0, 0〉〈k, 0, 0, 0|W†

= 1− 2∑k

|k〉|φk〉|0, 0〉〈k|〈φk|〈0, 0|

=∑k

|k〉〈k|in ⊗ 1tst,tr,B − 2∑k

|k〉〈k|in ⊗ |φk〉〈φk| ⊗ |0〉〈0|tst,B

(78)

This is

WS0W† =∑k

|k〉〈k| ⊗(1− 2|0〉〈0|tst, B ⊗ |φk〉〈φk|tr

)=∑k

|k〉〈k|in ⊗ Sktst, tr,B (79)

as required.

G Eigenvectors of Gj for the fidelity based QkNN

Recall that Gj = USjU†ZB and U |0〉tst|φjtr〉|0〉B = |Ψj〉 This implies,

USjU† = 1− 2|Ψj〉〈Ψj |. (80)

Let

|Ψj0〉 = 12αj

(|ψ〉tr|φj〉tst + |φj〉tr|ψ〉tst

)|0〉B and |Ψj1〉 = 1

2βj

(|ψ〉tr|φj〉tst − |φj〉tr|ψ〉tst

)|1〉B. (81)

where,

αj =√

12(1 + Fj) and βj =

√12(1− Fj). (82)

Therefore we have,|Ψj〉 = αj |Ψj0〉+ βj |Ψj1〉. (83)

Consider the subspace H = span(|Ψj0〉, |Ψj1〉

). We have

ZB∣∣∣H

= |Ψj0〉〈Ψj0| − |Ψj1〉〈Ψj1|, (84)

and

USjU†∣∣∣H

= (1− 2α2j )|Ψj0〉〈Ψj0|+ (1− 2β2

j )|Ψj1〉〈Ψj1| − 2αkβk(|Ψj1〉〈Ψj0|+ |Ψj0〉〈Ψj1|). (85)

This implies that

Gj∣∣∣H

= USjU†ZB

∣∣∣H

= (1− 2α2j )|Ψj0〉〈Ψj0| − (1− 2β2

j )|Ψj1〉〈Ψj1| − 2αkβk(|Ψj1〉〈Ψj0| − |Ψj0〉〈Ψj1|).(86)

We can see that this Gj∣∣∣H

has the same structure as the analogous operator in [18]. By using thesame proof of correctness of abs-QADC in [18] we can see that each |Ψj〉tr,tst,B can be decomposedinto two eigenstates of Gj . Let αj = sin(πθj) for θj ∈ [1

4 ,12). Substituting this in Eq. (86), we get

Gj∣∣∣H

= cos(2πθj)(|Ψj0〉〈Ψj0|+ |Ψj1〉〈Ψj1|)− sin(2πθj)(|Ψj1〉〈Ψj0| − |Ψj0〉〈Ψj1|). (87)

19

We may write Gj∣∣∣H

as

Gj∣∣∣H

=[cos(2πθj) sin(2πθj)−sin(2πθj) cos(2πθj)

]. (88)

This implies that Gj has eigenvectors

|Ψj±〉 = 1√2

(|Ψj0〉 ± i|Ψj1〉), (89)

with eigenvalues Ψ± = e±i2πθj respectively. Now, we can decompose |Ψj〉 as

|Ψj〉 = −i√2

(eiπθj |Ψj+〉 − e−iπθj |Ψj−〉), (90)

as required.

H Action of H as controlled Hj for the dot product based QkNNRecall that

H = Vin, data,B S0data,B V†in, data,B ZB,

Hj = (1− 2|Ψj〉〈Ψj |data,B)ZB(91)

Then,

H = Vin,data,B(1in,data,B − 1in ⊗ 2|0〉〈0|data,B

)V †in,data,B

(1in,data ⊗ ZB

)=(1in,data,B −

M−1∑k=0

2|k〉〈k|in ⊗ |Ψ〉〈Ψ|data,B)(1in,data ⊗ ZB

)

=M−1∑k=0|k〉〈k|in ⊗

(1data,B − 2|Ψj〉〈Ψj |data,B

)(1in,data ⊗ ZB

)

=M−1∑k=0|k〉〈k|in ⊗Hkdata,B.

(92)

I Eigenvectors of Hj for the dot product based QkNN

Recall that Hj =(1− 2|Ψj〉〈Ψj |

)ZB. Let

|Ψj0〉 = 12αj

(|v〉data + |uj〉data

)|0〉B and |Ψj1〉 = 1

2βj

(|v〉data − |uj〉data

)|1〉B, (93)

where,

αj =√

12(1 +Xj) and βj =

√12(1−Xj). (94)

So we may write,|Ψj〉 = αj |Ψj0〉+ βj |Ψj1〉. (95)

Consider the subspace H = span(|Ψj0〉, |Ψj1〉

). Then

ZB∣∣∣H

= |Ψj0〉〈Ψj0| − |Ψj1〉〈Ψj1|. (96)

20

|a1〉U>|b1〉

|0〉

|0〉 X

|a2〉U>|b2〉

|0〉

|0〉 X

|a3〉U>|b3〉

|0〉

Figure 10: Circuit for implementing J gate for 3 qubit bit strings. Note that by replacing the U> with U6= gates, wecan implement D(i) gates.

|a〉|b〉

|0〉

(a) Circuit for U6=

|a〉

|b〉 X X

|0〉

(b) Circuit for U>.

Figure 11: Circuits used in bitstring comparison.

We have(1−2|Ψj〉〈Ψj |

)∣∣∣H

= (1−2α2j )|Ψj0〉〈Ψj0|+(1−2β2

j )|Ψj1〉〈Ψj1|−2αkβk(|Ψj1〉〈Ψj0|+ |Ψj0〉〈Ψj1|). (97)

This implies

Hj

∣∣∣H

= (1− 2α2j )|Ψj0〉〈Ψj0| − (1− 2β2

j )|Ψj1〉〈Ψj1| − 2αkβk(|Ψj1〉〈Ψj0| − |Ψj0〉〈Ψj1|). (98)

The result immediately follows by comparing Eq. (98) with Eq. (86).

J Bit-string comparisonIn this section, we give a basic example of a circuit capable of comparing two-qubit bit string states.The J gate, mentioned in Eq. (11), for 1 qubit bit strings, can be implemented by the U> gate givenin Fig. 11b.

Using U> as a subroutine, in Fig. 10 we give an example of a circuit capable of comparing 3-qubitbit strings |a〉 = |a1〉|a3〉|a3〉 and |b〉 = |b1〉|b2〉|b3〉.

This circuit compares the bits from left to right using the U> gate, that is, first a3 and b3 arecompared, then a2 and b2 and so on. At any point, if U> returns 1, then the final output will be 1.

For executing the D(i) used in Eq. (13), we replace the U> gate with the U6= gate given in Fig. 11a.This gate returns 1 if the input bits are unequal. By using U6= in the circuit U , we can check, from leftto right, if each pair of qubits are unequal or not. At any point, if we see an unequal pair of qubits, thecircuit outputs 1, else 0. Different versions of these circuits and extension to n qubits are explained in[40].

21