View on GitHub
Open this notebook in GitHub to run it yourself
Dealing with constraint using portfolio optimization
In this workshop, we will solve the Portfolio Optimization problem using the Quantum Approximate Optimization Algorithm (QAOA), by introducing how to add various types of constraints of the problem to the QAOA algorithm.Guidance for the workshop:
The# TODO or # Your code is there for you to do yourself.
**The # Solution start and # Solution end are only for helping you.
Please delete the Solution and try doing it yourself…**
Portfolio Optimization with the Quantum Approximate Optimization Algorithm (QAOA)
Introduction
Portfolio optimization is the process of allocating a portfolio of financial assets optimally, according to some predetermined goal. Usually, the goal is to maximize the potential return while minimizing the financial risk of the portfolio. One can express this problem as a combinatorial optimization problem like many other real-world problems. In this demo, we’ll show how the Quantum Approximate Optimization Algorithm (QAOA) can be employed on the Classiq platform to solve the problem of portfolio optimization.Modeling the Portfolio Optimization Problem
As a first step, we have to model the problem mathematically. We will use a simple yet powerful model, which captures the essence of portfolio optimization:- A portfolio is built from a pool of financial assets, each asset labeled .
- Every asset’s return is a random variable, with expected value and variance (modeling the financial risk involved in the asset).
- Every two assets have covariance (modeling market correlation between assets).
- Every asset has a weight in the portfolio, with defined as the budget for asset (modeling the maximum allowed weight of the asset).
- The return vector , the covariance matrix and the weight vector are defined naturally from the above (with the domain for ).
First we will solve the problem without adding constraint.
Just:Then, we will add equality constraint:
subject to:Finaly, we will add inequality constraints:
subject to:The Portfolio Optimization Problem Parameters
First we define the parameters of the optimization problem, which include the expected return vector, the covariance matrix, the total budget and the asset-specific budgets.Defining the variables
Define the expected return
Define a function that describes where is thereturn vector.
Define the risk term
Define a function that describes the risk term in the objective function where is thecovariances matrix.
Define the entire portfolio optimization objective function
Combine the risk term and the expected return functions. There a a term called return coefficientreturn_coeff that defines how much you prefer certainly over return.
Higher values is more risky but can be more profitable.
Later try changing it to see how the result changes.
Build the QAOA circuit
Synthesizing and visualizing
Output:
Execution and post processing
For the hybrid execution, we useExecutionSession, which can evaluate the circuit in multiple methods, such as sampling the circuit, giving specific values for the parameters, and evaluating to a specific Hamiltonian, which is very common in chemical applications.
In QAOA, we will use the estimate_cost method, which samples the cost function and returns their average cost from all measurements.
That helps to optimize easily.