This example will only work on the emulator backend
What does it do?
Classification is the act of assigning labels to data, often with respect to other data. For instance, we can classify between animals having or legs or a different number. Often we classify new data based on already classified (learned) data. We classify this new data point by representing all data points by features or characteristics and compute the distance between the features of the new data point and the features of the learned data points. The new data point is assigned the label to which it has the shortest distance.
This is a typical example of -nearest neighbor classification, which classically has a complexity of , with the number of data points and the number of features. A quantum version of this classifier was proposed by Schuld et al. , see ref.1, and has a constant complexity .
What is it used for?
Classification has wide-spread use in everyday life and we often classify objects without fully realizing we do it. Classically, naively we would have to compute the distance between the new data point and all other data points separately, after which we combine the results. This approach can be approved upon, but we are still left with a complexity of .
With a quantum-based approach, we do the same computations, but now only requiring one single-qubit operation and two measurements. Hence, the quantum-based approach is in theory better than the classical one. A caveat is however that the result is probabilistic, hence, multiple measurement rounds must be needed. Furthermore, a specific quantum state is assumed to begin with. Without something like a quantum RAM or a quantum process that produces the input state efficiently, we are left with the same complexity as the classical approach.
How does it work?
Let our data points be given by , with the data point and the corresponding label and let be the unlabeled new data point. Furthermore, there are only two labels . The new label is then given by evaluating
If both classes have the same number of data points, we can simplify the above function to
We can create a quantum state that will produce precisely this probability distribution, if we start with
Here encodes the data points and . Furthermore, is the -th computational basis state to keep track of the -th training point and the second register is an ancilla qubit.
Given this initial state, the quantum algorithm consists of only three operations. First, we apply a Hadamard on the second register. This gives
We then measure the second register and only conditionally continue if we measured the -state. This gives
with the probability of obtaining after measuring the second register and the -th feature of the -th data point.
Now measuring the last register which encodes the label gives the probability of each class
Note that the two measurement rounds can be combined by classically post-processing the results.
Examination of the code
Let us consider a simple implementation of the above quantum distance-based classifier. As we have to quantum RAM, we will explicitly prepare the quantum state. Furthermore, we will consider only two data points, each with two features.
For normalized and standardized data, we can assume without loss of generality that
for specific angles , and .
The code is then given by
version 1.0 qubits 4 prep_z q[0:3] H q[0:1] #e ncode the new data point, this implements a cRy(omega) CNOT q, q Ry q, -omega/2 CNOT q, q Ry q, omega/2 X q # encode the first data point, this implements a ccRy(theta) toffoli q,q,q CNOT q,q Ry q, theta/4 CNOT q,q Ry q, -theta/4 toffoli q,q,q CNOT q,q Ry q, -theta/4 CNOT q,q Ry q, theta/4 X q # encode the second data point, this implements a ccRy(phi) toffoli q,q,q CNOT q,q Ry q, phi/4 CNOT q,q Ry q, -phi/4 toffoli q,q,q CNOT q,q Ry q, -phi/4 CNOT q,q Ry q, phi/4 # encode the labels CNOT q, q # The actual algorithm H q measure_z q[1,3]
Here we see the results of the execution for angles , and . Note that we must discard the measurement results where the measurement of the second qubit gives . Hence, we see that , as we would expect.
Want to know more?
For more information see the article by Schuld et al., the notebooks on this algorithm on our GitHub or this article , where the practicalities of the state preparation are given.
 Implementing a distance-based classifier with a quantum interference circuit, M. Schuld, M. Fingerhuth, and F. Petruccione, EPL (Europhysics Letters), Volume 119, Number 6, 2017
: R. Wezeman, N. Neumann, and F. Phillipson. “Distance-based classifier on the Quantum Inspire”. In: Digitale Welt4.1 (Jan. 2020), pp. 85–91