Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
[GraphColoring] Add Notebook frontend for part IV (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivanwin authored Jan 11, 2022
1 parent 9c6fc56 commit bcf92fa
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 8 deletions.
232 changes: 229 additions & 3 deletions GraphColoring/GraphColoring.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part II. Vertex coloring problem"
"## Part II. Vertex coloring problem\n",
"The vertex graph coloring is a coloring of graph vertices which \n",
"labels each vertex with one of the given colors so that \n",
"no two vertices of the same color are connected by an edge."
]
},
{
Expand Down Expand Up @@ -375,7 +378,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part III. Vertex coloring problem"
"## Part III. Weak coloring problem\n",
"Weak graph coloring is a coloring of graph vertices which \n",
"labels each vertex with one of the given colors so that \n",
"each vertex is either isolated or is connected by an edge\n",
"to at least one neighbor of a different color."
]
},
{
Expand Down Expand Up @@ -581,7 +588,7 @@
"\n",
" 1. The number of vertices in the graph $V$ ($V \\leq 6$).\n",
"\n",
" 2. An array of $E$ tuples of integers, representing the edges of the graph (E $\\leq$ 12). \n",
" 2. An array of $E$ tuples of integers, representing the edges of the graph ($E \\leq 12$). \n",
"Each tuple gives the indices of the start and the end vertices of the edge. \n",
"The vertices are indexed $0$ through $V - 1$.\n",
"\n",
Expand Down Expand Up @@ -660,6 +667,225 @@
"source": [
"*Can't come up with a solution? See the explained solution in the [Graph Coloring Workbook](./Workbook_GraphColoring.ipynb#Task-3.6.-Using-Grover's-search-to-find-weak-coloring).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part IV. Triangle-free coloring problem\n",
"\n",
"Triangle-free graph coloring is a coloring of graph edges which labels each edge with one of two colors so that no three edges of the same color form a triangle."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 4.1. Convert the list of graph edges into an adjacency matrix\n",
"\n",
"**Inputs:**\n",
"\n",
" 1. The number of vertices in the graph $V$ ($V \\leq 6$).\n",
" \n",
" 2. An array of $E$ tuples of integers, representing the edges of the graph ($E \\leq 12$). Each tuple gives the indices of the start and the end vertices of the edge. The vertices are indexed $0$ through $V - 1$.\n",
"\n",
"**Output:** \n",
"A 2D array of integers representing this graph as an adjacency matrix: the element [i][j] should be $-1$ if the vertices i and j are not connected with an edge, or store the index of the edge if the vertices i and j are connected with an edge. Elements [i][i] should be $-1$ unless there is an edge connecting vertex i to itself.\n",
"\n",
"**Example:**\n",
"Consider a graph with $V = 3$ and edges = `[(0, 1), (0, 2), (1, 2)]`.\n",
"The adjacency matrix for it would be:\n",
"\n",
"$$\n",
"\\begin{bmatrix}\n",
"-1 & 0 & 1 \\\\\n",
" 0 & -1 & 2 \\\\\n",
" 1 & 2 & -1\n",
"\\end{bmatrix}\n",
"$$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T41_EdgesListAsAdjacencyMatrix\n",
"\n",
"function EdgesListAsAdjacencyMatrix (V : Int, edges : (Int, Int)[]) : Int[][] {\n",
" // ...\n",
" return [];\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 4.2. Extract a list of triangles from an adjacency matrix\n",
"\n",
"\n",
"**Inputs:**\n",
"\n",
" 1. The number of vertices in the graph $V (V \\leq 6)$.\n",
" \n",
" 2. An adjacency matrix describing the graph in the format from task 4.1.\n",
" \n",
"**Output:** \n",
"\n",
"An array of 3-tuples listing all triangles in the graph, that is, all triplets of vertices connected by edges. Each of the 3-tuples should list the triangle vertices in ascending order, and the 3-tuples in the array should be sorted in ascending order as well.\n",
"\n",
"**Example:** \n",
"\n",
"Consider the adjacency matrix\n",
"$$\n",
"\\begin{bmatrix}\n",
"-1 & 0 & 1 \\\\\n",
" 0 & -1 & 2 \\\\\n",
" 1 & 2 & -1\n",
"\\end{bmatrix}\n",
"$$\n",
"\n",
"The list of triangles for it would be `[(0, 1, 2)]`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T42_AdjacencyMatrixAsTrianglesList\n",
"\n",
"function AdjacencyMatrixAsTrianglesList (V : Int, adjacencyMatrix : Int[][]) : (Int, Int, Int)[] {\n",
" // ...\n",
" return [];\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 4.3. Classical verification of triangle-free coloring\n",
"**Inputs:**\n",
" 1. The number of vertices in the graph $V (V \\leq 6)$.\n",
" \n",
" 2. An array of $E$ tuples of integers, representing the edges of the graph $(E \\leq 12)$. Each tuple gives the indices of the start and the end vertices of the edge. The vertices are indexed $0$ through $V - 1$.\n",
" \n",
" 3. An array of $E$ integers, representing the edge coloring of the graph. $i^{th}$ element of the array is the color of the edge number $i$, and it is $0$ or $1$. The colors of edges in this array are given in the same order as the edges in the \"edges\" array.\n",
"\n",
"**Output:** \n",
"\n",
"`true` if the given coloring is triangle-free (i.e., no triangle of edges connecting 3 vertices has all three edges in the same color), and `false` otherwise.\n",
"\n",
"**Example:** \n",
"\n",
"Consider a graph with $V = 3$ and edges = `[(0, 1), (0, 2), (1, 2)]`. Some of the valid colorings for it would be `[0, 1, 0]` and `[-1, 5, 18]`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T43_IsVertexColoringTriangleFree\n",
"\n",
"function IsVertexColoringTriangleFree (V : Int, edges: (Int, Int)[], colors: Int[]) : Bool {\n",
" // ...\n",
" return true;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 4.4. Oracle to check that three colors don't form a triangle (f(x) = 1 if at least two of three input bits are different)\n",
"\n",
"**Inputs:**\n",
"\n",
" 1. a 3-qubit array `inputs`,\n",
" \n",
" 2. a qubit `output`.\n",
" \n",
"**Goal:** \n",
"\n",
"Flip the output qubit if and only if at least two of the input qubits are different.\n",
"\n",
"**Example:** \n",
"\n",
"For the result of applying the operation to state $(|001\\rangle + |110\\rangle + |111\\rangle) \\otimes |0\\rangle$ will be $|001\\rangle \\otimes |1\\rangle + |110\\rangle \\otimes |1\\rangle + |111\\rangle \\otimes |0\\rangle$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T44_ValidTriangleOracle\n",
"\n",
"operation ValidTriangleOracle (inputs : Qubit[], output : Qubit) : Unit is Adj+Ctl {\n",
" // ...\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Task 4.5. Oracle for verifying triangle-free edge coloring (f(x) = 1 if the graph edge coloring is triangle-free)\n",
"\n",
"**Inputs:**\n",
" 1. The number of vertices in the graph $V (V \\leq 6)$.\n",
" \n",
" 2. An array of $E$ tuples of integers `edges`, representing the edges of the graph ($0 \\leq E \\leq V(V-1)/2$). Each tuple gives the indices of the start and the end vertices of the edge. The vertices are indexed $0$ through $V - 1$. The graph is undirected, so the order of the start and the end vertices in the edge doesn't matter.\n",
" \n",
" 3. An array of $E$ qubits `colorsRegister` that encodes the color assignments of the edges. Each color will be $0$ or $1$ (stored in 1 qubit). The colors of edges in this array are given in the same order as the edges in the `edges` array.\n",
" \n",
" 4. A qubit `target` in an arbitrary state.\n",
"\n",
"**Goal:** \n",
"\n",
"Implement a marking oracle for function $f(x) = 1$ if the coloring of the edges of the given graph described by this colors assignment is triangle-free, i.e., no triangle of edges connecting 3 vertices has all three edges in the same color.\n",
"\n",
"In this task you are not allowed to use quantum gates that use more qubits than the number of edges in the graph,\n",
"unless there are 3 or less edges in the graph. For example, if the graph has 4 edges, you can only use 4-qubit gates or less.\n",
"You are guaranteed that in tests that have 4 or more edges in the graph the number of triangles in the graph \n",
"will be strictly less than the number of edges.\n",
"\n",
"**Example:** \n",
"A graph with 3 vertices and 3 edges `[(0, 1), (1, 2), (2, 0)]` has one triangle. \n",
"The result of applying the operation to state $\\frac{1}{\\sqrt{3}} \\big(|001\\rangle + |110\\rangle + |111\\rangle\\big) \\otimes |0\\rangle$ will be $\\frac{1}{\\sqrt{3}} \\big(|001\\rangle \\otimes |1\\rangle + |110\\rangle \\otimes |1\\rangle + |111\\rangle \\otimes |0\\rangle\\big)$.\n",
"The first two terms describe triangle-free colorings, and the last term describes a coloring where all edges of the triangle have the same color.\n",
"\n",
"<br/>\n",
"<details>\n",
" <summary><b>Need a hint? Click here</b></summary>\n",
" Make use of functions and operations you've defined in previous tasks. \n",
"</details>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%kata T45_TriangleFreeColoringOracle\n",
"\n",
"operation TriangleFreeColoringOracle (\n",
" V : Int, \n",
" edges : (Int, Int)[], \n",
" colorsRegister : Qubit[], \n",
" target : Qubit\n",
") : Unit is Adj+Ctl {\n",
" // ...\n",
"}"
]
}
],
"metadata": {
Expand Down
10 changes: 5 additions & 5 deletions GraphColoring/Tasks.qs
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,17 @@ namespace Quantum.Kata.GraphColoring {
// the coloring of the edges of the given graph described by this colors assignment is triangle-free,
// i.e., no triangle of edges connecting 3 vertices has all three edges in the same color.
//
// In this task you are not allowed to use quantum gates that use more qubits than the number of edges in the graph,
// unless there are 3 or less edges in the graph. For example, if the graph has 4 edges, you can only use 4-qubit gates or less.
// You are guaranteed that in tests that have 4 or more edges in the graph the number of triangles in the graph
// will be strictly less than the number of edges.
//
// Example: a graph with 3 vertices and 3 edges [(0, 1), (1, 2), (2, 0)] has one triangle.
// The result of applying the operation to state (|001⟩ + |110⟩ + |111⟩)/√3 ⊗ |0⟩
// will be 1/√3|001⟩ ⊗ |1⟩ + 1/√3|110⟩ ⊗ |1⟩ + 1/√3|111⟩ ⊗ |0⟩.
// The first two terms describe triangle-free colorings,
// and the last term describes a coloring where all edges of the triangle have the same color.
//
// In this task you are not allowed to use quantum gates that use more qubits than the number of edges in the graph,
// unless there are 3 or less edges in the graph. For example, if the graph has 4 edges, you can only use 4-qubit gates or less.
// You are guaranteed that in tests that have 4 or more edges in the graph the number of triangles in the graph
// will be strictly less than the number of edges.
//
// Hint: Make use of functions and operations you've defined in previous tasks.
operation TriangleFreeColoringOracle (
V : Int,
Expand Down

0 comments on commit bcf92fa

Please sign in to comment.