-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/coloring: Four colorings for Zephyr and Pegasus, and a two coloring for Chimera #243
Changes from 3 commits
9ed868f
389c166
f63283b
0c5a7f2
db513fa
feda6d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,6 +30,7 @@ | |||||
'pegasus_coordinates', | ||||||
'pegasus_sublattice_mappings', | ||||||
'pegasus_torus', | ||||||
'pegasus_four_color', | ||||||
] | ||||||
|
||||||
def pegasus_graph(m, create_using=None, node_list=None, edge_list=None, data=True, | ||||||
|
@@ -538,7 +539,7 @@ def fragmented_edges(pegasus_graph): | |||||
else: | ||||||
yield ((fw1, fw0, u0, k0&1), (fw1, fw0, u1, k1&1)) | ||||||
|
||||||
|
||||||
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# Developer note: we could implement a function that creates the iter_*_to_* and | ||||||
# iter_*_to_*_pairs methods just-in-time, but there are a small enough number | ||||||
# that for now it makes sense to do them by hand. | ||||||
|
@@ -1303,3 +1304,28 @@ def relabel(u,w,k,z): | |||||
G.graph['boundary_condition'] = 'torus' | ||||||
|
||||||
return G | ||||||
|
||||||
def pegasus_four_color(q): | ||||||
""" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Node color assignment sufficient for four coloring of a pegasus graph. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Parameters | ||||||
---------- | ||||||
q : tuple | ||||||
Qubit label in standard coordinate format. | ||||||
|
||||||
Returns | ||||||
------- | ||||||
color : int | ||||||
Colors 0, 1, 2 or 3 | ||||||
Examples | ||||||
======== | ||||||
A mapping of every qubit (default integer labels) in the Pegasus[m] | ||||||
graph to one of 4 colors | ||||||
>>> m = 2 | ||||||
>>> G = dnx.pegasus_graph(m) | ||||||
>>> colors = {q: dnx.pegasus_four_color(dnx.pegasus_coordinates(m).linear_to_zephyr(q)) for q in G.nodes()} # doctest: +SKIP | ||||||
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
""" | ||||||
u, w, k, z = q | ||||||
return 2 * u + ((k ^ z) & 1) |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||
# Copyright 2024 D-Wave Systems Inc. | ||||
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
# | ||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
# | ||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||
# | ||||
# Unless required by applicable law or agreed to in writing, software | ||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
|
||||
import unittest | ||||
|
||||
import dwave_networkx as dnx | ||||
import numpy as np | ||||
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
class TestRegularColoring(unittest.TestCase): | ||||
|
||||
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
def test_valid(self): | ||||
test_cases = {'chimera': [(3,3), (4,4)], | ||||
'pegasus': [(2,), (4,)], | ||||
'zephyr': [(1,2), (3,4)]} | ||||
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
for topology_type, topology_shapes in test_cases.items(): | ||||
if topology_type == 'zephyr': | ||||
graph = dnx.zephyr_graph | ||||
color = dnx.zephyr_four_color | ||||
num_colors = 4 | ||||
elif topology_type == 'pegasus': | ||||
graph = dnx.pegasus_graph | ||||
color = dnx.pegasus_four_color | ||||
num_colors = 4 | ||||
elif topology_type == 'chimera': | ||||
graph = dnx.chimera_graph | ||||
color = dnx.chimera_two_color | ||||
num_colors = 2 | ||||
else: | ||||
raise ValueError('unrecognized topology') | ||||
|
||||
for topology_shape in topology_shapes: | ||||
G = graph(*topology_shape, coordinates=True) | ||||
col_dict = {q: color(q) for q in G.nodes} | ||||
self.assertTrue(np.all(np.unique(list(col_dict.values())) == | ||||
randomir marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
np.arange(num_colors))) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
self.assertTrue(all(col_dict[q1] != col_dict[q2] | ||||
for q1, q2 in G.edges), | ||||
f'{topology_type}[{topology_shape}]') | ||||
|
||||
def test_non_default_schemes(self): | ||||
jackraymond marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
graph = dnx.zephyr_graph | ||||
color = dnx.zephyr_four_color | ||||
num_colors = 4 | ||||
topology_type = 'zephyr' | ||||
topology_shapes = [(2,2), (3,1)] | ||||
for topology_shape in topology_shapes: | ||||
G = graph(*topology_shape, coordinates=True) | ||||
col_dicts = {} | ||||
for scheme in [0,1]: | ||||
col_dict = {q: color(q, scheme=scheme) for q in G.nodes} | ||||
self.assertTrue(np.all(np.unique(list(col_dict.values())) == | ||||
np.arange(num_colors))) | ||||
self.assertTrue(all(col_dict[q1] != col_dict[q2] | ||||
for q1, q2 in G.edges), | ||||
f'{topology_type}[{topology_shape}]') | ||||
col_dicts[scheme] = col_dict | ||||
self.assertFalse(all(col_dicts[0][q] == col_dicts[1][q] for q in G.nodes)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.