|
| 1 | +# Copyright 2026 D-Wave |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# ================================================================================================ |
| 16 | + |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from enum import Enum |
| 21 | +from functools import total_ordering |
| 22 | + |
| 23 | +from dwave.graphs.generators.common.shape import TopologyShape |
| 24 | +from dwave.graphs.tuplelike import TupleLike |
| 25 | + |
| 26 | +__all__ = ["Coord", "CoordKind"] |
| 27 | + |
| 28 | + |
| 29 | +class CoordKind(Enum): # Kinds of coordinates of nodes in topologies |
| 30 | + CARTESIAN = 0 |
| 31 | + TOPOLOGY = 1 |
| 32 | + |
| 33 | + |
| 34 | +@total_ordering |
| 35 | +class Coord(TupleLike): |
| 36 | + """A class to represent the coordinate of a topology node.""" |
| 37 | + |
| 38 | + __hash__ = TupleLike.__hash__ |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def topology_name(cls) -> str: |
| 42 | + """Returns the name of the topology associated with the class. |
| 43 | +
|
| 44 | + Raises: |
| 45 | + NotImplementedError: To be implemented in subclasses for |
| 46 | + specific topologies. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + str: The name of the topology the class is designed for. |
| 50 | + """ |
| 51 | + raise NotImplementedError |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def kind(cls) -> CoordKind: |
| 55 | + """Returns the kind of the coordinate associated with the class. |
| 56 | +
|
| 57 | + Raises: |
| 58 | + NotImplementedError: To be implemented in subclasses for |
| 59 | + specific coordinates. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + CoordKind: The kind of class's coordinate. |
| 63 | + """ |
| 64 | + raise NotImplementedError |
| 65 | + |
| 66 | + def __init__(self, *args, **kwargs) -> None: |
| 67 | + super().__init__(*args, **kwargs) |
| 68 | + |
| 69 | + def _args_valid_topology(self, *args, **kwargs) -> None: |
| 70 | + """Verifies the given coordinate is a valid coordinate.""" |
| 71 | + raise NotImplementedError |
| 72 | + |
| 73 | + def is_shape_consistent(self, shape: TopologyShape) -> bool: |
| 74 | + """Tells whether the coordinate is consistent with a topology shape. |
| 75 | +
|
| 76 | + Args: |
| 77 | + shape: The shape to check the consistency of the coordinate with. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + bool: Whether the coordinate is consistent with the shape. |
| 81 | + """ |
| 82 | + raise NotImplementedError |
| 83 | + |
| 84 | + def is_quotient(self) -> bool: |
| 85 | + """Whether the given coordinate is a quotient coordinate.""" |
| 86 | + raise NotImplementedError |
| 87 | + |
| 88 | + def to_quotient( |
| 89 | + self, |
| 90 | + ) -> Coord: |
| 91 | + """Converts the coordinate to its corresponding coordinate in a quotient graph.""" |
| 92 | + raise NotImplementedError |
| 93 | + |
| 94 | + def to_non_quotient( |
| 95 | + self, |
| 96 | + shape: TopologyShape, |
| 97 | + **kwargs, |
| 98 | + ) -> list[Coord]: |
| 99 | + """Expands the coordinate to a non-quotient shape; i.e. it gives |
| 100 | + all coordinates in a non-quotient graph whose quotient is the coordinate. |
| 101 | +
|
| 102 | + Args: |
| 103 | + shape: The non-quotient shape to expand the coordinate to. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + list[Coord]: The expansion of the coordinate into non-quotient. |
| 107 | + """ |
| 108 | + raise NotImplementedError |
| 109 | + |
| 110 | + def convert(self, coord_kind: CoordKind) -> Coord: |
| 111 | + """Converts the coordinate to other kinds of coordinate in the same topology. |
| 112 | +
|
| 113 | + Args: |
| 114 | + coord_kind (CoordKind): The coordinate kind to convert the coordinate to. |
| 115 | +
|
| 116 | + Raises: |
| 117 | + NotImplementedError: To be implemented in subclasses for |
| 118 | + specific coordinates. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + Coord: The converted coordinate. |
| 122 | + """ |
| 123 | + raise NotImplementedError |
| 124 | + |
| 125 | + def __eq__(self, other: object) -> bool: |
| 126 | + if (type(self) is not type(other)) or (self.is_quotient() != other.is_quotient()): |
| 127 | + return NotImplemented |
| 128 | + return self._tuple_format == other._tuple_format |
| 129 | + |
| 130 | + def __lt__(self, other: object) -> bool: |
| 131 | + if (type(self) is not type(other)) or (self.is_quotient() != other.is_quotient()): |
| 132 | + return NotImplemented |
| 133 | + return self._tuple_format < other._tuple_format |
0 commit comments