Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
In this tutorial, we solve the problem of coloring an undirected graph so that adjacent vertices do not share the same color while satisfying the given constraints. As a toy example, we consider a four-coloring problem for the graph shown below.

Toy Problem

output

Grover Algorithm

We assign red, blue, orange, and green to the unassigned black nodes above under the given constraints. Here, in order to encode the colors in a program, we replace them with integers:
  • red = 0,
  • blue = 1,
  • orange = 2,
  • green =
  1. Therefore, representing a color requires at most two qubits. We assign two qubits to each node as follows.

Oracle Function

Next, we construct the oracle that enforces the rule that no two adjacent nodes may share the same color. This condition can be expressed using propositional logic. Here, we denote the color of each node by a variable a,b,c,d,ea,b,c,d,e, where each alphabet represents the node color. For example, aa refers to the color of node A. Let us first consider node A as a concrete example. Node A is adjacent to three other nodes, so it must satisfy the following constraints: (a≠red)∧(a≠b)∧(a≠blue)(a \ne \text{red}) \land (a \ne b) \land (a \ne \text{blue}) In other words, node A must not be red or blue, and it must also have a different color from its neighbor B. Now consider node B. Since node B is connected to multiple nodes, it has the following constraints: (b≠red)∧(b≠c)∧(b≠e)∧(b≠orange)(b \ne \text{red}) \land (b \ne c) \land (b \ne e) \land (b \ne \text{orange}) Here, ∧\land denotes AND. This means that B must not be red or orange, and it must also have a different color from its neighbors C and E. By applying this idea to each node and summarizing their respective constraints, we can express the oracle in Qmod as follows.

Bulding Block of Grover

Output:
Output:
vars.a vars.b vars.c vars.d vars.e counts probability bitstring
0 2 1 2 0 0 21 0.010254 0000100110
1 3 1 2 0 0 21 0.010254 0000100111
2 2 3 2 3 1 21 0.010254 0111101110
3 2 3 2 3 0 20 0.009766 0011101110
4 2 3 2 0 1 20 0.009766 0100101110
… … … … … … … … …
831 3 1 3 3 3 1 0.000488 1111110111
832 1 2 3 3 3 1 0.000488 1111111001
833 2 2 3 3 3 1 0.000488 1111111010
834 3 2 3 3 3 1 0.000488 1111111011
835 2 3 3 3 3 1 0.000488 1111111110

836 rows × 8 columns

Output:

Post Processing

output