‫In this lesson we will study the code for the quantum machine learning algorithm.
‫This code is exactly equivalent to the classical algorithm.
‫The difference is that this will execute on a quantum computer.
‫We have already imported the necessary Qiskit libraries
‫at the beginning of the code.
‫This line gets us a simulator for running our quantum code.
‫Next, we create a quantum circuit with 4 qubits and 2 classical bits.
‫Why do we need 4 qubits?
‫That will be explained in a later lesson on the math analysis of this algorithm.
‫These two lines set up the initial state of the qubits.
‫This is the state vector of all 4 qubits.
‫Observe that the training data and the query data are both being loaded into the state vector.
‫Where are the labels that indicate how the training data was resolved?
‫Shouldn't there be a 1 to label a dispute
‫that was resolved in favor of the passenger
‫and a 0 to label data that was resolved in favor of the driver?
‫Where are those labels?
‫The labels are encoded in these 0s you see in the state vector.
‫I will explain the math in a later lesson.
‫At that time you will understand how the labels are encoded.
‫Next, we apply a Hadamard gate on qubit 3.
‫Observe that the state vector can be entangled.
‫So applying a gate on qubit 3 will affect the entire state vector, not just that of qubit 3.
‫This line helps us examine the state vector immediately after the hadamard gate.
‫After that we measure qubit 3 and store the result in classical bit 0.
‫And then we capture the state vector again.
‫Then we measure qubit 0 and store the result in classical bit 1.
‫We prepare the code to run on the simulator.
‫We will collect statistics using these two variables.
‫As the names indicate, the numerator and denominator
‫will be part of a fraction.
‫The next block of code runs the simulator 10,000 times to obtain statistics.
‫If the result of measuring qubit 3
‫which is stored in classical bit 0
‫is '0', then we increment denominator.
‫If the result of measuring qubit 3
‫was not 0, then we discard that result.
‫If in addition to qubit 3 being measured to be 0,
‫the measurement of qubit 0 was 1,
‫then we increment the numerator.
‫The result of the quantum algorithm is the fraction numerator/denominator.
‫We run the code and observe
‫that the results of the classical algorithm
‫and the quantum algorithm are similar.
‫These are the state vectors after the hadamard gate
‫and after the measurement of qubit 3.
‫We will refer back to these state vectors when we study the math
‫in a later lesson.