CSS product code  0.1
C++ library to estimate distance of CSS codes. Some particular construction of CSS codes are implemented.
hypergraph.c
Go to the documentation of this file.
1 //July 22, 2018 Weilei Zeng
2 //An integrated program to check all parameter in Table 1 in Leonid's construction of Hypergraph product codes.
3 
4 
5 //Weilei Zeng, July 19, 2018
6 //According to Leonid's note on construction of high dimension hypergraph product code, using small random Matrix P_j, j={1,2,3,4} to generate code in 2D, 3D, and 4D. Then get parameters of [n,k,d] for those codes.
7 //This program is modified from the hypercubic code project (dated Jul 19, 2018)
8 
9 //if G_x.row_rank()+G_z.row_rank()=full rank, then it is not a quantum code.
10 //construct G_x and G_z for toric code, cubic code, and hypercubic code. Following the structure and notation in model.pdf
11 #include "weilei_lib/my_lib.h"
12 #include <itpp/itbase.h>
13 using namespace itpp;
14 using namespace std;
15 
16 /*bool is_quantum_code(GF2mat G_x, GF2mat G_z){
17  //check if the code is quantum or not
18  //not used here. originally designed for generator like [7,3,4] code at random size, which has been confirmed to be valid only for size L = 7n
19  if (G_x.row_rank()+G_z.row_rank() == G_x.cols()){
20  return false;
21  }else{
22  return true;
23  }
24 }
25 
26 GF2mat make_it_full_rank(GF2mat fat){
27  //Can not use the functoin here. I can not remove the degeneracy in the check matrix, which is actually the syndrome code. By removing it, I am removing the syndrome code.
28  //reduce a fat matrix with degenerate rows to a thin matrix with full rank; remove the dependent rows
29  GF2mat thin, T,U;
30  ivec P;
31  int rank = fat.T_fact(T,U,P);
32  thin = U.get_submatrix(0,0,rank-1,U.cols()-1);
33  thin.permute_cols(P,true);
34  return thin;
35  }*/
36 
37 GF2mat get_check_code734(int L){//L=7n
38  //return check matrix code code [7,3,4], find definition in research note.pdf
39  GF2mat G(L,L);
40  for ( int i=0;i<L;i++){
41  G.set(i,i,1);
42  if (i+2>L-1) {
43  G.set(i,i+2-L,1);
44  } else {
45  G.set(i,i+2,1);
46  }
47  if (i+3>L-1) {
48  G.set(i,i+3-L,1);
49  }else{
50  G.set(i,i+3,1);
51  }
52  }
53  return G;
54 }
55 GF2mat get_check_code743(int L){//L=7n
56  //return check matrix code code [7,4,3], find definition in research note.pdf
57  GF2mat G(L,L);
58  for ( int i=0;i<L;i++){
59  G.set(i,i,1);
60  if (i+2>L-1) {
61  G.set(i,i+2-L,1);
62  } else {
63  G.set(i,i+2,1);
64  }
65  if (i+3>L-1) {
66  G.set(i,i+3-L,1);
67  }else{
68  G.set(i,i+3,1);
69  }
70  if (i+4>L-1) {
71  G.set(i,i+4-L,1);
72  }else{
73  G.set(i,i+4,1);
74  }
75  }
76  return G;
77 }
78 
79 GF2mat get_check_rept(int L){//return circulant check matrix for repetition code of length L
80  GF2mat a(L,L);
81  for (int i=0;i<L-1;i++){//construct a : circulant check matrix for repetition code
82  a.set(i,i,1);
83  a.set(i,i+1,1);
84  }
85  a.set(L-1,L-1,1);
86  a.set(L-1,0,1);
87  //cout<<"circulant check matrix for repetition code : a = "<<a<<endl;
88  return a;
89 }
90 GF2mat get_check(int generator_flag, int L){
91  //return check matric a for generating toric code, cubic code and hypercubic code.
92  switch(generator_flag){
93  case 1: return get_check_rept(L);break;
94  case 2: return get_check_code734(L);break;
95  case 3: return get_check_code743(L);break;
96  }
97  // return get_check_734(L);//code [7,3,4]
98  // return get_check_743(L);//code [7,4,3]
99  // return get_check_rept(L); //circulant check matrix for repetition code.
100 }
101 
102 GF2mat extensionBj(GF2mat Aj,GF2mat Ajless, GF2mat P){
103  // GF2mat Aj=*Aj_p,Ajless=*Ajless_p,P=*P_p;
104  //cout<<"extensionBj"<<endl;
105  GF2mat Bj=kron(Aj,gf2dense_eye(P.rows()))
106  .concatenate_horizontal(kron(gf2dense_eye(Aj.rows()),P))
107  .concatenate_vertical(
108  GF2mat(Ajless.rows()*P.cols(),Aj.cols()*P.rows()).concatenate_horizontal(
109  kron(Ajless,gf2dense_eye(P.cols()))
110  ));
111  return Bj;
112 }
113 
114 GF2mat * extension(GF2mat *A1_p,GF2mat P,int m){
115  //from an (m-1) chain complex A = {A1,A2,A3,...A_(m-1)}, and an r x c binary matrix P, construct an extended m chain complex B={B1,B2,...,Bm}.
116  GF2mat A[m-1]; //construct A from pointer A1_p
117  for (int i=0;i<m-1;i++){
118  A[i]=*(A1_p+i);
119  }
120  const int MAX_m=6;//maximum length of chain complex
121  // cout<<"extension: m = "<<m<<endl;
122  static GF2mat B[MAX_m];
123  GF2mat A1=A[0],B1;
124  B1=kron(A1,gf2dense_eye(P.rows()))
125  .concatenate_horizontal(kron(gf2dense_eye(A1.rows()),P));
126  B[0]=B1;
127  if (m==2){
128  GF2mat B2=kron(gf2dense_eye( A1.cols() ),P)
129  .concatenate_vertical(kron(A1,gf2dense_eye(P.cols())));
130  B[1]=B2;
131  return B;
132  }
133  //if m>2
134  for (int j=2;j<m;j++){//run m-1 loop
135  GF2mat Bj = extensionBj(A[j-1],A[j-2],P);
136  B[j-1]=Bj;
137  }
138  GF2mat Amless = A[m-2];
139  GF2mat Bm=kron(gf2dense_eye( Amless.cols() ),P)
140  .concatenate_vertical(kron(Amless,gf2dense_eye(P.cols())));
141  B[m-1] = Bm;
142  return B;
143 }
144 
145 int printNKD(int nkd[][MAX_M], int m){//print n k d in table format
146  //5 is the maximum I will try, but just put 10 here since it is small
147  for ( int i=1;i<m+1;i++){
148  for ( int j=0;j<i+1;j++){
149  cout<<nkd[i][j]<<"\t";
150  }
151  cout<<endl;
152  }
153 }
154 vector<int> parameterP(GF2mat P[], int m){//m is the size of P
155  //print parameters of matrix P, and derived parameter of Cm including
156  //P: r,c,kappa,\tilde\kappa,\delta,\tilde\kappa,rank
157  //C:n,k,d_left,d_right
158  cout<<"#Expected value calculated from Table 1:"<<endl;
159  /* cout<<"n0\tn1\tn2\tn3\tn4"<<endl;
160  cout<<P[0].rows()<<"\t"<<P[0].cols()<<endl;
161  cout<<P[0].rows()*P[1].rows()<<"\t"<<P[0].rows()*P[1].cols()+P[0].rows()*P[1].cols()<<"\t"<<P[0].cols()*P[1].cols()<<endl;
162  cout<<P[0].rows()*P[1].rows()*P[2].rows()<<"\t"
163  <<P[0].rows()*P[1].cols()*P[2].rows()+P[0].rows()*P[1].cols()*P[2].rows()+P[0].rows()*P[1].rows()*P[2].cols()<<"\t"
164  <<P[0].rows()*P[1].cols()*P[2].cols()+P[0].cols()*P[1].rows()*P[2].cols()+P[0].cols()*P[1].cols()*P[2].rows()<<"\t"
165  <<P[0].cols()*P[1].cols()*P[2].cols()<<endl;
166  cout<<P[0].rows()*P[1].rows()*P[2].rows()*P[3].rows()<<"\t"
167  <<P[0].rows()*P[1].cols()*P[2].rows()*P[3].rows()+P[0].rows()*P[1].cols()*P[2].rows()*P[3].rows()+P[0].rows()*P[1].rows()*P[2].cols()*P[3].rows()+P[0].rows()*P[1].rows()*P[2].rows()*P[3].cols()<<"\t"
168  <<P[0].rows()*P[1].rows()*P[2].cols()*P[3].cols()
169  +P[0].rows()*P[1].cols()*P[2].rows()*P[3].cols()+P[0].cols()*P[1].rows()*P[2].rows()*P[3].cols()
170  +P[0].rows()*P[1].cols()*P[2].cols()*P[3].rows()+P[0].cols()*P[1].rows()*P[2].cols()*P[3].rows()+P[0].cols()*P[1].cols()*P[2].rows()*P[3].rows()<<"\t"
171  <<P[0].rows()*P[1].cols()*P[2].cols()*P[3].cols()+P[0].cols()*P[1].rows()*P[2].cols()*P[3].cols()+P[0].cols()*P[1].cols()*P[2].rows()*P[3].cols()+P[0].cols()*P[1].cols()*P[2].cols()*P[3].rows()<<"\t"
172  <<P[0].cols()*P[1].cols()*P[2].cols()*P[3].cols()<<endl;
173  */
174  int n[m+1][MAX_M];
175  n[1][0]=P[0].rows();
176  n[1][1]=P[0].cols();
177  for ( int i=2;i<m+1;i++){
178  n[i][0]=n[i-1][0]*P[i-1].rows();
179  n[i][i]=n[i-1][i-1]*P[i-1].cols();
180  for ( int j=1;j<i;j++){
181  n[i][j]=n[i-1][j]*P[i-1].rows()+n[i-1][j-1]*P[i-1].cols();
182  }
183  }
184  cout<<"n0\tn1\tn2\tn3\tn4"<<endl;
185  printNKD(n,m);
186 
187  int k[m],kt[m],r;//\kappa, \tilde\kappa, rank
188  cout<<"-----------------------------------"<<endl;
189  for ( int i=0;i<m;i++){
190  r=P[i].row_rank();
191  //cout<<r<<"\t";
192  k[i]=P[i].cols()-r;
193  kt[i]=P[i].rows()-r;
194  }
195  /*
196  cout<<endl;
197  cout<<"k0\tk1\tk2\tk3\tk4"<<endl;
198  cout<<kt[0]<<"\t"<<k[0]<<endl;
199  cout<<kt[0]*kt[1]<<"\t"<<kt[0]*k[1]+k[0]*kt[1]<<"\t"<<k[0]*k[1]<<endl;
200  cout<<kt[0]*kt[1]*kt[2]<<"\t"
201  <<k[0]*kt[1]*kt[2]+kt[0]*k[1]*kt[2]+kt[0]*kt[1]*k[2]<<"\t"
202  <<k[0]*k[1]*kt[2]+kt[0]*k[1]*k[2]+k[0]*kt[1]*k[2]<<"\t"
203  <<k[0]*k[1]*k[2]<<endl;
204  cout<<kt[0]*kt[1]*kt[2]*kt[3]<<"\t"
205  <<k[0]*kt[1]*kt[2]*kt[3]+ kt[0]*k[1]*kt[2]*kt[3]+ kt[0]*kt[1]*k[2]*kt[3]+ kt[0]*kt[1]*kt[2]*k[3]<<"\t"
206  << k[0]*k[1]*kt[2]*kt[3]+ k[0]*kt[1]*k[2]*kt[3]+ k[0]*kt[1]*kt[2]*k[3]
207  + kt[0]*k[1]*k[2]*kt[3]+ kt[0]*k[1]*kt[2]*k[3]+ kt[0]*kt[1]*k[2]*k[3]<<"\t"
208  <<k[0]*k[1]*k[2]*kt[3]+k[0]*k[1]*kt[2]*k[3]+k[0]*kt[1]*k[2]*k[3]+kt[0]*k[1]*k[2]*k[3]<<"\t"
209  <<k[0]*k[1]*k[2]*k[3]<<endl;
210  */
211  int kC[m+1][MAX_M];//kC[m][j];k value of (Aj,Ajplus^T)
212  kC[1][0]=kt[0];
213  kC[1][1]=k[0];
214  for ( int i=2;i<m+1;i++){
215  kC[i][0]=kC[i-1][0]*kt[i-1];
216  kC[i][i]=kC[i-1][i-1]*k[i-1];
217  for ( int j=1;j<i;j++){
218  kC[i][j] = kC[i-1][j-1]*k[i-1]+kC[i-1][j]*kt[i-1];
219  }
220  }
221  cout<<"k0\tk1\tk2\tk3\tk4"<<endl;
222  printNKD(kC,m);
223  /* for ( int i=1;i<m+1;i++){
224  for ( int j=0;j<i+1;j++){
225  cout<<kC[i][j]<<"\t";
226  }
227  cout<<endl;
228  }*/
229  //distance
230  int delta[m],delta_tilde[m];//distance of P and P^T
231  int dl1,dl2; //temperary distance of P and P^T
232 
233  cout<<"-----------------------------------parameters of P:"<<endl;
234  cout<<"row\tcol\trank\tkappa,delta,kappa_tilde,delta_tilde"<<endl;
235  for ( int i=0;i<m;i++){
236  dl1 = classical_dist(P[i]);
237  dl2 = classical_dist(P[i].transpose());
238  // cout<<"dist(P["<<i<<"]) = "
239  cout<<P[i].rows()<<"\t"<<P[i].cols()<<"\t"<<P[i].row_rank()<<"\t";
240  cout<<k[i]<<"\t"<<dl1<<"\t"<<kt[i]<<"\t"<<dl2<<"\t"<<endl;
241  delta[i]=dl1;
242  delta_tilde[i]=dl2;
243  }
244  cout<<"-----------------------------------"<<endl;
245  /*
246  int d[5][5];//d[m][i]
247  d[1][0]= (kt[0]>0)?1:999;
248  d[1][1]=delta[1-1];
249  d[2][0]=(kt[1]>0)?d[1][0]:999;
250  d[2][1]= kt[2-1]>0 ? min(d[1][1],delta[2-1]*d[1][0]) :d[1][0]*delta[2-1];
251  d[2][2]=delta[1-1]*delta[2-1];
252  // d[3][0]=1;
253  d[3][0]=(kt[2]>0)?d[2][0]:999;
254  d[3][1]= kt[3-1]>0 ? min(d[2][1],delta[3-1]*d[2][0]) :d[2][0]*delta[3-1];
255  d[3][2]= kt[3-1]>0 ? min(d[2][2],delta[3-1]*d[2][1]) :d[2][1]*delta[3-1];
256  d[3][3]=delta[1-1]*delta[2-1]*delta[3-1];
257  // d[4][0]=1;
258  d[4][0]=(kt[3]>0)?d[3][0]:999;
259  d[4][1]= kt[4-1]>0 ? min(d[3][1],delta[4-1]*d[3][0]) :d[3][0]*delta[4-1];
260  d[4][2]= kt[4-1]>0 ? min(d[3][2],delta[4-1]*d[3][1]) :d[3][1]*delta[4-1];
261  d[4][3]= kt[4-1]>0 ? min(d[3][3],delta[4-1]*d[3][2]) :d[3][2]*delta[4-1];
262  d[4][4]=delta[1-1]*delta[2-1]*delta[3-1]*delta[4-1];
263  */
264  // const int INF = 999;
265  int dl[m+1][MAX_M];
266  dl[1][1]=delta[0];
267  dl[1][0]= (kt[0]>0)? 1:INF;//delta_tilde[0];
268  for ( int i=2;i<m+1;i++){
269  dl[i][i]=dl[i-1][i-1]*delta[i-1];//not sure what to put here
270  dl[i][0]=(kt[i-1]>0)? dl[i-1][0]:INF;//delta_tilde[i-1];//last element
271  for ( int j=1;j<i;j++){//elements in between
272  dl[i][j]= (kt[i-1]>0)? min(dl[i-1][j-1]*delta[i-1],dl[i-1][j]) : dl[i-1][j-1]*delta[i-1];
273  }
274  }
275 
276  // cout<<"d0\td1\td2\td3\td4\td(left)"<<endl;
277  for (int i=1;i<m+1;i++){
278  for (int j=0;j<i+1;j++){
279  if (dl[i][j]>999){//normalize infinity
280  dl[i][j]=999;
281  }
282  }
283  }
284  // printNKD(dl,m);
285  vector<int> dist(m*(m+1)*2);
286  int index=0;
287  for (int i=1;i<m+1;i++){
288  for (int j=0;j<i+1;j++){
289  dist[index]=dl[i][j];
290  index++;
291  }
292  }
293 
294  //right distance
295  int dr[m+1][MAX_M];
296  dr[1][0]=delta_tilde[0];
297  dr[1][1]= (k[0]>0)? 1:INF;//delta_tilde[0];
298  for ( int i=2;i<m+1;i++){
299  dr[i][0]=dr[i-1][0]*delta_tilde[i-1];//not sure what to put here
300  dr[i][i]=(k[i-1]>0)? dr[i-1][i-1]:INF;//delta_tilde[i-1];//last element
301  for ( int j=1;j<i;j++){//elements in between
302  dr[i][j]= (k[i-1]>0)? min(dr[i-1][j]*delta_tilde[i-1],dr[i-1][j-1]) : dr[i-1][j]*delta_tilde[i-1];
303  }
304  }
305  // cout<<"d0\td1\td2\td3\td4\td(right)"<<endl;
306  for (int i=1;i<m+1;i++){
307  for (int j=0;j<i+1;j++){
308  if (dr[i][j]>999){//normalize infinity for output
309  dr[i][j]=999;
310  }
311  }
312  }
313  // printNKD(dr,m);
314 
315  //add to vector dist
316  index=m*(m+1);
317  for (int i=1;i<m+1;i++){
318  for (int j=0;j<i+1;j++){
319  dist[index]=dr[i][j];
320  index++;
321  }
322  }
323 
324  //print distance
325 
326  int d_min[m+1][MAX_M];
327  for (int i=1;i<m+1;i++){
328  for (int j=0;j<i+1;j++){
329  d_min[i][j]=min(dl[i][j],dr[i][j]);
330  }
331  }
332  cout<<"d0\td1\td2\td3\td4\td_min=min(d_left,d_right)"<<endl;
333  printNKD(d_min,m);
334  cout<<"d0\td1\td2\td3\td4\td_left"<<endl;
335  printNKD(dl,m);
336  cout<<"d0\td1\td2\td3\td4\td_right"<<endl;
337  printNKD(dr,m);
338 
339 
340  cout<<"\# Numerical results on [n,k,d] value of code Q (Aj,A(j+1)^T)"<<endl;
341  return dist;
342 }
343 
344 int parameterC(GF2mat *B1, int m, int m_max, vector<int> dist){//*B1=Cm[0]
345  //input: pointer of first matrix, length of the chain, max chain length, expected distance
346  //print rank and size of Cm
347  int r[m];//rank
348  cout<<" n= ";
349  for (int i=0;i<m;i++){
350  r[i]=(B1+i)->row_rank();
351  cout<<(B1+i)->rows()<<"\t";
352  // cout<<"m="<<m<<",i="<<i<<", rank="<<r[i]<<", size("<<(B1+i)->rows()<<","<<(B1+i)->cols()<<")"<<endl;
353  }
354  cout<<(B1+m-1)->cols()<<endl;
355  cout<<" k= ";
356  for (int i=0;i<m-1;i++){
357  int k = (B1+i)->cols() - r[i] -r[i+1];
358  cout<<k<<"\t";
359  }
360  cout<<endl;
361  //check distance
362 
363 
364  /* for (int i=0;i<30;i++){//14 for m_max=4
365  cout<<"["<<""<<dist[i]<<"]";
366  }*/
367  //left distance
368  cout<<"d_left=\t";
369  for (int i=0;i<m-1;i++){
370  int index = m*(m+1)/2+i;
371  int d_left = hypergraph_dist(*(B1+i),*(B1+i+1),dist[index] );
372  cout<<d_left<<"\t";
373  }
374  cout<<endl;
375 
376  cout<<" d_right=";
377  // int m_max=5;
378  for (int i=0;i<m-1;i++){
379  int index = m_max*(m_max+1)+m*(m+1)/2+i;
380  int d_right = hypergraph_dist(*(B1+i),*(B1+i+1),dist[index] ,1);//1 for flipping to right distance
381  cout<<d_right<<"\t";
382  }
383  cout<<endl;
384  return 0;
385 }
386 
387 int saveC(GF2mat *B1,int m, char * folder_Cm){//*B1=C[0]
388  //save or print the result
389  for (int i=0;i<m;i++){
390  char filename[256];
391  // char folder[]="data/random/C";
392  sprintf(filename,"%s/C%d_%d.mm",folder_Cm,m,i);
393  GF2mat_to_MM(*(B1+i),filename);
394 
395  /* char matrixname[256];
396  sprintf(matrixname,"m=%d C[%d]",m,i);
397  GF2matPrint(*(B1+i),matrixname);*/
398  }
399  return 0;
400 }
401 
402 GF2mat generate_random_P(double p, int x,int y,char * filename){
403  //probability p to get 1 for each element
404  //return P with density p
405  GF2mat P(x,y);
406  for (int i=0;i<x;i++){
407  for ( int j=0;j<y;j++){
408  if (randu()<p) P.set(i,j,1);
409  }
410  }
411  // cout<<"Pj"<<P<<endl;
412  // GF2mat_to_MM(P,filename);
413  return P;
414 }
415 
416 //int check(GF2mat Aj,GF2mat Ajplus, GF2mat Bj,GF2mat Bjplus){
417 int check(){
418  GF2mat Aj=MM_to_GF2mat("data/random/temp/C2_0.mm");
419  GF2mat Ajplus=MM_to_GF2mat("data/random/temp/C2_1.mm");
420  GF2mat Bj=MM_to_GF2mat("data/random/temp/C3_0.mm");
421  GF2mat Bjplus=MM_to_GF2mat("data/random/temp/C3_1.mm");
422 
423  cout<<Aj.rows()<<"x"<<Aj.cols()<<endl;
424  cout<<Ajplus.rows()<<"x"<<Ajplus.cols()<<endl;
425  cout<<Bj.rows()<<"x"<<Bj.cols()<<endl;
426  cout<<Bjplus.rows()<<"x"<<Bjplus.cols()<<endl;
427  cout<<"check"<<endl;
428 
429  GF2mat T,U;
430  ivec P;
431  int rank_of_Aj =Aj.transpose().T_fact(T,U,P);
432  GF2mat C = T.get_submatrix(rank_of_Aj,0,Aj.cols()-1,Aj.cols()-1);//rank_of_T = Aj.cols()-rank_of_Aj
433  int dAj=2;
434  bvec codeword=zeros_b(Aj.cols());
435  bvec zero=zeros_b(Aj.cols());
436  for ( int i=0;i<C.rows();i++){
437  codeword = C.get_row(i);
438  int wt = BERC::count_errors(zero,codeword);
439  if (wt==dAj){
440  cout<<"codeword : "<<codeword<<endl;
441  break;
442  }
443  }
444  cout<<Aj*codeword<<endl;
445  bvec codewordB;
446  int r=4;
447  GF2mat y(1,r);
448  GF2mat x(1,Aj.cols());
449  x.set_row(0,codeword);
450  y.set(0,1,1);
451  GF2mat z=kron(x,y);
452  z.set_size(1,Bj.cols(),true);
453  //bvec e=z.get_row(0);
454  if ( (Bj*z.transpose()).is_zero() ){
455  cout<<"Bj*z=0"<<endl;
456  }
457  GF2mat BjplusT=Bjplus.transpose();
458  GF2mat Bz=BjplusT.concatenate_vertical(z);
459  cout<<Bjplus.row_rank()<<"<>"<<Bz.rows()<<"<>"<<Bz.row_rank()<<endl;
460  return 0;
461 }
462 
463 int check2(){//check if the codeword with smaller distance is inside Bjplus
464  GF2mat Aj=MM_to_GF2mat("data/random/temp/C3_1.mm");
465  GF2mat Ajplus=MM_to_GF2mat("data/random/temp/C3_2.mm");
466  GF2mat Bj=MM_to_GF2mat("data/random/temp/C4_2.mm");
467  GF2mat Bjplus=MM_to_GF2mat("data/random/temp/C4_3.mm");
468 
469  cout<<Aj.rows()<<"x"<<Aj.cols()<<endl;
470  cout<<Ajplus.rows()<<"x"<<Ajplus.cols()<<endl;
471  cout<<Bj.rows()<<"x"<<Bj.cols()<<endl;
472  cout<<Bjplus.rows()<<"x"<<Bjplus.cols()<<endl;
473  cout<<"check2"<<endl;
474 
475  GF2mat T,U;
476  ivec P;
477  int rank_of_Aj =Aj.transpose().T_fact(T,U,P);
478  GF2mat C = T.get_submatrix(rank_of_Aj,0,Aj.cols()-1,Aj.cols()-1);//rank_of_T = Aj.cols()-rank_of_Aj
479  int dAj=4;
480  bvec codeword=zeros_b(Aj.cols());
481  bvec zero=zeros_b(Aj.cols());
482  for ( int i=0;i<C.rows();i++){
483  codeword = C.get_row(i);
484  int wt = BERC::count_errors(zero,codeword);
485  if (wt==dAj){
486  cout<<"codeword : "<<codeword<<endl;
487  break;
488  }
489  }
490  cout<<Aj*codeword<<endl;
491 
492  GF2mat AjplusT=Ajplus.transpose();
493  GF2mat z(1,AjplusT.cols());
494  z.set_row(0,codeword);
495  GF2mat Az=AjplusT.concatenate_vertical(z);
496  cout<<Ajplus.row_rank()<<"<>"<<Az.rows()<<"<>"<<Az.row_rank()<<endl;
497  if ( (Aj*Az.transpose()).is_zero() ){
498  cout<<"Aj*AzT=0"<<endl;
499  }
500  /*
501  GF2mat BjplusT=Bjplus.transpose();
502  GF2mat Bz=BjplusT.concatenate_vertical(z);
503  cout<<Bjplus.row_rank()<<"<>"<<Bz.rows()<<"<>"<<Bz.row_rank()<<endl;
504  */
505  return 0;
506 }
507 
508 
509 int generate(GF2mat P[],int max_m,vector<int> dist, char * folder_Cm){//folder_Cm: the folder to save all C matrix
510  //from P[] create m chain complex for m=2,3,4, and save it in folder_Cm
511  // int L=6;//15 min for size 6
512  // GF2mat P=get_check_rept(L);
513  // check(); check2();return 0;
514  // const int max_m=4;
515 
516  GF2mat *Cm1;//the first matrix in m-chain complex C
517  Cm1=&P[0];//initialize
518 
519  GF2mat Cms[max_m*(max_m)];//save all Cm matrices for test
520  Cms[0]=P[0];
521  for ( int i=2;i<max_m+1;i++){
522  Cm1 = extension(Cm1,P[i-1],i);
523  // GF2mat *temp= & Cm1;
524  for ( int j=0;j<i;j++){
525  Cms[(i-1)*max_m+j] = *(Cm1+j);
526  }
527  //saveC(Cm1,i,folder_Cm);
528  parameterC(Cm1,i,max_m,dist);
529  }
530  // check(Cms[4],Cms[5],Cms[8],Cms[9] );
531  return 0;
532 }
533 
534 int main(int args, char ** argv){
535 
536 
537  RNG_randomize();
538  Real_Timer timer;
539  timer.tic();
540  int max_m=4;
541  GF2mat P[max_m];
542  char * folder_Cm = "null";//argv[5];
543 
544  if (args == 2) {//generate random P, and save with title
545  char * title = argv[1];
546  int px,py; //size of P[i]
547  double density = 0.5; //density of P[i]
548  for ( int i =0;i<max_m;i++){
549  // P[i] = MM_to_GF2mat(argv[i+1]);
550  char pname[256];
551  sprintf(pname,"%s-P%d.mm",title,i+1);//different input title to save the matrix in file
552  // px=randi(4,8);
553  //py=randi(4,8);
554  px=5;py=px;density=3.5/py;
555  P[i]=generate_random_P(density,px,py,pname);//p,x,y,filename
556  //check some condition to be satisfied. maximum number should be controled
557  int condition_trials=50000;
558  for (int j=0;j<condition_trials;j++){
559  /*//check full rank, typical max run 2 to remain some full rank matrix
560  if (P[i].row_rank()==P[i].rows()){//reduce prob of full rank
561  P[i]=generate_random_P(density,px,py,pname);//p,x,y,filename
562  }else{
563  break;
564  }*/
565  //check condition for TZ's code, Alexey 2018
566  int kappa=P[i].cols()-P[i].row_rank();
567  int delta=classical_dist(P[i]);
568  int delta_tilde=classical_dist(P[i].transpose());
569  int delta_min = min(delta,delta_tilde);
570  if (delta<INF &&delta_tilde<INF && kappa*delta_min >P[i].cols()){
571  cout<<"condition satidfied when j = "<<j<<endl;
572  GF2mat_to_MM(P[i],pname);//save file disabled in generate_random_P()
573  break;
574  }else{
575  P[i]=generate_random_P(density,px,py,pname);//p,x,y,filename
576  }
577  }
578  // P[i] = get_check_rept(4);
579  //P[i]=get_check_code743(7);
580  // cout<<"P["<<i+1<<"] "<<P[i]<<endl;
581  }
582  }else{//from given P
583  for ( int i =0;i<max_m;i++){
584  char * filename_Pi = argv[i+1];
585  P[i]=MM_to_GF2mat(filename_Pi);
586  cout<<"P["<<i+1<<"] "<<P[i]<<endl;
587  }
588  /* char * filename_P1 = argv[1];
589  char * filename_P2 = argv[2];
590  char * filename_P3 = argv[3];
591  char * filename_P4 = argv[4];*/
592  folder_Cm = argv[5];
593 
594  }
595  vector<int> dist;
596  dist = parameterP(P,max_m);
597 
598  generate(P,max_m,dist,folder_Cm);
599  timer.toc_print();
600  return 0;
601 }
get_check_rept
GF2mat get_check_rept(int L)
Definition: hypergraph.c:79
common::classical_dist
int classical_dist(itpp::GF2mat G)
Definition: dist.cpp:114
get_check_code743
GF2mat get_check_code743(int L)
Definition: hypergraph.c:55
MAX_M
const int MAX_M
Definition: product_lib.h:13
MM_to_GF2mat
itpp::GF2mat MM_to_GF2mat(std::string file_name)
Definition: mm_read.cpp:36
printNKD
int printNKD(int nkd[][MAX_M], int m)
Definition: hypergraph.c:145
GF2mat_to_MM
int GF2mat_to_MM(itpp::GF2mat G, char *file_name, int debug)
Definition: mm_write.cpp:18
common::INF
const int INF
Definition: dist.h:19
get_check
GF2mat get_check(int generator_flag, int L)
Definition: hypergraph.c:90
generate_random_P
GF2mat generate_random_P(double p, int x, int y, char *filename)
Definition: hypergraph.c:402
get_check_code734
GF2mat get_check_code734(int L)
Definition: hypergraph.c:37
main
int main(int args, char **argv)
Definition: hypergraph.c:534
common::kron
itpp::GF2mat kron(itpp::GF2mat A, itpp::GF2mat B)
Definition: lib.cpp:162
parameterP
vector< int > parameterP(GF2mat P[], int m)
Definition: hypergraph.c:154
parameterC
int parameterC(GF2mat *B1, int m, int m_max, vector< int > dist)
Definition: hypergraph.c:344
check2
int check2()
Definition: hypergraph.c:463
extensionBj
GF2mat extensionBj(GF2mat Aj, GF2mat Ajless, GF2mat P)
Definition: hypergraph.c:102
common::hypergraph_dist
int hypergraph_dist(itpp::GF2mat Aj, itpp::GF2mat Ajplus, int dist_expected, int flip=0)
Definition: dist.cpp:292
generate
int generate(GF2mat P[], int max_m, vector< int > dist, char *folder_Cm)
Definition: hypergraph.c:509
check
int check()
Definition: hypergraph.c:417
saveC
int saveC(GF2mat *B1, int m, char *folder_Cm)
Definition: hypergraph.c:387
extension
GF2mat * extension(GF2mat *A1_p, GF2mat P, int m)
Definition: hypergraph.c:114