qoolqit.graphs
graphs
Graph creation and manipulation in QoolQit.
Modules:
-
base_graph– -
data_graph– -
utils–
Classes:
-
BaseGraph–The BaseGraph in QoolQit, directly inheriting from the NetworkX Graph.
-
DataGraph–The main graph structure to represent problem data.
Functions:
-
all_node_pairs–Return all pairs of nodes (u, v) where u < v.
-
distances–Return a dictionary of edge distances.
-
random_coords–Generate a random set of node coordinates on a square of side L.
-
random_edge_list–Generates a random set of k edges linkings items from a set of nodes.
-
scale_coords–Scale the coordinates by a given value.
-
space_coords–Spaces the coordinates so the minimum distance is equal to a set spacing.
BaseGraph(edges: Iterable = [])
The BaseGraph in QoolQit, directly inheriting from the NetworkX Graph.
Defines basic functionalities for graphs within the Rydberg Analog, such as instantiating from a set of node coordinates, directly accessing node distances, and checking if the graph is unit-disk.
Parameters:
-
(edgesIterable, default:[]) –set of edge tuples (i, j)
Methods:
-
distances–Returns a dictionary of distances for a given set of edges.
-
draw–Draw the graph.
-
from_coordinates–Construct a base graph from a set of coordinates.
-
from_nodes–Construct a base graph from a set of nodes.
-
from_nx–Convert a NetworkX Graph object into a QoolQit graph instance.
-
interactions–Rydberg model interaction 1/r^6 between pair of nodes.
-
is_ud_graph–Check if the graph is unit-disk.
-
max_distance–Returns the maximum distance in the graph.
-
min_distance–Returns the minimum distance in the graph.
-
rescale_coords–Rescales the node coordinates by a factor.
-
set_ud_edges–Reset the set of edges to be equal to the set of unit-disk edges.
-
ud_edges–Returns the set of edges given by the intersection of circles of a given radius.
-
ud_radius_range–Return the range (R_min, R_max) where the graph is unit-disk.
Attributes:
-
all_node_pairs(set) –Return a list of all possible node pairs in the graph.
-
coords(dict) –Return the dictionary of node coordinates.
-
has_coords(bool) –Check if the graph has coordinates.
-
has_edge_weights(bool) –Check if the graph has edge weights.
-
has_edges(bool) –Check if the graph has edges.
-
has_node_weights(bool) –Check if the graph has node weights.
-
sorted_edges(set) –Returns the set of edges (u, v) such that (u < v).
Source code in qoolqit/graphs/base_graph.py
all_node_pairs: set
property
Return a list of all possible node pairs in the graph.
coords: dict
property
writable
Return the dictionary of node coordinates.
has_coords: bool
property
Check if the graph has coordinates.
Requires all nodes to have coordinates.
has_edge_weights: bool
property
Check if the graph has edge weights.
Requires all edges to have a weight.
has_edges: bool
property
Check if the graph has edges.
has_node_weights: bool
property
Check if the graph has node weights.
Requires all nodes to have a weight.
sorted_edges: set
property
Returns the set of edges (u, v) such that (u < v).
distances(edge_list: Iterable | None = None) -> dict
Returns a dictionary of distances for a given set of edges.
Distances are calculated directly from the coordinates. Raises an error if there are no coordinates on the graph.
Parameters:
-
(edge_listIterable | None, default:None) –set of edges.
Source code in qoolqit/graphs/base_graph.py
draw(ax: Axes | None = None, **kwargs: Any) -> None
Draw the graph.
Uses the draw_networkx function from NetworkX.
Parameters:
-
(axAxes | None, default:None) –Axes object to draw on. If None, uses the current Axes.
-
(**kwargsAny, default:{}) –keyword-arguments to pass to draw_networkx.
Source code in qoolqit/graphs/base_graph.py
from_coordinates(coords: list | dict) -> BaseGraph
classmethod
Construct a base graph from a set of coordinates.
Parameters:
-
(coordslist | dict) –list or dictionary of coordinate pairs.
Source code in qoolqit/graphs/base_graph.py
from_nodes(nodes: Iterable) -> BaseGraph
classmethod
Construct a base graph from a set of nodes.
Parameters:
-
(nodesIterable) –set of nodes.
Source code in qoolqit/graphs/base_graph.py
from_nx(g: nx.Graph) -> BaseGraph
classmethod
Convert a NetworkX Graph object into a QoolQit graph instance.
The input networkx.Graph graph must be defined only with the following allowed
Node attributes
pos (tuple): represents the node 2D position. Must be a list/tuple of real numbers. weight: represents the node weight. Must be a real number.
Edge attributes: weight: represents the edge weight. Must be a real number.
Returns an instance of the class with following attributes
- _node_weights : dict[node, float or None]
- _edge_weights : dict[(u,v), float or None]
- _coords : dict[node, (float,float) or None]
Source code in qoolqit/graphs/base_graph.py
interactions() -> dict
is_ud_graph() -> bool
max_distance(connected: bool | None = None) -> float
Returns the maximum distance in the graph.
Parameters:
-
(connectedbool | None, default:None) –if True/False, computes only over connected/disconnected nodes.
Source code in qoolqit/graphs/base_graph.py
min_distance(connected: bool | None = None) -> float
Returns the minimum distance in the graph.
Parameters:
-
(connectedbool | None, default:None) –if True/False, computes only over connected/disconnected nodes.
Source code in qoolqit/graphs/base_graph.py
rescale_coords(*args: Any, scaling: float | None = None, spacing: float | None = None) -> None
Rescales the node coordinates by a factor.
Accepts either a scaling or a spacing factor.
Parameters:
-
(scalingfloat | None, default:None) –value to scale by.
-
(spacingfloat | None, default:None) –value to set as the minimum distance in the graph.
Source code in qoolqit/graphs/base_graph.py
set_ud_edges(radius: float) -> None
Reset the set of edges to be equal to the set of unit-disk edges.
Parameters:
-
(radiusfloat) –the radius to use in determining the set of unit-disk edges.
Source code in qoolqit/graphs/base_graph.py
ud_edges(radius: float) -> set
Returns the set of edges given by the intersection of circles of a given radius.
Parameters:
-
(radiusfloat) –the value
Source code in qoolqit/graphs/base_graph.py
ud_radius_range() -> tuple
Return the range (R_min, R_max) where the graph is unit-disk.
The graph is unit-disk if the maximum distance between all connected nodes is smaller than the minimum distance between disconnected nodes. This means that for any value R in that interval, the following condition is true:
graph.ud_edges(radius = R) == graph.sorted edges
Source code in qoolqit/graphs/base_graph.py
DataGraph(edges: Iterable = [])
The main graph structure to represent problem data.
Parameters:
-
(edgesIterable, default:[]) –set of edge tuples (i, j)
- API reference
Methods:
-
circle–Constructs a circle graph, with the respective coordinates.
-
distances–Returns a dictionary of distances for a given set of edges.
-
draw–Draw the graph.
-
from_coordinates–Construct a base graph from a set of coordinates.
-
from_matrix–Constructs a graph from a symmetric square matrix.
-
from_nodes–Construct a base graph from a set of nodes.
-
from_nx–Convert a NetworkX Graph object into a QoolQit graph instance.
-
from_pyg–Convert a PyTorch Geometric Data object into a DataGraph instance.
-
heavy_hexagonal–Constructs a heavy-hexagonal lattice graph, with respective coordinates.
-
hexagonal–Constructs a hexagonal lattice graph, with respective coordinates.
-
interactions–Rydberg model interaction 1/r^6 between pair of nodes.
-
is_ud_graph–Check if the graph is unit-disk.
-
line–Constructs a line graph, with the respective coordinates.
-
max_distance–Returns the maximum distance in the graph.
-
min_distance–Returns the minimum distance in the graph.
-
random_er–Constructs an Erdős–Rényi random graph.
-
random_ud–Constructs a random unit-disk graph.
-
rescale_coords–Rescales the node coordinates by a factor.
-
set_ud_edges–Reset the set of edges to be equal to the set of unit-disk edges.
-
square–Constructs a square lattice graph, with respective coordinates.
-
to_pyg–Convert the DataGraph to a PyTorch Geometric Data object.
-
triangular–Constructs a triangular lattice graph, with respective coordinates.
-
ud_edges–Returns the set of edges given by the intersection of circles of a given radius.
-
ud_radius_range–Return the range (R_min, R_max) where the graph is unit-disk.
Attributes:
-
all_node_pairs(set) –Return a list of all possible node pairs in the graph.
-
coords(dict) –Return the dictionary of node coordinates.
-
edge_weights(dict) –Return the dictionary of edge weights.
-
has_coords(bool) –Check if the graph has coordinates.
-
has_edge_weights(bool) –Check if the graph has edge weights.
-
has_edges(bool) –Check if the graph has edges.
-
has_node_weights(bool) –Check if the graph has node weights.
-
node_weights(dict) –Return the dictionary of node weights.
-
sorted_edges(set) –Returns the set of edges (u, v) such that (u < v).
Source code in qoolqit/graphs/data_graph.py
all_node_pairs: set
property
Return a list of all possible node pairs in the graph.
coords: dict
property
writable
Return the dictionary of node coordinates.
edge_weights: dict
property
writable
Return the dictionary of edge weights.
has_coords: bool
property
Check if the graph has coordinates.
Requires all nodes to have coordinates.
has_edge_weights: bool
property
Check if the graph has edge weights.
Requires all edges to have a weight.
has_edges: bool
property
Check if the graph has edges.
has_node_weights: bool
property
Check if the graph has node weights.
Requires all nodes to have a weight.
node_weights: dict
property
writable
Return the dictionary of node weights.
sorted_edges: set
property
Returns the set of edges (u, v) such that (u < v).
circle(n: int, spacing: float = 1.0, center: tuple = (0.0, 0.0)) -> DataGraph
classmethod
Constructs a circle graph, with the respective coordinates.
Parameters:
-
(nint) –number of nodes.
-
(spacingfloat, default:1.0) –distance between each node.
-
(centertuple, default:(0.0, 0.0)) –point (x, y) to set as the center of the graph.
Source code in qoolqit/graphs/data_graph.py
distances(edge_list: Iterable | None = None) -> dict
Returns a dictionary of distances for a given set of edges.
Distances are calculated directly from the coordinates. Raises an error if there are no coordinates on the graph.
Parameters:
-
(edge_listIterable | None, default:None) –set of edges.
Source code in qoolqit/graphs/base_graph.py
draw(ax: Axes | None = None, **kwargs: Any) -> None
Draw the graph.
Uses the draw_networkx function from NetworkX.
Parameters:
-
(axAxes | None, default:None) –Axes object to draw on. If None, uses the current Axes.
-
(**kwargsAny, default:{}) –keyword-arguments to pass to draw_networkx.
Source code in qoolqit/graphs/base_graph.py
from_coordinates(coords: list | dict) -> BaseGraph
classmethod
Construct a base graph from a set of coordinates.
Parameters:
-
(coordslist | dict) –list or dictionary of coordinate pairs.
Source code in qoolqit/graphs/base_graph.py
from_matrix(data: ArrayLike) -> DataGraph
classmethod
Constructs a graph from a symmetric square matrix.
The diagonal values are set as the node weights. For each entry (i, j) where M[i, j] != 0 an edge (i, j) is added to the graph and the value M[i, j] is set as its weight.
Parameters:
-
(dataArrayLike) –symmetric square matrix.
Source code in qoolqit/graphs/data_graph.py
from_nodes(nodes: Iterable) -> BaseGraph
classmethod
Construct a base graph from a set of nodes.
Parameters:
-
(nodesIterable) –set of nodes.
Source code in qoolqit/graphs/base_graph.py
from_nx(g: nx.Graph) -> BaseGraph
classmethod
Convert a NetworkX Graph object into a QoolQit graph instance.
The input networkx.Graph graph must be defined only with the following allowed
Node attributes
pos (tuple): represents the node 2D position. Must be a list/tuple of real numbers. weight: represents the node weight. Must be a real number.
Edge attributes: weight: represents the edge weight. Must be a real number.
Returns an instance of the class with following attributes
- _node_weights : dict[node, float or None]
- _edge_weights : dict[(u,v), float or None]
- _coords : dict[node, (float,float) or None]
Source code in qoolqit/graphs/base_graph.py
from_pyg(data: torch_geometric.data.Data, node_attrs: Iterable[str] | None = None, edge_attrs: Iterable[str] | None = None, graph_attrs: Iterable[str] | None = None, node_weights_attr: str | None = None, edge_weights_attr: str | None = None) -> DataGraph
classmethod
Convert a PyTorch Geometric Data object into a DataGraph instance.
Requires torch_geometric. Uses to_networkx internally.
Default attributes copied (if present on data ):
- Node:
x,pos(posis also stored in_coords) - Edge:
edge_attr - Graph:
y
Use node_attrs, edge_attrs, graph_attrs for extras.
QoolQit weights (_node_weights, _edge_weights) are not
populated automatically — use the explicit parameters:
node_weights_attr: real-valued tensor of shape(N,)or(N, 1). Defaults toNone.edge_weights_attr: real-valued tensor of shape(E,)or(E, 1)whereE = edge_index.shape[1](directed count). Defaults toNone.
The weight attribute is also stored as a regular node/edge attribute.
Parameters:
-
(dataData) –PyTorch Geometric Data object to convert.
-
(node_attrsIterable[str] | None, default:None) –extra node attributes to copy (beyond x and pos).
-
(edge_attrsIterable[str] | None, default:None) –extra edge attributes to copy (beyond edge_attr).
-
(graph_attrsIterable[str] | None, default:None) –extra graph-level attributes to copy (beyond y).
-
(node_weights_attrstr | None, default:None) –Data attribute to use as node weights.
-
(edge_weights_attrstr | None, default:None) –Data attribute to use as edge weights.
Returns:
-
DataGraph–DataGraph with
_coords,_node_weights,_edge_weights -
DataGraph–populated where applicable.
Raises:
-
ImportError–if
torch_geometricis not installed. -
TypeError–if
datais not atorch_geometric.data.Datainstance, or if a weight attribute is not atorch.Tensor. -
AttributeError–if a specified weight attribute is missing.
-
ValueError–if a weight tensor has an incompatible shape or size.
Source code in qoolqit/graphs/data_graph.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
heavy_hexagonal(m: int, n: int, spacing: float = 1.0) -> DataGraph
classmethod
Constructs a heavy-hexagonal lattice graph, with respective coordinates.
Parameters:
-
(mint) –Number of rows of hexagons.
-
(nint) –Number of columns of hexagons.
-
(spacingfloat, default:1.0) –The distance between adjacent nodes on the final lattice.
Notes
The heavy-hexagonal lattice is a regular hexagonal lattice where each edge is decorated with an additional lattice site.
Source code in qoolqit/graphs/data_graph.py
hexagonal(m: int, n: int, spacing: float = 1.0) -> DataGraph
classmethod
Constructs a hexagonal lattice graph, with respective coordinates.
Parameters:
-
(mint) –Number of rows of hexagons.
-
(nint) –Number of columns of hexagons.
-
(spacingfloat, default:1.0) –The distance between adjacent nodes on the final lattice.
Source code in qoolqit/graphs/data_graph.py
interactions() -> dict
is_ud_graph() -> bool
line(n: int, spacing: float = 1.0) -> DataGraph
classmethod
Constructs a line graph, with the respective coordinates.
Parameters:
-
(nint) –number of nodes.
-
(spacingfloat, default:1.0) –distance between each node.
Source code in qoolqit/graphs/data_graph.py
max_distance(connected: bool | None = None) -> float
Returns the maximum distance in the graph.
Parameters:
-
(connectedbool | None, default:None) –if True/False, computes only over connected/disconnected nodes.
Source code in qoolqit/graphs/base_graph.py
min_distance(connected: bool | None = None) -> float
Returns the minimum distance in the graph.
Parameters:
-
(connectedbool | None, default:None) –if True/False, computes only over connected/disconnected nodes.
Source code in qoolqit/graphs/base_graph.py
random_er(n: int, p: float, seed: int | None = None) -> DataGraph
classmethod
Constructs an Erdős–Rényi random graph.
Parameters:
-
(nint) –number of nodes.
-
(pfloat) –probability that any two nodes connect.
-
(seedint | None, default:None) –random seed.
Source code in qoolqit/graphs/data_graph.py
random_ud(n: int, radius: float = 1.0, L: float | None = None) -> DataGraph
classmethod
Constructs a random unit-disk graph.
The nodes are sampled uniformly from a square of size (L x L). If L is not given, it is estimated based on a rough heuristic that of packing N nodes on a square of side L such that the expected minimum distance is R, leading to L ~ (R / 2) * sqrt(π * n).
Parameters:
-
(nint) –number of nodes.
-
(radiusfloat, default:1.0) –radius to use for defining the unit-disk edges.
-
(Lfloat | None, default:None) –size of the square on which to sample the node coordinates.
Source code in qoolqit/graphs/data_graph.py
rescale_coords(*args: Any, scaling: float | None = None, spacing: float | None = None) -> None
Rescales the node coordinates by a factor.
Accepts either a scaling or a spacing factor.
Parameters:
-
(scalingfloat | None, default:None) –value to scale by.
-
(spacingfloat | None, default:None) –value to set as the minimum distance in the graph.
Source code in qoolqit/graphs/base_graph.py
set_ud_edges(radius: float) -> None
Reset the set of edges to be equal to the set of unit-disk edges.
square(m: int, n: int, spacing: float = 1.0) -> DataGraph
classmethod
Constructs a square lattice graph, with respective coordinates.
Parameters:
-
(mint) –Number of rows of square.
-
(nint) –Number of columns of square.
-
(spacingfloat, default:1.0) –The distance between adjacent nodes on the final lattice.
Source code in qoolqit/graphs/data_graph.py
to_pyg(node_attrs: Iterable[str] | None = None, edge_attrs: Iterable[str] | None = None, graph_attrs: Iterable[str] | None = None, node_weights_attr: str = 'weight', edge_weights_attr: str = 'edge_weight') -> torch_geometric.data.Data
Convert the DataGraph to a PyTorch Geometric Data object.
Requires torch_geometric. Uses from_networkx internally.
Default attributes exported (if present on the graph):
- Node
"x"→data.x; Edge"edge_attr"→data.edge_attr - Graph
"y"→data.y
Use node_attrs, edge_attrs, graph_attrs for extras.
QoolQit internal dicts exported when populated:
_coords→data.pos(float64, shape(N, 2))_node_weights→data.<node_weights_attr>(float64, shape(N,)). Defaults to"weight"._edge_weights→data.<edge_weights_attr>(float64, shape(2*E,)). Defaults to"edge_weight".
Parameters:
-
(node_attrsIterable[str] | None, default:None) –extra node attributes to export (beyond x).
-
(edge_attrsIterable[str] | None, default:None) –extra edge attributes to export (beyond edge_attr).
-
(graph_attrsIterable[str] | None, default:None) –extra graph-level attributes to export (beyond y).
-
(node_weights_attrstr, default:'weight') –Data attribute name for node weights. Defaults to
"weight". -
(edge_weights_attrstr, default:'edge_weight') –Data attribute name for edge weights. Defaults to
"edge_weight".
Returns:
-
Data–PyTorch Geometric Data object.
Raises:
-
ImportError–if
torch_geometricis not installed.
Source code in qoolqit/graphs/data_graph.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
triangular(m: int, n: int, spacing: float = 1.0) -> DataGraph
classmethod
Constructs a triangular lattice graph, with respective coordinates.
Parameters:
-
(mint) –Number of rows of triangles.
-
(nint) –Number of columns of triangles.
-
(spacingfloat, default:1.0) –The distance between adjacent nodes on the final lattice.
Source code in qoolqit/graphs/data_graph.py
ud_edges(radius: float) -> set
Returns the set of edges given by the intersection of circles of a given radius.
Parameters:
-
(radiusfloat) –the value
Source code in qoolqit/graphs/base_graph.py
ud_radius_range() -> tuple
Return the range (R_min, R_max) where the graph is unit-disk.
The graph is unit-disk if the maximum distance between all connected nodes is smaller than the minimum distance between disconnected nodes. This means that for any value R in that interval, the following condition is true:
graph.ud_edges(radius = R) == graph.sorted edges
Source code in qoolqit/graphs/base_graph.py
all_node_pairs(nodes: Iterable) -> set
Return all pairs of nodes (u, v) where u < v.
Parameters:
-
(nodesIterable) –set of node indices.
distances(coords: dict, edge_list: Iterable) -> dict
Return a dictionary of edge distances.
Parameters:
-
(coordsdict) –dictionary of node coordinates.
-
(edge_listIterable) –edge list to compute the distances for.
Source code in qoolqit/graphs/utils.py
random_coords(n: int, L: float = 1.0) -> list
Generate a random set of node coordinates on a square of side L.
Parameters:
-
(nint) –number of coordinate pairs to generate.
-
(Lfloat, default:1.0) –side of the square.
Source code in qoolqit/graphs/utils.py
random_edge_list(nodes: Iterable, k: int) -> list
Generates a random set of k edges linkings items from a set of nodes.
scale_coords(coords: dict, scaling: float) -> dict
Scale the coordinates by a given value.
Parameters:
-
(coordsdict) –dictionary of node coordinates.
-
(scalingfloat) –value to scale by.
Source code in qoolqit/graphs/utils.py
space_coords(coords: dict, spacing: float) -> dict
Spaces the coordinates so the minimum distance is equal to a set spacing.
Parameters:
-
(coordsdict) –dictionary of node coordinates.
-
(spacingfloat) –value to set as minimum distance.