View on GitHub
Open this notebook in GitHub to run it yourself
Modeling the Portfolio Optimization Problem
First, model the problem mathematically using a simple yet powerful model that captures the essence of portfolio optimization:- A portfolio is built from a pool of financial assets, with 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 ).
Setup
With the mathematical definition in place, begin the implementation by importing necessary packages and classes. Use these external dependencies:- NumPy
- Matplotlib
- Pyomo - a Python framework for modeling optimization problems, which the Classiq platform uses as an interface to these types of problems
classiq package, import classes related to combinatorial optimization and QAOA:
Portfolio Optimization Problem Parameters
Define the parameters of the optimization problem, which include the expected return vector, the covariance matrix, and the total budget:Pyomo Model for the Problem
Define the Pyomo model to use on the Classiq platform, using the problem parameters defined above:Setting Up the Classiq Problem Instance
To solve the Pyomo model defined above, use theCombinatorialProblem Python class.
Under the hood, it translates the Pyomo model to a quantum model of the QAOA algorithm, with the cost Hamiltonian translated from the Pyomo model.
Choose the number of layers for the QAOA ansatz using the num_layers argument and the penalty_factor, which is the coefficient of the constraints term in the cost Hamiltonian:
Synthesizing the QAOA Circuit and Solving the Problem
Synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:Output:
Output:
optimize method of the CombinatorialProblem object.
For the classical optimization part of the QAOA algorithm, define the maximum number of classical iterations (maxiter) and the -parameter (quantile) for running CVaR-QAOA, an improved variation of the QAOA algorithm [3]:
Output:

Optimization Results
Examine the statistics of the algorithm. The optimization is always defined as a minimization problem, so the positive maximization objective is translated to negative minimization by the Pyomo-to-Qmod translator. To get samples with the optimized parameters, call thesample method:
| solution | probability | cost | |
|---|---|---|---|
| 202 | {‘w’: [1, 2, 1], ‘budget_rule_slack_var’: [0, … | 0.000977 | -4.8 |
| 410 | {‘w’: [2, 1, 1], ‘budget_rule_slack_var’: [0, … | 0.000977 | -4.8 |
| 114 | {‘w’: [3, 1, 2], ‘budget_rule_slack_var’: [0, … | 0.001465 | -4.6 |
| 452 | {‘w’: [2, 2, 1], ‘budget_rule_slack_var’: [1, … | 0.000977 | -4.5 |
| 514 | {‘w’: [1, 2, 0], ‘budget_rule_slack_var’: [0, … | 0.000488 | -4.5 |

Output:
Output: