Finding the Smallest Number of an Array with Quantum Computer
Photo by Nick Hillier on Unsplash

One of the fundamental problems of Computer Science algorithms is to find the smallest or largest number (Real) from an array of different elements. There are a lot of classical approaches which are available these days and also provides enormous power to sorting methods (like merge sort, bubble sort, quick sort etc). Now the question is, can we solve the same problem with a Quantum computer available these days. Our aim here is to provide a solution to the problem of finding the smallest or largest number from an array of real numbers. In this article, we’ll try to formulate the problem suitable for a quantum processing unit, then discuss the implementation on a real quantum processor and then finally discuss the results and conclusions.

The complete article is structured like this -

* Problem definition

* Ising model

* Quantum formulation of the problem

* Algorithm description

* Experiment

* Result & Analysis

* Conclusions

Problem Definition

Let us assume we have an array of 3 real numbers. i.e N = [17,3,49]. Now we can easily say that the 2nd element of the array is the smallest number we have in the array, and that is 3. Now we’ll take another bit string (bit array) X of the same length. The mapping will be like,

N = [17,3,49] — — — — — — — — — — — — — - — → X₀ = [1,0,0] N = [17,3,49] — — — — — — — — — — —— — — - → X₁ = [0,1,0] N = [17,3,49] — — — — — — — — — — —— — — - → X₂ = [0,0,1]

Here 2³=8 bit-strings are possible for a 3-bit array and only 3 arrays map the elements of N to 3 distinct numbers. Here the bit-array X₁ will be our required answer, as it maps to the 2nd element of N, which is 3 and it’s the smallest among others numbers present in the array N.

Other bits arrays like [1,1,0], [1,1,1], [0,1,1], [1,0,1] and [0,0,0] are of no direct importance as they do not map to any distinct number of our primary array N.

So now we know, if we have an array of “n” elements and we are interested in finding the smallest element of the array, then we will try to find the best bit-string or bit-array Xᵢ among 2ⁿ possible bit-arrays (without bothering about (2ⁿ-n) bit-arrays), which maps itself to any distinct element of the “n” elements.

Quantum Formulation of the Problem

Here we will try to reformulate the problem in the form of a Combinatorial Optimisation problem. We know that quantum computers are good at solving combinatorial optimisation problems, and we can solve this with Quantum Approximate Optimisation Algorithm (QAOA), Variational Quantum Eigensolver (VQE) or Quantum annealing. To convert our problem of interest into a combinatorial optimisation problem we have to write the hamiltonian in the form of Ising type. (For more details on the Ising model click here)

The Ising Model

Ising model is a classical model of magnetism given by Lenz in the year 1920, and later in 1925, it was solved by Ernst Ising (1-dimensional problem). This mathematical model incorporates the nearest neighbour interaction between atomic spin variables (sᵢ ∈ [-1,+1]). The general form of Ising hamiltonian is,

Figure 1: The general form of Ising hamiltonian

where hᵢ is the bias and Jᵢⱼ is the coupling strength. The sᵢ and sⱼ are the interacting spin variables in the material. This hamiltonian can be converted into a square symmetric matrix called QUBO (Quadratic Unconstriant Optimisation problem). The QUBO objective looks like this,

Figure 2: The general form of QUBO

where xᵢ ∈ X is a binary variable having a value of either 0 or 1 and Q is the QUBO matrix. Although QUBOs are native to Annealing machines but one can interchangeably use QUBO or Ising model just by replacing xᵢ=(sᵢ+1)/2.

The Objective Function

Assume we have an array of 3 real numbers (we could have taken a large array of real numbers. But for the sake of simplicity we’ll restrict ourselves to 3 only), N=[n₀,n₁,n₂] and a corresponding boolean bit array X=[x₀,x₁,x₂]. We can define a function f(N,X),

Figure 3

If the element nᵢ is the smallest element, then f(N,X) will have the minimum value for a specific Xᵢ. Hence, a quadratic cost hamiltonian can be written as,

Figure 4

As one of the numbers in the array (N) will always be minimum, the sum of all the bits in a valid Xᵢ will always be equals to unity. This helps us to write our constraint (xᵢ ∈ X),

Hence we can construct a penalty function P(X) just squaring up the above equation and simplifying it with xᵢ²=xᵢ,

Figure 5: Penalty function for the constraint

We can add H(N, X) and P(X) with a scaling factor λ and obtain a global cost function Q(N, X). Where λ will be the square of the sum of all the elements in the sequence.

Figure 6: Complete Objective Function

The above Q(N, X) is our final Quadratic objective function or QUBO, which is solvable by Gate based quantum computer or quantum annealer.

Algorithm Description

Although the algorithmic approach is versatile enough to solve minimisation and maximisation problems interchangeably by putting a negation in the global hamiltonian,

To find the largest element of an array use -H(N,X)

If there are any negative numbers as well as positive numbers then some classical preprocessing techniques will be required. There is no requirement for preprocessing if the elements of the sequence are all positive or all negative.

The classical preprocessing steps follow,

1. Sum up the absolute of all the elements in the sequence, Sum=∑ᵢ|nᵢ|

2. Now, add this Sum with every element of the sequence irrespective of signs and values. These new numbers nᵢ will be taken as the input of the actual algorithm as they are all positive.

This scaling method is essential when there are both positive and negative numbers are present in the sequence. The whole steps of the algorithm are distributed into some CPU and QPU. Encoding of the problem into QPU specific tasks and the result extraction process is entirely classical, where the main solution part is quantum and it is sandwiched between classical processes.

Still, a high-level pseudocode approach can be expressed as,

1. Preprocess the inputs using the above equation, if required.

2. Construct the cost hamiltonian Q(N, X) using all the input elements.

3. Find the best X using Quantum Computer.

4. Extract the smallest number using f(N,Xᵢ).

Experiment

Let us do an experiment. As we have mentioned already, our problem of interest is N = [17,3,49]. So, our Q(N, X) or QUBO matrix will be,

Figure 7: Left: Circuit from QUBO, Center: Problem QUBO, Right: The Graph on the right side is for an array of 10 elements (NOT FOR THE PROBLEM OF 3 ELEMENTS IN THE ARRAY MENTIONED ABOVE)

Here the QUBO for this problem will be an N node complete graph. As we know already that a combinatorial optimisation problem can be solved on both Quantum Annealer and Gate model Quantum computer. QUBO is native to Quantum Annealer, but to solve this problem on a Gate based QPU (IBM, Rigetti etc) we need to convert this as a Quadratic program. Then we will be able to solve it via QAOA or VQE. As we are using IBM Qiskit so we have constructed a Quadratic Program (doCPLEX) using the “qiskit.optimisation” module.
['Aniruddha Biswas']
['Quantum Computer', 'Algorithms', 'Quantum Computing', 'Optimization']