Graph coloring with Grover search
Graph coloring is one of the most fundamental problems in graph theory and computer science, and has a myriad of applications in the real world.
Not only is the coloring problem hard to solve in general even for 3 colors, the best known classical algorithms for 3-coloring require exponential time.
Our goal in this series of blog posts is to design and implement an algorithm for a
Basic Definitions
- A graph
consists of a vertex set and edge setG=(V,E) whose elements are distinct pairsE from\{x,y\} .V - For
, ak\ge 2 -coloring of a graphk is an assignment of colors to verticesG such that edges that are adjacent receive distinct colors! For example, the graph drawn below isf:V\to \{c_1,\ldots,c_k\} -colorable via3 ,f(0)=f(2)=f(4)=c_1 ,f(1)=f(3)=f(5)=c_2 .f(6)=c_3
import networkx as nx
def create_cycle(length: int):
# Returns a nx.Graph() object representing a cycle of prescribed length
graph = nx.Graph()
graph.add_nodes_from(range(length))
graph.add_edges_from([(i,(i+1)%num_vertex) for i in range(length)])
return graph
nx.draw(create_cycle(7), with_labels=True, font_weight='bold')
<Figure size 432x288 with 1 Axes>
The usual way to solve the
We shall take a different route and solve the problem using a quantum circuit from scratch.
Grover's Algorithm and Amplitude Amplification
Recall that the standard Grover's algorithm works in the following three rough steps:
- Initialization: Create a uniform superposition of all possible quantum states.
- Oracle: Apply an oracle that marks a specific state and changes its sign to negative.
- Diffusion: Rotate the state towards the marked state.
Below is a basic circuit that implements Grover's Algorithm to mark the state
The state evolves as follows:
Refer to this page for an exposition on Grover's Algorithm.
Organization of this series of posts
We shall break down and tackle the problem of
- Part 1: Describe a circuit solving the 3-coloring problem for the triangle graph (
).K_3 - Part 2: Implement the circuit for 3-coloring
.K_3 - Part 3.1: Generalize the above circuit for general graphs.
- Part 3.2: Generalize the circuit to a construction for
colors.k
So our goal in this blog post is to describe a quantum circuit that can solve the 3-coloring problem using Grover search.
Part 1: Solving the 3-coloring problem
Below is an illustration of
num_vertex = 3
graph = create_cycle(num_vertex)
num_edges = len(graph.edges())
nx.draw(graph, with_labels=True, font_weight='bold')
<Figure size 432x288 with 1 Axes>
Circuit Construction
1.1: Design
- Our goal is to create a circuit where the output will encode information about the colors given to a vertex. Since 3 colors are used, each vertex would require 2 qubits. Let us use the states
for denoting the three colors corresponding to a given vertex.\mid 01\rangle, \mid 10\rangle, \mid 11\rangle - The oracle would require checking that each edge receives a different color. This implementation would require 3 more qubits corresponding to the number of edges, plus a phase-flip ancilla.
- The diffusion operator needs to reflect and rotate around the subspace spanned by
instead of the full space. This requires redesigning the diffusion operator more carefully than using\{\mid 01\rangle, \mid 10\rangle, \mid 11\rangle and multi-anticontrol (H ) gates.\overline C \cdots \overline CX
In short, we need
1.2: Initialization
We need to figure out a way to send the state
A simple computation shows us that
Hence, for each vertex, we initialize its corresponding qubit pair using
1.3: Oracle
The point of the oracle is to imitate the condition for 3-coloring: For every edge
We can do this for a single edge using the following circuit:
The main idea is to use the phase kickback from the
It can be checked that the state of the system evolves as (here
As we need to make sure that all valid colorings have their amplitudes flipped, we would need to use a single phase flip qubit to flip all the edges, as seen in the circuit diagram below.
1.4: Diffusion
The original role of the diffusion operator in the usual Grover's algorithm is to produce a reflection around the initial state
This requires us to implement the operator
Which is exactly the phase flip oracle that flips the phase of
The complete circuit therefore, is in this Quirk Link (for a single iteration).
Conclusion: Part 1
We have finished designing a circuit that would give a
In the next part, we'll implement this algorithm and run it on both a simulator and a quantum computer.
Of course, due to the sheer number of controlled operations happening in this algorithm, we're pretty much doomed to get terrible results on a real computer, but nevertheless it'd be an interesting experiment to try.
Food for thought
The following issues are yet to be addressed, and the reader is encouraged to think about these issues and play around with the quirk circuit above before reading the next post! :)
- How do we even implement the multi-controlled X gate in the diffusion operator? Most quantum computers and libraries support only upto Toffoli gates. Is it really required, or can we optimize it for our special case of 3-coloring the triangle?
- What about an oracle in general? We claim that our algorithm generalizes to multiple colors, but the construction of the oracle is still an unanswered question.
- How does the requirements of this algorithm scale with the number of qubits? Is its performance worse or better than the QUBO formulation?