Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Welcome to the Jungle of HHL

  • Rabbits vs.
Foxes The Lanchester model is widely used to describe the dynamics of combat between two opposing forces. Originally formulated for military applications, this model can also be adapted to other contexts, such as ecological competition between two species or market competition between two businesses. The model uses linear differential equations to capture the interactions between the populations, making it suitable for scenarios where interaction terms are purely linear. In this notebook, we explore solving this modified Lanchester model using the Harrow-Hassidim-Lloyd (HHL) algorithm, a quantum algorithm designed for efficiently solving linear systems of equations.

Lanchester Model

Lanchester Model is described in this link. This differential equation describes the behavior of two forces, xx and yy: dxdt=ax+bxy+cy+d\frac{d x}{d t} = a x + b x y + cy +d dydt=ey+fxy+gx+h\frac{d y}{d t} = e y + f x y + g x +h where the coefficients described in the next sections indicate the sensitivities of each side to the other and to itself.

Describing the Classical Model Using the Finite Difference Method

Assuming b=0b=0, f=0f=0: xi+1=dt⋅axi+dt⋅cyi+dt⋅d+xi=(dt⋅a+1)xi+dt⋅cyi+dt⋅dx_{i+1} = dt \cdot a x_i + dt\cdot c y_i + dt\cdot d + x_i = (dt\cdot a+1) x_i+ dt\cdot c y_i + dt\cdot d yi+1=dt⋅eyi+dt⋅gxi+dt⋅h+yi=(dt⋅e+1)yi+dt⋅gxi+dt⋅h  ,y_{i+1} = dt\cdot e y_i + dt\cdot g x_i + dt\cdot h + y_i = (dt\cdot e+1) y_i + dt \cdot g x_i + dt\cdot h~~, where the dot product was added explicitly to emphasize that dtdt is a variable. The first sample is the initial condition: x0=X0x_0 = X_0 y0=Y0y_0 = Y_0 Then, for any next point, we solve numerically using the previous point: −(dt⋅a+1)xi−dt⋅cyi+xi+1=dt⋅d- (dt\cdot a+1) x_i - dt \cdot c y_i + x_{i+1} = dt\cdot d −(dt⋅e+1)yi−dt⋅gxi+yi+1=dt⋅h- (dt\cdot e+1) y_i - dt\cdot g x_i + y_{i+1} = dt\cdot h The system of linear equations describing the model looks like this:

Building the Matrix with NumPy

We choose specific values for the matrix, representing a predator-prey system. Here, xx represents the prey population (e.g., rabbits), and yy represents the predator population (e.g., foxes). The coefficients have the following meanings and specific values:
  • aa and ee define the rate of natural losses or birth (due to death, disease, etc.).
For example, a=−0.01a = -0.01 (1% natural death rate for rabbits), e=−0.02e = -0.02 (22% natural death rate for foxes).
  • bb and ff define the rate of losses due to environmental factors (affecting both species).
For example, b=0b = 0, f=0f = 0 (assuming no such environmental exposure for simplicity).
  • cc and gg are losses or gains due to interactions between species (prey hunted by predators and vice versa).
For example, c=−0.1c = -0.1 (1010% loss of rabbits due to predation), g=0.2g = 0.2 (2020% increase in predator population due to hunting prey).
  • dd and hh are gains due to migration.
For example, d=0.4d = 0.4 (0.4K0.4 K Rabbits/year migration rate for rabbits), h=0.01h = 0.01 (0.01K0.01K Foxes/year migration rate for foxes).
output output

Classical Solution

output
output Note that at some critical point, the rabbit population is so low that the fox population also starts to decrease. This is typical predator-prey model behavior.

Redefining the Matrix

The matrix of HHL is a canonical one, assuming the following properties:
  1. The RHS vector b⃗\vec{b} is normalized.
  2. The matrix AA is a Hermitian one.
  3. The matrix AA is of size 2n×2n2^n\times 2^n .
  4. The eigenvalues of AA are in the range (0,1)(0,1).
However, any general problem that does not follow these conditions can be resolved, as below.

1) Normalized b

As preprocessing, normalize b⃗\vec{b} and then return the normalization factor as postprocessing.

2) Hermitian Matrix

Symmetrize the problem: (0ATA0)(x⃗0)=(0b⃗).\begin{pmatrix} 0 & A^T \\ A & 0 \end{pmatrix} \begin{pmatrix} \vec{x} \\ 0 \end{pmatrix} = \begin{pmatrix} 0 \\ \vec{b} \end{pmatrix}. This increases the number of qubits by
output output

3) Make the Matrix AA of Size 2n×2n2^n\times 2^n

Complete the matrix dimension to the closest 2n2^n with an identity matrix. The vector b⃗\vec{b} is completed with zeros. (A00I)(b⃗0)=(x⃗0).\begin{pmatrix} A & 0 \\ 0 & I \end{pmatrix} \begin{pmatrix} \vec{b} \\ 0 \end{pmatrix} = \begin{pmatrix} \vec{x} \\ 0 \end{pmatrix}. However, our matrix is already the right size.

4) Rescaled Matrix

If the eigenvalues of AA are in the range [wmin⁡,wmax⁡][w_{\min},w_{\max}], we can employ transformations to the exponentiated matrix and then undo them to extract the results: A~=(A−wmin⁡I)(1−12m)1wmax⁡−wmin⁡.\tilde{A}=(A-w_{\min}I)\left(1-\frac{1}{2^{m}}\right)\frac{1}{w_{\max}-w_{\min}}. The eigenvalues of this matrix lie in the interval [0,1)[0,1), and are related to the eigenvalues of the original matrix via λ=(wmax⁡+wmin⁡)λ~[1/(1−12m)]+wmin⁡,\lambda = (w_{\max}+w_{\min})\tilde{\lambda}\left[1/\left(1-\frac{1}{2^{m}}\right)\right]+w_{\min}, with λ~\tilde{\lambda} being an eigenvalue of A~\tilde{A} resulting from the QPE algorithm. This relation between eigenvalues is then used for the expression inserted into the eigenvalue inversion, via the AmplitudeLoading function.
output

Defining HHL Algorithm for the Quantum Solution

This section is based on Classiq HHL in the user guide, here and here. Note the rescaling in simple_eig_inv based on the matrix rescaling. image.png
Besides HHL, we also perform swap test as in the user guide, comparing the HHL solution to a state preparation of the known solution.
We create the circuit with width and depth constraints suitable for efficient simulation. We also set the preferences for synthesis and execution.

Synthesize and Show

Output:

Execution and Results Analysis

The analysis is explained in the swap test user guide. We compare the measured overlap with the exact overlap using the expected probability of measuring the state ∣0⟩|0\rangle, defined as α2=12(1+∣⟨ψ1∣ψ2⟩∣2).\alpha^2 = \frac{1}{2}\left(1+|\langle \psi_1 |\psi_2 \rangle |^2\right). We extract the overlap ∣⟨ψ1∣ψ2⟩∣2=2P(qtest=∣0⟩)−1|\langle \psi_1 |\psi_2 \rangle |^2=\sqrt{2 P\left(q_{\text{test}}=|0\rangle\right)-1}. The exact overlap is computed with the dot product of the two state vectors. Note that for the sake of this demonstration we execute this circuit 100,000100,000 times to improve the precision of the probability estimate. This is usually not required. Since we are in the HHL context, we filter only the indicator==1 results.
Output:
Output:
In the IDE we can also check that among the indicator=1 results, the bar of test=0 is much higher than test=1.
Output:

State Vector Simulation

We can also run the state vector of the HHL result and examine it without a swap test. Extracting the full information is exponentially hard, and used here just for educational purposes. Extension of this work can extract some information from the state vector, such as the last element, which is the most important. We can also pad the last solution with an extension, and therefore measure the last point with high probability.
Output:
Output:
We filter the results, then extract the amplitudes of the state vector and compare them to the classical solution. We filter only the indicator=1 and phase=0 results.
Let’s compare the raw solution to the Hermitian matrix with the classical one.
output Now we can compare the two solutions in the time domain, after putting back the normalization factor.
output The fidelity of the state vector solution can be calculated as the overlap of the two solutions.
Output: