Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions dwave/graphs/generators/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2026 D-Wave
#
# 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.

from dwave.graphs.generators.common.common import *
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Copyright 2026 D-Wave
#
# 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.

__all__ = ["_add_compatible_edges", "_add_compatible_nodes", "_add_compatible_terms"]


def _add_compatible_edges(G, edge_list):
# Check edge_list defines a subgraph of G and create subgraph.
Expand All @@ -11,6 +27,7 @@ def _add_compatible_edges(G, edge_list):
if G.number_of_edges() < len(edge_list):
raise ValueError('edge_list contains duplicates.')


def _add_compatible_nodes(G, node_list):
if node_list is not None:
if not all(G.has_node(n) for n in node_list):
Expand All @@ -20,7 +37,8 @@ def _add_compatible_nodes(G, node_list):
G.remove_nodes_from(remove_nodes)
if G.number_of_nodes() < len(node_list):
raise ValueError('node_list contains duplicates.')



def _add_compatible_terms(G, node_list, edge_list):
_add_compatible_edges(G, edge_list)
_add_compatible_nodes(G, node_list)
Expand Down
16 changes: 16 additions & 0 deletions dwave/graphs/generators/zephyr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2026 D-Wave
#
# 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.

from dwave.graphs.generators.zephyr.coords import *
from dwave.graphs.generators.zephyr.graphs import *
195 changes: 195 additions & 0 deletions dwave/graphs/generators/zephyr/coords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Copyright 2026 D-Wave
#
# 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.

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

__all__ = ["zephyr_coordinates"]


from dwave.graphs.generators.zephyr.graphs import zephyr_graph


class zephyr_coordinates(object):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is directly copied over from the former zephyr.py, now zephyr/graphs.py below.

"""Provides coordinate converters for the Zephyr indexing schemes.

Parameters
----------
m : int
Grid parameter for the Zephyr lattice.
t : int
Tile parameter for the Zephyr lattice; must be even.

See also
--------
:func:`.zephyr_graph` : Describes the various coordinate conventions.

"""
def __init__(self, m, t=4):
self.args = m, 2 * m + 1, t

def zephyr_to_linear(self, q):
"""Converts a 5-term Zephyr coordinate into a linear index.

Parameters
----------
q : 5-tuple
Zephyr coordinate.

Examples
--------
>>> dwave.graphs.zephyr_coordinates(2).zephyr_to_linear((0, 1, 2, 1, 0))
26
"""
u, w, k, j, z = q
m, M, t = self.args
return (((u * M + w) * t + k) * 2 + j) * m + z

def linear_to_zephyr(self, r):
"""Converts a linear index into a 5-term Zephyr coordinate.

Parameters
----------
r : int
Linear index.

Examples
--------
>>> dwave.graphs.zephyr_coordinates(2).linear_to_zephyr(137)
(1, 3, 2, 0, 1)

"""
m, M, t = self.args
r, z = divmod(r, m)
r, j = divmod(r, 2)
r, k = divmod(r, t)
u, w = divmod(r, M)
return u, w, k, j, z

def iter_zephyr_to_linear(self, qlist):
"""Converts a sequence of 5-term Zephyr coordinates to linear indices.
"""
m, M, t = self.args
for (u, w, k, j, z) in qlist:
yield (((u * M + w) * t + k) * 2 + j) * m + z

def iter_linear_to_zephyr(self, rlist):
"""Converts a sequence of linear indices to 5-term Zephyr coordinates.
"""
m, M, t = self.args
for r in rlist:
r, z = divmod(r, m)
r, j = divmod(r, 2)
r, k = divmod(r, t)
u, w = divmod(r, M)
yield u, w, k, j, z

@staticmethod
def _pair_repack(f, plist):
"""Flattens a sequence of pairs to pass through ``f``, and then
re-pairs the result.
"""
ulist = f(u for p in plist for u in p)
for u in ulist:
v = next(ulist)
yield u, v

def iter_zephyr_to_linear_pairs(self, plist):
"""Converts pairs of 5-term Zephyr coordinates to pairs of linear indices.
"""
return self._pair_repack(self.iter_zephyr_to_linear, plist)

def iter_linear_to_zephyr_pairs(self, plist):
"""Converts pairs of linear indices to pairs of 5-term Zephyr coordinates.
"""
return self._pair_repack(self.iter_linear_to_zephyr, plist)

def graph_to_linear(self, g):
"""Returns a copy of the graph ``g`` relabeled to have linear indices.

Parameters
----------
g : NetworkX Graph
The Zephyr graph to be relabeled.

Returns
-------
G : NetworkX Graph
A Zephyr graph relabeled with linear indices.
"""
labels = g.graph.get('labels')
if labels == 'int':
return g.copy()
elif labels == 'coordinate':
nodes = self.iter_zephyr_to_linear(g)
edges = self.iter_zephyr_to_linear_pairs(g.edges)
else:
raise ValueError(
f"Node labeling {labels} not recognized. "
"Input must be generated by dwave.graphs.zephyr_graph."
)

return zephyr_graph(
g.graph['rows'],
t = g.graph['tile'],
node_list=nodes,
edge_list=edges,
data=g.graph['data'],
)

def graph_to_zephyr(self, g):
"""Returns a copy of the graph ``g`` relabeled to have Zephyr coordinates.

Parameters
----------
g : NetworkX Graph
The Zephyr graph to be relabeled.

Returns
-------
G : NetworkX Graph
A Zephyr graph relabeled with Zephyr coordinates.
"""
labels = g.graph.get('labels')
if labels == 'int':
nodes = self.iter_linear_to_zephyr(g)
edges = self.iter_linear_to_zephyr_pairs(g.edges)
elif labels == 'coordinate':
return g.copy()
else:
raise ValueError(
f"Node labeling {labels} not recognized. "
"Input must be generated by dwave.graphs.zephyr_graph."
)

return zephyr_graph(
g.graph['rows'],
t=g.graph['tile'],
node_list=nodes,
edge_list=edges,
data=g.graph['data'],
coordinates=True,
)


class __zephyr_coordinates_cache_dict(dict):
"""An internal-use cached factory for `zephyr_coordinates` objects"""

def __missing__(self, key):
self[key] = val = zephyr_coordinates(*key)
return val


_zephyr_coordinates_cache = __zephyr_coordinates_cache_dict()
Loading