common.title
Cloud support

Nobisuke

Dekisugi

RAG


autoQAOA
RAG for dev
Fortune telling app
Annealing
DEEPSCORE
Translation

Overview
Service overview
Terms of service

Privacy policy

Contact
Research

Sign in
Sign up
common.title

Coloring graphs using Grover search: Part 1

Sayan Mukherjee

2021/06/08 03:50

#algorithm #graph theory #grover #coloring #amplitude amplification

1

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 kk-coloring of any arbitrary graph GG.

Basic Definitions

    • A graph G=(V,E)G=(V,E) consists of a vertex set and edge set EE whose elements are distinct pairs {x,y}\{x,y\} from VV.
    • For k2k\ge 2, a kk-coloring of a graph GG is an assignment of colors to vertices f:V{c1,,ck}f:V\to \{c_1,\ldots,c_k\} such that edges that are adjacent receive distinct colors! For example, the graph drawn below is 33-colorable via f(0)=f(2)=f(4)=c1f(0)=f(2)=f(4)=c_1, f(1)=f(3)=f(5)=c2f(1)=f(3)=f(5)=c_2, f(6)=c3f(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>output

The usual way to solve the kk-coloring problem is to transform it into a QUBO problem, which can be then solved using QAOA, and a tutorial on how to solve this problem using Blueqat-SDK is available here: EN /JP .

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 11\mid 11\rangle. The state evolves as follows: 000H3+++I2×Z++CCX(00+01+10112)Diffusion11.\mid 000\rangle \stackrel{H^{\otimes 3}}\longrightarrow \mid +++\rangle \stackrel{I^{\otimes 2}\times Z}\longrightarrow \mid ++\rangle \mid -\rangle \stackrel{CCX}\longrightarrow \left(\frac{\mid 00\rangle + \mid 01\rangle + \mid 10\rangle - \mid 11\rangle}{\sqrt2}\right)\mid -\rangle\stackrel{\text{Diffusion}}\longrightarrow \mid11\rangle\mid -\rangle. Grover-example

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 kk-coloring a graph in the following parts:

    • Part 1: Describe a circuit solving the 3-coloring problem for the triangle graph (K3K_3).
    • Part 2: Implement the circuit for 3-coloring K3K_3.
    • Part 3.1: Generalize the above circuit for general graphs.
    • Part 3.2: Generalize the circuit to a construction for kk colors.

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 K3K_3:

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>output

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 01,10,11\mid 01\rangle, \mid 10\rangle, \mid 11\rangle for denoting the three colors corresponding to a given vertex.
    • 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 {01,10,11\{\mid 01\rangle, \mid 10\rangle, \mid 11\rangle instead of the full space. This requires redesigning the diffusion operator more carefully than using HH and multi-anticontrol (CCX\overline C \cdots \overline CX) gates.

In short, we need 2\cdot \text{num_vertices} + \text{num_edges} + 1 = 10 qubits.

1.2: Initialization

We need to figure out a way to send the state 00\mid 00\rangle to the state 13(01+10+11)\frac 1{\sqrt3} (\mid01\rangle + \mid 10\rangle + \mid11\rangle). In advanced libraries such as qiskit, this can be achieved by built-in functions. However, these gates are not available on real systems, so we shall quickly figure out how to achieve this construction. Let E3E_3 denote the circuit below:

E3-circuit

A simple computation shows us that E300E_3\mid 00\rangle is a uniform superposition of the states 01,10\mid01\rangle, \mid10\rangle and 11\mid11\rangle: 00Ry(sin1(23))I1300+2310CH1300+2312(10+11)X1301+1311+1310.\begin{aligned}\mid 00\rangle \stackrel{R_y(\sin^{-1}\left(\frac{\sqrt2}{\sqrt3}\right))\otimes I}{\longrightarrow} \frac 1{\sqrt3}\mid 00\rangle + \frac{\sqrt 2}{\sqrt 3}\mid 10\rangle&\stackrel{CH}{\longrightarrow} \frac 1{\sqrt3}\mid00\rangle + \frac{\sqrt2}{\sqrt3}\cdot \frac1{\sqrt2}(\mid 10\rangle + \mid 11\rangle)\\& \stackrel{X}{\longrightarrow} \frac 1{\sqrt 3}\mid01\rangle + \frac 1{\sqrt3}\mid 11\rangle + \frac 1{\sqrt3} \mid 10\rangle.\end{aligned}

Hence, for each vertex, we initialize its corresponding qubit pair using E3E_3.

1.3: Oracle

The point of the oracle is to imitate the condition for 3-coloring: For every edge {u,v}E(K3)\{u,v\}\in E(K_3), the states that we need to flip the signs of are 0110,0111,1001,1011,1101\mid 01\rangle\mid10\rangle, \mid01\rangle\mid11\rangle, \mid10\rangle\mid01\rangle, \mid10\rangle\mid11\rangle,\mid11\rangle\mid01\rangle and 1110\mid11\rangle\mid10\rangle. We can do this for a single edge using the following circuit:

oracle-flip

The main idea is to use the phase kickback from the \mid-\rangle state in the last qubit to switch the signs of the six states mentioned above. It can be checked that the state of the system evolves as (here i,j{01,10,11}i,j\in\{01, 10, 11\}):

coloring-eq

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.

circuit-initialization-and-oracle

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 0n\mid 0\rangle^{\otimes n}. However, since we are restricting ourselves to the state u:=13(01+10+11)\mid u \rangle := \frac 1{\sqrt3} (\mid 01\rangle + \mid 10\rangle + \mid 11\rangle), we have to implement a reflection around un\mid u\rangle^{\otimes n}.

This requires us to implement the operator D=2ununID = 2\mid u^n\rangle\langle u^n\mid - I. Observe that, E3nDE3n=2E3nununE3nI=200n00nI,{E_3^\dagger}^{\otimes n}DE_3^{\otimes n} = 2{E_3^\dagger}^{\otimes n}\mid u^n\rangle\langle u^n\mid E_3^{\otimes n}- I = 2\mid 00^n\rangle \langle 00^n\mid - I,

Which is exactly the phase flip oracle that flips the phase of 02n\mid 0\rangle^{\otimes 2n}! So, we need to use the same diffuser as Grover's algorithm, but instead of hadamard gates we use E3E_3 and E3E_3^\dagger gates.

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 33-coloring of the triangle graph K3K_3. 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?

© 2024, blueqat Inc. All rights reserved