Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Dealing with Constraint Using Portfolio Optimization

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 nn financial assets, each asset labeled i{1,,n}i \in \{1,\ldots,n\}.
  • Every asset’s return is a random variable, with expected value μi\mu_i and variance Σi\Sigma_i (modeling the financial risk involved in the asset).
  • Every two assets iji \neq j have covariance Σij\Sigma_{ij} (modeling market correlation between assets).
  • Every asset ii has a weight wiDi={0,,bi}w_i \in D_i = \{0,\ldots,b_i\} in the portfolio, with bib_i defined as the budget for asset ii (modeling the maximum allowed weight of the asset).
  • The return vector μ\mu, the covariance matrix Σ\Sigma and the weight vector ww are defined naturally from the above (with the domain D=D1×D2××DnD = D_1 \times D_2 \times \ldots \times D_n for ww).
With the above definitions, the total expected return of the portfolio is μTw\mu^T w and the total risk is wTΣww^T \Sigma w. We’ll use a simple difference of the two as our cost function, with the additional constraint that the total sum of assets does not exceed a predefined budget BB. We note that there are many other possibilities for defining a cost function (e.g. add a scaling factor to the risk/return or even some non-linear relation). For reasons of simplicity we select the model below, and we assume all constants and variables are dimensionless. Thus, the problem is, given the constant inputs μ,Σ,D,B\mu, \Sigma, D, B, to find optimal variable ww as follows: minwDwTΣwμTw,\min_{w \in D} w^T \Sigma w - \mu^T w, subject to ΣiwiB\Sigma_{i} w_i \leq B The case presented above is called integer portfolio optimization, since the domains DiD_i are over the (positive) integers. Another variation of this problem defines weights over binary domains, and will not be discussed here.

Finaly, We Will Add Inequality Constraints:

minwDwTΣwμTw,\min_{w \in D} w^T \Sigma w - \mu^T w, subject to: ΣiwiB\Sigma_{i} w_i \leq B We will do it similarly to the equality constraint but we add slack variable that can take multiple values to make sure ΣiwiB\Sigma_{i} w_i \leq B In this case, we will change the objective function as follows: minwDwTΣwμTw+P(Σiwi+slackB)2\min_{w \in D} w^T \Sigma w - \mu^T w + P * (\Sigma_{i} w_i + slack - B)^2 Where PP is the penalty value you need to define.

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

The number of slack qubits needs to reach to get to the number BB.

Define the Expected Return

Define a function that describes μTw\mu^T w where μ\mu is the return vector.

Define the Risk Term

Define a function that describes the risk term in the objective function wTΣww^T \Sigma w where Σ\Sigma is the covariances matrix. minwDwTΣwμTw+P(Σiwi+slackB)2\min_{w \in D} w^T \Sigma w - \mu^T w + P * (\Sigma_{i} w_i + slack - B)^2

Define the Entire Portfolio Optimization Objective Function

Combine the risk term and the expected return functions. There a a term called return coefficient return_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:
Output:

Execution and Post Processing

For the hybrid execution, we use ExecutionSession, 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.

Define a Callback Function to Track the Optimization

Define the Objective Function

Optimize

Look at the Results

Output:

Convergence Graph

Output:
output

Solution

Output:
Output:
Output:
Output:
output