Quantum Graph API¶
The quantum graph module provides a complete implementation of k-means clustering on metric graphs where points can exist anywhere on edges.
Quantum Graph Space¶
The main class representing a quantum graph as a metric space.
kmeanssa_ng.quantum_graph.space.QuantumGraph ¶
Bases: Graph, Space
A quantum graph is a metric graph where points can lie on edges.
This class extends NetworkX Graph to provide: - Distance computation between points on edges - Sampling of random points and centers - k-means clustering support
Each edge should have a 'length' attribute representing its metric length. Nodes and edges can have 'weight' and 'distribution' attributes for sampling.
Attributes: diameter: The diameter of the graph (max distance between nodes). node_position: Layout positions for visualization.
Example:
graph = QuantumGraph()
graph.add_edge(0, 1, length=1.0)
graph.add_edge(1, 2, length=2.0)
graph.precomputing() # Cache pairwise distances
points = graph.sample_points(100)
centers = graph.sample_centers(5)
Initialize a quantum graph.
Args: incoming_graph_data: Input graph data (see networkx.Graph). **attr: Additional graph attributes.
Source code in src/kmeanssa_ng/quantum_graph/space.py
distance ¶
Compute distance between two points on the graph.
Args: p1: First point. p2: Second point.
Returns: Geodesic distance.
Source code in src/kmeanssa_ng/quantum_graph/space.py
sample_points ¶
Sample n random points from the graph.
Args: n: Number of points to sample. where: Sampling mode ("Node" or "Edge").
Returns: List of n sampled points.
Source code in src/kmeanssa_ng/quantum_graph/space.py
sample_centers ¶
Sample k random centers.
Args: k: Number of centers to sample. where: Sampling mode ("Node" or "Edge").
Returns: List of k sampled centers.
Source code in src/kmeanssa_ng/quantum_graph/space.py
precomputing ¶
Precompute and cache all pairwise node distances.
This significantly speeds up distance queries. Should be called once after graph construction.
Raises: ValueError: If graph is not connected or has invalid edge lengths.
Source code in src/kmeanssa_ng/quantum_graph/space.py
compute_clusters ¶
Assign each node to its nearest center.
Updates node 'cluster' attribute.
Args: centers: List of cluster centers.
Source code in src/kmeanssa_ng/quantum_graph/space.py
Points and Centers¶
Classes representing points and centers on quantum graphs.
kmeanssa_ng.quantum_graph.point.QGPoint ¶
Bases: Point
A point on a quantum graph.
A quantum graph point is located on an edge at a specific position. The edge is represented as a tuple (node1, node2) and the position is the distance from node1 along the edge.
Attributes: space: The quantum graph this point belongs to. edge: The edge (node1, node2) containing this point. position: Distance from node1 along the edge.
Example:
Initialize a point on a quantum graph.
Args: quantum_graph: The quantum graph containing this point. edge: Tuple (node1, node2) representing the edge. position: Distance from node1 along the edge.
Raises: ValueError: If quantum_graph is None, edge doesn't exist in graph, or position is outside [0, edge_length].
Source code in src/kmeanssa_ng/quantum_graph/point.py
__str__ ¶
String representation of the point.
Source code in src/kmeanssa_ng/quantum_graph/point.py
kmeanssa_ng.quantum_graph.center.QGCenter ¶
A movable cluster center on a quantum graph.
Centers can perform: - Brownian motion: Random walk for exploration - Drift: Directed movement toward target points
Attributes: space: The quantum graph this center belongs to. edge: The edge containing this center. position: Position along the edge.
Example:
graph = QuantumGraph(...)
point = QGPoint(graph, edge=(0, 1), position=0.5)
center = QGCenter(point)
center.brownian_motion(0.1)
center.drift(target_point, 0.5)
Initialize a center from a point.
Args: point: The initial point location. rng: Random number generator. If None, creates a new default_rng().
Source code in src/kmeanssa_ng/quantum_graph/center.py
brownian_motion ¶
Perform Brownian motion on the quantum graph.
The center performs a random walk with step size proportional to sqrt(time_to_travel).
Args: time_to_travel: Time parameter (distance ~ sqrt(time)).
Raises: ValueError: If time_to_travel is negative or not numeric.
Source code in src/kmeanssa_ng/quantum_graph/center.py
Quantum Graph Simulated Annealing¶
Specialized simulated annealing implementation with quantum graph-specific features.
kmeanssa_ng.quantum_graph.qg_simulated_annealing.QGSimulatedAnnealing ¶
QGSimulatedAnnealing(
observations: list[Point],
k: int,
lambda_param: int = 1,
beta: float = 1.0,
step_size: float = 0.1,
)
Bases: SimulatedAnnealing
Simulated annealing specialized for quantum graphs.
This class extends the core SimulatedAnnealing with quantum graph specific methods that rely on node proximity information.
Example:
from kmeanssa_ng import generate_sbm, QGSimulatedAnnealing
graph = generate_sbm(sizes=[50, 50], p=[[0.7, 0.1], [0.1, 0.7]])
points = graph.sample_points(100)
sa = QGSimulatedAnnealing(points, k=2)
node_ids = sa.run_for_kmeans(robust_prop=0.1)
Source code in src/kmeanssa_ng/core/simulated_annealing.py
run_for_kmeans ¶
Run simulated annealing and return most frequent closest nodes for each center.
This method is specific to quantum graphs and returns the node IDs that appear most frequently as closest nodes for each center during the robustification period.
Args: robust_prop: Proportion of final iterations for robustification.
Returns: List of k node IDs (most frequent for each center).
Raises: ValueError: If robust_prop not in [0, 1].
Source code in src/kmeanssa_ng/quantum_graph/qg_simulated_annealing.py
run_for_mean ¶
Run simulated annealing for k=1 and return most frequent closest node.
This is a special case for computing a robust mean (single cluster) on a quantum graph. The result is the node ID that appears most frequently as the closest node during the robustification period.
Args: robust_prop: Proportion of final iterations for robustification.
Returns: Index of the most frequent closest node during robustification period.
Raises: ValueError: If robust_prop not in [0, 1] or k != 1.