Commit 5e427e4
committed
Parallelize analysis sampling loops in expressibility, entanglement, and trainability
The three core analysis functions (compute_expressibility,
compute_entanglement_capability, estimate_trainability) all sample
many independent quantum circuits and aggregate their results. Their
hot paths were sequential Python for-loops; with the new opt-in
parallelism users get up to 2.4x wall-clock speedup with the default
thread pool on modest batches, and larger speedups on big batches
with the process pool.
API:
- Adds keyword-only parallel and max_workers parameters to:
- compute_expressibility
- compute_fidelity_distribution
- compute_entanglement_capability
- estimate_trainability
- parallel accepts False (default, sequential), True or 'thread'
(ThreadPoolExecutor), or 'process' (ProcessPoolExecutor with the
initializer/initargs pattern so the encoding is pickled once per
worker, not once per sample).
- API mirrors BaseEncoding.get_circuits exactly so users learn one
set of knobs for the whole library.
- Process pool works for all three backends here, unlike
get_circuits — workers exchange only NumPy arrays and floats, not
PennyLane closures.
Numerical determinism is the single most important guarantee:
- The RNG is fully consumed in the main process before any work is
dispatched. For numpy.random.Generator, batched rng.uniform calls
produce an identical sequence to per-iteration calls with the
same seed, so the existing per-iteration code path is preserved
byte-for-byte while gaining the option to dispatch work in
parallel.
- For a fixed seed, every parallel mode produces output identical to
the sequential baseline. This is the property the new tests
enforce — if it ever regresses, the analysis pipeline silently
loses reproducibility.
Implementation details:
- New encoding_atlas.analysis._parallel module exposing the small
ParallelArg type alias and resolve_parallel_mode normaliser
shared across all three analysis files.
- Each analysis file adds top-level worker plumbing
(initializer/initargs pattern, picklable worker compute function,
shared _compute_one_* helper used by all code paths so the
arithmetic is identical regardless of mode).
- Trainability is the trickiest: failures must remain tolerated
per-sample (raising from a worker would tear down the entire
pool), and successful gradients must still be packed contiguously
from index 0 to match the sequential pack-on-success semantics.
- Bad parallel values raise a clean ValueError up front (not wrapped
in AnalysisError by the broad sampling try/except).
Tests (tests/unit/analysis/test_parallel.py, 61 cases across 6
classes) cover: every parallel mode x every installed backend x both
return_details=False and =True, the lower-level
compute_fidelity_distribution, all four supported parallel values
plus six rejected values for the normaliser, Scott measure with
explicit k, observable kwarg propagating into trainability workers,
n_samples=1 short-circuit, max_workers=1 edge case, and clean
ValueError on bad parallel. Determinism is verified by exact
equality (not approximate) at the numerical output level.
Full test suite (4502 not-slow + 382 slow with optional backends)
passes; ruff, black, build, and mkdocs --strict all clean. All 1239
pre-existing analysis tests still pass — defaults preserve previous
sequential behavior.1 parent 933fa31 commit 5e427e4
5 files changed
Lines changed: 1049 additions & 103 deletions
File tree
- src/encoding_atlas/analysis
- tests/unit/analysis
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| 119 | + | |
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
| 126 | + | |
125 | 127 | | |
126 | 128 | | |
127 | 129 | | |
| |||
252 | 254 | | |
253 | 255 | | |
254 | 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 | + | |
255 | 334 | | |
256 | 335 | | |
257 | 336 | | |
| |||
268 | 347 | | |
269 | 348 | | |
270 | 349 | | |
| 350 | + | |
| 351 | + | |
271 | 352 | | |
272 | 353 | | |
273 | 354 | | |
| |||
282 | 363 | | |
283 | 364 | | |
284 | 365 | | |
| 366 | + | |
| 367 | + | |
285 | 368 | | |
286 | 369 | | |
287 | 370 | | |
| |||
295 | 378 | | |
296 | 379 | | |
297 | 380 | | |
| 381 | + | |
| 382 | + | |
298 | 383 | | |
299 | 384 | | |
300 | 385 | | |
| |||
340 | 425 | | |
341 | 426 | | |
342 | 427 | | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
343 | 445 | | |
344 | 446 | | |
345 | 447 | | |
| |||
470 | 572 | | |
471 | 573 | | |
472 | 574 | | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
473 | 578 | | |
474 | 579 | | |
475 | 580 | | |
| |||
528 | 633 | | |
529 | 634 | | |
530 | 635 | | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
531 | 644 | | |
532 | 645 | | |
533 | 646 | | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
541 | | - | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
547 | 655 | | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
552 | | - | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
553 | 680 | | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
| 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 | + | |
560 | 717 | | |
561 | | - | |
562 | 718 | | |
563 | 719 | | |
564 | 720 | | |
565 | | - | |
| 721 | + | |
566 | 722 | | |
567 | 723 | | |
568 | | - | |
569 | | - | |
570 | 724 | | |
571 | 725 | | |
572 | 726 | | |
| 727 | + | |
573 | 728 | | |
574 | 729 | | |
575 | 730 | | |
576 | | - | |
577 | | - | |
578 | | - | |
579 | | - | |
580 | | - | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
585 | 734 | | |
586 | 735 | | |
587 | 736 | | |
| |||
0 commit comments