Generators API
kmeanssa_ng.quantum_graph.generators
Generators for quantum graphs used in testing and benchmarking.
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
:::