‫Let's connect the quantum KNN algorithm to the math we studied.
‫These two lines initialize the state vector
‫with amplitude encoding.
‫Observe that the state vector is correctly normalized.
‫We have divided each data value by 2.
‫This is the amplitude encoded state vector
‫we are using to initialize the qubits.
‫We had studied this table in an earlier lesson.
‫This is the circuit.
‫The hadamard is nominally applied on qubit 3,
‫but it affects all the qubits because the qubits are entangled.
‫We measure the qubit 3, and store the measured value in classical bit 0.
‫Then we measure qubit 0, and store the measured value in classical bit 1.
‫We apply the hadamard and then save the state vector
‫with a name of "v1".
‫The saved state vector will be printed out later.
‫Saving the state vector can help with debugging.
‫This is the state vector after applying the hadamard gate on qubit3.
‫This loop runs the circuit 10,000 times.
‫This IF statement checks the values of the classical bits.
‫This is classical bit 0.
‫This is classical bit 1.
‫By checking for "00" or "10",
‫we are actually checking if the classical bit 0 has the value 0.
‫Recall that qubit 3 was measured and the value was stored in classical bit 0.
‫So this IF is checking if qubit 3 was measured to be 0.
‫If not, then there is no ELSE clause in this IF statement.
‫That is, we proceed with collecting statistics only if qubit 3 was measured to be 0.
‫Otherwise we discard the results.
‫This is why the part of the state vector where qubit 3 is 1 gets set to 0.
‫This was the trick we discussed in the earlier math lesson.
‫The statistics for numerator and denominator we collect here
‫is used to compute the probability of qubit0
‫being 1.
‫Recall that qubit0 was measured and its results will be in classical bit 1.
‫The results of the quantum computation are printed out here.
‫These are the intermediate values of the state vectors.
‫If you want to compare the math expressions we discussed in an earlier lesson
‫with this state vector, remember that these numbers are normalized.
‫You will need to normalize the math expressions to compare.
‫Another thing to observe is that we are working with real numbers
‫in the state vector.
‫Qiskit prints out some imaginary quantities here,
‫but these are only rounding errors.
‫10 raised to the power of minus 17 is essentially 0.
‫Ignore the imaginary parts of these complex numbers.
‫Other quantum machine learning algorithms are more complex.
‫Usually only quantum ML researchers
‫will want to analyze quantum machine learning algorithms with math.
‫Practitioners will be able to benefit from quantum machine learning
‫by treating algorithms as black boxes.
‫If you feel uncomfortable with the idea of working with QML algorithms
‫without analyzing their math, remember that even in classical machine learning,
‫some complex algorithms such as support vector machines are used as black boxes.
‫The math for the kernel trick is complex,
‫so most practitioners use SVMs as black boxes.
‫If you still want analyze quantum machine learning algorithms,
‫then you will need to know all the math discussed in the follow-on course QC201.
‫The following lessons will explain how to use a quantum support vector machine
‫as a black-box implementation.