Skip to content

[P8-01] Optimize CPU and Metal CLI execution while preserving final-state views - #569

Merged
ruslan-splynx merged 3 commits into
mainfrom
codex/p8-01-optimized-cli
Nov 1, 2026
Merged

ruslan-splynx merged 3 commits into
mainfrom
codex/p8-01-optimized-cli

Conversation

@ruslan-splynx

@ruslan-splynx ruslan-splynx commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

CPU FP64/FP32 and Metal CLI state-vector runs previously used raw execution even when benchmarks/Python exercised fusion. CLI now selects a backend-appropriate optimized executor independently of formatting; --raw retains the reference driver and --execution-stats exposes actual retained IR kinds/planning cost.

Closes #551. The previously deferred Mac validation is now complete (2026-11-01), after integrating main 4e72028. Integration preserves noise-budget/CUDA execution APIs and benchmark diagnostics. Review and green CI are required on this integrated head.

Final-state pipelines omit measurement-seeded DCE so preparation on unmeasured qubits, collapsed-final-state sampling, amplitudes and expectations survive partial measurements. Relabelling is restored before views. CPU retains small precomputed diagonal gates; a phase-polynomial pilot made FP64 QFT 54% slower despite a smaller IR plan. Metal uses its supported GPU final-state wrapper. FP32 supports dense/diagonal fusion and correct tiled replay. Stabilizer/MPS/noise/CUDA drivers and the legacy bench subcommand are unchanged; the original Rust run_circuit API remains available.

Actual public CPU CLI A/B on i5-13500 Linux, release, RAYON_NUM_THREADS=1, 32 shots/seed 7, three interleaved fresh processes per mode:

Precision / workload Raw → optimized caller wall time
FP64 Bell n=2 1.051 → 1.095 ms
FP64 QFT n=20 929.441 → 955.058 ms (no gain; 2.8% slower)
FP64 brickwall n=20 d=12 774.322 → 537.925 ms
FP32 Bell n=2 1.146 → 1.182 ms
FP32 QFT n=20 2085.930 → 1050.777 ms
FP32 brickwall n=20 d=12 1130.771 → 579.510 ms

Public-process Criterion (10 samples, 1 s warmup, 2 s requested measurement, committed n=15 fixtures): FP64 brickwall 25.605 → 22.854 ms; FP32 QFT 37.224 → 19.194 ms; FP32 brickwall 40.369 → 24.355 ms. FP64 QFT median confidence intervals overlap, so no speedup is claimed. Tiny inputs expose added planning overhead. Comparisons use the same binary's raw/optimized modes, not historical binaries or kernel-only timers.

Fusion can change FP rounding/alias bins and hence a seed's exact histogram. Each mode is reproducible. Full n=20 brickwall verification found optimized FP32 max amplitude error 2.85e-9 and probability L1 error 5.52e-7 versus raw FP64 (raw FP32 L1 1.69e-6). CLI seed help documents the mode-dependent mapping.

Validation: 35 CLI unit tests, 40 existing command tests, GPU-init regression, three new public optimizer tests; final-state amplitude oracles for CPU FP64/FP32 including relabelling, external controls, partial measurements, phases/barriers and 64 generated circuits; existing optimized-driver oracles; three public CPU Python/CLI equivalence fixtures. Real Metal CLI raw/optimized oracle executed successfully. CUDA CLI compilation, workspace/Metal all-target clippy, formatting and diff checks pass. Raw data, Criterion samples, planning counts/costs, limitations and pending work: docs/perf/p8-01-optimized-cli.md.

Deferred Metal validation completed (2026-11-01)

Apple M5 MacBook Air, 10 CPU/10 GPU cores, 32 GiB unified memory. Release public CLI, FP32 Metal, same binary raw vs optimized, 32 shots/seed 7, RAYON_NUM_THREADS=1. One untimed process per mode, then four ABBA blocks (8 observations per mode), with diagnostics and host telemetry. Whole-process timings include planning/startup/output. No programs/services stopped; shared desktop, CPU idle median 74.68% (range 57.11–94.36%), GPU/thermals not isolated.

Metal workload Raw → optimized median Ratio
QFT n=20 91.931 → 42.257 ms 2.18×
Brickwall n=20 d=12 69.918 → 38.411 ms 1.82×
QFT n=24 3139.286 → 331.996 ms 9.46×
Brickwall n=24 d=12 1628.686 → 294.843 ms 5.52×

Bell n=2 has no consistent gain: 28.861 → 27.552 ms in the first series; 32.747 → 32.793 ms in the second. Public-process Criterion without diagnostics (10 samples, 1 s warmup/2 s requested measurement) gives median estimates: Bell n=2 26.699 → 27.358 ms; QFT n=15 33.196 → 28.375 ms; brickwall n=15 d=20 33.298 → 34.138 ms. Small Bell/brickwall remain no-gain cases; large-n gains are not universal.

Integrated validation: 81 release CLI tests with actual Metal explicitly enabled; 5 final-state oracle tests (1 existing ignored offline case); 4 optimized-driver oracle tests; 4 Metal oracle tests plus 1 property test. Three focused public optimizer tests passed again with Metal structural assertions. New n=16 real-Metal oracle verifies all 65,536 amplitudes against raw CPU FP64 after permutation restoration and partial measurement/unmeasured preparation. Workspace all-target Clippy, Metal CLI/backend all-target Clippy, formatting, Python syntax and changed documentation/checksums passed. Manual integration review found no blocking findings.

Raw samples, confidence intervals, logs, host telemetry, source/binary fingerprints and commands: docs/perf/data/p8-01-metal/. Report: docs/perf/p8-01-optimized-cli.md. Same-mode seed reproducibility passed; cross-mode histogram equality is not required because floating-point fusion can alter alias bins.

@ruslan-splynx
ruslan-splynx marked this pull request as ready for review November 1, 2026 21:06
@ruslan-splynx
ruslan-splynx merged commit ae537ad into main Nov 1, 2026
14 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P8-01] Use optimized CPU and Metal execution in the CLI

1 participant