Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
Guidance for the workshop: The # TODO is there for you to do yourself. **The # Solution start and # Solution end are only for helping you. Try doing it yourself…** Solving linear equations appears in many research, engineering, and design fields. For example, many physical and financial models, from fluid dynamics to portfolio optimization, are described by partial differential equations, which are typically treated by numerical schemes, most of which are eventually transformed to a set of linear equations. The HHL algorithm [1] is a quantum algorithm for solving a set of linear equations. It is one of the fundamental quantum algorithms that is expected to give a speedup over its classical counterpart. A set of linear equations of size NN is represented by an N×NN\times N matrix and a vector bb of size NN, Ax⃗=b⃗A\vec{x} = \vec{b}, where the solution to the problem is designated by the solution variable x⃗\vec{x}. For simplicity, the demo below treats a usecase where b⃗\vec{b} is a normalized vector ∣b⃗∣=1|\vec{b}|=1, and AA is an Hermitian matrix of size 2n×2n2^n\times 2^n, whose eigenvalues are in the interval (0,1)(0,1). Generalizations to other usecases are discussed at the end of this demo.

  1. Defining a Specific Problem
Start by defining the specific problem.
Output:

  1. Building Simple HHL with Classiq

2.1 Define the Model

This tutorial gives instructions on building an HHL algorithm and presents the theory of the algorithm. The algorithm consists of 4 steps:
  1. State preparation of the RHS vector b⃗\vec{b}.
  2. QPE for the unitary matrix e2πiAe^{2\pi iA}, which encodes eigenvalues on a quantum register of size mm.
  3. An inversion algorithm that loads amplitudes according to the inverse of the eigenvalue registers.
  4. An inverse QPE with the parameters in (2).

2.1.1 State Preparation for the Vector b⃗\vec{b}

The first stage of the HHL algorithm is to load the normalized RHS vector b⃗\vec{b} into a quantum register: ∣0⟩n→SP∑i=02n−1bi∣i⟩n|0\rangle_n \xrightarrow[{\rm SP}]{} \sum^{2^n-1}_{i=0}b_i|i\rangle_n where ∣i⟩|i\rangle are states in the computational basis. Comments:
  • The relevant built-in function is the prepare_amplitudes one, which gets 2n2^n values of b⃗\vec{b}, as well as an upper bound for its functional error through the bound parameter.
Let’s see the loading of b in a state vector simulator Update the qmod to have aer_simulator_statevector as backend, with one shot Refer to Execution Preferences documentation and to Classiq backends documentation
Output:
Take a look at the resulted circuit. Now let’s execute and see if bb was built correctly
Output:
Check if you see a match between the original bb to the resulted state vector
Output:

2.1.2 Quantum Phase Estimation (QPE) for the Hamiltonian Evolution U=e2πiAU=e^{2\pi i A }

The QPE function block, which is at the heart of the HHL algorithm, operates as follows: Unitary matrices have eigenvalues of norm 1, and thus are of the form e2πiλe^{2\pi i \lambda}, with 0≤λ<10\leq\lambda<1. For a quantum state ∣ψ⟩n|\psi\rangle_n, prepared in an eigenvalue of some unitary matrix UU of size 2n×2n2^n\times 2^n, the QPE algorithm encodes the corresponding eigenvalue into a quantum register: ∣0⟩m∣ψ⟩n→QPE(U)∣λ⟩m∣ψ⟩n,|0\rangle_m|\psi\rangle_n \xrightarrow[{\rm QPE}(U)]{} |\lambda\rangle_m|\psi\rangle_n, where mm is the precision of the binary representation of λ\lambda, λ=12m∑k=02m−1λ(k)2k\lambda=\frac{1}{2^m}\sum^{2^m-1}_{k=0}\lambda^{(k)}2^k with λ(k)\lambda^{(k)} being the state of the kk-th qubit. In the HHL algorithm a QPE for the unitary U=e2πiAU=e^{2\pi i A } is applied. The mathematics: First, note that the eigenvectors of UU are the ones of the matrix AA, and that the corresponding eigenvalues λ\lambda defined for U=e2πiAU=e^{2\pi i A } are the eigenvalues of AA. Second, represent the prepared state in the basis given by the eigenvalues of AA. This is merely a mathematical transformation; with no algorithmic considerations here. If the eigenbasis of AA is given by the set {∣ψj⟩n}j=02n−1\{|\psi_j\rangle_n \}^{2^n-1}_{j=0}, then ∑i=02n−1bi∣i⟩n=∑j=02n−1βj∣ψj⟩n.\sum^{2^n-1}_{i=0}b_i|i\rangle_n = \sum^{2^n-1}_{j=0}\beta_j|\psi_j\rangle_n. Applying the QPE stage gives ∣0⟩m∑j=02n−1βj∣ψj⟩n→QPE∑j=02n−1βj∣λj⟩m∣ψj⟩n.|0\rangle_m \sum^{2^n-1}_{j=0}\beta_j|\psi_j\rangle_n \xrightarrow[{\rm QPE}]{} \sum^{2^n-1}_{j=0}\beta_j |\lambda_j\rangle_m |\psi_j\rangle_n. Comments:
  • Use the built-in qpe function.

2.1.3 Eigenvalue Inversion

The next step in the HHL algorithm is to pass the inverse of the eigenvalue registers into their amplitudes, using the Amplitude Loading (AL) construct. Given a function f:[0,1)→[−1,1]f:[0,1)\rightarrow [-1,1], it implements ∣0⟩∣λ⟩m→AL(f)f(λ)∣1⟩∣λ⟩m+1−f2(λ)∣0⟩∣λ⟩m|0\rangle|\lambda\rangle_m \xrightarrow[{\rm AL}(f)]{} f(\lambda)|1\rangle|\lambda\rangle_m+\sqrt{1-f^2(\lambda)}|0\rangle|\lambda\rangle_m. For the HHL algorithm, apply an AL with f=C/xf=C/x where CC is a lower bound for the minimal eigenvalue of AA. Applying this AL gives ∑j=02n−1βj∣λj⟩m∣ψj⟩n→AL(C/x)∣0⟩(∑j=02n−11−C2λj2βj∣λj⟩m∣ψj⟩n)+∣1⟩(∑j=02n−1Cλjβj∣λj⟩m∣ψj⟩n),\sum^{2^n-1}_{j=0}\beta_j |\lambda_j\rangle_m |\psi_j\rangle_n \xrightarrow[{\rm AL}(C/x)]{} |0\rangle\left(\sum^{2^n-1}_{j=0}\sqrt{1-\frac{C^2}{\lambda^2_j}}\beta_j |\lambda_j\rangle_m |\psi_j\rangle_n\right)+ |1\rangle\left(\sum^{2^n-1}_{j=0}\frac{C}{\lambda_j}\beta_j |\lambda_j\rangle_m |\psi_j\rangle_n\right), where CC is a normalization coefficient. The normalization coefficient CC, which guarantees that the amplitudes are normalized, can be taken as the lower possible eigenvalue that can be resolved with the QPE: C=1/2precision.C=1/2^{\rm precision}. The built-in construct to define an amplitude loading is the assign_amplitude_table function.

2.1.4 Inverse QPE

As the final step in the HHL model, clean the QPE register by applying an inverse-QPE. (Note that it is not guaranteed that this register is completely cleaned; namely, that all the qubits in the QPE register return to zero after the inverse-QPE. Generically they are all zero with very high probability). In this model we will simply call the QPE function with the same parameters in stage
  1. This is how the quantum state looks now
∣0⟩(∑j=02n−11−C2λj2βj∣λj⟩m∣ψj⟩n)+∣1⟩(∑j=02n−1Cλjβj∣λj⟩m∣ψj⟩n)→inv−QPE(U)∣0⟩m∣0⟩(∑j=02n−11−C2λj2βj∣ψj⟩n)+∣0⟩m∣1⟩(∑j=02n−1Cλjβj∣ψj⟩n) |0\rangle\left(\sum^{2^n-1}_{j=0}\sqrt{1-\frac{C^2}{\lambda^2_j}}\beta_j |\lambda_j\rangle_m |\psi_j\rangle_n\right)+ |1\rangle\left(\sum^{2^n-1}_{j=0}\frac{C}{\lambda_j}\beta_j |\lambda_j\rangle_m |\psi_j\rangle_n\right) \xrightarrow[{\rm inv-QPE}(U)]{} |0\rangle_m|0\rangle\left(\sum^{2^n-1}_{j=0}\sqrt{1-\frac{C^2}{\lambda^2_j}}\beta_j |\psi_j\rangle_n\right)+ |0\rangle_m|1\rangle\left(\sum^{2^n-1}_{j=0}\frac{C}{\lambda_j}\beta_j |\psi_j\rangle_n\right) The state entangled with ∣1⟩|1\rangle stores the solution to our problem (up to some normalization problem) ∑j=02n−1Cλjβjψj⃗=Cx⃗.\sum^{2^n-1}_{j=0} \frac{C}{\lambda_j}\beta_j \vec{\psi_j} = C\vec{x}.

2.1.5 Putting It All Together

Let’s remind that the entire HHL algorithm is composed of:
  1. State preparation of the RHS vector b⃗\vec{b}.
  2. QPE for the unitary matrix e2πiAe^{2\pi iA}, which encodes eigenvalues on a quantum register of size mm.
  3. An inversion algorithm that loads amplitudes according to the inverse of the eigenvalue registers.
  4. An inverse QPE with the parameters in (2).
And put all together in my_hhl function You can apply QPE†^\dagger * EigenValInv * QPE using the within_apply operator
The first entry point of any model would be the main function. Since you already have done all the job in my_hhl function, all we have to do now is to call it with the relevant inputs Call my_hhl with QPE_SIZE digits resolution, on the normalized b, where the unitary is based on unitary_mat.

2.2 Synthesize

  • From Qmod to Qprog
Once we have a high level model, we would like to compile and get the actual quantum program. This is done using the synthesize command.
Viewing in IDE
Output:
Details about qprog as depth for example, can be seen both in IDE and in Python SDK
Output:
Output:

2.3 Execution

Here we execute the circuit on state vector simulator (the backend for execution is defined before the synthesis stage). We can show the results in the IDE and save the state-vector into a variable.
Output:
Output:

2.4 Post-Process

We would like to look at the answers that are encoded in the amplitudes of the terms that their indicator qubit value=1
Define a run over all the relevant strings holding the solution. The solution vector will be inserted into the variable qsol. Factor out C=1/2mC=1/2^m.
Output:
Output:

  1. Comparing Classical and Quantum Solutions.
Note that the HHL algorithm returns a statevector result up to some global phase (coming from transpilation or from the quantum functions themselves). Therefore, to compare with the classical solution, correct for this global phase.
Output:
output

  1. Generalizations
The usecase treated above is a canonical one, assuming the following properties:
  1. The RHS vector b⃗\vec{b} is normalized.
  2. The matrix AA is an 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 follows:
  1. As preprocessing, normalize b⃗\vec{b} and then return the normalization factor as a post-processing
  2. Symmetrize the problem as follows:
(0ATA0)(b⃗0)=(0x⃗).\begin{pmatrix} 0 & A^T \\ A & 0 \end{pmatrix} \begin{pmatrix} \vec{b} \\ 0 \end{pmatrix} = \begin{pmatrix} 0 \\ \vec{x} \end{pmatrix}. This increases the number of qubits by
  1. Complete the matrix dimension to the closest 2n2^n with an identity matrix.
The vector b⃗\vec{b} will be 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}.
  1. If the eigenvalues of AA are in the range [−wmin⁡,wmax⁡][-w_{\min},w_{\max}] you can employ transformations to the exponentiated matrix that enters into the Hamiltonian simulation, and then undo them for extracting the results:
A~=(A+wmin⁡I)(1−12m)1wmin⁡+wmax⁡.\tilde{A}=(A+w_{\min}I)\left(1-\frac{1}{2^{m}}\right)\frac{1}{w_{\min}+w_{\max}}. The eigenvalues of this matrix lie in the interval [0,1)[0,1), and are related to the eigenvalues of the original matrix via λ=(wmin⁡+wmax⁡)λ~[1/(1−12nm)]−wmin⁡,\lambda = (w_{\min}+w_{\max})\tilde{\lambda}\left[1/\left(1-\frac{1}{2^{n_{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.

References

[1]: Harrow, A. W., Hassidim, A., & Lloyd, S., Quantum Algorithm for Linear Systems of Equations. Physical Review Letters 103, 150502 (2009).