sci-run / tasks /surface-code-decoding /surface-code-decoding-astra-xhigh-1h100-48h-20261020b /finalization /submission /d3_fbqs_kernel.py
Download tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d3_fbqs_kernel.py from s42chen/sci-run: direct link, hf CLI and curl.
- Browser
- Download file 4.45 kB
-
https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d3_fbqs_kernel.py
- Command line
-
hf download hf://datasets/s42chen/sci-run/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d3_fbqs_kernel.py
-
curl -L -o d3_fbqs_kernel.py https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d3_fbqs_kernel.py
4.45 kB
| """Persistent exact frontier for K simultaneous quality chains and shared logits.""" | |
| import jax | |
| import jax.numpy as jnp | |
| from jax import lax | |
| from jax.experimental import pallas as pl | |
| from jax.experimental.pallas import triton as plt | |
| from d3_fbqs_scalar_backend import gather, physical | |
| def make_decoder(width=10, families=3, interpret=False, derivatives=False, num_warps=8): | |
| size = 1 << (width + families) | |
| def decode(theta, dets, probabilities, masks, measured, quality_times, transition_gaps): | |
| nops, batch = probabilities.shape | |
| directions = 3 if derivatives else 1 | |
| def kernel(th, ds, ps, ms, ts, qs, gs, out, jac): | |
| shot, direction = pl.program_id(0), pl.program_id(1) | |
| theta = tuple(pl.load(th, (j,)) for j in range(3)) | |
| unit = tuple((direction == j).astype(jnp.float32) for j in range(3)) | |
| ix = jnp.arange(size, dtype=jnp.int32) | |
| quality = tuple((ix & (1 << (width + j))) != 0 for j in range(families)) | |
| def initial(t0, t1, t2): | |
| pi, _, _ = physical(t0, t1, t2) | |
| f = ((ix & ((1 << width) - 1)) == 0).astype(jnp.float32) | |
| for j in range(families): | |
| f = f * jnp.where(quality[j], pi, 1 - pi) | |
| return f | |
| if derivatives: | |
| f, df = jax.jvp(initial, theta, unit) | |
| else: | |
| f = initial(*theta); df = jnp.zeros_like(f) | |
| def iteration(op, carry): | |
| f, df = carry | |
| p, mask, detector = pl.load(ps, (op, shot)), pl.load(ms, (op,)), pl.load(ts, (op,)) | |
| times = tuple(pl.load(qs, (j, op)) for j in range(families)) | |
| gaps = tuple(pl.load(gs, (j, op)) for j in range(families)) | |
| observed = pl.load(ds, (shot, jnp.maximum(detector, 0))).astype(jnp.int32) | |
| def step(t0, t1, t2, f): | |
| pi, rho, alpha = physical(t0, t1, t2) | |
| for j in range(families): | |
| def transition(f): | |
| persistence = rho ** gaps[j] | |
| a, b = pi * (1 - persistence), pi + (1 - pi) * persistence | |
| flipped = gather(f, ix ^ (1 << (width + j))) | |
| return jnp.where(quality[j], b * f + a * flipped, (1 - a) * f + (1 - b) * flipped) | |
| f = lax.cond(gaps[j] > 0, transition, lambda f: f, f) | |
| def error(f): | |
| deviation = jnp.zeros_like(f) | |
| count = jnp.int32(0) | |
| for j in range(families): | |
| active = times[j] >= 0 | |
| deviation = deviation + jnp.where(active, quality[j].astype(jnp.float32) - pi, 0.) | |
| count = count + active.astype(jnp.int32) | |
| deviation = deviation / jnp.maximum(count, 1).astype(jnp.float32) | |
| rate = p + alpha * jnp.minimum(p / pi, (1 - p) / (1 - pi)) * deviation | |
| return (1 - rate) * f + rate * gather(f, ix ^ mask) | |
| def project(f): | |
| g = gather(f, (ix & ~mask) | (observed * mask)) * ((ix & mask) == 0) | |
| return g / jnp.maximum(jnp.sum(g), 1e-30) | |
| return lax.cond(detector < 0, error, project, f) | |
| if derivatives: | |
| return jax.jvp(step, (*theta, f), (*unit, df)) | |
| return step(*theta, f), df | |
| f, df = lax.fori_loop(0, nops, iteration, (f, df)) | |
| def observable(f): | |
| return jnp.sum(jnp.where((ix & 1) != 0, f, 0)) / jnp.maximum(jnp.sum(f), 1e-30) | |
| if derivatives: | |
| q, dq = jax.jvp(observable, (f,), (df,)) | |
| else: | |
| q, dq = observable(f), jnp.float32(0) | |
| pl.store(out, (direction, shot), q) | |
| pl.store(jac, (direction, shot), dq) | |
| return pl.pallas_call(kernel, out_shape=(jax.ShapeDtypeStruct((directions, batch), jnp.float32), jax.ShapeDtypeStruct((directions, batch), jnp.float32)), | |
| grid=(batch, directions), interpret=interpret, backend='triton', | |
| compiler_params=plt.CompilerParams(num_warps=num_warps, num_stages=1))( | |
| theta, dets, probabilities, masks, measured, quality_times, transition_gaps) | |
| return jax.jit(decode) | |