Skip to content

Results are limited to Qoolqit v0.4.0

Product news
You are reading Qoolqit v0.4.0, an older version of this documentation. Go to the latest version (v1.5.0)

qoolqit.graphs

package
qoolqit.graphs

Graph creation and manipulation in QoolQit.

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.

class
BaseGraph (edges: Iterable = [])

Bases : nx.Graph

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.

Default constructor for the BaseGraph.

Parameters

  • edges : Iterable — set of edge tuples (i, j)

Attributes

  • adj — Graph adjacency object holding the neighbors of each node.

  • name — String identifier of the graph.

  • nodes — A NodeView of the Graph as G.nodes or G.nodes().

  • edges — An EdgeView of the Graph as G.edges or G.edges().

  • degree — A DegreeView for the Graph as G.degree or G.degree().

  • sorted_edges : set — Returns the set of edges (u, v) such that (u < v).

  • all_node_pairs : set — Return a list of all possible node pairs in the graph.

  • has_coords : bool — Check if the graph has coordinates.

  • has_edges : bool — Check if the graph has edges.

  • coords : dict — Return the dictionary of node coordinates.

Methods

  • from_nodes — Construct a base graph from a set of nodes.

  • from_coordinates — Construct a base graph from a set of coordinates.

  • from_nx — Convert a NetworkX Graph object into a QoolQit graph instance.

  • from_pyg — Convert a PyTorch Geometric Data object into a QoolQit graph instance.

  • distances — Returns a dictionary of distances for a given set of edges.

  • interactions — Rydberg model interaction 1/r^6 between pair of nodes.

  • min_distance — Returns the minimum distance in the graph.

  • max_distance — Returns the maximum distance in the graph.

  • ud_radius_range — Return the range (R_min, R_max) where the graph is unit-disk.

  • is_ud_graph — Check if the graph is unit-disk.

  • ud_edges — Returns the set of edges given by the intersection of circles of a given radius.

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

  • draw — Draw the graph.

classmethod
from_nodes (nodes: Iterable) → BaseGraph

Construct a base graph from a set of nodes.

Parameters

  • nodes : Iterable — set of nodes.

classmethod
from_coordinates (coords: list | dict) → BaseGraph

Construct a base graph from a set of coordinates.

Parameters

  • coords : list | dict — list or dictionary of coordinate pairs.

classmethod
from_nx (g: nx.Graph) → BaseGraph

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]

Raises

  • TypeError

  • ValueError

classmethod
from_pyg (g: torch_geometric.data.Data) → BaseGraph

Convert a PyTorch Geometric Data object into a QoolQit graph instance.

This method requires installing the torch_geometric package. The input torch_geometric.data.Data object must be defined only with the following allowed attributes: x (torch.Tensor): node weights as a matrix with shape (num_nodes, 1). edge_index (torch.Tensor): graph connectivity as a matrix with shape (2, num_edges). num_nodes (int): minimum number of nodes. The total number of nodes will be inferred from this number and from the edges of the graph. pos (torch.Tensor): node 2D positions as a matrix with shape (num_nodes, 2) edge_attr (torch.Tensor): edge weights as a matrix with shape (num_edges, 1).

If the graph is defined only through the edge_index attribute, num_nodes is required. The input graph will be converted to a unidirectional graph.

Raises

  • TypeError

  • AttributeError

  • ImportError

  • ValueError

property
sorted_edges : set

Returns the set of edges (u, v) such that (u < v).

property
all_node_pairs : set

Return a list of all possible node pairs in the graph.

property
has_coords : bool

Check if the graph has coordinates.

Requires all nodes to have coordinates.

property
has_edges : bool

Check if the graph has edges.

property
coords : dict

Return the dictionary of node coordinates.

method
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_list : Iterable | None — set of edges.

Raises

  • AttributeError

  • ValueError

method
interactions () → dict

Rydberg model interaction 1/r^6 between pair of nodes.

method
min_distance (connected: bool | None = None) → float

Returns the minimum distance in the graph.

Parameters

  • connected : bool | None — if True/False, computes only over connected/disconnected nodes.

method
max_distance (connected: bool | None = None) → float

Returns the maximum distance in the graph.

Parameters

  • connected : bool | None — if True/False, computes only over connected/disconnected nodes.

method
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

Raises

  • AttributeError

  • ValueError

method
is_ud_graph () → bool

Check if the graph is unit-disk.

method
ud_edges (radius: float) → set

Returns the set of edges given by the intersection of circles of a given radius.

Parameters

  • radius : float — the value

Raises

  • AttributeError

method
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

  • scaling : float | None — value to scale by.

  • spacing : float | None — value to set as the minimum distance in the graph.

Raises

  • AttributeError

  • TypeError

method
set_ud_edges (radius: float) → None

Reset the set of edges to be equal to the set of unit-disk edges.

Parameters

  • radius : float — the radius to use in determining the set of unit-disk edges.

method
draw (return_fig: bool = False, *args: Any, **kwargs: Any) → Figure | None

Draw the graph.

Uses the draw_networkx function from NetworkX.

Parameters

  • *args : Any — arguments to pass to draw_networkx.

  • **kwargs : Any — keyword-arguments to pass to draw_networkx.

class
DataGraph (edges: Iterable = [])

Bases : BaseGraph

The main graph structure to represent problem data.

Default constructor for the BaseGraph.

Parameters

  • edges : Iterable — set of edge tuples (i, j)

Attributes

  • adj — Graph adjacency object holding the neighbors of each node.

  • name — String identifier of the graph.

  • nodes — A NodeView of the Graph as G.nodes or G.nodes().

  • edges — An EdgeView of the Graph as G.edges or G.edges().

  • degree — A DegreeView for the Graph as G.degree or G.degree().

  • sorted_edges : set — Returns the set of edges (u, v) such that (u < v).

  • all_node_pairs : set — Return a list of all possible node pairs in the graph.

  • has_coords : bool — Check if the graph has coordinates.

  • has_edges : bool — Check if the graph has edges.

  • coords : dict — Return the dictionary of node coordinates.

  • node_weights : dict — Return the dictionary of node weights.

  • edge_weights : dict — Return the dictionary of edge weights.

  • has_node_weights : bool — Check if the graph has node weights.

  • has_edge_weights : bool — Check if the graph has edge weights.

Methods

  • line — Constructs a line graph, with the respective coordinates.

  • circle — Constructs a circle graph, with the respective coordinates.

  • random_er — Constructs an Erdős–Rényi random graph.

  • triangular — Constructs a triangular lattice graph, with respective coordinates.

  • hexagonal — Constructs a hexagonal lattice graph, with respective coordinates.

  • heavy_hexagonal — Constructs a heavy-hexagonal lattice graph, with respective coordinates.

  • square — Constructs a square lattice graph, with respective coordinates.

  • random_ud — Constructs a random unit-disk graph.

  • from_matrix — Constructs a graph from a symmetric square matrix.

  • set_ud_edges — Reset the set of edges to be equal to the set of unit-disk edges.

classmethod
line (n: int, spacing: float = 1.0) → DataGraph

Constructs a line graph, with the respective coordinates.

Parameters

  • n : int — number of nodes.

  • spacing : float — distance between each node.

classmethod
circle (n: int, spacing: float = 1.0, center: tuple = (0.0, 0.0)) → DataGraph

Constructs a circle graph, with the respective coordinates.

Parameters

  • n : int — number of nodes.

  • spacing : float — distance between each node.

  • center : tuple — point (x, y) to set as the center of the graph.

classmethod
random_er (n: int, p: float, seed: int | None = None) → DataGraph

Constructs an Erdős–Rényi random graph.

Parameters

  • n : int — number of nodes.

  • p : float — probability that any two nodes connect.

  • seed : int | None — random seed.

classmethod
triangular (m: int, n: int, spacing: float = 1.0) → DataGraph

Constructs a triangular lattice graph, with respective coordinates.

Parameters

  • m : int — Number of rows of triangles.

  • n : int — Number of columns of triangles.

  • spacing : float — The distance between adjacent nodes on the final lattice.

classmethod
hexagonal (m: int, n: int, spacing: float = 1.0) → DataGraph

Constructs a hexagonal lattice graph, with respective coordinates.

Parameters

  • m : int — Number of rows of hexagons.

  • n : int — Number of columns of hexagons.

  • spacing : float — The distance between adjacent nodes on the final lattice.

classmethod
heavy_hexagonal (m: int, n: int, spacing: float = 1.0) → DataGraph

Constructs a heavy-hexagonal lattice graph, with respective coordinates.

Parameters

  • m : int — Number of rows of hexagons.

  • n : int — Number of columns of hexagons.

  • spacing : float — The distance between adjacent nodes on the final lattice.

classmethod
square (m: int, n: int, spacing: float = 1.0) → DataGraph

Constructs a square lattice graph, with respective coordinates.

Parameters

  • m : int — Number of rows of square.

  • n : int — Number of columns of square.

  • spacing : float — The distance between adjacent nodes on the final lattice.

classmethod
random_ud (n: int, radius: float = 1.0, L: float | None = None) → DataGraph

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

  • n : int — number of nodes.

  • radius : float — radius to use for defining the unit-disk edges.

  • L : float | None — size of the square on which to sample the node coordinates.

classmethod
from_matrix (data: ArrayLike) → DataGraph

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

  • data : ArrayLike — symmetric square matrix.

Raises

  • ValueError

property
node_weights : dict

Return the dictionary of node weights.

property
edge_weights : dict

Return the dictionary of edge weights.

property
has_node_weights : bool

Check if the graph has node weights.

Requires all nodes to have a weight.

property
has_edge_weights : bool

Check if the graph has edge weights.

Requires all edges to have a weight.

method
set_ud_edges (radius: float) → None

Reset the set of edges to be equal to the set of unit-disk edges.

all_node_pairs (nodes: Iterable) → set

Return all pairs of nodes (u, v) where u < v.

Parameters

  • nodes : Iterable — set of node indices.

distances (coords: dict, edge_list: Iterable) → dict

Return a dictionary of edge distances.

Parameters

  • coords : dict — dictionary of node coordinates.

  • edge_list : Iterable — edge list to compute the distances for.

random_coords (n: int, L: float = 1.0) → list

Generate a random set of node coordinates on a square of side L.

Parameters

  • n : int — number of coordinate pairs to generate.

  • L : float — side of the square.

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

  • coords : dict — dictionary of node coordinates.

  • scaling : float — value to scale by.

space_coords (coords: dict, spacing: float) → dict

Spaces the coordinates so the minimum distance is equal to a set spacing.

Parameters

  • coords : dict — dictionary of node coordinates.

  • spacing : float — value to set as minimum distance.