Download tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/contextual.py from s42chen/sci-run: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/contextual.py
- Command line
-
hf download hf://datasets/s42chen/sci-run/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/contextual.py
-
curl -L -o contextual.py https://huggingface.co/datasets/s42chen/sci-run/resolve/main/tasks/surface-code-decoding/surface-code-decoding-astra-xhigh-1h100-48h-20261020b/finalization/submission/contextual.py
1.51 kB
| """Portable contextual exact decoder; no sample ID enters any computation.""" | |
| from pathlib import Path | |
| import numpy as np | |
| class ContextualDecoder: | |
| def __init__(self): | |
| import jax | |
| from context_model import probabilities | |
| from exact import make_decoder | |
| exact=make_decoder(10) | |
| def infer(weights,ds,context,static,base_logits,valid,masks,measured): | |
| p=probabilities(weights,ds,context,static,base_logits,valid) | |
| return exact(ds,p,masks,measured) | |
| self.infer=infer | |
| self.weights={} | |
| def predict(self,root,record,group_record,packed,num_detectors,batch=1024): | |
| import jax.numpy as jnp | |
| path=group_record['weights'] | |
| if path not in self.weights: | |
| self.weights[path]={k:jnp.asarray(v) for k,v in dict(np.load(root/path,allow_pickle=False)).items()} | |
| plan=dict(np.load(root/record['contextual_plan'],allow_pickle=False)) | |
| ds=np.unpackbits(packed,axis=1,count=num_detectors,bitorder='little') | |
| ds=np.pad(ds,((0,0),(0,201-num_detectors))) | |
| args=[plan[k] for k in ['context','static','base_logits','valid','masks','measured']] | |
| outputs=[] | |
| for begin in range(0,len(ds),batch): | |
| selected=ds[begin:begin+batch];n=len(selected) | |
| selected=np.pad(selected,((0,batch-n),(0,0))) | |
| outputs.append(np.asarray(self.infer(self.weights[path],selected,*args))[:n]) | |
| return np.concatenate(outputs).astype(np.float32) | |