Quantum Graph API
kmeanssa_ng.quantum_graph
Quantum graph implementation and utilities.
MinimizeEnergyNodeUpdate
Bases: LloydUpdateStrategy
Update strategy that finds the node that minimizes k-means energy.
This strategy first maps each point in the cluster to its nearest node, then finds the graph node that minimizes the sum of squared distances (k-means energy) to this set of cluster nodes.
Source code in kmeanssa_ng/quantum_graph/lloyd_update.py
update(points, space)
Compute the new center by finding the node that minimizes energy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
list[QGPoint]
|
A list of points belonging to a single cluster. |
required |
space
|
'QuantumGraph'
|
The quantum graph, which must have distances precomputed. |
required |
Returns:
| Type | Description |
|---|---|
'QGCenter'
|
The new center for the cluster. |
Source code in kmeanssa_ng/quantum_graph/lloyd_update.py
MostFrequentNode
Bases: RobustificationStrategy[list[Any]]
Strategy to find the most frequent node for each center.
Returns QGCenter objects located at the most frequently visited nodes during the robustification phase.
Source code in kmeanssa_ng/quantum_graph/robustification.py
collect(sa)
Collect the closest node for each center at the current step.
Source code in kmeanssa_ng/quantum_graph/robustification.py
get_result()
Return QGCenter objects at the most frequent nodes.
Always returns a list of QGCenter objects, even for k=1.
Source code in kmeanssa_ng/quantum_graph/robustification.py
initialize(sa)
Initialize an empty list to store node collections.
Raises:
| Type | Description |
|---|---|
TypeError
|
If the space is not a QuantumGraph. |
Source code in kmeanssa_ng/quantum_graph/robustification.py
MostFrequentNodeUpdate
Bases: LloydUpdateStrategy
Update strategy that computes the new center as the most frequent closest node to the points in the cluster.
Source code in kmeanssa_ng/quantum_graph/lloyd_update.py
update(points, space)
Compute the new center for a given cluster of points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
list[QGPoint]
|
A list of points belonging to a single cluster. |
required |
space
|
'QuantumGraph'
|
The quantum graph in which the points and center exist. |
required |
Returns:
| Type | Description |
|---|---|
'QGCenter'
|
The new center for the cluster. |
Source code in kmeanssa_ng/quantum_graph/lloyd_update.py
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:
| Name | Type | Description |
|---|---|---|
space |
QuantumGraph
|
The quantum graph this center belongs to. |
edge |
tuple[int, int]
|
The edge containing this center. |
position |
tuple[int, int]
|
Position along the edge. |
Example
Source code in kmeanssa_ng/quantum_graph/center.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 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 | |
__init__(point, rng=None)
Initialize a center from a point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
QGPoint
|
The initial point location. |
required |
rng
|
Generator | None
|
Random number generator. If None, creates a new default_rng(). |
None
|
Source code in kmeanssa_ng/quantum_graph/center.py
__repr__()
__str__()
brownian_motion(time_to_travel)
Perform Brownian motion on the quantum graph.
The center performs a random walk with step size proportional to sqrt(time_to_travel).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_to_travel
|
float
|
Time parameter (distance ~ sqrt(time)). |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If time_to_travel is negative or not numeric. |
Source code in kmeanssa_ng/quantum_graph/center.py
clone()
Create an independent copy of this center.
The cloned center shares the same quantum graph (space) but has independent edge and position attributes. This is much faster than deepcopy as it doesn't duplicate the entire graph structure.
Note: This creates a shallow copy of the center's state, bypassing init validation: the state comes from an existing center, so it is already valid, and cloning happens on the hot path of the annealing loop.
Returns:
| Type | Description |
|---|---|
QGCenter
|
A new QGCenter with the same location but independent state. |
Example
Source code in kmeanssa_ng/quantum_graph/center.py
drift(target_point, prop_to_travel)
Move toward a target point.
Moves a proportion of the distance to the target point along the geodesic path in the quantum graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_point
|
QGPoint
|
The point to move toward. |
required |
prop_to_travel
|
float
|
Proportion of distance to travel (0 to 1). |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If target_point is None, prop_to_travel is not numeric, or prop_to_travel is not in [0, 1]. |
Source code in kmeanssa_ng/quantum_graph/center.py
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:
| Name | Type | Description |
|---|---|---|
space |
QuantumGraph
|
The quantum graph this point belongs to. |
edge |
tuple[int, int]
|
The edge (node1, node2) containing this point. |
position |
tuple[int, int]
|
Distance from node1 along the edge. |
Source code in kmeanssa_ng/quantum_graph/point.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
edge
property
writable
The edge containing this point.
space
property
The quantum graph this point belongs to.
__init__(quantum_graph, edge, position)
Initialize a point on a quantum graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quantum_graph
|
QuantumGraph
|
The quantum graph containing this point. |
required |
edge
|
tuple[int, int]
|
Tuple (node1, node2) representing the edge. |
required |
position
|
float
|
Distance from node1 along the edge. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If quantum_graph is None, edge doesn't exist in graph, or position is outside [0, edge_length]. |
Source code in kmeanssa_ng/quantum_graph/point.py
__repr__()
__str__()
String representation of the point.
Source code in kmeanssa_ng/quantum_graph/point.py
closest_node()
Get the closest node to this point.
Returns:
| Type | Description |
|---|---|
int
|
The node (edge[0] or edge[1]) closest to this point. |
Source code in kmeanssa_ng/quantum_graph/point.py
reverse()
Reverse the edge orientation and adjust position.
Changes edge from (a, b) to (b, a) and updates position accordingly.
Source code in kmeanssa_ng/quantum_graph/point.py
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:
| Name | Type | Description |
|---|---|---|
diameter |
float
|
The diameter of the graph (max distance between nodes). |
node_position |
dict
|
Layout positions for visualization. |
Example
Source code in kmeanssa_ng/quantum_graph/space.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 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 428 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 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 | |
diameter
property
Compute and cache the graph diameter.
Returns:
| Type | Description |
|---|---|
float
|
Maximum distance between any two nodes. |
node_position
property
Compute layout positions for visualization.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary mapping nodes to (x, y) positions. |
__init__(incoming_graph_data=None, precompute=False, **attr)
Initialize a quantum graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
incoming_graph_data
|
Input graph data (see networkx.Graph). |
None
|
|
precompute
|
bool
|
If True, automatically precompute distances after initialization. |
False
|
**attr
|
Additional graph attributes. |
{}
|
Source code in kmeanssa_ng/quantum_graph/space.py
add_edge(u_for_edge, v_for_edge, **attr)
Add an edge with validation of the length attribute.
Invalidates the precomputed distance cache: call precomputing()
again after editing the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u_for_edge
|
First node. |
required | |
v_for_edge
|
Second node. |
required | |
**attr
|
Edge attributes. Must include 'length' with a positive value. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'length' is missing, not positive, or not a number. |
Source code in kmeanssa_ng/quantum_graph/space.py
add_edges_from(ebunch_to_add, **attr)
Add edges in bulk; invalidates the precomputed distance cache.
add_node(node_for_adding, **attr)
Add a node; invalidates the precomputed distance cache.
add_nodes_from(nodes_for_adding, **attr)
Add nodes in bulk; invalidates the precomputed distance cache.
calculate_energy(centers, how='uniform', observations=None)
Calculate k-means energy for given centers.
Dispatches to the Numba kernels when pairwise node distances are
precomputed (precomputing()), and falls back to pure-Python
distance computations otherwise — correct on any graph, just slower.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centers
|
list[QGCenter]
|
List of cluster centers. |
required |
how
|
str
|
Which reference measure to average under (see
:meth: |
'uniform'
|
observations
|
list[QGPoint] | None
|
The points defining the empirical measure
( |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Average squared distance to nearest center. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in kmeanssa_ng/quantum_graph/space.py
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | |
calculate_energy_numba(centers, how='uniform', observations=None)
Numba-accelerated energy calculation for centers.
Uses the precomputed distance matrix; calculate_energy dispatches
here automatically once precomputing() has run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centers
|
list[QGCenter]
|
List of cluster centers. |
required |
how
|
str
|
Reference measure — "uniform", "empirical" or "node_measure"
(see |
'uniform'
|
observations
|
list[QGPoint] | None
|
The points of the empirical measure
( |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Average squared distance to nearest center. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pairwise distances not precomputed. |
Source code in kmeanssa_ng/quantum_graph/space.py
center_from_point(point, rng=None)
clear()
clear_edges()
distance(p1, p2)
distance_matrix()
Compute the pairwise distance matrix between all nodes.
Returns:
| Type | Description |
|---|---|
ndarray
|
n×n matrix of pairwise distances. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pairwise distances not precomputed. |
Source code in kmeanssa_ng/quantum_graph/space.py
distances_from_centers(centers, target)
Compute distances from multiple centers to a single target point.
This is a Numba-accelerated operation that efficiently computes distances from all centers to one target point. Works for any target location (on nodes or edges).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centers
|
list[QGCenter]
|
List of k centers to compute distances from. |
required |
target
|
QGPoint
|
The target point (can be on a node or edge). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (k,) with distances from each center to target. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pairwise distances not precomputed. |
Example
Source code in kmeanssa_ng/quantum_graph/space.py
draw(color_by='cluster', centers=None, node_size_by_obs=True, ax=None, **kwargs)
Draws the graph using matplotlib.
This method provides a flexible way to visualize the graph, its clusters,
and the results of the k-means algorithm. To use this method, you need
to install the 'plot' extras: pip install kmeanssa-ng[plot]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_by
|
str | None
|
Node attribute to use for coloring. - "cluster": Colors nodes by their assigned cluster. - "block": Colors nodes by a pre-defined 'block' attribute. - None: All nodes will have the same default color. (Default: "skyblue") |
'cluster'
|
centers
|
list[QGCenter] | None
|
A list of QGCenter objects to be highlighted on the graph. |
None
|
node_size_by_obs
|
bool
|
If True, sizes nodes based on 'obs_weight' attribute. |
True
|
ax
|
'plt.Axes' | None
|
A matplotlib axes object to draw on. If None, a new figure and axes are created. |
None
|
**kwargs
|
Additional arguments passed to nx.draw(). |
{}
|
Source code in kmeanssa_ng/quantum_graph/space.py
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 | |
get_edge_length(n1, n2)
Get the length of an edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n1
|
int
|
First node of the edge. |
required |
n2
|
int
|
Second node of the edge. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Edge length. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If edge does not exist. |
Source code in kmeanssa_ng/quantum_graph/space.py
get_point_type()
index_to_centers(indices)
Convert node indices to centers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
list[int]
|
List of node indices. |
required |
Returns:
| Type | Description |
|---|---|
list[QGCenter]
|
List of centers at the specified nodes. |
Source code in kmeanssa_ng/quantum_graph/space.py
node_as_center(node, rng=None)
Create a center at a specific node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
int
|
The node to place the center at. |
required |
rng
|
Optional random number generator for neighbor selection. |
None
|
Returns:
| Type | Description |
|---|---|
QGCenter
|
A center located at the node. |
Source code in kmeanssa_ng/quantum_graph/space.py
node_center_distances(centers)
Geodesic distance from every node to every center.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centers
|
list[QGCenter]
|
List of k centers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ndarray
|
Array of shape (n_nodes, k), with rows in |
|
order |
ndarray
|
entry (v, i) is the geodesic distance from node v to center i. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pairwise distances have not been precomputed. |
Source code in kmeanssa_ng/quantum_graph/space.py
node_distance(n1, n2)
Compute shortest path distance between two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n1
|
int
|
First node. |
required |
n2
|
int
|
Second node. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Shortest path length using 'length' edge attribute. |
Source code in kmeanssa_ng/quantum_graph/space.py
node_energy(centers, weights=None)
Node-weighted k-means energy sum_v w[v] * min_i d(v, c_i)^2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centers
|
list[QGCenter]
|
List of cluster centers. |
required |
weights
|
ndarray | None
|
Per-node weights in |
None
|
Returns:
| Type | Description |
|---|---|
float
|
The weighted energy. |
Source code in kmeanssa_ng/quantum_graph/space.py
nodes_as_points()
Convert all nodes to points.
Returns:
| Type | Description |
|---|---|
list[QGPoint]
|
List of points, one at each node. |
Source code in 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:
| Type | Description |
|---|---|
ValueError
|
If graph is not connected or has invalid edge lengths. |
Source code in kmeanssa_ng/quantum_graph/space.py
quantum_path(p1, p2)
Compute the geodesic between two points on the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p1
|
QGPoint
|
First point. |
required |
p2
|
QGPoint
|
Second point. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float | tuple | None]
|
Dictionary with: - 'distance': geodesic distance - 'path': (node_from_p1_edge, node_from_p2_edge) or None if same edge |
Source code in kmeanssa_ng/quantum_graph/space.py
register_observations(points)
Set the per-node observation measure obs_weight from these points.
Each point counts at its closest node. This replaces any previous measure — the measure describes one observation set — so callers combining several samples must register the union explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
list[QGPoint]
|
The observation points (any points of this graph). |
required |
Source code in kmeanssa_ng/quantum_graph/space.py
remove_edge(u, v)
remove_edges_from(ebunch)
Remove edges in bulk; invalidates the precomputed distance cache.
remove_node(n)
remove_nodes_from(nodes)
Remove nodes in bulk; invalidates the precomputed distance cache.
validate_edge_lengths()
Validate that all edges have positive length attributes.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any edge is missing 'length' or has invalid length. |
Source code in kmeanssa_ng/quantum_graph/space.py
as_quantum_graph(graph, node_weight=1.0, edge_length=1.0, edge_weight=1.0, precompute=False)
Convert a NetworkX graph to a quantum graph with uniform attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The NetworkX graph to convert. |
required |
node_weight
|
float
|
Uniform weight to assign to all nodes. |
1.0
|
edge_length
|
float
|
Uniform length to assign to all edges. |
1.0
|
edge_weight
|
float
|
Uniform weight to assign to all edges. |
1.0
|
precompute
|
bool
|
If True, precompute pairwise distances (default: False for compatibility). |
False
|
Returns:
| Type | Description |
|---|---|
QuantumGraph
|
The converted quantum graph. |
Source code in kmeanssa_ng/quantum_graph/generators.py
complete_quantum_graph(objects, similarities=None, true_labels=None, precompute=True)
Create a complete quantum graph from objects with optional similarity matrix.
Useful for clustering when you have a pairwise distance/similarity matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list
|
List of objects (nodes will be indexed by position). |
required |
similarities
|
ndarray | None
|
Optional n×n matrix of similarities/distances. If None, all edges have length 1. |
None
|
true_labels
|
list | None
|
Optional true cluster labels for each object. |
None
|
precompute
|
bool
|
If True, precompute pairwise distances (default: True). |
True
|
Returns:
| Type | Description |
|---|---|
QuantumGraph
|
A complete quantum graph where edge lengths are given by the similarity matrix. |
Example
Source code in kmeanssa_ng/quantum_graph/generators.py
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 | |
generate_random_sbm(sizes=None, p=None, weights=None, lengths=None, precompute=True, random_state=None)
Generate an SBM quantum graph with block-specific edge lengths and node weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sizes
|
list[int] | None
|
Number of nodes in each block. Defaults to [50, 50]. |
None
|
p
|
list[list[float]] | None
|
Matrix of edge probabilities. Defaults to [[0.7, 0.1], [0.1, 0.7]]. |
None
|
weights
|
list[float] | None
|
Node weight for each block. Defaults to [1, 1]. |
None
|
lengths
|
list[list[float]] | None
|
Matrix of edge lengths. Element (i, j) gives the length for edges between blocks i and j. Defaults to [[1, 4], [4, 1]]. |
None
|
precompute
|
bool
|
If True, precompute pairwise distances (default: True). |
True
|
random_state
|
int | Generator | None
|
Seed or Generator controlling the block-model edge draws, for a reproducible graph structure. None (default) is random. |
None
|
Returns:
| Type | Description |
|---|---|
QuantumGraph
|
A quantum graph with block-specific attributes. |
Example
Source code in kmeanssa_ng/quantum_graph/generators.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 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 | |
generate_sbm(sizes=None, p=None, precompute=True, random_state=None)
Generate a Stochastic Block Model quantum graph.
Creates a quantum graph from a stochastic block model with uniform edge lengths and node weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sizes
|
list[int] | None
|
Number of nodes in each block. Defaults to [50, 50]. Must be a non-empty list of positive integers. |
None
|
p
|
list[list[float]] | None
|
Matrix of edge probabilities. Element (r, s) gives the density of edges from block r to block s. Must be symmetric for undirected graphs. Defaults to [[0.7, 0.1], [0.1, 0.7]]. Must be a square matrix with probabilities in [0, 1]. |
None
|
precompute
|
bool
|
If True, precompute pairwise distances (default: True). |
True
|
random_state
|
int | Generator | None
|
Seed or Generator controlling the block-model edge draws, for a reproducible graph structure. None (default) is random. |
None
|
Returns:
| Type | Description |
|---|---|
QuantumGraph
|
A quantum graph representing the SBM. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If sizes is empty, contains non-positive values, or if p is not a valid probability matrix matching sizes. |
Example
Source code in kmeanssa_ng/quantum_graph/generators.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
generate_simple_graph(n_a=5, n_aa=3, bridge_length=2.0, precompute=True, **attr)
Generate a symmetric two-cluster graph connected by a bridge.
Creates a graph with two symmetric star-like clusters (A and B) connected by a single edge. Each cluster has a central node with n_a neighbors, and each neighbor has n_aa further neighbors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_a
|
int
|
Number of neighbors for each central node (must be >= 0). |
5
|
n_aa
|
int
|
Number of second-level neighbors (must be >= 0). |
3
|
bridge_length
|
float
|
Length of the edge connecting the two clusters (must be > 0). |
2.0
|
precompute
|
bool
|
If True, precompute pairwise distances (default: True). |
True
|
**attr
|
Additional graph attributes. |
{}
|
Returns:
| Type | Description |
|---|---|
QuantumGraph
|
A quantum graph with two symmetric clusters. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n_a, n_aa < 0 or bridge_length <= 0. |
Source code in kmeanssa_ng/quantum_graph/generators.py
generate_simple_random_graph(n_a=5, n_b=5, lam_a=0, lam_b=0, bridge_length=10.0, precompute=True, random_state=None, **attr)
Generate a random two-cluster graph with Poisson branching.
Similar to generate_simple_graph but with: - Asymmetric clusters (different sizes) - Random edge lengths - Poisson-distributed third-level neighbors
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_a
|
int
|
Number of first-level neighbors of A0. |
5
|
n_b
|
int
|
Number of first-level neighbors of B0. |
5
|
lam_a
|
int
|
Poisson parameter for A cluster third-level branching. |
0
|
lam_b
|
int
|
Poisson parameter for B cluster third-level branching. |
0
|
bridge_length
|
float
|
Mean length of the bridge edge (actual length is uniform random). |
10.0
|
precompute
|
bool
|
If True, precompute pairwise distances (default: True). |
True
|
random_state
|
int | Generator | None
|
Seed or Generator controlling edge lengths and Poisson branching, for reproducible graphs. None (default) is random. |
None
|
**attr
|
Additional graph attributes. |
{}
|
Returns:
| Type | Description |
|---|---|
QuantumGraph
|
A random quantum graph with two clusters. |
Source code in kmeanssa_ng/quantum_graph/generators.py
:::