Comparing Two Numbers Using A Quantum Algorithm
Quantum computing doesn’t need to be complicated
Do you want to get started with Quantum Machine Learning? Have a look at Hands-On Quantum Machine Learning With Python.
You have two integers! Write a quantum algorithm that tells you which one is greater.
In the previous post, we solved this problem for single-digit binary numbers.
We introduced the bit_compare
function that compares two bits and tells us which one is greater by flipping one of two auxiliary qubits that correspond to the according qubit to one. If both numbers are equal, none of the auxiliary qubits is flipped to one.
The following listing provides the source code of this circuit.
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer
from qiskit.visualization import plot_histogram
def bit_compare():
qr = QuantumRegister(2, "bits")
aux = QuantumRegister(2, "aux")
qc = QuantumCircuit(qr, aux)
qc.x(qr[1])
qc.mcx(qr, aux[0])
qc.x(qr[0])
qc.x(qr[1])
qc.mcx(qr, aux[1])
qc.x(qr[0])
return qc
Today, we will expand this algorithm to numbers of an arbitrary number of digits, therefore, of any size.