Date of publication xxxx 00, 0000, date of current version xxxx 00, 0000. 10.1109/ACCESS.2022.0122113
Part of this work was supported by the National Science Foundation under Grant 2046220.
Corresponding author: Hiu Yung Wong (email: [email protected]). A. Zaman and H. J. Morrell have equal contributions.
A Step-by-Step HHL Algorithm Walkthrough to Enhance Understanding of Critical Quantum Computing Concepts
Abstract
After learning basic quantum computing concepts, it is desirable to reinforce the learning using an important and relatively complex algorithm through which students can observe and appreciate how qubits evolve and interact with each other. Harrow-Hassidim-Lloyd (HHL) quantum algorithm, which can solve linear system problems with exponential speed-up over the classical method and is the basis of many important quantum computing algorithms, is used to serve this purpose. The HHL algorithm is explained analytically followed by a 4-qubit numerical example in bra-ket notation. Matlab code corresponding to the numerical example is available for students to gain a deeper understanding of the HHL algorithm from a pure matrix point of view. A quantum circuit programmed using qiskit is also provided for real hardware execution in IBM quantum computers. After going through the material, students are expected to have a better appreciation of the concepts such as basis transformation, bra-ket and matrix representations, superposition, entanglement, controlled operations, measurement, quantum Fourier transformation, quantum phase estimation, and quantum programming. To help readers review these basic concepts, brief explanations augmented by the HHL numerical examples in the main text are provided in the Appendix.
Index Terms:
Harrow-Hassidim-Lloyd (HHL) quantum algorithm, Quantum Fourier transform (QFT), inverse quantum Fourier transform (IQFT), quantum phase estimation(QPE),linear system problem (LSP), Quantum Education=-15pt
I Introduction
Quantum Computing is promising in solving challenging engineering [1], biomedical [2] and finance [3] problems. It has a tremendous advancement in the last two decades and, recently, quantum breakthrough has been demonstrated using a 53-qubit system [4].However, according to the paper [5], due to less efficient hardware implementation till date, the goal to reach superconducting quantum supremacy is yet to achieve. Therefore, the training of a quantum technology workforce is an imminent goal for many countries (e.g. [23]) to support this fast-growing industry. However, quantum technology is based on concepts very different from our daily and classical experiences. In the early stage of learning quantum computing, although linking to daily and classical experience may enhance the understanding of certain quantum concepts and such an approach should not be de-emphasized, we believe a fast and robust way of training a quantum workforce is to train the students to be able to emulate a quantum processor and trace the evolution of the qubits. This is particularly useful in learning quantum algorithms without a quantum mechanics background. Such an approach obviates the students from cognitive conflicts, which can be resolved later, if possible, after they understand how quantum computing works. This also embraces the “Shut up and calculate!” approach proposed by Mermin on how to deal with the uncomfortable feeling toward quantum mechanics interpretation [6].
Besides analytical equations, matrix representation and computer simulations are important tools to enhance the understanding of qubit evolution. However, available examples that include computer simulations are usually of simple algorithms and, very often, without matrix representation. There is a lack of examples of important and relatively complex algorithms which combine some of the most important quantum computing concepts and basic algorithms. Such examples are desirable to allow students to appreciate the roles and the interplay of various basic concepts in a more realistic quantum algorithm. Harrow-Hassidim-Lloyd (HHL) quantum algorithm [7][8] which can be used to solve linear system problems (LSP) and can provide exponential speedup over the classical conjugate gradient method [9] is chosen for this purpose. HHL is the basic of many more advanced algorithms and is important in various applications such as machine learning [10] and modeling of quantum systems [2][11]. HHL solves system of linear equation which is a discretization of [12][13]. In this paper, we detail the qubit evolution in Harrow-Hassidim-Lloyd (HHL) quantum algorithm analytically with a 4-qubit circuit as a numerical example. Although HHL examples are available elsewhere (e.g. [14][15]), this paper has certain characteristics which are not all found in those examples. Firstly, the HHL algorithm is discussed analytically step-by-step and is self-contained. Secondly, a numerical example is given in bra-ket notation mirroring the analytical equations. Thirdly, a Matlab code corresponding exactly to the numerical example is available to enhance the understanding from a matrix point of view. The Matlab code allows the students to trace how the wavefunction evolves instead of just seeing the magnitudes of the coefficients as in IBM-Q. Fourthly, a qiskit code written in python [16] corresponding to the numerical example is available and can be run in IBM simulation and hardware machines [17]. Finally, in the example, all the 4 qubits are traced throughout the process without simplification.
The readers are assumed to have the following background concepts which are further enhanced through the step-by-step walkthrough of the HHL algorithm: basis transformation, bra-ket and matrix representations, superposition, entanglement, encoding, controlled operations, measurement, quantum Fourier transformation, and quantum programming. To make this paper self-contained and to help the readers better appreciate the roles of these basic concepts in the HHL, an Appendix is devoted to briefly explaining these concepts using the examples from the main text. A more detailed explanation of these concepts using a similar approach as in this paper can be found in [19].
I-A How to use this paper
For readers who have a fresh memory of the basic concepts, they can start reading from Section II, in which the HHL algorithm is discussed step-by-step analytically followed by a numerical example in Section III. The basic concepts mentioned in the Appendix are referred to in the main text and readers are encouraged to review them when needed.
For readers who need reviews on the basic concepts first, they are encouraged to go over the Appendix first before reading the main text.
For readers who have devoted substantial time to learning HHL elsewhere but just need a numerical example to reinforce the understanding, they might start with the numerical example in Section III.
Equations in the Appendix begin with ’V’. If the equations are examples from the main text, the same equation number is used in the Appendix.
II HHL Algorithm
II-A Definitions and Overview
We will first give an overview of the problem and the HHL algorithm. Details will be discussed in the following subsections with reference to the Appendix for reviewing basic concepts. A linear system problem (LSP) can be represented as the following
(1) |
where is a Hermitian matrix and and are -dimensional vectors. For simplicity, it is assumed , where is the number of qubits in the quantum circuit, and is the total combinations due number of qubits,. In matrix representation, qubits are represented by their total combinations(), or we can say for number of unknowns, we need qubits to solve unknowns. Dummy equations can be added otherwise to convert the system satisfy this assumption. and are known and is the unknown to be solved, i.e.
(2) |

As an example, , , and with and . Readers may refer to Appendix V-13 and Appendix V-14 to review how LSP is solved classically using Gaussian Elimination and Conjugate Gradient Method, respectively.
is assumed to be Hermitian (See Appendix V-1). If it is not Hermitian, the can be converted to , which is Hermitian. Readers may refer to [20] for the more advanced treatment when is not Hermitian.
Figure 1 shows the schematic of the algorithm and the corresponding circuit to solve LSP. In the HHL quantum algorithm, the components of and are encoded as the amplitudes/coefficients (amplitude encoding) of basis states of the -qubits, , which form a Hilbert space. These qubits are called b-register. Qubit is chosen to be large enough to encode , i.e. needs to be the same as the length of the vectors and . The matrix is simulated through Hamiltonian encoding by encoding it as the Hamiltonian of a unitary gate. Appendix V-9 reviews examples of various encoding schemes.
The HHL algorithm has 5 main components, namely state preparation, quantum phase estimation (QPE), ancilla bit rotation, inverse quantum phase estimation (IQPE), and measurement. In this paper, the little-endian convention is used. In a little-endian convention, the rightmost (ending) qubit represents the least significant bit (LSB). For example, in a 4-qubit system, in binary is in decimal because the in the basis state is the LSB, representing instead of (if it were the most significant bit, MSB). Moreover, in the circuit diagrams, the lowest qubit represents the MSB and the topmost qubit represents the LSB, which is a convention used in qiskit [16] and the IBM-Q platform [17]
As shown in Figure 1, besides the b-register, which belongs to the more significant bits, there are two more sets of inputs to the algorithm. The first set is sometimes called the c-register because it is related to the time (clock) in the controlled rotation in the QPE part. Therefore, they are also called the clock qubits.The c-register stores the values of the phase of the eigenvalues of the matrix after the QPE. There are qubits in the c-register. Since basis encoding is used (i.e. the phase value is encoded as the basis number (See Appendix V-9), the value of determines how accurately the phase can be stored. A larger results in higher accuracy when the encoding is not exact. We set .
The last set of qubits is the ancilla qubit which is the LSB. The ancilla qubit, as its name implies, is important to help achieve the goal although it will be discarded at the end, as will be detailed later.
The matrix , which is a Hamiltonian, may be written as a linear combination of the outer products of its eigenvectors, weighted by its eigenvalues, ,in Eq.(3), (See Appendix V-8).
(3) |
Since A is diagonal in its eigenvector basis, its inverse is simply, . can be also expressed in the basis formed by the eigenvectors of A, such that
(4) |
Therefore, Eq. (2) can be encoded as,
(5) |
by using the fact that . The goal of the HHL algorithm is to find the solution in this form and is stored in the b-register.Storing the right hand side of (5) in the b-register is equivalent to storing in the b-register. But the solution is encoded as the amplitudes of the basis vectors (the measurement basis). Therefore, the solutions are not , which are the amplitudes of the eigenvector basis vectors. However, one will naturally get the correct amplitudes if it is measured in the basis and this is only possible if the qubits are not entangled with other qubits. This can be checked mathematically replacing by based on their relationship. If are entangled with other qubits, one cannot obtain the desired solution. This will be clear after the ancilla bit rotation to be detailed later.
II-B State Preparation
There are total qubits and they are initialized as
(8) |
In the state preparation, in the b-register needs to be rotated to have the amplitudes correspond to the coefficients of . That is
(9) |
The vector is represented in a column form on the left with coefficients . This is also a valid representation of . On the right, the corresponding basis of the Hilbert space formed by the qubits is written explicitly. Therefore,
(10) |
From now on, some of the subscripts of the kets will be omitted when there is no ambiguity. Since the state preparation depends on the actual value of , it will be discussed in more detail in the numerical example.
II-C Quantum Phase Estimation
Quantum phase estimation (QPE) is also an eigenvalue estimation algorithm. QPE has three components, namely the superposition of the clock qubits through Hadamard gates, controlled rotation, and inverse quantum Fourier transform (IQFT). The goal of QPE is to estimate the phase of the eigenvalues of the unitary rotation matrix, , in the controlled gate, , (Fig. 1) used in the QPE. Again, this gate encodes the matrix as its Hamiltonian. It is also instructive to note that the eigenvalues of must be roots of unity (i.e. in the form of ) as is unitary. Therefore, the phase of the eigenvalue of the gate is proportional to the eigenvalue of . As a result, by using QPE in the HHL algorithm, it is expected the eigenvalues of will be encoded in the c-register after the QPE, i.e. at . As it will be clear later, the eigenvalues are only encoded through basis encoding. The c-register does not store the exact eigenvalues.
Here we assume the readers are already familiar with IQFT and it will not be explained in detail. Readers may review the basic concepts in Appendices V-11 and V-12.
In the first step of QPE, Hadamard gates are applied to the clock qubits to create a superposition of the clock qubits,
(11) | |||||
(12) |

In the controlled rotation part, controlled gates are applied to with the clock qubits as the controlling qubits (Figure 2). The number of the qubit, , of the c-register determines the number of the controlled gates. The gates are in the form of , where is the index of the clock qubit. Also, . For the most significant bit in the c-register, , it controls the gate on the b-register while the least significant one, , controls the gate on the b-register.
To understand how it works, we begin by assuming that is an eigenvector of with eigenvalue . The eigenvalue is written in this form so that the phase, , will be encoded as the basis state in the c-register (See Eq. (II-C)). Therefore, based on the definition of eigenvalues and eigenvectors (See Section V-8),
(13) |
When the control clock qubit is , will not be affected. If the clock bit is , will be applied to . This is equivalent to multiplying in front of the of the th clock qubit, , as one can assign the prefactor to the controlling qubit. Therefore, after the controlled- operation, we have
(14) |
In the IQFT part,(II-C), only the clock qubits are affected. Note that in certain literature, this is called Quantum Fourier Transform (QFT) (Appendix V-11).
(15) |
Due to interference, only satisfying the condition will have a finite amplitude of . Otherwise, the amplitude is due to destructive interference. By ignoring the states with zero amplitude, we may rewrite as
(16) |
Therefore, in QPE, the clock qubits are used to represent the phase information of , which is , and the accuracy depends on the number of qubits, .
Since in Hamiltonian encoding, is related to through
(17) |
where is the evolution time for that Hamiltonian. is also diagonal in eigenvector, , basis. If ,
(18) |
(19) |
Thus the eigenvalues of have been encodeded in the clock qubits (basis encoding). However, in general, given in (4), by superposition,
(20) |
The are usually not integers. We will choose so that are integers. Therefore, are usually scaled version of .
can be rewritten as
(21) |
II-D Controlled Rotation and Measurement of the Ancilla Qubit
The next step is to rotate the ancilla qubit, , based on the encoded eigenvalues in the c-register, such that,
(22) |
where is a constant. The goal is to show why this is useful.
When the ancilla qubit is measured, the ancilla qubit wavefunction will collapse to either or . If it is , the result will be discarded and the computation will be repeated until the measurement is . Therefore, the final wavefunction of interest is
(23) |
where the prefactor is due to normalization after measurement. Since is the probabily of obtaining when the ancilla bit is measured, should be chosen to be as large as possible. Compared to (5), the result resembles the answer that we are looking for. However, we can only obtain the correct result if the b-register is measured in the eigenvector basis (i.e. instead of ). However, the b-register is entangled with the clock qubits, . This means that we cannot factorize the result into a tensor product of the c-register and b-register (See the discussion after (5) and Appendix V-6). As a result, we cannot convert the b-register into the measurement basis with the desired amplitudes. We will need to uncompute the state so that it gives the right results in the measurement during which the b-register and c-register will be unentangled.
The measurement of the ancilla qubit can be and is usually performed after uncomputation. However, since the ancilla bit is not involved in any operations after the controlled rotation, measuring the ancilla bit before the uncomputation gives the same result. For simplicity in the derivation, it is thus performed before the uncomputation.
II-E Uncomputation - Inverse QPE
The uncomputation is done by using inverse QPE. Firstly, QFT is applied to the clock qubits as,
(24) |
Then inverse controlled-rotations of the b-register by the clock qubits are applied with . Similar to the forward process, when the controlling th clock qubit is , will not be affected. If the th clock qubit is , will be applied to . This is equivalent to multiplying if the c-register is . This is due to the similar argument in Eq. (II-C) and the fact that . Therefore,
(25) |
Since we earlier chose to set , therefore, the two exponential terms cancel each other and
(26) |
The clock qubits and the b-register are now unentangled and the b-register stores . By applying the Hadamard gate on the clock qubits, finally, we have
(27) |
If is real and by using (7),
(28) |
Here, the solution (Eq. 5) is stored in the b-register successfully.
III Numerical Example
We will present a numerical example and apply HHL to it step-by-step. The implementation is shown in Figure 3. Firstly, we will discuss how to implement the controlled- and ancilla qubit rotations.
III-A Encoding Scheme
In this example, the matrix and vector are set to be
(29) |
(30) |
The eigenvectors of are , with eigenvalues and respectively. We need to using basis encoding to encode the eigenvalues in the basis formed by the clock qubit and 2 qubits are needed by encoding as and as so that it maintains the ratio of . This means and or in other words, and . This gives a perfect encoding with (i.e. ). Therefore, is chosen to be to achieve the encoding scheme since .
Since is a 2-dimensional complex vector, it can be encoded using 1 qubit and, thus, .
The solution to the LSP is found to be
(31) |
whereby, the ratio of to is .
III-B Controlled-U Implementation
In reality, we expect the controlled- operation to be implemented by Hamiltonian simulation [18]. However, to understand the algorithm, we will derive the matrix for and then map this to the gate used in IBM-Q directly. Since , there are two operations needed, namely and , controlled by and , respectively.
In order to find the corresponding matrix for and , we need to perform similarity transformation on and , exponentiate then, and transforms back to the original basis.
The transformation matrix from the original basis to the eigenvector basis is
(32) |
Since is real and symmetric,its conjugate, equals itself.
The diagonalized , i.e. expressed in the basis formed by and , is
(33) |
As it is diagonal, can be obtained by exponentiation of the elements accordingly.
(34) |
where as mentioned earlier is used. Also,
(35) |
It is worth noting that both are naturally unitary which is a requirement for a quantum operation.
To obtain and in the original basis, we need to apply similarity transformation again in the reverse direction,
(36) |
(37) |
To implement and , a 4-parameter arbitrary unitary gate with global phase [19],
(38) |
By choosing , is implemented.
By choosing , is implemented.
For the IQPE part, we also need to implement and . Since in this example, , one can use the same set of parameters to implement .
However,
(39) |
We need to choose to implement .
The controlled version of matrix can then be constructed using
(40) |
Note that in this equation, only the controlling clock bit and the b-register are included for simplicity. For example,
(41) |
III-C Implementataion of the Controlled-Rotation of Ancilla Qubit
The coefficients of and of the ancilla bit after rotation in Eq. (22) are and , respectively. The sum of the square of the magnitudes of the coefficients is as required. This means also . Since the minimal is , we will set to maximize the probability of measuring during the ancilla bit measurement.
The transformation of to is known to be equivalent to rotation,
(42) |
with . One can check this by multiplying to .
(43) |
Therefore, we will establish a function to implement this rotation and this function only need to be valid when the input are the encoded eigenvalues because only encoded eigenvalues have zero magnitudes in the c-register as shown in (21). The function is defined as
(44) |
where is the value of the clock qubits and is its binary form.
Since only has non-zero amplitude in (21), we only need to set up (44) such that it is correct for = and , namely
(45) | |||
(46) |
The following function can achieve the goal,
(47) |
Therefore, the controlled rotation can be implemented as
(48) |
where the operators operate on qubits , , and from left to right, respectively.
III-D Quantum Circuit
An HHL circuit for the numerical example is then built and shown in Figure 3. We will then walk through the circuit using numerical substitution.

III-E Numerical Substitution
The algorithm begins with
(49) |
X-gate is then applied to convert to with
(50) |
After applying the Hadamard gates to create a superposition among the clock qubits,
(51) |
Before applying the CU3(controlled rotation of ancillary qubit) gates in the bra-ket notation, it will be convenient to perform a basis change to the eigenvector basis of . Since , we have and . Therefore,
(52) |
In the controlled rotation operations, when the corresponding c-register is , a phase change of is added (i.e. multiplied by ) for . Since , and , we have
(53) |
Before applying , the terms are regrouped for simplicity.
(54) |
Now apply to the clock qubits, e.g.
(55) |
Similarly,
(56) |
(57) |
(58) |
Therefore, applying to and substituting Eq. (55) to (58),
(59) |
It can be seen that after , the eigenvalues are encoded in the clock qubits as and with non-zero amplitudes due constructive interference. and . We clearly see the entanglement between the b-register and the c-register that goes with and goes with .
After performing the ancilla qubit rotation,
(60) |
If the measurement of the ancilla bit is ,
(61) |
Applying QFT to the encoded eigenvalues, we have
(62) |
(63) |
For the controlled rotation, the state is multiplied by and if and , respectively. Since , , , , and =
(65) |
Finally, by applying Hadamard gate to the clock qubits,
(66) |
It can be verified that is a normalized vector as it should be because every operation in the HHL circuit is unitary and preserves the norm.
Equation (III-E) can be simplified by substituting and . We obtain,
(67) |
The probability ratio of obtaining and when b-register is measured is thus as expected.
III-F Simulation Results
Matlab code implementing the numerical example using matrix approach is created and available at [21]. In the Matlab code, measurement is not performed (i.e. not partical tracing of the matrix). is found to be,
(68) |
Since are discarded during the measurement step, only and are left. Their amplitude ratio is as expected.
The circuit in Fig. 3 is also simulated in the IBM-Q system (code available at [21]). Since only the b-register and the ancilla qubit are measured, there are only four possible outputs as shown in Figure 4. Again, only should be considered. The ratio of the measurement probability of to is , which is close to the expected value.

On the other hand, due to the imperfection and noise in a real quantum computer, the hardware execution of the same circuit does not give a satisfactory result (Figure 5). The ratio of the measurement probability of to is only .

IV Conclusion
In this paper, we presented the HHL algorithm through a step-by-step walkthrough of the derivation. A numerical example is also presented in the bra-ket notation. The numerical example echos the analytical derivation to help students understand how qubits evolve in this important and relatively complex algorithm. A Matlab code corresponding to the numerical example is constructed to help understand the algorithm from the matrix point of view. Qiskit circuit of the corresponding circuit which can be simulated in IBM-Q and run on their quantum computing hardware is also available. Through this self-contained and step-by-step walkthrough, the basic concepts in quantum computing are reinforced.
V APPENDIX
V-1 Hermitian matrix
A Hermitian matrix is a matrix that equals to its adjoint matrix (transpose followed by complex conjugation). That is, if is a Hermitian matrix, then it is defined as,
(V.1) |
where is the transpose of .
In this paper, the matrix, , in the LPS to be solved is assumed to be Hermitian.
V-2 Bra-ket Notation
Bra-ket notation is commonly used in quantum mechanics. A vector is represented as in its ket form. The bra form of the vectors forms a dual space to the space of the kets. The bra form of is .
In matrix representation, ket is the complex conjugate transpose of bra and vice versa. For example, if , then
V-3 Superposition
Superposition or Quantum Superposition is a quantum state which is the linear combination of two or more basis states. For example, a superposition state can be , where and are complex number and and are basis states. A Hadamard gate is a gate commonly used to create a superposition state (Appendix V-5).
V-4 Basis Transformation and Quantum Gate
In quantum computing, we only care about the basis transformation due to rotation in the hyperspace. The transformation is equivalent to the multiplication of the basis vectors by a unitary matrix, , which is the transformation matrix. All quantum gates can be defined as how the basis vectors are transformed from the initial basis vector to the final basis vectors. Usually, a quantum gate rotates a basis state into another basis state (e.g. the NOT gate) and has its classical counterpart. But there are some gates that rotate a basis state to a superposition of two or more basis states. Such gates have no classical counterparts. For example, a Hadamard gate defines how an initial basis vector is rotated to an equal superposition of two basis vectors (Appendix V-5).
V-5 Hadamard Gate
The Hadamard gate is a quantum gate that does not have a classical counterpart. It rotates the basis state to create an equal superposition of the basis states. For a 1-qubit case, this means it has equal probability (i.e. ) of measuring and .
The matrix form of the Hadamard gate is,
(V.2) |
When it is applied on the basis state , which is in matrix form, we have,
(V.3) | |||
(V.4) |
which can also be represented in bra-ket form as,
(V.5) |
In this paper, Hadamard gates are applied in clock qubit to create superposition from to . For example, in (11),
(11) |
where is obtained by applying tensor product of identity gates and an -qubit Hadamard gate to . The identity gates are applied to the b-register and the ancilla qubit while the Hadamard gate is applied to the clock qubits. In this equation, the -qubit Hadamard gate is represented as , i.e. the tensor product of 1-qubit Hadamard gates.
V-6 Entanglement
Entanglement refers to the quantum state of a 2- or more-qubit system that cannot be expressed as a tensor product of the individual qubit. This is an important feature that quantum computing uses often. As an example,
(V.6) |
is an entangled state. It cannot be expressed as a tensor product of two individual qubit states.
In this paper, after the ancilla bit rotation, we have
(61) |
where the b-register and the c-register are entangled and () always appears with () after the measurement.
If the b-register were not entangled with the c-register, we have
(V.7) |
By substituting and and after simplification, we have
(V.8) |
This is the same as (67). The probability of measuring and has the ratio of 1:9 as expected.
However, when there is entanglement, the probability of measuring and would not be 1:9 because the previous grouping is impossible.
(V.9) |
V-7 Controlled Operation
Controlled operation requires more than one qubit. For a 2-qubit controlled gate, an operation is applied to a qubit (the target qubit), if the value of the controlling qubit is 1 in the basis vector.
For example, in Figure 2, is the target qubit and is the controlling qubit. The operation of is applied to only if is 1 in the basis state (e.g. .
In general, the controlled version of a unitary gate, , can be implemented using the following equation if the LSB is the controlling qubit.
(40) |
which literally means that if the controlling qubit is 0, Identity gate is applied to the target qubit (MSB). Otherise, is applied to the target qubit.
V-8 Eigenvalue and Eigenvector
When a non-zero matrix is applied to an -dimensional vector and has the following relationship,
(V.10) |
where is a scalar, then, by definition, and are the eigenvector and eigenvalue of , respectively. This is similar to (3), where the matrix is expressed as a linear combination of the outer products of its eigenvectors, .
(3) |
This can be checked by applying to its eigenvector ,
(V.11) | ||||
which meets the definition in Eq. (V.10).
V-9 Different Types of Encoding
The three common types of encodings are explained here.
-
•
Basis Encoding- Basis encoding converts classical information such as numbers or matrix to quantum information in the form of basis states. For example,
(V.12) -
•
Amplitude Encoding- Amplitude encoding encodes the information as the coefficients of the basis vectors. For example, for
(V.13) which is assumed to be normalized (), it can be encoded as in the following quantum state,
(V.14) where and become the coefficients of the basis states, and , respectively. In the main text, Eq. (9) shows how the values of the components of vector are encoded using amplitude encoding.
-
•
Hamiltonian Encoding- One type of the Hamiltonian encoding is to encode the matrix as the Hamiltonian in a unitary gate. For example, in this paper, Eq. (17) shows that
(17) where it encodes matrix as the Hamiltonian of the unitary gate . Matrix needs to be Hermitian as it is used to represent the Hamiltonian (the energy) of the system. However, does not need to be unitary and will be unitary due to its definition in (17).
V-10 Discrete Fourier Transform (DFT)
The discrete Fourier Transform (DFT) transforms an -dimensional vector to another -dimensional vector . The transformation matrix contains the powers of the -th root of unity, . The transformation is represented as,
(V.15) |
V-11 Inverse Quantum Fourier Transform (IQFT) and Quantum Fourier Transform(QFT)
Mathematically, IQFT is similar to DFT (See Appendix V-10). The transformation matrix, , is for an -dimensional Hilbert space. Therefore, for an -qubit system. Eq. (V.15) becomes
(V.16) |
and has the same expression as in DFT.
(V.17) |
Note that in some literature, e.g. [19], this form of IQFT is called QFT. and are the quantum states/vectors in the -dimensional Hilbert space. IQFT can be treated as the rotation of to .
If is a basis vector , applying IQFT to using Eq. (V.17), we have
(V.18) |
This is the equation used often in this paper. It tells us that by applying IQFT to a basis vector, the basis vector is rotated to a superposition of all basis vectors weighted by the powers of the -th root of unity.
For example, in (55) in the main text,
(55) |
where , , in (V.18) is used. The basis state becomes a superposition of all other basis states after .
(II-C) |
Here, is applied to the c-register which is a superposition of basis states, . Using the distribution law of matrix operations, is applied to individual and (V.18) is used with and .
QFT is the inverse of and can be treated as the rotation of the basis. The rotation matrix is given by
(V.19) |
Equivalently,
(V.20) |
It can be shown that or .
V-12 Implementation of and
and are constructed using Hadamard gates, controlled phase shift gates, and gates. Readers may refer to other sources for the details (e.g. [19]). Here, we show the circuit of a 2-qubit gate (Fig. 6).

In general, the phase shift angle is , is the distance between the controlling qubit and the target qubit. For the 2-qubit case, there is only one controlled phase shift gate and and this results in the phase .
V-13 Gaussian elimination method
Here, Gaussian elimination is demonstrated by solving (1) using the numerical example in Section III.
(1) |
(V.21) |
which is rewritten as an augmented matrix followed by Gaussian method of Elimination to solve for and ,
This the solution is
The complexity of Gaussian Elimination is . This is much slower than the classical conjugate gradient method (Appendix V-14), to which HHL is compared.
V-14 Conjugate Gradient Method
The Conjugate Gradient Method (CGM) solves the LSP with a complexity of and is the fastest known classical solver. Therefore, the speed of HHL, which has a complexity of , is often compared to the speed of CGM [22]. Thus, HHL provides an exponential speedup over the fastest known classical method.
When we solve a system of linear equation, according to (Eq.(1)),where is a matrix, is a vector and is to be solved. If is a matrix and is ,then can be solved easily. But is is a long matrix, for example and is vector, and in this case is . To solve in Classical Gaussian Elimination method we need speed, where is omega. In Classical Conjugate Gradient Method with sparse matrix that contains lots of zeros, it will take speed. And for HHL Quantum Algorithm, with sparse matrix, it takes speed.
However, according to the paper [24], the speed of inner product in HHL Quantum Algorithm is only steps when certain amplitudes is obtained and distinguished among other amplitudes, otherwise the speed is only quadratically faster than classical algorithm.
To solve (1) in method,
(1) |
initial guess of is used as the starting point. The residual is then found and the search direction is determined by finding the steepest descent. This is repeated until a stable condition is met.
The residual in the þsearch is given as,
(V.22) |
The readers do not need to understand CGM to understand HHL. Interested readers may refer to the literature (e.g. [9]) for more details.
References
- [1] P. W. Shor, “Algorithms for quantum computation: discrete logarithms and factoring,” Proceedings 35th Annual Symposium on Foundations of Computer Science, 1994, pp. 124–134, doi: 10.1109/SFCS.1994.365700.
- [2] C. Outeiral, M. Strahm, J. Shi, G. M. Morris, S. C. Benjamin, and C. M. Deane, “The prospects of quantum computing in computational molecular biology,” WIREs Comput. Mol. Sci., 11, e1481 (2021).
- [3] D. J. Egger et al., “Quantum Computing for Finance: State-of-the-Art and Future Prospects,” in IEEE Transactions on Quantum Engineering, 1, pp. 1–24, 2020, Art no. 3101724, doi: 10.1109/TQE.2020.3030314.
- [4] Arute, F., Arya, K., Babbush, R. et al. Quantum supremacy using a programmable superconducting processor. Nature 574, 505–510 (2019). doi.org/10.1038/s41586-019-1666-5
- [5] Madsen, L.S., Laudenbach, F., Askarani, M.F. et al. Quantum computational advantage with a programmable photonic processor. Nature 606, 75–81 (2022). doi.org/10.1038/s41586-022-04725-x
- [6] N. David Mermin, ”Could Feynman Have Said This?,” Physics Today 57 (5), 10 (2004); doi: 10.1063/1.1768652
- [7] A. Harrow, A. Hassidim, and S. Lloyd, ”Quantum algorithm for linear systems of equations,” Phys. Rev. Lett. 103, 150502 (2009).
- [8] Yudong Cao, Anmer Daskin, Steven Frankel, and Sabre Kais, “Quantum circuit design for solving linear systems of equations,” Molecular Physics 110, 15–16 (2011).
- [9] R. Chandra, S.C. Eisenstat, and M.H. Schultz, ”Conjugate Gradient Methods for Partial Differential Equations,” in the Proceedings of the AICA International Symposium on Computer Methods for Partial Differential Equations, Bethlehem, Pennsylvania, June 1975.
- [10] S. Dmitry et al., “The Potential of Quantum Computing and Machine Learning to Advance Clinical Research and Change the Practice of Medicine.” Missouri medicine 115 (5), 463–467 (2018).
- [11] Bojia Duan, Jiabin Yuan, Chao-Hua Yu, Jianbang Huang, and Chang-Yu Hsieh, ”A survey on HHL algorithm: From theory to application in quantum machine learning”, Physics Letters A 384, 126595 (2020).
- [12] Shengbin Wang, Zhimin Wang, Wendong Li, Lixin Fan, Zhiqiang Wei, and Yongjian Gu, “Quantum fast Poisson solver: the algorithm and complete and modular circuit design,” Quantum Information Processing 19, Article number: 170 (2020).
- [13] H. J. Morrell and H. Y. Wong, ”Study of using Quantum Computer to Solve Poisson Equation in Gate Insulators,” 2021 International Conference on Simulation of Semiconductor Processes and Devices (SISPAD), 2021, pp. 69-72, doi: 10.1109/SISPAD54002.2021.9592604.
- [14] Schleich, P., 2019. How to solve a linear system of equations using a quantum computer. [Online]. Available: http://www.acom.rwth-aachen.de/_media/3teaching/ 00projects/schleich.pdf.
- [15] HHL Example using Qiskit. [Online]. Available: https://qiskit.org/textbook/ch-applications/hhl_tutorial.html.
- [16] Gadi Aleksandrowicz, et al., (2019). Qiskit: An Open-source Framework for Quantum Computing (0.7.2). Zenodo. doi.org/10.5281/zenodo.2562111.
- [17] IBM Quantum Site. [Online]. Available: https://quantumcomputing.ibm.com/.
- [18] Dominic W. Berry, Graeme Ahokas, Richard Cleve, and Barry C. Sanders, ”Efficient quantum algorithms for simulating sparse Hamiltonians,” arXiv:quant-ph/0508139, 2007.
- [19] Hiu Yung Wong, Introduction to Quantum Computing: From a Layperson to a Programmer in 30 Steps. Switzerland: Springer Nature, 2022, pp. 170. doi.org/10.1007/978-3-030-98339-0. ISBN-10: 3030983382.
- [20] Danial Dervovic, Mark Herbster, Peter Mountney, Simone Severini, Naïri Usher, and Leonard Wossnig, ”Quantum linear systems algorithms: a primer,” arXiv:1802.08227v1.
- [21] Matlab code and Jupyter Notebook, [Online]. Available: https://github.com/hywong2/HHL_Example.
- [22] Vandenbrocque, Adrien. ”On Quantum Algorithms for Solving Linear Systems of Equations.”Master’s semester Project I”, 2019. [Online]. Available: https://adrienvdb.com/projects-and-reports/.
- [23] National Strategic Overview for Quantum Information Science Report (National Science and Technology Council, 2018). [Online]. Available: https://www.quantum.gov/wp-content/uploads/2020/10/ 2018_NSTC_National_Strategic _Overview_QIS.pdf.
- [24] Aaronson, S. Read the fine print. Nature Phys 11, 291–293 (2015). [Online]. Available: https://doi.org/10.1038/nphys3272