View on GitHub
Open this notebook in GitHub to run it yourself
- Introduction - portfolio optimization
1.1 Portfolio optimization problem as a linear equation
As a first step, we have to model the problem mathematically:- A portfolio is built from a pool of financial assets.
- The assets’ return values are random variables, whose statistical properties are represented by the expected return vector and a covariance matrix .
- The prices of the assets today are represented by the prices vector .
- The portfolio has an allocation vector , representing the weight of each asset in the portfolio —
- this is the variable we would like to find.
- Finally, the portfolio problem has two constants: a desired total return , and a total budget .
- The HHL algorithm
2.1 The basic HHL algorithm
The HHL algorithm [2] (after Harrow, Hassidim, and Lloyd) is a quantum linear solver, treating problems which can be defined by Eq. (1). The basic implementation assumes that the is a normalized vector of size , and that is an Hermitian matrix of size , whose eigenvalues are in the interval . For the mathematical introduction we will follow these assumptions, whereas in the following sections we show how to tweek the quantum model in order to relax them. The basic HHL algorithm contains 4 functional blocks:- State Preparation for the vector : Let us assume that is of size , then:
- Quantum Phase Estimation (QPE) for the unitary : This quantum block approximates the eigenvalues of the matrix , , into a register of size . If we write the vector in the basis of eigenvectors of , , then the QPE stage gives
2.1.1 Getting the result - a swap-test example
Looking at Eq. (6) we can see that the result vector is coupled to the indicator state (up to normalization with the known constant ): In order to obtain the solution from the quantum circuit we must measure the complete statevector, however, this exponential readout procedure halts quantum advantage. In the framework of portfolio optimization, it was suggested that one can use a swap-test [4] to compare the result to some guess of the solution. Let us write the quantum state at the final stage of the HHL algorithm, Eq. (6), as , where we neglect the QPE register, and is some non-normalized state. For the swap-test we add an extra test qubit and we need to prepare a quantum register with the guessed solution . Applying the swap-test gives the final state: From the above equation we find that the probability of measuring 1 on the HHL indicator qubit and 0 on the test qubit is From this equation one can determine the desired overlap .2.2 A hybrid approach
The hybrid approach [3] comes to facilitate the Eigenvalue Inversion function which is not easy to implement. This quantum block can be obtained using multi-controlled RY rotations: However, to apply this procedure we should have a quantum block which caclulates , and such general arithmetics requires many qubits. Another approach is to use a generic amplitude loading for the function , but this procedure contains exponential number, , of multi-controlled rotations (the gray-code approch might reduce the gate count by some factor, but the total gate count is still ). The hybrid approach containes three steps:- QPE of on the State Preparation of : This is simply the first two steps of the HHL algorithm, corresponding to the final state .
- Classical post-process: We analyze the results in the context of the following two facts: (i) The matrix has eigenvalues, whereas the QPE gives approximation of these over different values. (ii) Not all the eigenvalues of are relevant to the solution, but only eigenvalues such that , with being some user-defined tolarance.
- A full HHL routine: We now apply a usual HHL algorithm, where the Eigenvalue Inversion block is implemented by hard-coded multi-controlled-RY rotation according to the specific values found in the previous step. Moreover, we can represent the relevant eigenvalues with a smaller binary represantation of size .

- Eigenvalue Inversion Quantum functions
- Full eigenvalue inversion function
assign_amplitude_table.
- Partial eigenvalue inversion, with prescribed values
- Moreover, we take into account a situation where the rotation angles are given with higher precision than the eigenvalues stored in the phase variable returning from the QPE.
- Taking a specific example
n_unitary=2 qubits, for more complex examples see Sec. 8):
4.1 Preparing the vector
We shall normalize the vector before loading it into a quantum register. In addition, the user can control the functional error of this function block.4.2 Calculating the maximal eigenvalue
As we will see below, the HHL algorithm assumes that we have some bound on the maximal eigenvalue (in absolute value) of the matrix. For the sake of demonstration, we will perform a full classical eigenvalue decomposition. This also serves us for verifying the QPE in Step4.3 Preparing the unitary from the matrix for the Quantum Phase Estimation
The QPE function gets a phase quantum variable, whose size refers to the precision of the eigenphases estimations. We set it with to- (note that this number corresponds to the condition number of the matrix ).
Output:
mat_rescaling, mat_shift, and w_min respectively.
- Solving with a Hybrid HHL
unitary for decomposing the unitary into quantum gates.
Note that this procedure is not scalable, as the decomposition is exponential in the number of qubits. In addition, the classical calculation is equivalent to solving the linear equation. Yet, this approach is good for small-scale problems.
One relevant way to implement the unitary is with Hamiltonian simulation, see Sec. (8)
—
- Step 1: a QPE applied on the StatePreparation of
Output:

- Step 2: Post-Processing the results to get data for the eigenvalue inversion
qpe_eigs: the values of the QPE eigenvalues ,original_eigs: the values of the original matrix eigenvalues,eigs_prob: the probability of each eigenvalue, this corresponds to in Eq. (4).projected_x: the effect of each phase on the solution , given by .
Output:

QPE_SIZE=4, resulting in values. We can take the two most significant eigenvalues for which we will apply the eigenvalue inversion.
First, find the the 4 eigenvalues.
From the upper graph we can see 4 local maxima, which can be attributed for the four eigenvalues in our problem.
Output:

- Step 3: A full HHL routine, including a swap test with the classical solution
indicatorof size
- When measured 1 it means the unitary register collapsed to the linear problem solution.
testof size
- The test qubit of the swap test.
5.
- Synthesizing the quantum model and executing the resulting quantum program
max_width=12 qubits, and ask for optimization over depth.
Output:


5.
- Post-processing the results
- Following Eq. (9) we have:
Output:
5.
- Synthesizing with different constraints
Output:

- Comparing with a basic HHL approach
simple_eig_inversion function defined in Sec 3.
Output:
- Summary
| num. qubits | depth | cx_count | fidelity | |
|---|---|---|---|---|
| hyb. HHL C (depth) | 12 | 416 | 260 | 0.909921 |
| hyb. HHL C (width) | 9 | 443 | 268 | 0.909921 |
| basic HHL | 10 | 477 | 304 | 0.951662 |
- Comments
8.1 Hamiltonian simulation
As mentioned above, the correct way to implement the unitary on which we apply the QPE is Hamiltonian simulation. One way to apply this quantum function is with the built-inexponentiation_with_depth_constraint function.
This includes another level of approximation to our algorithm.
The exponentiation is implemented with Trotter-Suzuki product.
Estimating the functional error of such implementation is a hard problem. An indirect way to manage the error is by increasing the depth of Exponentiation block in order to allow the engine to explore higher order Trotter-Suzuki products.
Another option is to use suzuki_trotter functions directly.
Both functions are then should be plugged it into qpe or qpe_flexible function, instead of plugging the built-in unitary; see HHL notebook.
8.2 Approximating the maximal relevant eigenvalue
In Ref.[5] it was shown how one can use an hybrid quantum-classical routine in order to increase the precision in the eigenvalue resolution in step 1 of the hybrid HHL. The algorithm is based on running this step with various evolution coefficients in , and find the optimal one (in a sense that we increase the resolution in the eigenvalue estimation, while using the sameqpe_size ).
Note that while this algorithm increases eigenvalue precision it increases the evolution coeffecient, as typically .
This in turn requires more resources for performing well-approximated Exponentiation.
8.3 Using iterative QPE
In Ref.[5] it was shown how one can use an iterative-QPE approach to perform step 1 in the hybrid HHL routine. This allows to get an estimate of the eigenvalues with only one qubit, instead ofqpe_size qubits.