CSS product code  0.1
C++ library to estimate distance of CSS codes. Some particular construction of CSS codes are implemented.
test.cpp
Go to the documentation of this file.
1 //#include "weilei_lib/my_lib.h"
3 #include <stdio.h>
4 #include <itpp/itbase.h>
5 #include <fstream>
6 
7 //#include <cstdlib>
8 //#include <iostream>
9 #include <ctime> //get time in seconds
10 #include <chrono> // get time in milli seconds
11 using namespace itpp;
12 using namespace std;
13 using namespace common;
14 
15 
16 int copy_test(){//fail
17  cout<<"copy the text in a file to the end of another file"<<endl;
18  ifstream infile("tmp/a.txt");
19  ofstream outfile("tmp/b.txt");
20  outfile << infile.rdbuf();
21  infile.close();
22  outfile.close();
23  return 0;
24 }
25 
26 int mm_test(int k){//test the limit of array length
27  GF2mat G;
28  // int k=750;//750, segmentation fault
29  // for ( int k=500;k<1000;k+=50){
30  cout<<"k = "<<k<<endl;
31  G.set_size(k,k,true);
32  for (int i=0;i<k;i++){
33  for ( int j=0;j<k;j++){
34  G.set(i,j,1);
35  }
36  }
37  GF2mat_to_MM(G,"data/tmp/bigG.mm");
38  //}
39  cout<<"done"<<endl;
40  return 0;
41 
42 }
43 int mm_test2(){
44  GF2mat G;
45  int k=3;
46  // for ( int k=500;k<1000;k+=50){
47  cout<<"k = "<<k<<endl;
48  G.set_size(k,k,true);
49  for (int i=0;i<k;i++){
50  for ( int j=0;j<k;j++){
51  G.set(i,i,1);
52  }
53  }
54  GF2mat_to_MM(G,"data/tmp/smallG.mm");
55  //}
56 
57  return 0;
58 
59 }
60 
61 int mm_test3(){
62  GF2mat G;
63  G = MM_to_GF2mat("data/tmp/bigG.mm7000");
64  cout<<"got matrix G("<<G.rows()<<", "<<G.cols()<<")"<<endl;
65  return 0;
66 }
67 
68 int G_s_test(){
69  //check the rank of G_sx and G_sz for cubic repetition code
70  //result: G_sx.rows() = 1;
71  char *filename_G_sx="data/repetition/cubic_size_5_G_sx.mm";
72  char *filename_G_sz="data/repetition/cubic_size_5_G_sz.mm";
73  cout<<filename_G_sx<<endl;
74  GF2mat G_sx = MM_to_GF2mat(filename_G_sx);
75  GF2mat G_sz = MM_to_GF2mat(filename_G_sz);
76  cout<<G_sx.rows()<<" + "<<G_sx.cols()<<endl;
77  cout<<G_sz.rows()<<" + "<<G_sz.cols()<<endl;
78  return 0;
79 
80 }
81 
82 GF2mat * array_pointer(){
83  GF2mat A=gf2dense_eye(2),B(3,3);
84  static GF2mat p[]={A,B};
85  cout<<"A = "<<A<<endl;
86  return &p[0];
87 }
88 
90  const int m=5;
91  int d[m][m];
92  d[0][0]=1;
93  d[0][1]=2;
94  d[1][1]=3;
95  return &d[0][0];
96 }
97 
99  GF2mat *p;
100  p= array_pointer();
101  cout<<p<<endl;
102  cout<<*(p+1)<<endl;
103 
104  // const int m=5;
105  int *d;//[m][m];
106  d=array_pointer2();
107  cout<<d<<endl;
108  cout<<*(d+1)<<endl;
109  return 0;
110 }
111 
112 vector<vector<int> > vector_test(){
113  vector<vector<int> > vec(10);
114  vector<int> sub(10);
115  sub[0]=1;
116  vec[0]=sub;
117  for ( int i=0;i<10;i++){
118  cout<<vec[i][i]<<endl;
119  }
120  return vec;
121 }
122 
124  vector<vector<int> > vec;
125  // cout<<vec<<endl;
126  //cout<<vec[1]<<endl;
127  vec = vector_test();
128  for ( int i=0;i<10;i++){
129  cout<<vec[i][i]<<endl;
130  }
131 
132  return 0;
133 }
134 
135 
136 
138  int max=3;
139  GF2mat *ptr[max];
140  //int var[]={1,3,5};
141  GF2mat G(2,3);
142  cout<<G<<endl;
143  ptr[0]=&G;
144  GF2mat Q=*ptr[0];
145  cout<<Q<<endl;
146  GF2mat H[]={G,G};
147  cout<<sizeof(H)/sizeof(H[0])<<endl;
148 
149  GF2mat F[]={G};
150  cout<<sizeof(F)/sizeof(F[0])<<endl;
151 
152 
153  /*
154  for (int i=0;i<max;i++){
155  ptr[i] = &var[i];
156  }
157  for ( int i = 0; i < max; i++) {
158  printf("Value of var[%d] = %d\n", i, *ptr[i] );
159  }
160  */
161 
162  return 0;
163 }
164 
165 int char_test(){
166  char a[]="left";//b[]="right";
167 
168  if (a=="left"){
169  cout<<"done"<<endl;
170  }
171  return 0;
172 
173 }
174 
175 
176 int if_test(){
177 
178  int a= (3>2)? 1:0;
179  cout<<a<<endl;
180  int b= (3>3)? 1:0;
181  cout<<b<<endl;
182  return 0;
183 }
184 
186  int **board = new int*[4];
187  for (int i = 0; i < 4; i++) {
188  board[i] = new int[10];
189  }
190  return 0;
191 }
192 
193 void copy2(GF2mat &G){
194  G.set(1,1,G(1,1)+G(1,2));
195  return;
196 }
197 
198 void copy1(){
199  // check the initialization of GF2mat.
200  // result: each time when assgin G to a new matrix. It will copy a new one instead to pointing to the target.
201  GF2mat G(2,2),H(2,2);
202  H.set(1,2,1);
203  copy2(H);
204  G=H;
205  cout<<G<<endl;
206  cout<<H<<endl;
207  copy2(H);
208  cout<<G<<endl;
209  cout<<H<<endl;
210  return;
211 }
212 
213 int combine_random(int seed){
214  // int seed = 1;
215  itpp::RNG_reset(seed);
216  int a = randi(1,100000000);
217  std::cout<<a<<"\t";
218  std::cout<<std::time(nullptr)<<"\t";
219  itpp::RNG_reset(std::time(nullptr));
220  int b = randi(1,std::time(nullptr));
221  std::cout<<b<<"\t";
222  itpp::RNG_reset(b+seed);
223  std::cout<<randi(1,100000000)<<"\t";
224  std::cout<<std::endl;
225  return 0;
226 }
227 
229 {
230  std::srand(std::time(nullptr)); // use current time as seed for random generator
231  int random_variable = std::rand();
232  std::cout << "Random value on [0 " << RAND_MAX << "]: "
233  << random_variable <<"\t";
234 
235  // roll a 6-sided die 20 times
236  for (int n=0; n != 20; ++n) {
237  int x = 7;
238  while(x > 6)
239  x = 1 + std::rand()/((RAND_MAX + 1u)/6); // Note: 1+rand()%6 is biased
240  std::cout << x << ' ';
241  }
242  std::cout<<endl;
243  return 0;
244 }
245 
246 void random_test(int seed){
247  // RNG_randomize();
248 
249 
250 
251 
252  auto now = std::chrono::system_clock::now();
253  auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
254 
255  auto value = now.time_since_epoch();
256  long duration = value.count();
257 
258  int remain = duration % 1000000000;
259  // cout<<duration<<"\t";
260  //cout<<remain<<"\t";
261  std::chrono::milliseconds dur(duration);
262 
263  std::chrono::time_point<std::chrono::system_clock> dt(dur);
264  /*
265  if (dt != now_ms)
266  std::cout << "Failure.\t";
267  else
268  std::cout << "Success.\t";
269  */
270  // auto start = std::chrono::steady_clock::now();
271  // std::cout<<"now:"<<(double)start<<"\t";
272 
273 
274  std::cout<<get_time(1)<<"\t";
275  std::cout<<get_time(2)<<"\t";
276  std::cout<<get_time(3)<<"\t";
277  remain = get_time(2);
278  // cout<<std::time(nullptr)<<"\t";
279  // cout<<remain<<"\t";
280 
281  seed=remain;
282  itpp::RNG_reset(seed);
283  // ivec state;
284  // itpp::RNG_get_state(state);
285  // std::cout<<state<<endl;
286  for ( int i = 0; i < 7; i++){
287  cout<<randi(0,10000)<<"\t";
288  //cout<<randb(5)<<"\t";
289  }
290  cout<<endl;
291  return;
292 }
293 
294 void switch_test(int k){
295  switch (k) {
296  case 1:
297  case 2:
298  cout<<"case 1 2\t";
299  break;
300  case 3:
301  cout<<"case 3\t";
302  break;
303  case 4:
304  cout<<"case 4\t";
305  case 5:
306  cout<<"case 5\t";
307  break;
308  default:
309  cout<<"case otherwise\t";
310  }
311  cout<<endl;
312  return;
313 }
314 
315 
316 void syntax_test(){
317 
318  GF2mat G = gf2dense_eye(5);
319  G
320  .transpose();
321  cout<<G<<endl;
322  return;
323 }
324 
325 
326 void generate_code_test(int na, int Gax_row, int id_Gax, int Gaz_row, int id_Gaz, int debug){
327  GF2mat Gax, Gaz;
328  // int na=7, Gax_row=4, id_Gax=a, id_Gaz=b;
329  //is_quantum_code(Gax,Gaz,Gax,Gaz);
330  // int debug =1;
331  if (generate_code(Gax, Gaz, na, Gax_row, id_Gax, Gaz_row, id_Gaz,debug)==2){
332  cout<<"get return code 2"<<endl;
333  return;
334  }
335  int dax = quantum_dist_v2(Gax,Gaz);
336  cout<<"dax="<<dax<<endl;
337  int daz = quantum_dist_v2(Gax,Gaz,1);
338  cout<<"daz="<<daz<<endl;
339  //int generate_code(GF2mat & Gax, GF2mat & Gaz, int na, int Gax_row, int id_Gax, int id_Gaz);
340  return;
341 }
342 
343 void kron_test(){
344  GF2mat A(2,2), B(2,2);
345  A.set(1,1,1);
346  A.set(1,0,1);
347  B.set(1,1,1);
348  B.set(0,1,1);
349  GF2mat C = kron(A,B);
350  cout<<"A="<<A<<endl
351  <<"B="<<B<<endl
352  <<"C="<<C<<endl;
353  return;
354 }
355 
356 int main(int args, char ** argvs){
357  // cout<<"begin test"<<endl;
358  Parser parser;
359  parser.init(args, argvs);
360  parser.set_silentmode(true);
361  int na = 7; parser.get(na,"na");
362  int Gax_row = 3; parser.get(Gax_row,"Gax_row");
363 
364  int id_Gax = 1; parser.get(id_Gax,"id_Gax");
365  // bvec bvec_id_Gax("1 0 1 1 1 1 0 1 0 1 1 1 ");
366  // bvec bvec_id_Gax("1 0 0 1 0 0 ");
367  // id_Gax=bin2dec(bvec_id_Gax);
368  int Gaz_row = 1; parser.get(Gaz_row,"Gaz_row");
369  int id_Gaz = 1; parser.get(id_Gaz,"id_Gaz");
370  // bvec bvec_id_Gaz("0 0 0 1 0 1 1 0 1 1 0 0");//give [7 1 3] CSS code
371  // bvec bvec_id_Gaz("1 1");
372  // id_Gaz=bin2dec(bvec_id_Gaz);
373  int debug=0;parser.get(debug,"debug");
374 
375  // cout<<"args = "<<argvs[1] <<"\t";
376  // int k=atof(argvs[1]);
377 
378  // mm_test(k);//big G
379  //mm_test2();//small G
380  //mm_test3();//read
381  // copy_test();
382 
383  //G_s_test();
384  // pointer_test();
385  // array_pointer_test();
386  // char_test();
387  // run_vector_test();
388  // if_test();
389  // pointer_array_test();
390 
391  // copy1();
392  // cpp_random();
393  // random_test(k);
394  //combine_random(k);
395  // switch_test(k);
396 
397  // syntax_test();
398 
399  // generate_code_test(na,Gax_row,id_Gax,Gaz_row,id_Gaz,debug);
400  kron_test();
401 
402 
403  CSSCode code;
404  generate_code(code,1);
405  //cout<<"finish test"<<endl;
406  return 0;
407 }
array_pointer
GF2mat * array_pointer()
Definition: test.cpp:82
common::get_time
int get_time(int mode=1)
Definition: lib.cpp:53
random_test
void random_test(int seed)
Definition: test.cpp:246
vector_test
vector< vector< int > > vector_test()
Definition: test.cpp:112
pointer_array_test
int pointer_array_test()
Definition: test.cpp:185
MM_to_GF2mat
itpp::GF2mat MM_to_GF2mat(std::string file_name)
Definition: mm_read.cpp:36
mm_test
int mm_test(int k)
Definition: test.cpp:26
weilei_lib.h
this file links all other headfiles in this folder.
mm_test2
int mm_test2()
Definition: test.cpp:43
GF2mat_to_MM
int GF2mat_to_MM(itpp::GF2mat G, char *file_name, int debug)
Definition: mm_write.cpp:18
pointer_test
int pointer_test()
Definition: test.cpp:137
cpp_random
int cpp_random()
Definition: test.cpp:228
syntax_test
void syntax_test()
Definition: test.cpp:316
G_s_test
int G_s_test()
Definition: test.cpp:68
kron_test
void kron_test()
Definition: test.cpp:343
copy1
void copy1()
Definition: test.cpp:198
combine_random
int combine_random(int seed)
Definition: test.cpp:213
common::quantum_dist_v2
int quantum_dist_v2(itpp::GF2mat G_x, itpp::GF2mat G_z, int flip=0)
Definition: dist.cpp:179
mm_test3
int mm_test3()
Definition: test.cpp:61
common::kron
itpp::GF2mat kron(itpp::GF2mat A, itpp::GF2mat B)
Definition: lib.cpp:162
array_pointer_test
int array_pointer_test()
Definition: test.cpp:98
generate_code
int generate_code(itpp::GF2mat &Gax, itpp::GF2mat &Gaz, int na, int Gax_row, int id_Gax, int Gaz_row, int id_Gaz, int debug)
Definition: product_lib.cpp:278
copy_test
int copy_test()
Definition: test.cpp:16
copy2
void copy2(GF2mat &G)
Definition: test.cpp:193
switch_test
void switch_test(int k)
Definition: test.cpp:294
if_test
int if_test()
Definition: test.cpp:176
char_test
int char_test()
Definition: test.cpp:165
main
int main(int args, char **argvs)
Definition: test.cpp:356
generate_code_test
void generate_code_test(int na, int Gax_row, int id_Gax, int Gaz_row, int id_Gaz, int debug)
Definition: test.cpp:326
run_vector_test
int run_vector_test()
Definition: test.cpp:123
common
common function shared by many program
Definition: dist.h:15
CSSCode
Definition: product_lib.h:54
array_pointer2
int * array_pointer2()
Definition: test.cpp:89