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=bA\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.
# !pip install -U classiq

import numpy as np
import scipy as scipy

A = np.array(
    [
        [0.28, -0.01, 0.02, -0.1],
        [-0.01, 0.5, -0.22, -0.07],
        [0.02, -0.22, 0.43, -0.05],
        [-0.1, -0.07, -0.05, 0.42],
    ]
)

b = np.array([1, 2, 4, 3])
b = b / np.linalg.norm(b)

print("A =", A, "\n")
print("b =", b, "\n")

# verifying that the matrix is symmetric and has eigenvalues in [0,1)
if not np.allclose(A, A.T, rtol=1e-6, atol=1e-6):
    raise Exception("The matrix is not symmetric")
w, v = np.linalg.eig(A)
for lam in w:
    if lam < 0 or lam > 1:
        raise Exception("Eigenvalues are not in (0,1)")

sol_classical = np.linalg.solve(A, b)
print("Classical solution: x = ", sol_classical)
num_qubits = int(np.log2(len(b)))
Output:

A = [[ 0.28 -0.01  0.02 -0.1 ]
   [-0.01  0.5  -0.22 -0.07]
   [ 0.02 -0.22  0.43 -0.05]
   [-0.1  -0.07 -0.05  0.42]] 

  b = [0.18257419 0.36514837 0.73029674 0.54772256] 

  Classical solution: x =  [1.3814374  2.50585064 3.19890483 2.43147877]
  

  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: 0nSPi=02n1biin|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.
from classiq import *


@qfunc
def load_b(b: CArray[CReal], res: Output[QArray]) -> None:
    # TODO prepare the state |b> in the "res" register - the amplitude of res states correspond to the values of the vector b
    # Solution start
    prepare_amplitudes(b, 0.0, res)
    # Solution end
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
@qfunc
def main(res: Output[QArray]):
    load_b(b.tolist(), res)


qmod_b_load = create_model(main)
# TODO update the qmod to have aer_simulator_statevector as backend, with one shot

# Solution start
qmod_b_load = set_execution_preferences(
    qmod_b_load,
    num_shots=1,
    backend_preferences=ClassiqBackendPreferences(backend_name="simulator_statevector"),
)
# Solution end
qprog_b_load = synthesize(qmod_b_load)
show(qprog_b_load)
Output:

Quantum program link: https://platform.classiq.io/circuit/2yjDzAXzUnVNMS5hsJGubDIKBR7
  

Take a look at the resulted circuit. Now let’s execute and see if bb was built correctly
job = execute(qprog_b_load)
job.open_in_ide()
Check if you see a match between the original bb to the resulted state vector
print("The original b is: ", b)
result = job.result_value()
print("The resulted state vector :", result.state_vector)
Output:

The original b is:  [0.18257419 0.36514837 0.73029674 0.54772256]
  The resulted state vector : {'00': (0.18257418583505527+0j), '01': (0.3651483716701124+0j), '10': (0.7302967433402199+0j), '11': (0.5477225575051674+0j)}
  

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: 0mψnQPE(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, λ=12mk=02m1λ(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 {ψjn}j=02n1\{|\psi_j\rangle_n \}^{2^n-1}_{j=0}, then i=02n1biin=j=02n1βjψjn.\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 0mj=02n1βjψjnQPEj=02n1βjλjmψjn.|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λmAL(f)f(λ)1λm+1f2(λ)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=02n1βjλjmψjnAL(C/x)0(j=02n11C2λj2βjλjmψjn)+1(j=02n1Cλjβjλjmψjn),\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.
@qfunc
def simple_eig_inv(phase: Const[QNum], indicator: Output[QBit]):
    # TODO allocate 1 qubit for indicator
    # TODO load its |1> state amplitude to be C/phase using the assign_amplitude_table function

    # Solution start
    allocate(indicator)
    assign_amplitude_table(
        lookup_table(lambda p: 0 if p == 0 else (1 / 2**phase.size) / p, phase),
        phase,
        indicator,
    )
    # Solution end

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=02n11C2λj2βjλjmψjn)+1(j=02n1Cλjβjλjmψjn)invQPE(U)0m0(j=02n11C2λj2βjψjn)+0m1(j=02n1Cλjβjψjn) |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=02n1Cλ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
@qfunc
def my_hhl(
    fraction_digits: int,
    b: CArray[CReal],
    unitary_with_matrix: QCallable[QArray],
    res: Output[QArray],
    phase: Output[QNum],
    indicator: Output[QBit],
) -> None:
    # TODO Call load_b you created, to load "b" vector into register "res"
    # Solution start
    load_b(b, res)
    # Solution end

    # TODO allocate a qnum register for "phase".

This qnum should be in the range [0,1) with fraction_digits precision
    # Solution start
    allocate(fraction_digits, False, fraction_digits, phase)
    # Solution end

    # TODO refer to applying (QPE\dagger)*(EigenValInv)*(QPE) : we want to apply "simple_eig_inv" within "qpe"
    # Solution start
    within_apply(
        lambda: qpe(unitary=lambda: unitary_with_matrix(res), phase=phase),
        lambda: simple_eig_inv(phase=phase, indicator=indicator),
    )
    # Solution end
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.
QPE_SIZE = 4


@qfunc
def main(res: Output[QNum], phase: Output[QNum], indicator: Output[QBit]):
    b_normalized = b.tolist()
    unitary_mat = scipy.linalg.expm(1j * 2 * np.pi * A).tolist()

    # TODO call my_hhl with QPE_SIZE digits resolution, on the normalized b, where the unitary is based on "unitary_mat"

    # Solution start
    my_hhl(
        fraction_digits=QPE_SIZE,
        b=b_normalized,
        unitary_with_matrix=lambda target: unitary(elements=unitary_mat, target=target),
        res=res,
        phase=phase,
        indicator=indicator,
    )
    # Solution end

2.2 Add Execution Preferences

Once we have a model qmod_hhl (by creating it from main), we would like to add execution details to prepare for the program execution stage.
backend_preferences = ClassiqBackendPreferences(backend_name="simulator_statevector")

qmod_hhl = create_model(
    entry_point=main,
    execution_preferences=ExecutionPreferences(
        num_shots=1, backend_preferences=backend_preferences
    ),
)

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.
qprog_hhl = synthesize(qmod_hhl)
Viewing in IDE
show(qprog_hhl)
Output:

Quantum program link: https://platform.classiq.io/circuit/2yjE1722b5qbbACiS9aynC2rmde
  

Details about qprog as depth for example, can be seen both in IDE and in Python SDK
print("depth = ", qprog_hhl.transpiled_circuit.depth)
Output:
depth =  464
  

print("depth = ", qprog_hhl.transpiled_circuit.get_circuit_metrics())
Output:
depth =  depth=464 count_ops={'u': 267, 'cx': 286}
  

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.
job = execute(qprog_hhl)

job.open_in_ide()

result = job.result_value()

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
target_pos = result.physical_qubits_map["indicator"][0]  # position of control qubit
sol_pos = list(result.physical_qubits_map["res"])  # position of solution
phase_pos = list(
    result.physical_qubits_map["phase"]
)  # position of the “phase” register, and flips for endianness as we will use the indices to read directly from the string
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.
qsol = [
    np.round(parsed_state.amplitude / (1 / 2**QPE_SIZE), 5)
    for solution in range(2**num_qubits)
    for parsed_state in result.parsed_state_vector
    if parsed_state["indicator"] == 1.0
    and parsed_state["res"] == solution
    and parsed_state["phase"]
    == 0.0  # this takes the entries where the “phase” register is at state zero
]
print("Quantum Solution: ", np.abs(qsol) / np.linalg.norm(qsol))
print("Classical Solution: ", sol_classical / np.linalg.norm(sol_classical))
Output:

Quantum Solution:  [0.2840631  0.50843613 0.64683772 0.4923432 ]
  Classical Solution:  [0.28005009 0.5079953  0.6484938  0.49291836]
  

fidelity = (
    np.abs(
        np.dot(
            sol_classical / np.linalg.norm(sol_classical),
            qsol / np.linalg.norm(qsol),
        )
    )
    ** 2
)
print("Solution Fidelity:", fidelity)
Output:

Solution Fidelity: 0.9999806280936833
  

  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.
sol_classical = np.linalg.solve(A, b)
global_phase = np.angle(qsol)
qsol_corrected = np.real(qsol / np.exp(1j * global_phase))
print("classical:  ", sol_classical)
print("HHL:        ", qsol_corrected)
print(
    "relative distance:  ",
    round(
        np.linalg.norm(sol_classical - qsol_corrected)
        / np.linalg.norm(sol_classical)
        * 100,
        1,
    ),
    "%",
)
Output:
classical:   [1.3814374  2.50585064 3.19890483 2.43147877]
  HHL:         [1.43559 2.56952 3.26897 2.48819]
  relative distance:   2.5 %
  

import matplotlib.pyplot as plt

plt.plot(sol_classical, "bo", label="classical")
plt.plot(qsol_corrected, "ro", label="HHL")
plt.legend()
plt.xlabel("$i$")
plt.ylabel("$x_i$")
plt.show()
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)(b0)=(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)(b0)=(x0).\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+wminI)(112m)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/(112nm)]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).