Interested in learning how to program quantum computers? Then check out our Qiskit textbook Introduction to Quantum Computing with Qiskit.
Introduction
In this tutorial we will see how to construct an integer comparator in Qiskit for IBM quantum computers.
What is an integer comparator?
An integer comparator is a circuit that compares two integers. In Qiskit the integer comparator circuit relies on two’s complement implementation of binary subtraction.
Implementation
In this section we will go through the full implementation step by step.
Step 1: Import modules
The first step is to import the necessary modules as seen below:
from qiskit import QuantumRegister, ClassicalRegister from qiskit import QuantumCircuit, execute from qiskit.circuit.library import IntegerComparator from qiskit import Aer
Step 2: Initialise the Backend
The next step is to intialise the backend device. Since this is only a tutorial we will use the Aer unitary simulator:
backend = Aer.get_backend('aer_simulator')
Step 3: Create the main circuit
The next step is to create the circuit.
For our quantum register we will only require 4 qubits and for the classical register we will only require 1 bit.
q = QuantumRegister(4,'q') c = ClassicalRegister(1,'c') circuit = QuantumCircuit(q,c)
Step 4: Set the value to be compared and initialize the comparator circuit
The next step is to set the comparison value that we wish to compare to the input value. For this tutorial we will set the comparison value to 3 as seen on the first line in the code below.
The second line is where we construct the comparator circuit where:
num_state_qubits is the number of qubits that will hold the integer
value is the integer we wish compare the register value against.
geq: A boolean flag that will check if the input value is >= to the comparison value when set to true. Otherwise will check if the value is < comparison value when set to false
a = 3 comparator = IntegerComparator(num_state_qubits=2, value=a, geq=True)
Step 5: Encode the input value on to the input qubits
Here we encode the binary value ‘11’ on to the input register (or 3 in decimal). This is done by applying Pauli-X gates to qubits 0 and 1. This will flip their states from |0〉 to |1〉.
circuit.x(q[0]) circuit.x(q[1])
Step 6: Apply the comparator and measure the qubits
The next step is to apply the comparator to our circuit and measure the output qubit. The first line applies the comparator while the second line measures the output qubit (in this case qubit 2).
circuit = circuit.compose(comparator) circuit.measure(q[2],c[0])
Step 7: Run the job and retrieve the results
The last step is to run the job and retrieve the results.
The first line executes the job that will be sent to the backend device where:
circuit: Our constructed circuit
backend: Our backend device (in our case a local Aer simulator)
shots: The number of times we wish to run the circuit
The second line gets our measurements and the third line prints them.
job = execute(circuit, backend, shots=1) counts = job.result().get_counts() print(counts)
Code
from qiskit import QuantumRegister, ClassicalRegister from qiskit import QuantumCircuit, execute from qiskit.circuit.library import IntegerComparator from qiskit import Aer backend = Aer.get_backend('aer_simulator') q = QuantumRegister(4,'q') c = ClassicalRegister(1,'c') circuit = QuantumCircuit(q,c) a = 3 comparator = IntegerComparator(num_state_qubits=2, value=a, geq=True) circuit.x(q[0]) circuit.x(q[1]) circuit = circuit.compose(comparator) circuit.measure(q[2],c[0]) print(circuit) job = execute(circuit, backend, shots=1) counts = job.result().get_counts() print("Comparing if qubit register is greater or equal to integer:",a) print(counts)