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.mcx(qr, aux[0])
qc.mcx(qr, aux[1])

return qc

Today, we will expand this algorithm to numbers of an arbitrary number of digits, therefore, of any size.



Frank Zickert | Quantum Machine Learning

You're interested in quantum computing and machine learning. But you don't know how to get started? Let me help