-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulation.cc
More file actions
856 lines (616 loc) · 21.8 KB
/
simulation.cc
File metadata and controls
856 lines (616 loc) · 21.8 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
#include "simulation.h"
#include "constants.h"
#include <omp.h>
// Constructor reads parameters from a config file.
Simulation::Simulation (void)
{
string tmp;
string item;
int pos;
ifstream f_conf("in/parameters.txt");
getline(f_conf,tmp);
pos = tmp.find(" = ");
tmp = tmp.substr (pos + 3);
stringstream ss(tmp);
while(ss >> item)
{
XSize.push_back(atoi(item.c_str()));
}
getline(f_conf,tmp);
pos = tmp.find(" = ");
tmp = tmp.substr (pos + 3);
iterations = atoi(tmp.c_str());
getline(f_conf,tmp);
pos = tmp.find(" = ");
tmp = tmp.substr (pos + 3);
pMin = atof(tmp.c_str());
getline(f_conf,tmp);
pos = tmp.find(" = ");
tmp = tmp.substr (pos + 3);
pMax = atof(tmp.c_str());
getline(f_conf,tmp);
pos = tmp.find(" = ");
tmp = tmp.substr (pos + 3);
pStep = atof(tmp.c_str());
getline(f_conf,tmp);
pos = tmp.find(" = ");
tmp = tmp.substr (pos + 3);
q_sqr_factor = atof(tmp.c_str());
getline(f_conf,tmp);
pos = tmp.find(" = ");
tmp = tmp.substr (pos + 3);
q_oct_factor = atof(tmp.c_str());
f_conf.close();
}
// Simulate a simple decoding algorithm. Run simulation and save result.
void Simulation::run (void)
{
// Initialize the random number generator seed
srand (time(0));
// Initialize constant random number seed
//srand(8844);
//Initialize Runtime TImer
clock_t start, end;
start = clock();
//loop for different XSizes
for (int i = 0; i < (int) XSize.size(); i++)
{
//data file
ofstream f_out;
stringstream ss;
//add a 0 to the name if XSize is smaller than 0
if(XSize[i] < 10)
{
ss << "out/results_0" << XSize[i] << ".txt";
}
else
{
ss << "out/results_" << XSize[i] << ".txt";
}
//open data file stream
f_out.open (ss.str().c_str());
//Print Information to screen
cout << "RUNNING SIMULATION: XSize=" << XSize[i] << " iterations=" << iterations << " pMin=" << pMin << " pMax=" << pMax << " pStep=" << pStep << endl;
cout << "ERROR [%]:\t\t\t\t\t\t\tSUCCESS [%]"<<endl<<"Level\tEffective\tper_Step\tSQR\tOCT\tSuccess"<< endl;
// loop for different p's
for (double p = pMin; p <= pMax + 0.00001; p += pStep)
{
//###################
//configuration
//number of succesful iterations
int cntSucc = 0;
//Error probability per time step
double p_per_step = p;//determine_p_per_step(p,XSize[i]);
//SquareSyndrome measurement error probability
double q_SQR = p_per_step*q_sqr_factor;
//OctogonSyndrome measurement error probability
double q_OCT = p_per_step*q_oct_factor;
//effective error probability
double p_effective = determine_p_effective(p_per_step,XSize[i]);
//Measuerement and Error Repetitions
int Timesteps = XSize[i];
//####################
//Calculate all the distances in the history array
struct Distance_and_ID returnDistandID = Distance(XSize[i], Timesteps, p_per_step, q_SQR, q_OCT);
float** D = returnDistandID.Distance_Array;
int numberofids = returnDistandID.number_of_IDs;
//loop the iterations with
//enabled multithreading
//omp_set_num_threads(2);
#pragma omp parallel for schedule(dynamic) default(shared) reduction(+:cntSucc)
for (int iterate = 1; iterate <= iterations; iterate++ )
{
//There is a lot of debugging in this loop
//important tasks are marked with ######
//test the parallel computing
//cout << omp_get_num_threads() << endl;
if (debug4)
{
cout<<"iteration: "<<iterate<<endl;
}
if (debug1) cout << "1. ERROR WAS GENERATED:" << endl;
//############
//Build a lattice
Lattice myLattice (XSize[i],Timesteps);
//############
if (debug1) myLattice.printState();
if (debug3)
{
cout<<"Before Correction:"<<endl;
myLattice.printStateQB();
}
if (debug1) cout << "2. Z ERRORS CORRECTED USING SITE STABILIZERS:" << endl;
//############
//evolute and correct the lattice
myLattice.evolute_and_correct(p_per_step,q_SQR,q_OCT,D);
//############
if (DEBUG2D)
{
cout<<"Corrected"<<endl;
cout<<" "<<": ";
myLattice.print2DState();
}
if (debug1) myLattice.printState();
if (debug3)
{
cout<<"After Correction:"<<endl;
myLattice.printStateQB();
}
//#########
//do one last round of perfect correction
myLattice.perfect_correction();
//#########
if (debug4)
{
if ( !myLattice.is_corrected() )
{
cout<<"Not Corrected"<<endl;
assert(0);
}
}
//##########
//Check success of the recovery
if (myLattice.success ())
{
cntSucc++;
//##########
if (debug1) cout << "SIMULATION REPORTS SUCCESS!!!" << endl;
if(debug9)
{
char in;
cout<< "Correction Succeded"<<endl;
cin>>in;
}
}
else
{
if(debug7)
{
char in;
cout<< "Correction Failed"<<endl;
cin>>in;
}
if (debug1) cout << "SIMULATION REPORTS FAILURE!!!" << endl;
}
//Progressbar
if (PROGRESSBAR && !(iterations%100))
{
float progress = (float)iterate/(float)iterations;
cout<<"\rprogress[%]: "<<setprecision(3)<<100* progress;
//if(iterate == iterations-1){cout<<"\r";}
}
}// end of loop iterations
//Print to Screen
if(PROGRESSBAR)
{
cout<<"\r \r";
}
cout<< 100 * p <<"\t"<<setprecision(5)<<p_effective * 100<<"\t\t" <<p_per_step * 100<< "\t\t"<<100*q_SQR<<"\t"<<100*q_OCT<<"\t\t"<< (double) (100 * cntSucc) / iterations << endl;
//Print to file
f_out << 100 * p_per_step<< " " << (double) (100 * cntSucc) / iterations << endl;
//clean memory (the distances)
for ( int j = 0; j < numberofids; j++)
{
delete [] D[j];
}
delete [] D;
}//end of loop p
//close file
f_out.close();
}//end of loop XSize
//Runtime Timer end
//print Runtime information
end = clock();
if(RUNTIME)
{
cout<<endl<<"Computation Ended. Resume"<<endl<<"Starttime: "<<start<<endl;
cout<<"Endtime:"<<end<<endl;
cout<<"Clocks_per_Sec: "<<CLOCKS_PER_SEC<<endl;
cout<<"Runtime ca.: " <<(double) (end/CLOCKS_PER_SEC)<<" s"<<endl;
}
}//end of run()
//Determines the effective error probability for a given p and T per qubit
//If an error occurred 2,4,6,... times the qubit is error free
//So only if an error occures 1,3,5,.. times it really is an error
//This sums up to an effective error probability
double Simulation::determine_p_effective (double p_per_step, int T_in)
{
//hier errechne wahrscheinlichkeit nach T schritten
double p_eff_T = 0.0000f;
for (int k = 0; k <= T_in; k++)
{
//only sum up odd k's
if (k%2)
{
double k_loop = k;
double q = 1 - p_per_step;
double l = T_in -k_loop;
double prod1 = ncr(T_in,k_loop);
double prod2 = pow(p_per_step,k_loop);
double prod3 = pow(q,l);
p_eff_T += prod1*prod3*prod2;
}//endofif
}//endoffor
return p_eff_T;
}//
//for a given p and T we have an effective error probability
//Determines the probability p_per_step we need for an effective
//error probability after T steps
double Simulation::determine_p_per_step (double p_G, int T_in)
{
double p_step = PSTEP;
double p_max = PMAX;
double T = (double) T_in;
//runs
//Probability nach T-steps
vector <double> vec_p_eff_T;
//Effektive Einzelwahrscheinlichkeit per step (Thats what we are looking for)
vector <double> vec_p_per_step;
for (double p_per_step = 0.0000f; p_per_step <= p_max; p_per_step += p_step)
{
//hier calculate probability after T steps
double p_eff_T = 0.0000f;
for (int k = 0; k <= T; k++)
{
//only sum up odd k's
if (k%2)
{
double k_loop = k;
double q = 1- p_per_step;
double l = T-k_loop;
double prod1 =ncr(T,k_loop);
double prod2 = pow(p_per_step,k_loop);
double prod3 = pow(q,l);
p_eff_T += prod1*prod3*prod2;
/*
cout<<"kloop:"<<k_loop<<endl<<" ncr("<<T<<","<<k<<")="<<prod1<<endl<<"pow("<<p<<","<<k_loop<<")="<<prod2<<endl<<"pow("<<1-p<<","<<T-k_loop<<")="<<prod3<<endl;
cout<<"p_eff="<<p_eff<<endl;
*/
}//endofif
}//endoffor
vec_p_per_step.push_back(p_per_step);
vec_p_eff_T.push_back(p_eff_T);
}
/*
//debug
cout<<"p\tp_eff"<<endl;
for (int i = 0; i < abs((int)prob.size()); i++) {
cout<<prob[i]<<"\t"<<prob_eff[i]<<endl;
}
*/
//now check which p_real fits to which p_0
double delta_p_wunsch = DELTAPWUNSCH;
//nun suche ein p das das gewuenschte p gesamt ergibt
while (true)
{
for (int i = 0; i < (int)vec_p_per_step.size(); i++)
{
double delta_p = p_G - vec_p_eff_T[i];
//absolute value
if (delta_p < 0)
{
delta_p *= -1;
}
if (delta_p < delta_p_wunsch )
{
return vec_p_per_step[i];
}
}
//if there is no p found that fits
delta_p_wunsch += 0.000005;
if (delta_p_wunsch > 0.1)
{
cout<<"NO PROPER P FOUND!"<<endl;
break;
}
}
return p_G/ ( (double) T);
}//end of p_per_step
//Well, faculty
double Simulation::faculty (double n)
{
if (n == 0)
{
return 1;
}
else
{
double n_minus_1 = n-1;
n = n * faculty(n_minus_1);
}
return n;
}
//and n chose k
double Simulation::ncr (double n, double k)
{
double n_min_k = n - k;
double bin = faculty(n) / (faculty(n_min_k) * faculty(k));
return bin;
}
//This guy uses a Dijkstra algorithm to calculate all the distances
//from all vertices to all other vertices in the lattice and stores it
//in the struct. The second struct variable stores the dimensions of the
//Distancearray for the delete
//So it basically returns an array with all important distances
struct Distance_and_ID Simulation::Distance (int newXSize, int newT, double p_err, double q_SQR_err, double q_OCT_err)
{
int xSize = 2*newXSize;
int ySize = 2*newXSize;
int T = newT;
double p = p_err;
double q_SQR = q_SQR_err;
double q_OCT = q_OCT_err;
//Vertices
vector<Vertex*> vertices;
//3d ID Tracker
int ***IDTracker;
IDTracker = new int** [xSize];
int D_ID = 0;
//fill history with false;
//fill IDTracker with IDs
if(debug10)
{
cout<<"Create history and ID Tracker:"<<endl;
}
for (int i = 0; i < xSize; i++)
{
IDTracker[i] = new int *[ySize];
for (int j = 0; j < ySize; j++)
{
IDTracker[i][j] = new int [T+1];
for (int t = 0; t < T+1; t++)
{
if(debug10)
{
cout<<i<<j<<t<<" D_ID: "<<D_ID<<endl;
}
IDTracker[i][j][t] = D_ID;
Vertex* v = new Vertex (i, j, t);
vertices.push_back(v);
D_ID++;
}
}
}
// Calculate relative edge weights of time and space edges
double deltaXY = 1000;
double deltaT_SQR = 1000;
double deltaT_OCT = 1000;
if (p!=0) deltaXY = log ((1 - p) / p);
if (q_SQR!=0) deltaT_SQR = log ((1 - q_SQR) / q_SQR);
if (q_OCT!=0) deltaT_OCT = log ((1 - q_OCT) / q_OCT);
//DIJKSTRA ALGORITHM
//initialize size of adjacency list
if(debug10)
{
cout<<"Create adjacency list lenght=D_ID= "<<D_ID<<endl<<endl;
}
adjacency_list_t adjacency_list(D_ID);
// Add all weighted edges
// remember to insert edges both ways for an undirected graph
for (int t = 0; t < (T+1); t++)
{
for (int i = 0; i < xSize; i++)
{
for (int j = 0; j < ySize; j++)
{
//XOCTAGons do not have connections
if(!(i%2) && !(j%2))
{
if(debug10)
{
cout<<i<<j<<" XOctagon continue"<<endl;
}
continue;
}
//ZOctagon
if (i%2 && j%2)
{
if(debug10)
{
cout<<i<<j<<" ZOctagon: ";
}
//get the ID
int hereID = IDTracker[i][j][t];
//get the neigbor ids
int neighborID1 = IDTracker[(i+1)%xSize][j][t];
int neighborID2 = IDTracker[(i+xSize-1)%xSize][j][t];
int neighborID3 = IDTracker[i][(j+1)%ySize][t];
int neighborID4 = IDTracker[i][(j+ySize-1)%ySize][t];
if(debug10)
{
cout<<"hereID: "<<hereID<<" neighborIDS: "<<neighborID1<<" "<<neighborID2<<" "<<neighborID3<<" "<<neighborID4<<" ";
}
adjacency_list[hereID].push_back(neighbor(neighborID1, deltaXY));
adjacency_list[hereID].push_back(neighbor(neighborID2, deltaXY));
adjacency_list[hereID].push_back(neighbor(neighborID3, deltaXY));
adjacency_list[hereID].push_back(neighbor(neighborID4, deltaXY));
if(t!= 0)
{
int neighborID5 = IDTracker[i][j][t-1];
adjacency_list[hereID].push_back(neighbor(neighborID5, deltaT_OCT));
if(debug10)
{
cout<<neighborID5<<" ";
}
}
if(t!= T)
{
int neighborID6 = IDTracker[i][j][t+1];
adjacency_list[hereID].push_back(neighbor(neighborID6, deltaT_OCT));
if(debug10)
{
cout<<neighborID6<<" ";
}
}
if(debug10)
{
cout<<endl;
}
//horizontal connected Square
}
else if (i%2 && !(j%2))
{
if(debug10)
{
cout<<i<<j<<"horizontal square: ";
}
//get the ID
int hereID = IDTracker[i][j][t];
//get the neigbor ids
int neighborID3 = IDTracker[i][(j+1)%ySize][t];
int neighborID4 = IDTracker[i][(j-1+ySize)%ySize][t];
//add the neighbors to the list
adjacency_list[hereID].push_back(neighbor(neighborID3, deltaXY));
adjacency_list[hereID].push_back(neighbor(neighborID4, deltaXY));
if(debug10)
{
cout<<"hereID: "<<hereID<<"neighborIDS: "<<neighborID3<<" "<<neighborID4<<" ";
}
if(t!= 0)
{
int neighborID5 = IDTracker[i][j][t-1];
adjacency_list[hereID].push_back(neighbor(neighborID5, deltaT_SQR));
if(debug10)
{
cout<<neighborID5<<" ";
}
}
if(t!= T)
{
int neighborID6 = IDTracker[i][j][t+1];
adjacency_list[hereID].push_back(neighbor(neighborID6, deltaT_SQR));
if(debug10)
{
cout<<neighborID6<<" ";
}
}
if(debug10)
{
cout<<endl;
}
//vertical connected square
}
else if (!(i%2) && j%2)
{
if(debug10)
{
cout<<i<<j<<"vertical square: ";
}
//get the ID
int hereID = IDTracker[i][j][t];
//get the neigbor ids
int neighborID1 = IDTracker[(i+1)%xSize][j][t];
int neighborID2 = IDTracker[(i+xSize-1)%xSize][j][t];
//add the neigbors to the list
adjacency_list[hereID].push_back(neighbor(neighborID1, deltaXY));
adjacency_list[hereID].push_back(neighbor(neighborID2, deltaXY));
if(debug10)
{
cout<<"hereID: "<<hereID<<"neighborIDS: "<<neighborID1<<" "<<neighborID2<<" ";
}
if(t!= 0)
{
int neighborID5 = IDTracker[i][j][t-1];
adjacency_list[hereID].push_back(neighbor(neighborID5, deltaT_SQR));
if(debug10)
{
cout<<neighborID5<<" ";
}
}
if(t!= T)
{
int neighborID6 = IDTracker[i][j][t+1];
adjacency_list[hereID].push_back(neighbor(neighborID6, deltaT_SQR));
if(debug10)
{
cout<<neighborID6<<" ";
}
}
if(debug10)
{
cout<<endl;
}
}
else
{
cout<<"Assert"<<endl;
assert(0);
}
}//end of i
}//end of j
}//end of t
//FINISHEd adjecency list
//Now initialize the edgeLenghts and the IDStorage
float** D_edgeLengths;
D_edgeLengths = new float*[D_ID];
for (int i = 0; i < D_ID; i++)
{
D_edgeLengths[i] = new float[D_ID];
for (int j = 0; j < i; j++)
{
D_edgeLengths[i][j] = -1.0;
}
}
/*
double D_edgeLengths[D_ID][D_ID];
for (int i = 0; i < D_ID; i++) {
for (int j = 0; j < D_ID; j++) {
D_edgeLengths[i][j] = -1.0;
}
}
*/
// Add all weighted edges (u,v) with weight w in complete graph G
//initialize parallel computing
//every thread uses the same lattice
//this is possible, because every thread operates on a different pointer
#pragma omp parallel for schedule(dynamic) default(shared)
for (int i = 0; i < D_ID; i++)
{
//if XOctagon, kill dat thing
if (!(vertices[i]->x%2) && !(vertices[i]->y%2))
{
continue;
}
//for every start vertex i get the ID
int startID = i;
//and calculate all the distances
std::vector<weight_t> min_distance;
std::vector<vertex_t> previous;
DijkstraComputePaths(startID, adjacency_list, min_distance, previous);
//Remember, the Matrix is Triangular
for (int j = 0; j < i; j++)
{
//if i=j or XOctagon
if (i == j || (!(vertices[j]->x%2) && !(vertices[j]->y%2)))
{
continue;
}
//for every end vertex j get the ID
int endID = j;
//And get the minimum distance
float dist = (float) min_distance[endID];
//then save it
D_edgeLengths[i][j] = dist;
}//end of j
}//end of i
//Delete Vertices
for (int i = 0; i < D_ID; i++)
{
delete vertices[i];
}
// Delete IDTracker
for (int i = 0; i < xSize; i++)
{
for (int j = 0; j < ySize; j++)
{
delete IDTracker[i][j];
}
delete IDTracker[i];
}
delete IDTracker;
struct Distance_and_ID returnDistandID;
returnDistandID.Distance_Array = D_edgeLengths;
returnDistandID.number_of_IDs = D_ID;
return returnDistandID;
}//end of Distance
//end of file simulation.cc