File size: 2,833 Bytes
32d1b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from pathlib import Path
import json,sys
import numpy as np
import stim
sys.path.insert(0,'/workspace/research/exact_d3')
from exact import make_plan,parse_errors


def load_all(max_ops=3072):
    root=Path('/workspace/data')
    rows=[r for r in json.loads((root/'train/manifest.json').read_text())['conditions'] if r['distance']==3]
    groups=sorted({r['group'] for r in rows});result=[]
    for row in rows:
        dem=stim.DetectorErrorModel.from_file(root/'train'/row['noise']);plan=make_plan(dem)
        assert int(plan['width'])<=10
        count=len(plan['masks']);assert count<=max_ops,(row['name'],count)
        coords=dem.get_detector_coordinates()
        circuit=stim.Circuit.from_file(root/'train'/row['circuit'])
        qubit_coords=circuit.get_final_qubit_coordinates()
        first_measurement=next(ins for ins in circuit.flattened() if ins.name in ['M','MX','MR','MRX'])
        spaces=sorted(tuple(qubit_coords[t.value][:2]) for t in first_measurement.targets_copy())
        assert len(spaces)==8
        idx={(int(c[2]),tuple(c[:2])):d for d,c in coords.items()}
        errors=parse_errors(dem)
        context=np.full((max_ops,40),200,np.int32)
        static=np.zeros((max_ops,57),np.float32)
        for op,source in enumerate(plan['source_indices']):
            if source<0:continue
            ds,logical,p=errors[source]
            t=min(int(coords[d][2]) for d in ds) if ds else 0
            for dt in range(-2,3):
                for s,spatial in enumerate(spaces):context[op,(dt+2)*8+s]=idx.get((t+dt,spatial),200)
            for d in ds:
                c=coords[d];static[op,(int(c[2])-t+2)*8+spaces.index(tuple(c[:2]))]=1
            static[op,40]=np.log(max(p,1e-7)/(1-max(p,1e-7)))/8
            static[op,41]=logical;static[op,42]=len(ds)/4
            static[op,43]=t/25;static[op,44]=row['rounds']/25
            static[op,45]=t==0;static[op,46]=t>=row['rounds']-1
            static[op,47+groups.index(row['group'])]=1
            static[op,55]=row['basis']=='X';static[op,56]=1
        masks=np.pad(plan['masks'],(0,max_ops-count))
        measured=np.pad(plan['measured'],(0,max_ops-count),constant_values=-1)
        p=np.pad(plan['probabilities'],(0,max_ops-count))
        valid=((p>0)&(measured<0)).astype(np.float32)
        logits=np.log(np.clip(p,1e-7,1-1e-7)/np.clip(1-p,1e-7,1-1e-7)).astype(np.float32)
        item={'row':row,'context':context,'static':static,'masks':masks,'measured':measured,'base_logits':logits,'valid':valid}
        for split in ['train','validation']:
            a=np.load(root/split/row['arrays']);ds=np.unpackbits(a['detectors'],axis=1,count=row['num_detectors'],bitorder='little')
            item[split+'_dets']=np.pad(ds,((0,0),(0,201-ds.shape[1])))
            item[split+'_labels']=a['labels']
        result.append(item)
    return result