- Benchmarking unbalanced penalization using portfolio optimization (file: 1_portfolio_optimization_mdls_comparison.ipynb)
- Presenting an application of portfolio optimization using real stocks from S&P500 and test on real devices. (file: 2_portfolio_optimization_for_SP500.ipynb)
- Making a tool for portfolio optimization visualization (file: 3_portfolio_optimization_visualization.ipynb)
Portfolio optimization problems usually come with a set of constraints, for example, in [1] two inequality constraints are applied to ensure the investment in the assets is below the budget and to guarantee a minimum profit (see Sec. IV A of [1]). Usually, to encode these inequality constraints in quantum computers we require extra qubits (for the slack variables). This has two disadvantages, first the probability of finding optimal solutions decrease and second the number of qubits and the depth of the circuits increases. To solve this problem, I use unbalanced penalization [2], a new encoding method that does not require those extra qubits for the inequality constraints. This technique has been used in the traveling salesman problem, the bin packing problem, and the knapsack problem. I extended the solution to work with portfolio optimization. This method is expected to enhance the probability of finding optimal solutions, reducing the depth of the circuit and the number of qubits required. I solve the problem using QAOA, and VQE and compare it with the results from [1]. Additionally, results on real hardware are presented, and a visualization technique for the optimal solution and the circuit requirements.
In this section, it is used the unbalanced penalization method [2] to encode different portfolio optimization problems subject to the following conditions
- Cost function
- Budget constraint:
- Minimum return constraint
where
The unbalanced penalization method takes inequality constraints of the form
and translate them into
where
This solution gets rid of the slack variables usually used for inequality constraints of QUBOs. It has the advantage of reducing the number of qubits and gates needed and it shows a huge improvement in getting the optimal solution as it is shown in Sec. IV of [2].
In the figure presented below, it is shown the results of the unbalanced penalization and the results obtained in [1](Fig. 2 in that paper) for three different cases. (left) In this case, it shown cases from 5 up to 10 stocks for the portfolio optimization with the budget constraint. The solutions present the approximation ratio vs. the number of QAOA layers. The stars represent the unbalanced penalization method, the dotted X QAOA with X mixer, and the dashed X the Quantum Zeno dynamics with X mixer. (center) same problem but for + mixer. (right) In this case the portfolio optimization includes both constraints, budget constraint and minimum return. Note that in all the cases, the unbalanced penalization method outperform the other methods with approximation rations above 0.9 even with one QAOA layer.
[1] Montanez-Barrera, A., Willsch, D., A., Maldonado-Romo, & Michielsen, K. (2022). Unbalanced penalization: A new approach to encode inequality constraints of combinatorial problems for quantum optimization algorithms. 23–25. http://arxiv.org/abs/2211.13914
[2] Herman, D., Shaydulin, R., Sun, Y., Chakrabarti, S., Hu, S., Minssen, P., Rattew, A., Yalovetzky, R., & Pistoia, M. (2022). Portfolio Optimization via Quantum Zeno Dynamics on a Quantum Processor. http://arxiv.org/abs/2209.15024
In this section, it's presented the solution of portfolio optimization using real data of the S&P500 stock market index. I use the python based library yfinance to get updated stock prices during the last three months. The solution is presented for the portfolio optimization with budget inequality constraint
In the figure presented below, it is shown the results of the unbalanced penalization vs. slack variables encoding for the probability of finding the optimal solution in the portafolio optimization cases using QAOA with 1 to 5 layers. The optimal solution in this case is taking as solution found using docplex. Fig. 1 (left) shows the unbalance penalization vs. the slack variables approach, the performance of the unbalanced penalization is two orders of magnitude greater than that of the slack variables approach. In Fig. 1 (right) it is only shown the performance of the unbalanced penalization approach, for all the cases the results of the unbalanced penalization approach are above 0.1 what means that in 100 shots on a real device, I can fing the optimal solution on average more than 10 times.
Fig.1 Portfolio optimization probability of finding the optimal solution of (left) Unbalanced penalization vs. slack variables, (right) Unbalanced penalization.To have another perspective of the improvement in performance, Fig. 2 shows the ratio between the probability of finding the optimal solution using the unbalanced penalization encoding over the probability using the slack variables approach. The best case is for 10 Stocks with an improvement of more than 1400 times in the probability of finding the optimal solution.
Fig.2 Portfolio optimization probability ratio of finding the optimal solution using unbalanced penalization over slack variables approach on QAOA with different p values.
Additionally, we show the resource requirements of the unbalanced penalization method and the slack variables approach for QAOA with 1 layer. Fig. 3 (left) presents the number of qubits needed for each case (right) presents the number of cnot gates required for a fully connected device. Note that the number of qubits will increase considerably for real devices with no full connectivity because of the swap operations needed to move qubits around. Again, the unbalanced penalization method is a good option from the point of view of saving resources.
Fig.3 Resource requirements for portfolio optimization with 1 layer QAOA using the slack and unbalanced approaches. (left) the number of qubits needed (right) the number of CNOT gates for a fully connected device
To test the generalization capabilities of the unbalanced penalization method. I use a local simulator for up to 17 qubits and the AWS SV1 simulator to run from 18 to 24 stocks. Fig. 4 shows the results for the unbalanced penalization using QAOA and different number of layers p from 1 up to 5. The method as it was shown in 1 has good generalization capabilities because it was training with only two cases 6 and 8 stocks.
Fig.4 Scaling the unbalanced penalization method for the portfolio optimization using QAOA with 1 up to 5 layers.
After 17 qubits it takes a while to get the results on the local computer. Therefore, after this point, I used AWS SV1 to run tasks from 18 to 24 qubits. Fig. 5 shows the results in this case. I reduced the number of maximum iterations of the classical solver from 1000 to 500 and the number of shots to 500. Interestingly, at this point and with a restringed number of shots, I was able to find the optimal solutions.
Fig.5 Scaling the unbalanced penalization method for the portfolio optimization using QAOA with 1 up to 5 layers. The results are from 18 to 24 stocks using AWS-SV1 with 500 shots and maximum classical solver iterations 500
To test if in real devices, I'm still able to obtain similar energy expectation value landscapes compare to the ideal case, I tested AWS-SV1 (ideal) vs. ibm_guadalupe (real) for 5 stocks using QAOA with 1 layer (My idea was to test also ionQ device but it takes 20 minutes to get one solution and the total cases I run were 400 for each device but I could execute at least 9 cases on this device). I have to admit that I was expecting more noise on the real device but the energy landscapes shown in Fig.6 look really similar for both devices. Finally, I show results for the ionQ device for 10 shots and 9 different points in the landscape because the queue time was on average 20 minutes, no real comparison of ionQ can be taken at this point.
Fig.6 Comparison between AWS-SV1, ibm_guadalupe, and ionQ using the unbalanced penalization encoding for 5 stocks and QAOA with 1 layer. For AWS-SV1 and ibm_guadalupe the grid is 20x20 and for ionQ 3x3.
One of the main problems in general with combinatorial optmization problems after been solved by quantum computing algorithms is the visualization stage. For example in the portfolio optimization, after getting a string with zeros and ones, there is one step left and it is know if the solution looks right how it compares with other solutions and so on.
In this section, I try to answer this question with a visualization tool called 'portfolio_optimization_visualization', a graph representation of portfolio optimization. Fig. 1 shows the results of the visualization of one problem with 17 stocks from the S&P 500. In this Fig. the up-left plot shows the different 17 stocks' prices in the last 2 months, Fig. 1 up-right shows the encoding of the portfolio optimization for this specific case, and the marker size for each stock represents the expected return with a blue value as a negative expected return and orange otherwise. The linewidth of the edges encodes the covariance between both stocks it connects. Fig. 1 down-left represents the optimal solution of the problem with red stocks that are chosen and finally, Fig.1 down-right represents the profit vs. risk for different stock configurations with a red mark on the optimal solution.
Fig.1 Visualization of the portfolio optimization problem for 17 stocks (up-left) The stocks prices vs. time obtained from real data of S & P 500 (up-right) The problem encoding marker size represent the proportion of the stock expected return, the width of the edges represent the correlation between different stocks (down-left) Optimal solution configuration, the red colors mean the stocks chosen (down-right) profit vs. risk for the optimal solution in red and other possible solutions in grey.In this project I showed how we can encode combinatorial optimization problems with inequalities constraints using the unbalanced penalization, a tool I designed to remove slack variables. I use this tool on the portfolio optimization problem with two different inequality constraints, one for the maximum budget and another for the minimum expected return. I compare specific models from "Portfolio Optimization via Quantum Zeno Dynamics on a Quantum Processor" and show that the unbalanced penalization shows better results even for a low number of layers. In the second part of the project, I run large cases of portfolio optimization showing that it can generalize. Additionally, in this section, I show the energy landscape for real devices on ibm_guadalupe and ionQ. Finally, I presented a tool to visualize portfolio optimization encoding and visualization.