Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Background

Given a graph G=(V,E)G = (V,E), find the minimal number of colors k required to properly color it. A coloring is legal if:
  • each vetrex vi{v_i} is assigned with a color ki{0,1,...,k1}k_i \in \{0, 1, ..., k-1\}
  • adajecnt vertex have different colors: for each vi,vjv_i, v_j such that (vi,vj)E(v_i, v_j) \in E, kikjk_i \neq k_j. A graph which is k-colorable but not (k-1)-colorable is said to have chromatic number k.
The maximum bound on the chromatic number is DG+1D_G + 1, where DGD_G is the maximum vertex degree. The graph coloring problem is known to be in the NP-hard complexity class.

Solving the Problem with Classiq

Define the Optimization Problem

We encode the graph coloring with a matrix of variables X with dimensions k×Vk \times |V| using one-hot encoding, such that a Xki=1X_{ki} = 1 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

output

Show the Resulting Pyomo Model

Output:

Setting Up the Classiq Problem Instance

In order to solve the Pyomo model defined above, we use the CombinatorialProblem 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:
We now solve the problem by calling the 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 α\alpha-parameter (quantile) for running CVaR-QAOA, an improved variation of the QAOA algorithm [2]:
Output:
We can check the convergence of the run:
Output:
output

Optimization Results

We can also examine the statistics of the algorithm. In order to get samples with the optimized parameters, we call the sample method:
We will also want to compare the optimized results to uniformly sampled results:
And compare the histograms:
output Let us plot the solution:
Output:
output

References

[1]: Farhi, Edward, Jeffrey Goldstone, and Sam Gutmann. “A quantum approximate optimization algorithm.” arXiv preprint arXiv:1411.4028 (2014). [2]: Barkoutsos, Panagiotis Kl, et al. “Improving variational quantum optimization using CVaR.” Quantum 4 (2020): 256.