sci-run / tasks /surface-code-decoding /surface-code-decoding-astra-xhigh-1h100-48h-20261020b /finalization /submission /d5_base_context_model.py
Download tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d5_base_context_model.py from s42chen/sci-run: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d5_base_context_model.py
- Command line
-
hf download hf://datasets/s42chen/sci-run/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d5_base_context_model.py
-
curl -L -o d5_base_context_model.py https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/d5_base_context_model.py
3.19 kB
| """Small shared network corrects soft-conditioned relevant-CSS edge logits.""" | |
| import jax | |
| import jax.numpy as jnp | |
| def initialize(seed,hidden=64,input_features=394): | |
| assert input_features in (394,414) | |
| keys=jax.random.split(jax.random.key(seed),3) | |
| return {'w1':jax.random.normal(keys[0],(input_features,hidden))*jnp.sqrt(2/input_features),'b1':jnp.zeros(hidden), | |
| 'w2':jax.random.normal(keys[1],(hidden,hidden))*jnp.sqrt(2/hidden),'b2':jnp.zeros(hidden), | |
| 'w3':jax.random.normal(keys[2],(hidden,1))*.001,'b3':jnp.zeros(1)} | |
| def endpoint_features(events,measurements,density,static): | |
| """Twenty endpoint/history summaries computed solely from the input syndrome.""" | |
| support=static[:,:120].reshape(-1,5,24) | |
| selected=jnp.max(support,axis=1) | |
| endpoint_counts=jnp.maximum(support.sum(axis=-1),1) | |
| spatial_count=jnp.maximum(selected.sum(axis=-1),1) | |
| event_frames=events.reshape(len(events),-1,5,24) | |
| measurement_frames=measurements.reshape(len(events),-1,5,24) | |
| endpoint_events=(event_frames*support[None,:,:,:]).sum(axis=-1) | |
| endpoint_measurements=(measurement_frames*support[None,:,:,:]).sum(axis=-1) | |
| spatial_events=(event_frames*selected[None,:,None,:]).sum(axis=-1)/spatial_count[None,:,None] | |
| endpoint_total=endpoint_events.sum(axis=-1) | |
| endpoint_fraction=endpoint_total/jnp.maximum(support.sum(axis=(1,2)),1)[None,:] | |
| endpoint_parity=jnp.mod(endpoint_total,2) | |
| selected_density=(density[:,None,:]*selected[None,:,:]).sum(axis=-1)/spatial_count[None,:] | |
| adjacent=(event_frames[:,:,:-1,:]*event_frames[:,:,1:,:]*selected[None,:,None,:]).sum(axis=(2,3))/(4*spatial_count[None,:]) | |
| measurement_mean=(measurement_frames*selected[None,:,None,:]).sum(axis=(2,3))/(5*spatial_count[None,:]) | |
| return jnp.concatenate([ | |
| endpoint_events/endpoint_counts[None,:,:], | |
| endpoint_measurements/endpoint_counts[None,:,:],spatial_events, | |
| selected_density[:,:,None],endpoint_fraction[:,:,None],endpoint_parity[:,:,None], | |
| adjacent[:,:,None],measurement_mean[:,:,None]],axis=-1) | |
| def probabilities(parameters,dets,base_logits,context,static,valid,grid,measurement_indices,counts): | |
| event_grid=dets[:,grid] | |
| measured=(jnp.cumsum(event_grid.astype(jnp.int32),axis=1)&1).reshape(len(dets),624) | |
| measured=jnp.pad(measured,((0,0),(0,1))) | |
| events=dets[:,context].astype(jnp.float32) | |
| measurements=measured[:,measurement_indices[context]].astype(jnp.float32) | |
| density=jnp.sum(event_grid,axis=1)/counts[None,:] | |
| features=jnp.concatenate([events,measurements, | |
| jnp.broadcast_to(density[:,None,:],(len(dets),len(context),24)), | |
| jnp.broadcast_to(static[None,:,:],(len(dets),*static.shape)),base_logits[:,:,None]/8],axis=-1) | |
| if parameters['w1'].shape[0]==414: | |
| features=jnp.concatenate([features,endpoint_features(events,measurements,density,static)],axis=-1) | |
| else: | |
| assert parameters['w1'].shape[0]==394 | |
| h=jax.nn.silu(features@parameters['w1']+parameters['b1']) | |
| h=jax.nn.silu(h@parameters['w2']+parameters['b2']) | |
| delta=2*jnp.tanh((h@parameters['w3']+parameters['b3'])[...,0]) | |
| return (jax.nn.sigmoid(base_logits+delta)*valid[None,:]).T | |