View on GitHub
Open this notebook in GitHub to run it yourself
Background
Given a graph , find the minimal number of colors k required to properly color it. A coloring is legal if:- each vetrex is assigned with a color
- adajecnt vertex have different colors: for each such that , . A graph which is k-colorable but not (k−1)-colorable is said to have chromatic number k.
Solving the problem with classiq
Define the optimization problem
We encode the graph coloring with a matrix of variablesX with dimensions using one-hot encoding, such that a means that vertex i is colored by color k.
We require that each vertex is colored by exactly one color and that 2 adjacent vertices have different colors.
Initialize the model with example graph

show the resulting pyomo model
Output:
Setting Up the Classiq Problem Instance
In order to solve the Pyomo model defined above, we use theCombinatorialProblem python class.
Under the hood it translates the Pyomo model to a quantum model of the QAOA algorithm [1], with cost hamiltonian translated from the Pyomo model. We can choose the number of layers for the QAOA ansatz using the argument num_layers.
Synthesizing the QAOA Circuit and Solving the Problem
We can now synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:Output:
optimize method of the CombinatorialProblem object.
For the classical optimization part of the QAOA algorithm we define the maximum number of classical iterations (maxiter) and the -parameter (quantile) for running CVaR-QAOA, an improved variation of the QAOA algorithm [2]:
Output:
Output:

Optimization Results
We can also examine the statistics of the algorithm. In order to get samples with the optimized parameters, we call thesample method:
| solution | probability | cost | |
|---|---|---|---|
| 957 | {‘x’: [[0, 1, 1, 0, 1], [1, 0, 0, 0, 0], [0, 0… | 0.000488 | 3 |
| 1283 | {‘x’: [[0, 1, 1, 0, 0], [0, 0, 0, 1, 1], [1, 0… | 0.000488 | 3 |
| 1499 | {‘x’: [[1, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 1… | 0.000488 | 3 |
| 376 | {‘x’: [[1, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0… | 0.000488 | 3 |
| 1435 | {‘x’: [[1, 0, 1, 0, 0], [0, 0, 0, 1, 1], [0, 1… | 0.000488 | 3 |

Output:
