-
Notifications
You must be signed in to change notification settings - Fork 60
Update generators submodule #269
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
Draft
thisac
wants to merge
1
commit into
dwavesystems:main
Choose a base branch
from
thisac:upgrade/generators-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
| """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() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is directly copied over from the former
zephyr.py, nowzephyr/graphs.pybelow.