dwave-graphs 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. dwave/graphs/__init__.py +26 -0
  2. dwave/graphs/algorithms/__init__.py +26 -0
  3. dwave/graphs/algorithms/canonicalization.py +161 -0
  4. dwave/graphs/algorithms/clique.py +171 -0
  5. dwave/graphs/algorithms/coloring.py +276 -0
  6. dwave/graphs/algorithms/cover.py +174 -0
  7. dwave/graphs/algorithms/elimination_ordering.py +957 -0
  8. dwave/graphs/algorithms/independent_set.py +193 -0
  9. dwave/graphs/algorithms/markov.py +146 -0
  10. dwave/graphs/algorithms/matching.py +129 -0
  11. dwave/graphs/algorithms/max_cut.py +124 -0
  12. dwave/graphs/algorithms/partition.py +94 -0
  13. dwave/graphs/algorithms/social.py +114 -0
  14. dwave/graphs/algorithms/tsp.py +119 -0
  15. dwave/graphs/drawing/__init__.py +18 -0
  16. dwave/graphs/drawing/chimera_layout.py +325 -0
  17. dwave/graphs/drawing/distinguishable_colors.py +1026 -0
  18. dwave/graphs/drawing/parallel_embeddings.py +250 -0
  19. dwave/graphs/drawing/pegasus_layout.py +338 -0
  20. dwave/graphs/drawing/qubit_layout.py +489 -0
  21. dwave/graphs/drawing/zephyr_layout.py +296 -0
  22. dwave/graphs/exceptions.py +22 -0
  23. dwave/graphs/topologies/__init__.py +18 -0
  24. dwave/graphs/topologies/chimera.py +779 -0
  25. dwave/graphs/topologies/common.py +56 -0
  26. dwave/graphs/topologies/markov.py +116 -0
  27. dwave/graphs/topologies/pegasus.py +1165 -0
  28. dwave/graphs/topologies/zephyr/__init__.py +16 -0
  29. dwave/graphs/topologies/zephyr/coords.py +202 -0
  30. dwave/graphs/topologies/zephyr/graphs.py +633 -0
  31. dwave_graphs-1.0.0.dist-info/METADATA +102 -0
  32. dwave_graphs-1.0.0.dist-info/RECORD +35 -0
  33. dwave_graphs-1.0.0.dist-info/WHEEL +5 -0
  34. dwave_graphs-1.0.0.dist-info/licenses/LICENSE +176 -0
  35. dwave_graphs-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,26 @@
1
+ # Copyright 2018 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import dwave.graphs.topologies
16
+ from dwave.graphs.topologies import *
17
+
18
+ import dwave.graphs.algorithms
19
+ from dwave.graphs.algorithms import *
20
+
21
+ from dwave.graphs.exceptions import *
22
+
23
+ import dwave.graphs.drawing
24
+ from dwave.graphs.drawing import *
25
+
26
+ __version__ = "1.0.0"
@@ -0,0 +1,26 @@
1
+ # Copyright 2026 D-Wave
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from dwave.graphs.algorithms.independent_set import *
16
+ from dwave.graphs.algorithms.canonicalization import *
17
+ from dwave.graphs.algorithms.clique import *
18
+ from dwave.graphs.algorithms.cover import *
19
+ from dwave.graphs.algorithms.matching import *
20
+ from dwave.graphs.algorithms.social import *
21
+ from dwave.graphs.algorithms.elimination_ordering import *
22
+ from dwave.graphs.algorithms.coloring import *
23
+ from dwave.graphs.algorithms.max_cut import *
24
+ from dwave.graphs.algorithms.markov import *
25
+ from dwave.graphs.algorithms.tsp import *
26
+ from dwave.graphs.algorithms.partition import *
@@ -0,0 +1,161 @@
1
+ # Copyright 2018 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import math
16
+
17
+ __all__ = ['canonical_chimera_labeling']
18
+
19
+
20
+ def canonical_chimera_labeling(G, t=None):
21
+ """Returns a mapping from the labels of G to chimera-indexed labeling.
22
+
23
+ Parameters
24
+ ----------
25
+ G : NetworkX graph
26
+ A Chimera-structured graph.
27
+ t : int (optional, default 4)
28
+ Size of the shore within each Chimera tile.
29
+
30
+ Returns
31
+ -------
32
+ chimera_indices: dict
33
+ A mapping from the current labels to a 4-tuple of Chimera indices.
34
+
35
+ """
36
+ adj = G.adj
37
+
38
+ if t is None:
39
+ if hasattr(G, 'edges'):
40
+ num_edges = len(G.edges)
41
+ else:
42
+ num_edges = len(G.quadratic)
43
+ t = _chimera_shore_size(adj, num_edges)
44
+
45
+ chimera_indices = {}
46
+
47
+ row = col = 0
48
+
49
+ # need to find a node in a corner
50
+ root_edge = min(((u, v) for u in adj for v in adj[u]),
51
+ key=lambda edge: len(adj[edge[0]]) + len(adj[edge[1]]))
52
+ root, _ = root_edge
53
+
54
+ horiz, verti = rooted_tile(adj, root, t)
55
+ while len(chimera_indices) < len(adj):
56
+
57
+ new_indices = {}
58
+
59
+ if row == 0:
60
+ # if we're in the 0th row, we can assign the horizontal randomly
61
+ for si, v in enumerate(horiz):
62
+ new_indices[v] = (row, col, 0, si)
63
+ else:
64
+ # we need to match the row above
65
+ for v in horiz:
66
+ north = [u for u in adj[v] if u in chimera_indices]
67
+ assert len(north) == 1
68
+ i, j, u, si = chimera_indices[north[0]]
69
+ assert i == row - 1 and j == col and u == 0
70
+ new_indices[v] = (row, col, 0, si)
71
+
72
+ if col == 0:
73
+ # if we're in the 0th col, we can assign the vertical randomly
74
+ for si, v in enumerate(verti):
75
+ new_indices[v] = (row, col, 1, si)
76
+ else:
77
+ # we need to match the column to the east
78
+ for v in verti:
79
+ east = [u for u in adj[v] if u in chimera_indices]
80
+ assert len(east) == 1
81
+ i, j, u, si = chimera_indices[east[0]]
82
+ assert i == row and j == col - 1 and u == 1
83
+ new_indices[v] = (row, col, 1, si)
84
+
85
+ chimera_indices.update(new_indices)
86
+
87
+ # get the next root
88
+ root_neighbours = [v for v in adj[root] if v not in chimera_indices]
89
+ if len(root_neighbours) == 1:
90
+ # we can increment the row
91
+ root = root_neighbours[0]
92
+ horiz, verti = rooted_tile(adj, root, t)
93
+
94
+ row += 1
95
+ else:
96
+ # need to go back to row 0, and increment the column
97
+ assert not root_neighbours # should be empty
98
+
99
+ # we want (0, col, 1, 0), we could cache this, but for now let's just go look for it
100
+ # the slow way
101
+ vert_root = [v for v in chimera_indices if chimera_indices[v] == (0, col, 1, 0)][0]
102
+
103
+ vert_root_neighbours = [v for v in adj[vert_root] if v not in chimera_indices]
104
+
105
+ if vert_root_neighbours:
106
+
107
+ verti, horiz = rooted_tile(adj, vert_root_neighbours[0], t)
108
+ root = next(iter(horiz))
109
+
110
+ row = 0
111
+ col += 1
112
+
113
+ return chimera_indices
114
+
115
+
116
+ def rooted_tile(adj, n, t):
117
+ horiz = {n}
118
+ vert = set()
119
+
120
+ # get all of the nodes that are two steps away from n
121
+ two_steps = {v for u in adj[n] for v in adj[u] if v != n}
122
+
123
+ # find the subset of two_steps that share exactly t neighbours
124
+ for v in two_steps:
125
+ shared = set(adj[n]).intersection(adj[v])
126
+
127
+ if len(shared) == t:
128
+ assert v not in horiz
129
+ horiz.add(v)
130
+ vert |= shared
131
+
132
+ assert len(vert) == t
133
+ return horiz, vert
134
+
135
+
136
+ def _chimera_shore_size(adj, num_edges):
137
+ # we know |E| = m*n*t*t + (2*m*n-m-n)*t
138
+
139
+ num_nodes = len(adj)
140
+
141
+ max_degree = max(len(adj[v]) for v in adj)
142
+
143
+ if num_nodes == 2 * max_degree:
144
+ return max_degree
145
+
146
+ def a(t):
147
+ return -2*t
148
+
149
+ def b(t):
150
+ return (t + 2) * num_nodes - 2 * num_edges
151
+
152
+ def c(t):
153
+ return -num_nodes
154
+
155
+ t = max_degree - 1
156
+ m = (-b(t) + math.sqrt(b(t)**2 - 4*a(t)*c(t))) / (2 * a(t))
157
+
158
+ if m.is_integer():
159
+ return t
160
+
161
+ return max_degree - 2
@@ -0,0 +1,171 @@
1
+ # Copyright 2018 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import networkx as nx
16
+
17
+ from dwave.graphs.algorithms import maximum_independent_set
18
+
19
+ __all__ = ["maximum_clique", "clique_number", "is_clique"]
20
+
21
+
22
+ def maximum_clique(G, sampler, lagrange=2.0, **sampler_args):
23
+ r"""Returns an approximate maximum clique.
24
+
25
+ A clique in an undirected graph, G = (V, E), is a subset of the vertex set
26
+ :math:`C \subseteq V` such that for every two vertices in C there exists an edge
27
+ connecting the two. This is equivalent to saying that the subgraph
28
+ induced by C is complete (in some cases, the term clique may also refer
29
+ to the subgraph). A maximum clique is a clique of the largest
30
+ possible size in a given graph.
31
+
32
+ This function works by finding the maximum independent set of the compliment
33
+ graph of the given graph G which is equivalent to finding maximum clique.
34
+ It defines a QUBO with ground states corresponding
35
+ to a maximum weighted independent set and uses the sampler to sample from it.
36
+
37
+ Parameters
38
+ ----------
39
+ G : NetworkX graph
40
+ The graph on which to find a maximum clique.
41
+
42
+ sampler : :class:`dimod.Sampler`
43
+ A dimod sampler.
44
+
45
+ lagrange : optional (default 2)
46
+ Lagrange parameter to weight constraints (no edges within set)
47
+ versus objective (largest set possible).
48
+
49
+ sampler_args
50
+ Additional keyword parameters are passed to the sampler.
51
+
52
+ Returns
53
+ -------
54
+ clique_nodes : list
55
+ List of nodes that form a maximum clique, as
56
+ determined by the given sampler.
57
+
58
+ Notes
59
+ -----
60
+ Samplers by their nature may not return the optimal solution. This
61
+ function does not attempt to confirm the quality of the returned
62
+ sample.
63
+
64
+ References
65
+ ----------
66
+
67
+ `Maximum Clique on Wikipedia <https://en.wikipedia.org/wiki/Clique_(graph_theory)>`_
68
+
69
+ `Independent Set on Wikipedia <https://en.wikipedia.org/wiki/Independent_set_(graph_theory)>`_
70
+
71
+ `QUBO on Wikipedia <https://en.wikipedia.org/wiki/Quadratic_unconstrained_binary_optimization>`_
72
+
73
+ Lucas, A. (2014). Ising formulations of many NP problems.
74
+ Frontiers in Physics, Volume 2, Article 5.
75
+ """
76
+ if G is None:
77
+ raise ValueError("Expected NetworkX graph!")
78
+
79
+ # finding the maximum clique in a graph is equivalent to finding
80
+ # the independent set in the complementary graph
81
+ complement_G = nx.complement(G)
82
+ return maximum_independent_set(complement_G, sampler, lagrange, **sampler_args)
83
+
84
+
85
+ def clique_number(G, sampler, lagrange=2.0, **sampler_args):
86
+ r"""Returns the number of vertices in the maximum clique of a graph.
87
+
88
+ A maximum clique is a clique of the largest possible size in a given graph.
89
+ The clique number math:`\omega(G)` of a graph G is the number of
90
+ vertices in a maximum clique in G. The intersection number of
91
+ G is the smallest number of cliques that together cover all edges of G.
92
+
93
+ This function works by finding the maximum independent set of the compliment
94
+ graph of the given graph G which is equivalent to finding maximum clique.
95
+ It defines a QUBO with ground states corresponding
96
+ to a maximum weighted independent set and uses the sampler to sample from it.
97
+
98
+ Parameters
99
+ ----------
100
+ G : NetworkX graph
101
+ The graph on which to find a maximum clique.
102
+
103
+ sampler : :class:`dimod.Sampler`
104
+ A dimod sampler.
105
+
106
+ lagrange : optional (default 2)
107
+ Lagrange parameter to weight constraints (no edges within set)
108
+ versus objective (largest set possible).
109
+
110
+ sampler_args
111
+ Additional keyword parameters are passed to the sampler.
112
+
113
+ Returns
114
+ -------
115
+ clique_nodes : list
116
+ List of nodes that form a maximum clique, as
117
+ determined by the given sampler.
118
+
119
+ Notes
120
+ -----
121
+ Samplers by their nature may not return the optimal solution. This
122
+ function does not attempt to confirm the quality of the returned
123
+ sample.
124
+
125
+ References
126
+ ----------
127
+
128
+ `Maximum Clique on Wikipedia <https://en.wikipedia.org/wiki/Clique_(graph_theory)>`_
129
+ """
130
+ return len(maximum_clique(G, sampler, lagrange, **sampler_args))
131
+
132
+ def is_clique(G, clique_nodes):
133
+ """Determines whether the given nodes form a clique.
134
+
135
+ A clique is a subset of nodes of an undirected graph such that every two
136
+ distinct nodes in the clique are adjacent.
137
+
138
+ Parameters
139
+ ----------
140
+ G : NetworkX graph
141
+ The graph on which to check the clique nodes.
142
+
143
+ clique_nodes : list
144
+ List of nodes that form a clique, as
145
+ determined by the given sampler.
146
+
147
+ Returns
148
+ -------
149
+ is_clique : bool
150
+ True if clique_nodes forms a clique.
151
+
152
+ Example
153
+ -------
154
+ This example checks two sets of nodes, both derived from a
155
+ single Chimera unit cell, for an independent set. The first set is
156
+ the horizontal tile's nodes; the second has nodes from the horizontal and
157
+ verical tiles.
158
+
159
+ >>> import dwave.graphs
160
+ >>> G = dwave.graphs.chimera_graph(1, 1, 4)
161
+ >>> dwave.graphs.is_clique(G, [0, 1, 2, 3])
162
+ False
163
+ >>> dwave.graphs.is_clique(G, [0, 4])
164
+ True
165
+ """
166
+ for x in clique_nodes:
167
+ for y in clique_nodes:
168
+ if x != y:
169
+ if not(G.has_edge(x,y)):
170
+ return False
171
+ return True
@@ -0,0 +1,276 @@
1
+ # Copyright 2018 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from collections.abc import Sequence, Hashable
16
+ from typing import Literal
17
+
18
+ import dimod
19
+ import networkx as nx
20
+
21
+ from dimod.decorators import graph_argument
22
+ from dimod.typing import GraphLike
23
+
24
+ __all__ = ["is_cycle",
25
+ "is_vertex_coloring",
26
+ "min_vertex_coloring",
27
+ "vertex_coloring",
28
+ "zephyr_four_color",
29
+ "pegasus_four_color",
30
+ "chimera_two_color",
31
+ ]
32
+
33
+
34
+ @graph_argument('graph')
35
+ def vertex_coloring(graph: GraphLike,
36
+ colors: int | Sequence[Hashable],
37
+ sampler: dimod.Sampler,
38
+ **sampler_args,
39
+ ) -> dict[Hashable, Hashable]:
40
+ """Returns an approximate vertex coloring.
41
+
42
+ Vertex coloring is the problem of assigning a color to the vertices of a
43
+ graph in a way that no adjacent vertices have the same color.
44
+
45
+ Defines a binary quadratic model with ground states corresponding to valid
46
+ vertex colorings and uses the sampler to sample from it.
47
+
48
+ Args:
49
+ graph:
50
+ The graph on which to find a vertex coloring. Either an integer
51
+ ``n``, interpreted as a complete graph of size ``n``, a nodes/edges
52
+ pair, a list of edges or a NetworkX graph.
53
+
54
+ colors:
55
+ The colors. If an int, the colors are labelled ``[0, n)``. The
56
+ number of colors must be greater or equal to the chromatic number of
57
+ the graph.
58
+
59
+ sampler:
60
+ A dimod sampler.
61
+
62
+ sampler_args:
63
+ Additional keyword parameters are passed to the sampler.
64
+
65
+ Returns:
66
+ A coloring for each vertex in ``graph`` such that no adjacent nodes share
67
+ the same color. A dict of the form ``{node: color, ...}``.
68
+
69
+ """
70
+ if not isinstance(colors, Sequence):
71
+ # assume colors is Integral
72
+ colors = range(int(colors))
73
+
74
+ bqm = dimod.generators.vertex_coloring(graph, colors)
75
+
76
+ # get the lowest energy sample
77
+ sample = sampler.sample(bqm, **sampler_args).first.sample
78
+
79
+ return {v: c for (v, c), val in sample.items() if val}
80
+
81
+
82
+ def min_vertex_coloring(graph: nx.Graph,
83
+ sampler: dimod.Sampler,
84
+ chromatic_lb: int | None = None,
85
+ chromatic_ub: int | None = None,
86
+ **sampler_args
87
+ ) -> dict[Hashable, Hashable]:
88
+ """Returns an approximate minimum vertex coloring.
89
+
90
+ Vertex coloring is the problem of assigning a color to the
91
+ vertices of a graph in a way that no adjacent vertices have the
92
+ same color. A minimum vertex coloring is the problem of solving
93
+ the vertex coloring problem using the smallest number of colors.
94
+
95
+ Defines a binary quadratic model with ground states corresponding to minimum
96
+ vertex colorings and uses the sampler to sample from it.
97
+
98
+ Args:
99
+ graph:
100
+ The graph on which to find a minimum vertex coloring.
101
+
102
+ sampler:
103
+ A dimod sampler.
104
+
105
+ chromatic_lb:
106
+ A lower bound on the chromatic number. If one is not provided, a
107
+ bound is calculated.
108
+
109
+ chromatic_ub:
110
+ An upper bound on the chromatic number. If one is not provided, a
111
+ bound is calculated.
112
+
113
+ sampler_args:
114
+ Additional keyword parameters are passed to the sampler.
115
+
116
+ Returns:
117
+ A coloring for each vertex in ``graph`` such that no adjacent nodes
118
+ share the same color. A dict of the form ``{node: color, ...}``.
119
+
120
+ """
121
+ bqm = dimod.generators.min_vertex_coloring(
122
+ graph, chromatic_lb=chromatic_lb, chromatic_ub=chromatic_ub)
123
+
124
+ # get the lowest energy sample
125
+ sample = sampler.sample(bqm, **sampler_args).first.sample
126
+
127
+ return {v: c for (v, c), val in sample.items() if val}
128
+
129
+
130
+ def is_cycle(graph: nx.Graph) -> bool:
131
+ """Determines whether the given graph is a cycle or circle graph.
132
+
133
+ A cycle graph or circular graph is a graph that consists of a single cycle.
134
+
135
+ https://en.wikipedia.org/wiki/Cycle_graph
136
+
137
+ Args:
138
+ graph: The graph to examine.
139
+
140
+ Returns:
141
+ True if the graph consists of a single cycle.
142
+
143
+ """
144
+ if len(graph) <= 2:
145
+ return False
146
+
147
+ trailing, leading = next(iter(graph.edges))
148
+ start_node = trailing
149
+
150
+ # travel around the graph, checking that each node has degree exactly two
151
+ # also track how many nodes were visited
152
+ n_visited = 1
153
+ while leading != start_node:
154
+ neighbors = graph[leading]
155
+
156
+ if len(neighbors) != 2:
157
+ return False
158
+
159
+ node1, node2 = neighbors
160
+
161
+ if node1 == trailing:
162
+ trailing, leading = leading, node2
163
+ else:
164
+ trailing, leading = leading, node1
165
+
166
+ n_visited += 1
167
+
168
+ # if we haven't visited all of the nodes, then it is not a connected cycle
169
+ return n_visited == len(graph)
170
+
171
+
172
+ def is_vertex_coloring(graph: nx.Graph, coloring: dict[Hashable, Hashable]) -> bool:
173
+ """Determines whether the given coloring is a vertex coloring of graph G.
174
+
175
+ Args:
176
+ graph:
177
+ The graph on which the vertex coloring is applied.
178
+
179
+ coloring:
180
+ A coloring of the nodes of ``graph``. Should be a dict of the form
181
+ ``{node: color, ...}``.
182
+
183
+ Returns:
184
+ True if the given coloring defines a vertex coloring; that is, no
185
+ two adjacent vertices share a color.
186
+
187
+ Example:
188
+ This example colors checks two colorings for a graph, ``graph``, of a
189
+ single Chimera unit cell. The first uses one color (0) for the four
190
+ horizontal qubits and another (1) for the four vertical qubits, in which
191
+ case there are no adjacencies; the second coloring swaps the color of
192
+ one node.
193
+
194
+ >>> graph = dwave.graphs.chimera_graph(1,1,4)
195
+ >>> colors = {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 1}
196
+ >>> dwave.graphs.is_vertex_coloring(graph, colors)
197
+ True
198
+ >>> colors[4]=0
199
+ >>> dwave.graphs.is_vertex_coloring(graph, colors)
200
+ False
201
+
202
+ """
203
+ return all(coloring[u] != coloring[v] for u, v in graph.edges)
204
+
205
+
206
+ def zephyr_four_color(q: tuple[int, int, int, int, int], scheme: Literal[0, 1] = 0) -> int:
207
+ """Returns a node color sufficient for four coloring a Zephyr graph.
208
+
209
+ Args:
210
+ q:
211
+ Qubit label in standard coordinate format: ``(u, w, k, j, z)``.
212
+
213
+ scheme:
214
+ Pattern selector. Two patterns not related by automorphism are
215
+ supported.
216
+
217
+ Returns:
218
+ A color in ``{0, 1, 2, 3}``.
219
+
220
+ Example:
221
+ A mapping of every qubit in a Zephyr[m, t] graph to one of four colors.
222
+
223
+ >>> m = 2
224
+ >>> G = dwave.graphs.zephyr_graph(m, coordinates=True)
225
+ >>> colors = {q: dwave.graphs.zephyr_four_color(q) for q in G.nodes()}
226
+ """
227
+ u, w, _, j, z = q
228
+
229
+ if scheme == 0:
230
+ return j + ((w + 2*(z+u) + j)&2)
231
+ elif scheme == 1:
232
+ return (2*u + w + 2*z + j) & 3
233
+ else:
234
+ raise ValueError('Unknown scheme')
235
+
236
+
237
+ def pegasus_four_color(q: tuple[int, int, int, int]) -> int:
238
+ """Returns a node color sufficient for four coloring a Pegasus graph.
239
+
240
+ Args:
241
+ q:
242
+ Qubit label in standard coordinate format: ``(u, w, k, z)``.
243
+
244
+ Returns:
245
+ A color in ``{0, 1, 2, 3}``.
246
+
247
+ Example:
248
+ A mapping of every qubit in a Pegasus[m] graph to one of four colors.
249
+
250
+ >>> m = 2
251
+ >>> G = dwave.graphs.pegasus_graph(m, coordinates=True)
252
+ >>> colors = {q: dwave.graphs.pegasus_four_color(q) for q in G.nodes()}
253
+ """
254
+ u, w, k, z = q
255
+ return 2 * u + ((k ^ z) & 1)
256
+
257
+
258
+ def chimera_two_color(q: tuple[int, int, int, int]) -> int:
259
+ """Returns a node color sufficient for two coloring a Chimera graph.
260
+
261
+ Args:
262
+ q:
263
+ Qubit label in standard coordinate format: ``(i, j, u, k)``.
264
+
265
+ Returns:
266
+ A color in ``{0, 1}``.
267
+
268
+ Example:
269
+ A mapping of every qubit in a Chimera graph to one of two colors.
270
+
271
+ >>> m = 2
272
+ >>> G = dwave.graphs.chimera_graph(m, coordinates=True)
273
+ >>> colors = {q: dwave.graphs.chimera_two_color(q) for q in G.nodes()}
274
+ """
275
+ i, j, u, _ = q
276
+ return (i ^ j ^ u) & 1