Skip to content
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

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions dwave_networkx/generators/chimera.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'linear_to_chimera',
'chimera_sublattice_mappings',
'chimera_torus',
'chimera_two_color',
]


Expand Down Expand Up @@ -866,3 +867,30 @@ def chimera_torus(m, n=None, t=None, node_list=None, edge_list=None):
G.graph['boundary_condition'] = 'torus'

return G


def chimera_two_color(q):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
"""Node color assignment sufficient for four coloring of a Chimera graph.

Node color assignment sufficient for four coloring of a Chimera graph.
jackraymond marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
q : tuple
Qubit label in standard coordinate format: i, j, u, k

Returns
-------
color : int
Colors 0, 1, 2 or 3

Examples
========
A mapping of every qubit (default integer labels) in the Chimera[m, t]
graph to one of 2 colors
>>> m = 2
>>> G = dnx.chimera_graph(m)
>>> colors = {q: dnx.chimera_two_color(dnx.chimera_coordinates(m,t).linear_to_chimera(q)) for q in G.nodes()} # doctest: +SKIP

"""
i, j, u, _ = q
return (i ^ j ^ u) & 1
28 changes: 27 additions & 1 deletion dwave_networkx/generators/pegasus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Expand Down Expand Up @@ -1303,3 +1304,28 @@ def relabel(u,w,k,z):
G.graph['boundary_condition'] = 'torus'

return G

def pegasus_four_color(q):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Node color assignment sufficient for four coloring of a pegasus graph.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.


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)
35 changes: 34 additions & 1 deletion dwave_networkx/generators/zephyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
__all__ = ['zephyr_graph',
'zephyr_coordinates',
'zephyr_sublattice_mappings',
'zephyr_torus'
'zephyr_torus',
'zephyr_four_color',
]

def zephyr_graph(m, t=4, create_using=None, node_list=None, edge_list=None,
Expand Down Expand Up @@ -797,3 +798,35 @@ def relabel(u, w, k, j, z):
G.graph['boundary_condition'] = 'torus'

return G


def zephyr_four_color(q, scheme=0):
"""
jackraymond marked this conversation as resolved.
Show resolved Hide resolved
Node color assignment sufficient for four coloring of a Zephyr graph.
jackraymond marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
q : tuple
Qubit label in standard coordinate format: u, w, k, j, z
scheme : int
Two patterns not related by automorphism are supported
Returns
-------
color : int
Colors 0, 1, 2 or 3
Examples
========
A mapping of every qubit (default integer labels) in the Zephyr[m, t]
graph to one of 4 colors
>>> m = 2
>>> G = dnx.zephyr_graph(m)
>>> colors = {q: dnx.zephyr_four_color(dnx.zephyr_coordinates(m,t).linear_to_zephyr(q)) for q in G.nodes()} # doctest: +SKIP

"""
u, w, _, j, z = q

if scheme == 0:
return j + ((w + 2*(z+u) + j)&2)
else:
jackraymond marked this conversation as resolved.
Show resolved Hide resolved
return (2*u + w + 2*z + j) & 3

69 changes: 69 additions & 0 deletions tests/test_regular_coloring.py
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)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
np.arange(num_colors)))

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