From 2828eb86155c507d61d38f484a7f6eb17c0281ae Mon Sep 17 00:00:00 2001 From: qingyunqian Date: Thu, 30 Jul 2026 12:32:57 +0800 Subject: [PATCH 1/3] Add Task 08 bounded-batch memory fix --- optimized_solutions/challenge-08/README.md | 48 ++ .../challenge-08/factor-ablation.svg | 435 ++++++++++++++++++ .../solution_8_bounded_batches.py | 115 +++++ 3 files changed, 598 insertions(+) create mode 100644 optimized_solutions/challenge-08/README.md create mode 100644 optimized_solutions/challenge-08/factor-ablation.svg create mode 100644 optimized_solutions/challenge-08/solution_8_bounded_batches.py diff --git a/optimized_solutions/challenge-08/README.md b/optimized_solutions/challenge-08/README.md new file mode 100644 index 0000000..669d0d8 --- /dev/null +++ b/optimized_solutions/challenge-08/README.md @@ -0,0 +1,48 @@ +# Task 08 — bounded TensorCircuit sampling batches + +## Take-home insight + +**Keep the public expert’s TensorCircuit circuit and all 8192 +`perfect_sampling` trajectories, but execute the mapped sampler in contiguous +256-shot blocks.** The expert’s single 8192-shot `vmap` puts the shot axis into +every XLA contraction intermediate; bounding that axis is the only material +change and converts the canonical 7-GiB workload from **0/5 OOM** to +**5/5 PASS**. + +## Factor attribution + +| Factor | Canonical/full-workload result | Decision | +| --- | ---: | --- | +| One 8192-shot mapped batch | 0/5 PASS; attempted buffers 9.31–17.97 GiB | Root cause | +| 512-shot blocks | 50.843 s single screen | Feasible, not best | +| **256-shot blocks** | **44.028 s single screen** | **Promote** | +| 128-shot blocks | 55.182 s single screen | Dispatch overhead | + +![Task 08 factor attribution](factor-ablation.svg) + +*Figure — The bounded 256-shot execution is the necessary OOM-to-PASS factor +and the best of the three feasible chunk-size screens. Chunk-size timings are +single full-workload screens, not uncertainty estimates.* + +## What the factors mean + +- **Bounded mapped batch:** the same cached TensorCircuit + `K.jit(K.vmap(perfect_sampling))` function consumes 32 contiguous slices + instead of the complete status matrix at once. Every seeded status row is + still consumed exactly once and in the original order. +- **Chunk size:** 512 leaves larger contraction intermediates live; 128 adds + excess host dispatch. A 256-shot block is the measured balance for this + workload. + +## Result and claim boundary + +At 7 GiB, the immutable expert failed all five canonical attempts, while the +candidate passed all five with a mean runtime of **47.356 s**. This establishes +a peak-memory and reproducibility improvement; it does not provide a numerical +speedup denominator. + +With sufficient memory, the expert is algorithmically runnable. A separate +64-GiB, six-CPU-affinity five-pair session produced 126.675 s expert and +123.188 s candidate means. The candidate won only 3/5 pairs and its mean +pairwise speedup was 1.045x with 95% t-CI [0.818, 1.273]. Therefore this PR +does **not** claim a confirmed runtime speedup. diff --git a/optimized_solutions/challenge-08/factor-ablation.svg b/optimized_solutions/challenge-08/factor-ablation.svg new file mode 100644 index 0000000..5eaec5d --- /dev/null +++ b/optimized_solutions/challenge-08/factor-ablation.svg @@ -0,0 +1,435 @@ + + + + + + + + 2026-07-30T12:31:15.518141 + image/svg+xml + + + Matplotlib v3.10.8, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + Expert + 8192-shot batch + + + + + + + + + + Candidate + 32 × 256 shots + + + + + + + + + + + + + + + + + + 0 + + + + + + + + + + + + + 1 + + + + + + + + + + + + + 2 + + + + + + + + + + + + + 3 + + + + + + + + + + + + + 4 + + + + + + + + + + + + + 5 + + + + Passing canonical runs + + + + + + + + + + + + + + + + + + + + + + + + 0/5 + OOM + + + 5/5 + PASS + + + a + + + 7-GiB canonical outcome + + + + + + + + + + + + + + + + + + 0 + + + + + + + + + + + + + 10 + + + + + + + + + + + + + 20 + + + + + + + + + + + + + 30 + + + + + + + + + + + + + 40 + + + + + + + + + + + + + 50 + + + + + + + + + + + + + 60 + + + + Runtime, one full-workload screen (s) + + + + + + + + + + + 512 shots + + + + + + + + + + 256 shots + + + + + + + + + + 128 shots + + + + + + + + + + + + + + + + + + + + 50.8 s + + + 44.0 s + + + 55.2 s + + + b + + + lower is better + + + Feasible chunk-size screen + + + + + + + + + + + + diff --git a/optimized_solutions/challenge-08/solution_8_bounded_batches.py b/optimized_solutions/challenge-08/solution_8_bounded_batches.py new file mode 100644 index 0000000..e65888c --- /dev/null +++ b/optimized_solutions/challenge-08/solution_8_bounded_batches.py @@ -0,0 +1,115 @@ +""" +Task Suite Problem 8: 7x7 mixed-axis grid sampling. + +The solution builds the TensorCircuit tensor network directly and samples from it +without constructing the full 2^49 statevector. +""" + +import numpy as np + +import tensorcircuit as tc + +K = tc.set_backend("jax") +tc.set_dtype("complex64") +tc.set_contractor("omeco-4-4") + + +def ry_angle(row, col, config): + return ( + config["ry_offset"] + + config["ry_row_sin_scale"] + * np.sin(config["ry_row_sin_frequency"] * (row + 1)) + + config["ry_col_cos_scale"] + * np.cos(config["ry_col_cos_frequency"] * (col + 1)) + + config["ry_diag_sin_scale"] + * np.sin(config["ry_diag_sin_frequency"] * (row + col + 2)) + ) + + +def rzz_angle(row, col, edge_index, config): + return ( + config["rzz_offset"] + + config["rzz_edge_sin_scale"] + * np.sin(config["rzz_edge_sin_frequency"] * (edge_index + 1)) + + config["rzz_site_cos_scale"] + * np.cos(config["rzz_site_cos_frequency"] * (2 * row + col + 1)) + ) + + +def rxx_angle(row, col, edge_index, config): + return ( + config["rxx_offset"] + + config["rxx_edge_cos_scale"] + * np.cos(config["rxx_edge_cos_frequency"] * (edge_index + 1)) + + config["rxx_site_sin_scale"] + * np.sin(config["rxx_site_sin_frequency"] * (row + 2 * col + 1)) + ) + + +def rx_angle(row, col, config): + return ( + config["rx_offset"] + + config["rx_row_cos_scale"] + * np.cos(config["rx_row_cos_frequency"] * (row + 1)) + - config["rx_col_sin_scale"] + * np.sin(config["rx_col_sin_frequency"] * (col + 1)) + + config["rx_diag_cos_scale"] + * np.cos(config["rx_diag_cos_frequency"] * (row + col + 2)) + ) + + +def build_circuit(config): + n_side = config["grid_side"] + circuit = tc.Circuit(config["n_qubits"]) + + for row in range(n_side): + for col in range(n_side): + qubit = row * n_side + col + circuit.ry(qubit, theta=ry_angle(row, col, config)) + + edge_index = 0 + for row in range(n_side): + for col in range(n_side - 1): + left = row * n_side + col + right = left + 1 + circuit.rzz(left, right, theta=rzz_angle(row, col, edge_index, config)) + edge_index += 1 + + edge_index = 0 + for row in range(n_side - 1): + for col in range(n_side): + left = row * n_side + col + right = (row + 1) * n_side + col + circuit.rxx(left, right, theta=rxx_angle(row, col, edge_index, config)) + edge_index += 1 + + for row in range(n_side): + for col in range(n_side): + qubit = row * n_side + col + circuit.rx(qubit, theta=rx_angle(row, col, config)) + + return circuit + + +def run_solution(config): + circuit = build_circuit(config) + + rng = np.random.default_rng(2033) + status = K.convert_to_tensor( + rng.random((config["n_samples"], config["n_qubits"]), dtype=np.float32) + ) + + def sample_one(seed): + sample, _ = circuit.perfect_sampling(seed) + return sample + + sample_batch = K.jit(K.vmap(sample_one)) + chunk_size = 256 + samples = [ + np.asarray( + K.numpy(sample_batch(status[start : start + chunk_size])), + dtype=np.int32, + ) + for start in range(0, config["n_samples"], chunk_size) + ] + return {"samples": np.concatenate(samples, axis=0)} From e7d281cdd7e1d56fa954e678ce885857b67eb71c Mon Sep 17 00:00:00 2001 From: qingyunqian Date: Thu, 30 Jul 2026 12:37:21 +0800 Subject: [PATCH 2/3] Refocus Task 08 on complete benchmark evidence --- optimized_solutions/challenge-08/README.md | 48 +- .../challenge-08/factor-ablation.svg | 505 +++++++++++------- 2 files changed, 336 insertions(+), 217 deletions(-) diff --git a/optimized_solutions/challenge-08/README.md b/optimized_solutions/challenge-08/README.md index 669d0d8..cd3825b 100644 --- a/optimized_solutions/challenge-08/README.md +++ b/optimized_solutions/challenge-08/README.md @@ -4,25 +4,24 @@ **Keep the public expert’s TensorCircuit circuit and all 8192 `perfect_sampling` trajectories, but execute the mapped sampler in contiguous -256-shot blocks.** The expert’s single 8192-shot `vmap` puts the shot axis into -every XLA contraction intermediate; bounding that axis is the only material -change and converts the canonical 7-GiB workload from **0/5 OOM** to -**5/5 PASS**. +256-shot blocks.** A single 8192-shot `vmap` puts the shot axis into every XLA +contraction intermediate; bounding that axis is the only material change and +reduces the peak live tensor-network workload without changing the sampling +algorithm. ## Factor attribution -| Factor | Canonical/full-workload result | Decision | +| Factor | Full-workload result | Decision | | --- | ---: | --- | -| One 8192-shot mapped batch | 0/5 PASS; attempted buffers 9.31–17.97 GiB | Root cause | | 512-shot blocks | 50.843 s single screen | Feasible, not best | | **256-shot blocks** | **44.028 s single screen** | **Promote** | | 128-shot blocks | 55.182 s single screen | Dispatch overhead | ![Task 08 factor attribution](factor-ablation.svg) -*Figure — The bounded 256-shot execution is the necessary OOM-to-PASS factor -and the best of the three feasible chunk-size screens. Chunk-size timings are -single full-workload screens, not uncertainty estimates.* +*Figure — On the complete sufficient-memory five-pair comparison, chunking +does not resolve a runtime speedup. The 256-shot setting is retained because it +has the best full-workload chunk-size screen and bounds peak intermediates.* ## What the factors mean @@ -34,15 +33,22 @@ single full-workload screens, not uncertainty estimates.* excess host dispatch. A 256-shot block is the measured balance for this workload. -## Result and claim boundary - -At 7 GiB, the immutable expert failed all five canonical attempts, while the -candidate passed all five with a mean runtime of **47.356 s**. This establishes -a peak-memory and reproducibility improvement; it does not provide a numerical -speedup denominator. - -With sufficient memory, the expert is algorithmically runnable. A separate -64-GiB, six-CPU-affinity five-pair session produced 126.675 s expert and -123.188 s candidate means. The candidate won only 3/5 pairs and its mean -pairwise speedup was 1.045x with 95% t-CI [0.818, 1.273]. Therefore this PR -does **not** claim a confirmed runtime speedup. +## Complete canonical result + +A sufficient-memory, same-node, six-CPU-affinity five-pair session ran the +complete 8192-shot workload for both implementations: + +| Metric | Public expert | 256-shot candidate | +| --- | ---: | ---: | +| Passing runs | 5/5 | 5/5 | +| Mean runtime | 126.675 s | 123.188 s | +| Pair wins | 2/5 | 3/5 | +| Mean pairwise speedup | — | 1.045x | +| 95% Student-t interval | — | [0.818, 1.273] | + +The interval crosses 1.0, so there is **no confirmed runtime speedup**. The +promoted benefit is bounded peak memory: under tighter allocations the +monolithic mapped batch can exhaust memory, while the 256-shot implementation +completes the unchanged workload. The exact memory threshold is hardware and +contraction-path dependent; the full allocation study remains in the benchmark +research repository. diff --git a/optimized_solutions/challenge-08/factor-ablation.svg b/optimized_solutions/challenge-08/factor-ablation.svg index 5eaec5d..308bc8c 100644 --- a/optimized_solutions/challenge-08/factor-ablation.svg +++ b/optimized_solutions/challenge-08/factor-ablation.svg @@ -1,12 +1,12 @@ - + - 2026-07-30T12:31:15.518141 + 2026-07-30T12:36:22.281307 image/svg+xml @@ -21,19 +21,19 @@ - - @@ -41,395 +41,508 @@ z - - + - Expert - 8192-shot batch + Public expert - + - Candidate - 32 × 256 shots + 256-shot + candidate - + - - + - 0 + 100 - + - + - 1 + 110 - + - + - 2 + 120 - + - + - 3 + 130 - + - + - 4 + 140 - + - + - 5 + 150 - Passing canonical runs + Runtime (s) - - + + - - + + - - + + + + + + + + + + - - + - + - - + + + + + - 0/5 - OOM + 1 + + + + + + + + + + + - 5/5 - PASS + 2 + + + + + + + + + + + - a + 3 + + + + + + + + + + + - 7-GiB canonical outcome + 4 + + + + + + + + + + + + + + 5 + + + 3/5 candidate wins + 1.045× paired mean + 95% CI [0.818, 1.273] + + + a + + + Complete 8192-shot pairs - - + - - + + - + - + - - 0 + + 0 - - + + - + - + - - 10 + + 10 - - + + - + - + - - 20 + + 20 - - + + - + - + - - 30 + + 30 - - + + - + - + - - 40 + + 40 - - + + - + - + - - 50 + + 50 - - + + - + - + - - 60 + + 60 - - Runtime, one full-workload screen (s) + + Runtime, one full-workload screen (s) - + - + - - 512 shots + + 512 shots - + - + - - 256 shots + + 256 shots - + - + - - 128 shots + + 128 shots - - + +" clip-path="url(#p8ed51a866a)" style="fill: #a8adb4"/> - - + +" clip-path="url(#p8ed51a866a)" style="fill: #3b6fb6"/> - - + +" clip-path="url(#p8ed51a866a)" style="fill: #a8adb4"/> - - + - - + - - 50.8 s + + 50.8 s - - 44.0 s + + 44.0 s - - 55.2 s + + 55.2 s - - b + + b - - lower is better + + lower is better - - Feasible chunk-size screen + + Chunk-size factor screen - - + + - - + + From 04c002bbc46a64ac94cb2c3bf84dbc606d0d1dc3 Mon Sep 17 00:00:00 2001 From: qingyunqian Date: Thu, 30 Jul 2026 13:34:05 +0800 Subject: [PATCH 3/3] Clarify Task 08 factor attribution --- optimized_solutions/challenge-08/README.md | 9 +- .../challenge-08/factor-ablation.svg | 683 ++++++++++-------- 2 files changed, 377 insertions(+), 315 deletions(-) diff --git a/optimized_solutions/challenge-08/README.md b/optimized_solutions/challenge-08/README.md index cd3825b..515f83f 100644 --- a/optimized_solutions/challenge-08/README.md +++ b/optimized_solutions/challenge-08/README.md @@ -19,9 +19,12 @@ algorithm. ![Task 08 factor attribution](factor-ablation.svg) -*Figure — On the complete sufficient-memory five-pair comparison, chunking -does not resolve a runtime speedup. The 256-shot setting is retained because it -has the best full-workload chunk-size screen and bounds peak intermediates.* +*Figure — Panel a keeps the complete sufficient-memory five-pair comparison +and makes clear that its confidence interval includes `1x`. Panels b–c +normalize the 256-shot screen to `1.0x` and show the additional runtime from +512- and 128-shot blocks. The 256-shot setting is retained for bounded +execution and the best full-workload chunk-size screen, not a confirmed +end-to-end speedup.* ## What the factors mean diff --git a/optimized_solutions/challenge-08/factor-ablation.svg b/optimized_solutions/challenge-08/factor-ablation.svg index 308bc8c..00dcbfc 100644 --- a/optimized_solutions/challenge-08/factor-ablation.svg +++ b/optimized_solutions/challenge-08/factor-ablation.svg @@ -1,12 +1,11 @@ - + - 2026-07-30T12:36:22.281307 image/svg+xml @@ -21,19 +20,19 @@ - - @@ -41,508 +40,568 @@ z - +" style="stroke: #222222; stroke-width: 0.8"/> - + - Public expert + 256-shot + 123.19 s - + - 256-shot - candidate + Public expert + 126.68 s - + - +" style="stroke: #222222; stroke-width: 0.8"/> - + - 100 + 0.0 - + - + - 110 + 0.2 - + - + - 120 + 0.4 - + - + - 130 + 0.6 - + - + - 140 + 0.8 - + - + - 150 + 1.0 - Runtime (s) + Runtime normalized to recommended - - - - - - - - - - - - - - - - + - - - - - + + + - - - - - - - +" clip-path="url(#pdc99cfbbe7)" style="fill: #4472c4; stroke: #333333; stroke-width: 0.55; stroke-linejoin: miter"/> - - 1 - - - - + - - - - - - - +" clip-path="url(#pdc99cfbbe7)" style="fill: #9aa0a8; stroke: #333333; stroke-width: 0.55; stroke-linejoin: miter"/> + + + paired mean 1.045×; CI [0.818, 1.273] - 2 - - - - - - - - - - - + a - 3 - - - - - - - - - - - + 1.000× - 4 - - - - - - - - - - - + 1.028× - 5 - - - 3/5 candidate wins - 1.045× paired mean - 95% CI [0.818, 1.273] + Complete 8192 shots - - a - - - Complete 8192-shot pairs + + - - + + + + + + + + 256 shots + 44.03 s + + + + + + + + + + 512 shots + 50.84 s + + + + + + + + + + + + + + + 0.0 + + + - + - + - 0 + 0.2 - + - + - + - 10 + 0.4 - + - + - + - 20 + 0.6 - + - + - + - 30 + 0.8 - + - + - + - 40 + 1.0 - + - + - + - 50 + 1.2 - - - - + + + + + + + + + + + + + + + single full-workload screen + + + b + + + 1.000× + + + 1.155× + + + 256-shot chunks → 512-shot chunks + + + + + + + + + + + - + - - 60 + + 256 shots + 44.03 s - - Runtime, one full-workload screen (s) - - - - + - + - - 512 shots + + 128 shots + 55.18 s - + + + + + + - + - - 256 shots + + 0.0 - - + + + + + - + - - 128 shots + + 0.2 + + + + + + + + + + + + + 0.4 + + + + + + + + + + + + + 0.6 + + + + + + + + + + + + + 0.8 + + + + + + + + + + + + + 1.0 + + + + + + + + + + + + + 1.2 - - - - - + + + + + + + +" clip-path="url(#pf9a3aa45fd)" style="fill: #4472c4; stroke: #333333; stroke-width: 0.55; stroke-linejoin: miter"/> - - + +" clip-path="url(#pf9a3aa45fd)" style="fill: #c44e52; stroke: #333333; stroke-width: 0.55; stroke-linejoin: miter"/> - - - - - - - - 50.8 s + + single full-workload screen - - 44.0 s + + c - - 55.2 s + + 1.000× - - b + + 1.253× - - lower is better + + 256-shot chunks → 128-shot chunks - - Chunk-size factor screen + + + + Task 08 factor ablations — 256-shot chunks minimize the screen; full-run speedup is unconfirmed + + + Panel a: five complete 8192-shot pairs; paired 95% CI includes 1. Panels b–c: one full-workload screen per chunk size. + - - + + + + + - - + +