|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Complete graph geometries for quantum simulations. |
| 5 | +
|
| 6 | +This module provides classes for representing complete graphs and complete |
| 7 | +bipartite graphs as hypergraphs. These structures are useful for quantum |
| 8 | +systems with all-to-all or bipartite all-to-all interactions. |
| 9 | +""" |
| 10 | + |
| 11 | +from ..utilities import ( |
| 12 | + Hyperedge, |
| 13 | + Hypergraph, |
| 14 | + HypergraphEdgeColoring, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class CompleteGraph(Hypergraph): |
| 19 | + """A complete graph where every vertex is connected to every other vertex. |
| 20 | +
|
| 21 | + In a complete graph K_n, there are n vertices and n(n-1)/2 edges, |
| 22 | + with each pair of distinct vertices connected by exactly one edge. |
| 23 | +
|
| 24 | + Attributes: |
| 25 | + n: Number of vertices in the graph. |
| 26 | +
|
| 27 | + Example: |
| 28 | +
|
| 29 | + .. code-block:: python |
| 30 | + >>> graph = CompleteGraph(4) |
| 31 | + >>> graph.nvertices |
| 32 | + 4 |
| 33 | + >>> graph.nedges |
| 34 | + 6 |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self, n: int, self_loops: bool = False) -> None: |
| 38 | + """Initialize a complete graph. |
| 39 | +
|
| 40 | + Args: |
| 41 | + n: Number of vertices in the graph. |
| 42 | + self_loops: If True, include self-loop edges on each vertex |
| 43 | + for single-site terms. |
| 44 | + """ |
| 45 | + if self_loops: |
| 46 | + _edges = [Hyperedge([i]) for i in range(n)] |
| 47 | + else: |
| 48 | + _edges = [] |
| 49 | + |
| 50 | + # Add all pairs of vertices |
| 51 | + for i in range(n): |
| 52 | + for j in range(i + 1, n): |
| 53 | + _edges.append(Hyperedge([i, j])) |
| 54 | + super().__init__(_edges) |
| 55 | + |
| 56 | + self.n = n |
| 57 | + |
| 58 | + def edge_coloring(self) -> HypergraphEdgeColoring: |
| 59 | + """Compute edge coloring for this complete graph.""" |
| 60 | + coloring = HypergraphEdgeColoring(self) |
| 61 | + for edge in self.edges(): |
| 62 | + if len(edge.vertices) == 1: |
| 63 | + coloring.add_edge(edge, -1) |
| 64 | + else: |
| 65 | + if self.n % 2 == 0: |
| 66 | + i, j = edge.vertices |
| 67 | + m = self.n - 1 |
| 68 | + if j == m: |
| 69 | + coloring.add_edge(edge, i) |
| 70 | + elif (j - i) % 2 == 0: |
| 71 | + coloring.add_edge(edge, (j - i) // 2) |
| 72 | + else: |
| 73 | + coloring.add_edge(edge, (j - i + m) // 2) |
| 74 | + else: |
| 75 | + m = self.n |
| 76 | + i, j = edge.vertices |
| 77 | + if (j - i) % 2 == 0: |
| 78 | + coloring.add_edge(edge, (j - i) // 2) |
| 79 | + else: |
| 80 | + coloring.add_edge(edge, (j - i + m) // 2) |
| 81 | + return coloring |
| 82 | + |
| 83 | + |
| 84 | +class CompleteBipartiteGraph(Hypergraph): |
| 85 | + """A complete bipartite graph with two vertex sets. |
| 86 | +
|
| 87 | + In a complete bipartite graph K_{m,n} (m <= n), there are m + n |
| 88 | + vertices partitioned into two sets of sizes m and n. Every vertex |
| 89 | + in the first set is connected to every vertex in the second set, |
| 90 | + giving m * n edges total. |
| 91 | +
|
| 92 | + Vertices 0 to m-1 form the first set, and vertices m to m+n-1 |
| 93 | + form the second set. |
| 94 | +
|
| 95 | + Attributes: |
| 96 | + m: Number of vertices in the first set. |
| 97 | + n: Number of vertices in the second set. |
| 98 | +
|
| 99 | + Requires: |
| 100 | + m <= n |
| 101 | +
|
| 102 | + Example: |
| 103 | +
|
| 104 | + .. code-block:: python |
| 105 | + >>> graph = CompleteBipartiteGraph(2, 3) |
| 106 | + >>> graph.nvertices |
| 107 | + 5 |
| 108 | + >>> graph.nedges |
| 109 | + 6 |
| 110 | + """ |
| 111 | + |
| 112 | + def __init__(self, m: int, n: int, self_loops: bool = False) -> None: |
| 113 | + """Initialize a complete bipartite graph. |
| 114 | +
|
| 115 | + Args: |
| 116 | + m: Number of vertices in the first set (vertices 0 to m-1). |
| 117 | + n: Number of vertices in the second set (vertices m to m+n-1). |
| 118 | + self_loops: If True, include self-loop edges on each vertex |
| 119 | + for single-site terms. |
| 120 | + """ |
| 121 | + assert m <= n, "Require m <= n for CompleteBipartiteGraph." |
| 122 | + total_vertices = m + n |
| 123 | + |
| 124 | + if self_loops: |
| 125 | + _edges = [Hyperedge([i]) for i in range(total_vertices)] |
| 126 | + |
| 127 | + else: |
| 128 | + _edges = [] |
| 129 | + |
| 130 | + # Connect every vertex in first set to every vertex in second set |
| 131 | + for i in range(m): |
| 132 | + for j in range(m, m + n): |
| 133 | + _edges.append(Hyperedge([i, j])) |
| 134 | + super().__init__(_edges) |
| 135 | + |
| 136 | + self.m = m |
| 137 | + self.n = n |
| 138 | + |
| 139 | + def edge_coloring(self) -> HypergraphEdgeColoring: |
| 140 | + """Compute edge coloring for this complete bipartite graph.""" |
| 141 | + coloring = HypergraphEdgeColoring(self) |
| 142 | + m = self.m |
| 143 | + n = self.n |
| 144 | + for edge in self.edges(): |
| 145 | + if len(edge.vertices) == 1: |
| 146 | + coloring.add_edge(edge, -1) |
| 147 | + else: |
| 148 | + i, j = edge.vertices |
| 149 | + coloring.add_edge(edge, (i + j - m) % n) |
| 150 | + return coloring |
0 commit comments