Skip to content

Topology utils#275

Open
mahdiehmalekian wants to merge 6 commits into
dwavesystems:mainfrom
mahdiehmalekian:topology_utils
Open

Topology utils#275
mahdiehmalekian wants to merge 6 commits into
dwavesystems:mainfrom
mahdiehmalekian:topology_utils

Conversation

@mahdiehmalekian

Copy link
Copy Markdown

The added files in dwave/graphs/topologies/common provide blueprint classes for working with different aspects of any D-Wave topology.

  • coord.py: Coordinate systems used for node labels.
  • node_edge.py: Nodes and edges.
  • planeshift.py: Displacement of nodes in the Cartesian plane.
  • shape.py: Shape of a graph.
  • topology.py: Constructing a topology graph whose nodes and edges are equipped with all the specific-topology-relevant functionalities.

The added files in dwave/graphs/topologies/zephyr, i.e. zcoord.py, znode_edge.py, zplaneshift.py and zshape.py, contain the implementation of the corresponding objects for Zephyr topology. In zephyr.py the class Zephyr has been added that is built on Topology blueprint in dwave/graphs/topologies/common/topology.py

The test suite for these modules is added in tests.

Note: This is an evolution of previous zephyr_utils PR to minorminer.

Fix import

Fix syntax
@mahdiehmalekian

mahdiehmalekian commented Jul 17, 2026

Copy link
Copy Markdown
Author

@thisac Is this ready to be merged?

@randomir randomir left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did a quick review, it looks good, but I would like to do an in-depth review before me merge this.

The suggestions below are mostly minor, except the use of function objects as default argument values.

Comment thread dwave/graphs/topologies/common/__init__.py Outdated
Comment thread dwave/graphs/topologies/common/coord.py Outdated
Comment thread dwave/graphs/topologies/common/node_edge.py Outdated
Comment thread dwave/graphs/topologies/common/node_edge.py Outdated

def __eq__(self, other: object) -> bool:
if not type(self) is type(other):
return NotImplemented

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not:

Suggested change
return NotImplemented
return False

def neighbors(
self,
nbr_kind: EdgeKind | Iterable[EdgeKind] | None = None,
where: Callable[[Coord], bool] = lambda coord: True,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe the mutable default argument value gotcha applies to function objects as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here and elsewhere!

associated_edgekind: EdgeKind


class HasInternalNeighborsMixin(NeighborContributorMixin):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd drop the Has prefix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here and elsewhere.

Returns:
TopologyShape: The shape converted to its corresponding quotient shape.
"""
@abstractmethod

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
@abstractmethod
@abstractmethod

Comment thread dwave/graphs/topologies/zephyr/graphs.py Outdated
Comment thread dwave/graphs/topologies/zephyr/graphs.py Outdated
@@ -0,0 +1,20 @@
# Copyright 2016 D-Wave Systems Inc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since it's a new file.

Suggested change
# Copyright 2016 D-Wave Systems Inc.
# Copyright 2026 D-Wave

Comment thread dwave/graphs/topologies/common/common.py Outdated
Comment on lines +61 to +62
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should be moved to the top of the class.

Comment on lines +41 to +59
@classmethod
def topology_name(cls) -> str:
"""Returns the name of the topology associated with the class.

Returns:
str: The name of the topology the class is designed for.
"""
if hasattr(cls, "_topology_name"):
return cls._topology_name
raise NotImplementedError

@classmethod
@abstractmethod
def kind(cls) -> CoordKind:
"""Returns the kind of the coordinate associated with the class.

Returns:
CoordKind: The kind of class's coordinate.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These two both are used to return a static property of the different coord classes, but are implemented in different ways. Also, these should probably be properties instead, so that you call them as ZephyrCoord.topology_name.

The simplest I think might be to only have them declared directly in the subclasses, and skip them in the baseclass altogether (or have a topology_name = None).

class ZephyrCoord(Coord):
    topology_name: str = "zephyr"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A more interesting solution might be to create a metaclass for the property, which should act like a property for the subclasses themselves rather than for instances of them.

class _CoordClassProperties(type):

    @property
    def topology_name(cls) -> str:
        return cls._topology_name


class Coord(metaclass=_CoordClassProperties):
    ...


class ZephyrCoord(Coord):
    _topology_name: str = "zephyr" 

But this might be a bit overkill.


def __eq__(self, other: object) -> bool:
if (type(self) is not type(other)) or (self.is_quotient() != other.is_quotient()):
return NotImplemented

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like returning False might make more sense, no?

Suggested change
return NotImplemented
return False


def __eq__(self, other: object) -> bool:
if not (type(self) is type(other) and self._shape == other._shape):
return NotImplemented

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similarly to in other places, why not return False?

return f"{coord.to_tuple(), self._shape.to_tuple()}"


class NeighborContributorMixin:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This could potentially also be made abstract, as well as turning all the NotImplementedError methods in subclasses below into abstractmethods.

Comment on lines +329 to +330
except ValueError:
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When are these value errors expected? Is there a risk that there might be errors that should be raised being bypassed silently? Similarly for other generator methods below.

Comment on lines +72 to +73
) -> nx.Graph:
return zephyr_graph(*shape, create_using, node_list, edge_list, data, coordinates,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing docstring.

Comment thread dwave/graphs/topologies/zephyr/graphs.py
mahdiehmalekian and others added 5 commits July 20, 2026 16:00
Co-authored-by: Radomir Stevanovic <radomir.stevanovic@gmail.com>
Co-authored-by: Theodor Isacsson <tisacsson@dwavesys.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants