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:
We will also want to compare the optimized results to uniformly sampled results:

Output:
