-
Notifications
You must be signed in to change notification settings - Fork 0
/
css.c
207 lines (189 loc) · 5.62 KB
/
css.c
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
/*
#######################################
## file: css.c ##
## Tue Jun 9 00:12:37 PDT 2020 ##
## Author: Weilei Zeng ##
## https://weileizeng.com ##
## [email protected] ##
#######################################
*/
/*
search for random CSS code and give a table of parameter for [n k (d_x,d_z)]
*/
#include "weilei_lib/my_lib.h"
//#include <itpp/itbase.h>
#include <ctime> //to get current time
#include <vector>
//using namespace itpp;
//using namespace std;
const int MAX_SIZE=100; // max size of the code
//print all distances. (the new value will be red if applicable)
void print_dist_list( std::vector<mat> dist_list, int na_input, int na, int ka, int dax, int daz, int format=0){
switch (format){
case 0:{//plain format, with red hightlight
for ( int i1 = 5 ; i1<=na_input ; i1 ++ ){
cout<<"n="<<i1<<endl;
for ( int i2 = 1 ; i2<=i1-2 ; i2 ++ ){
cout<<"k"<<i2;
for ( int i3 = 1 ; i3<=i1 ; i3 ++ ){
if ( i1 == na && i2 == ka && i3 == dax )
cout<<"\033[31m("<<i3<<","<< dist_list[i1].get(i2,i3)<<")\033[0m";
else
cout<<"("<<i3<<","<< dist_list[i1].get(i2,i3)<<")";
}
cout<<endl;
}
}
cout<<"input case: n k d = "<<na<<","<<ka<<","<<dax<<","<<daz<<endl;
}
break;
case 1:{ //markdown table
int temp;
for ( int i1 = 5 ; i1<=na_input ; i1 ++ ){//i1 -> n
cout<<endl<<"n="<<i1<<endl<<endl;
//print table header
cout<<"| k,dx ";
for ( int j = 1 ; j< i1+1 ; j++)
cout<<"| "<<j;
cout<<"|"<<endl;
for ( int j = 0 ; j< i1+1 ; j++)
cout<<"|-";
cout<<"|"<<endl;
//print entries
for ( int i2 = 1 ; i2<=i1-2 ; i2 ++ ){ //i2 -> k
cout<<"| "<<i2;
for ( int i3 = 1 ; i3<=i1 ; i3 ++ ){ // i3 -> dx
temp = dist_list[i1].get(i2,i3) ;
if ( temp == 0 ){
cout<<"| ";
}else{
cout<<"|"<< temp <<"";
}
}
cout<<"|"<<endl;
}
}
cout<<endl;
}
break;
}
return;
}
//print a file literally
int print_file(string filename){
int c;
FILE *fp;
fp = fopen(filename.c_str(), "r");
if (fp)
{
while ((c = getc(fp)) != EOF)
putchar(c);
fclose(fp);
}
return 0;
}
//read distance saved in file
void read_dist(mat & dist, int n){
string filename="data/dist-size-"+to_string(n)+".mtx";
cout<<"read file: "<<filename.c_str()<<endl;
FILE *f;
if ( (f=fopen(filename.c_str(), "r") ) == NULL) {
cout<<"No such file:"<<filename<<endl;
}else{
fclose(f);
//file exists; read and update
dist = MM_to_mat(filename);
/* if ( n == 14 || n == 15 ) {
print_file(filename);
// cout<<"```\n n = "<<n<<"\n"<<dist<<endl<<"```\n";
}*/
}
return ;
}
//write distance for a particular size into file
void write_dist(mat dist, int n){
string filename="data/dist-size-"+to_string(n)+".mtx";
mat_to_MM(dist,filename);
return;
}
int main(int args, char ** argv){
itpp::Parser parser;
parser.init(args,argv);
parser.set_silentmode(true);
int mode = 1; parser.get(mode,"mode");
int num_core = 16; parser.get(num_core,"num_core");
int num_trial = 10; parser.get(num_trial,"num_trial");
// int sub_mode = 1; parser.get(sub_mode,"sub_mode");
// std::string title_str; parser.get(title_str,"title");
// const char * title = title_str.c_str();
// std::string note=""; parser.get(note,"note");
int debug = 1; //default debug on
parser.get(debug,"debug");
int seed=1; parser.get(seed,"seed");
// std::cout<<"\t seed:"<<seed;
if (debug) std::cout<<"input seed: "<<seed<<" --> ";
seed=seed+get_time(3);
itpp::RNG_reset(seed);
if (debug) std::cout<<"converted seed:"<<seed<<endl;
// int na_input; parser.get(na_input,"na_input");
int n_low; parser.get(n_low,"n_low");
int n_high; parser.get(n_high,"n_high");
Real_Timer timer; timer.tic();
cout<<"<details>\n <summary>Click to expand the log</summary> \n\n ```log";
std::vector<mat> dist_list;
for ( int i =0; i<MAX_SIZE; i++){
mat distance(MAX_SIZE+1,MAX_SIZE+1);
distance.zeros();
read_dist(distance, i);
dist_list.push_back(distance);
}
cout<<"```\n\n</details>\n";
//just print current result
if ( debug ){
if ( debug ==2 ){
print_dist_list( dist_list , n_high , 0,0,0, 0,1);
return 0;
}else{
print_dist_list( dist_list , n_high , 0,0,0, 0);
}
}
// if (debug) cout<<"mode, title ->"<<mode<<endl<<title<<endl;
switch( mode ){
case 1://generate random CSS code and estimate distance
#pragma omp parallel for num_threads(num_core)
// {
for ( int i =0; i<num_trial;i++){
cout<<"*";
GF2mat Gax,Gaz,Cax,Caz;
int na,ka, Gax_row,Gaz_row;//k is not necessary number of qubits
// na = na_input;
na=randi(n_low,n_high);
ka = randi(1,na-2);Gax_row=randi(1,na-ka-1); Gaz_row=na-ka-Gax_row;
//#pragma omp critical
{
getRandomQuantumCode(na,Gax_row,Gaz_row,Gax,Gaz,Cax,Caz);
}
if (! is_quantum_code(Gax,Gaz,Cax,Caz)) {
cout<<"not a quantum code"<<endl;
throw "invalid code";
}
int dax = quantum_dist_v2(Gax,Gaz);
int daz = quantum_dist_v2(Gax,Gaz,1);
//found larger distance ( either d_x or d_z )
#pragma omp critical
{
//TODO: use min weight decoder to verify the distance
if ( daz > dist_list[na].get(ka,dax)){
dist_list[na].set(ka,dax,daz);
if (debug) print_dist_list( dist_list ,n_high, na,ka,dax, daz);
cout<<"n k d = "<<na<<","<<ka<<","<<dax<<","<<daz<<", time: "<<timer.toc()<<endl;
write_dist(dist_list[na], na);
}
}
}
break;//for switch statement
}
if ( debug ) timer.toc_print();
cout<<"css.out:program ended"<<endl;
return 0;
}