sci-run / tasks /surface-code-decoding /surface-code-decoding-astra-xhigh-1h100-48h-20261020b /finalization /submission /conditional_edge_checkpoint_gradient.py
Download tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/conditional_edge_checkpoint_gradient.py from s42chen/sci-run: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/conditional_edge_checkpoint_gradient.py
- Command line
-
hf download hf://datasets/s42chen/sci-run/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/conditional_edge_checkpoint_gradient.py
-
curl -L -o conditional_edge_checkpoint_gradient.py https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/conditional_edge_checkpoint_gradient.py
3.18 kB
| """Exact logical BCE gradient with block recomputation of frontier histories.""" | |
| import jax | |
| import jax.numpy as jnp | |
| def make_loss_and_grad(width, block_size=32): | |
| indices = jnp.arange(1 << width, dtype=jnp.int32) | |
| def evaluate(dets, labels, probabilities, masks, measured): | |
| nops, batch = probabilities.shape | |
| padding = (-nops) % block_size | |
| p = jnp.pad(probabilities, ((0, padding), (0, 0))) | |
| m = jnp.pad(masks, (0, padding)) | |
| d = jnp.pad(measured, (0, padding), constant_values=-1) | |
| blocks = (p.reshape(-1, block_size, batch), m.reshape(-1, block_size), | |
| d.reshape(-1, block_size)) | |
| def forward(f, args): | |
| probability, mask, detector = args | |
| def error(f): | |
| out = (1 - probability[:, None]) * f + probability[:, None] * f[:, indices ^ mask] | |
| return out, jnp.ones(batch) | |
| def project(f): | |
| dst = (indices[None, :] & ~mask) | (dets[:, detector, None].astype(jnp.int32) * mask) | |
| g = jnp.take_along_axis(f, dst, axis=1) * ((indices & mask) == 0) | |
| norm = jnp.maximum(g.sum(axis=1), 1e-30) | |
| return g / norm[:, None], norm | |
| out, norm = jax.lax.cond(detector < 0, error, project, f) | |
| return out, (f, norm) | |
| def forward_block(f, args): | |
| def step(carry, item): | |
| result, _ = forward(carry, item) | |
| return result, None | |
| out, _ = jax.lax.scan(step, f, args) | |
| return out, f | |
| initial = jnp.zeros((batch, 1 << width), jnp.float32).at[:, 0].set(1) | |
| f, checkpoints = jax.lax.scan(forward_block, initial, blocks) | |
| indicator = ((indices[None, :] & 1) == labels[:, None]) | |
| correct = jnp.maximum(jnp.sum(f * indicator, axis=1), 1e-10) | |
| prediction = jnp.sum(f[:, 1::2], axis=1) | |
| terminal = 1 - indicator / correct[:, None] | |
| def backward(b, args): | |
| probability, mask, detector, old, norm = args | |
| def error(b): | |
| flipped = b[:, indices ^ mask] | |
| grad = jnp.sum(old * (flipped - b), axis=1) | |
| return (1 - probability[:, None]) * b + probability[:, None] * flipped, grad | |
| def project(b): | |
| obs = ((indices[None, :] & mask) == dets[:, detector, None].astype(jnp.int32) * mask) | |
| values = b[:, indices & ~mask] | |
| mean = jnp.sum(old * obs * values, axis=1) / norm | |
| return (values - mean[:, None]) * obs / norm[:, None], jnp.zeros(batch) | |
| return jax.lax.cond(detector < 0, error, project, b) | |
| def backward_block(b, args): | |
| probabilities, masks, measured, checkpoint = args | |
| _, (history, norms) = jax.lax.scan(forward, checkpoint, (probabilities, masks, measured)) | |
| return jax.lax.scan(backward, b, (probabilities, masks, measured, history, norms), reverse=True) | |
| _, gradients = jax.lax.scan(backward_block, terminal, (*blocks, checkpoints), reverse=True) | |
| return -jnp.log(correct), prediction, gradients.reshape(-1, batch)[:nops] | |
| return evaluate | |