CSS product code  0.1
C++ library to estimate distance of CSS codes. Some particular construction of CSS codes are implemented.
lib.cpp
Go to the documentation of this file.
1 
6 //Weilei Zeng. Some small functions for use
7 //#include <fstream>
8 //#include<string>
9 //#include<iostream>
10 //#include<sstream>
11 //#include "my_lib.h"
12 //#include <stdio.h>
13 #include <itpp/itbase.h>
14 #include <chrono> //for time
15 
16 #include "lib.h"
17 #include "mm_read.h"
18 
32 std::string common::color_text(std::string str){
33  //return text in red color
34  return red_text(str);
35 }
36 
37 std::string common::red_text(std::string str){
38  //return text in red color
39  return "\033[1;31m"+str+"\033[0m";
40 }
41 
42 std::string common::blue_text(std::string str){
43  //return text in blue color
44  return "\033[1;34m"+str+"\033[0m";
45 }
46 
47 
53 int common::get_time(int mode){
54 
55  auto now = std::chrono::system_clock::now();
56  // auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
57  auto value = now.time_since_epoch();
58  long duration = value.count();
59  int t=0;
60  const int DIGIT=1000000000;
61  switch ( mode ){
62  case 1: // seconds
63  t = ( duration / 100000000 ) % DIGIT ;
64  break;
65  case 2: // milli seconds
66  t = (duration / 100000) % DIGIT;
67  break;
68  case 3:
69  t = duration % DIGIT;
70  break;
71  }
72  return t;
73 }
74 
75 
76 
77 bool common::is_quantum_code(itpp::GF2mat & G_x, itpp::GF2mat & G_z){
78  //check if the code is quantum or not
79  if (G_x.row_rank()+G_z.row_rank() < G_x.cols()){
80  if ( (G_x*G_z.transpose()).is_zero()){
81  return true;
82  }
83  std::cout<<"Not a quantum code:pertation relation is not satisfied."<<std::endl;
84  return false;
85  }else{
86  std::cout<<"Not a quantum code: zero rank for codeword space."<<std::endl;
87  return false;
88  }
89 }
90 
91 bool common::is_quantum_code(itpp::GF2mat &Gx,itpp::GF2mat &Gz, itpp::GF2mat &Cx,itpp::GF2mat &Cz){
92  if (!(Gx*Gz.transpose()).is_zero()){
93  std::cout<<"(Gx*Gz.transpose()) is not zero"<<std::endl;
94  // throw "(Gx*Gz.transpose()) is not zero";
95  return false;
96  }
97  if (!(Gx*Cz.transpose()).is_zero()){
98  // throw "(Gx*Cz.transpose()) is not zero";
99  std::cout<<"(Gx*Cz.transpose()) is not zero"<<std::endl;return false;
100  }
101  if (!(Gz*Cx.transpose()).is_zero()){
102  // throw "(Gz*Cx.transpose()) is not zero";
103  std::cout<<"(Gz*Cx.transpose()) is not zero"<<std::endl;return false;
104  }
105  int rank_of_Gx=Gx.row_rank();
106  int rank_of_Gz=Gz.row_rank();
107  int rank_of_Cx=Cx.row_rank();
108  int rank_of_Cz=Cz.row_rank();
109  int n=Gx.cols();
110  if (rank_of_Gx+rank_of_Gz+rank_of_Cx != n){
111  std::cout<<"(rank_of_Gx+rank_of_Gz+rank_of_Cx != n)"<<std::endl;
112  return false;
113  }
114  if(rank_of_Cx != rank_of_Cz){
115  std::cout<<"(rank_of_Cx != rank_of_Cz)"<<std::endl;return false;
116  }
117  // std::cout<<"is_quantum_code(): It is a quantum code!"<<std::endl;
118  return true;
119 }
120 
121 
122 
123 itpp::GF2mat common::make_it_full_rank(itpp::GF2mat fat){
124  //reduce a fat matrix with degenerate rows to a thin matrix with full rank; remove the dependent rows
125  itpp::GF2mat thin, T,U;
126  itpp::ivec P;
127  int rank = fat.T_fact(T,U,P);
128  if (rank == fat.rows()){ //if it is already full rank //this was only = but not ==, weilei changed on Dec 13, 2019, much later than the program was runned.
129  return fat;
130  }
131  thin = U.get_submatrix(0,0,rank-1,U.cols()-1);
132  thin.permute_cols(P,true);
133  return thin;
134 }
135 
136 /*
137 int itpp::GF2matPrint(itpp::GF2mat &G,char * name){
138  //print brief information of G
139  std::cout<<"GF2mat "<<name<<", size = ("<<G.rows()<<","<<G.cols()<<"), density = "
140  <<G.density()<<std::endl;
141  return 0;
142 }
143 */
144 int common::GF2matPrint(itpp::GF2mat &G, std::string name){
145  //print brief information of G
146  //GF2matPrint(G, name.c_str());
147  std::cout<<"GF2mat "<<name<<", size = ("<<G.rows()<<","<<G.cols()<<"), density = "
148  <<G.density()<<std::endl;
149  return 0;
150 }
151 
153 int common::matPrint(itpp::mat G,char * name){
154  //print brief information of G
155  std::cout<<"mat "<<name<<", size = ("<<G.rows()<<","<<G.cols()<<")"<<std::endl;
156  return 0;
157 }
158 
162 itpp::GF2mat common::kron(itpp::GF2mat A, itpp::GF2mat B){//Kroneker tensor product of A x B
163  //how could I define this in the wrong way?
164  //although I define it in the wrong way, I dont think it will affect the result
165  //but in any later calculation, use the right definition. May 26, 2018
166 
167  //switch A B
168  itpp::GF2mat temp;
169  temp = A;
170  A=B;
171  B=temp;
172 
173  itpp::GF2mat G(1,1+A.cols()*B.cols());//an empty row; delete it finally
174  itpp::GF2mat zero(A.rows(),A.cols());
175  for (int i=0;i<B.rows();i++){
176  itpp::GF2mat G_row(A.rows(),1);//an empty column, delete it finally
177  for (int j=0;j<B.cols();j++){
178  //std::cout<<"debug: j = "<<j<<std::endl;
179  if (B.get(i,j)){//1,set A
180  G_row = G_row.concatenate_horizontal(A);
181  }else{//0, set zero
182  G_row = G_row.concatenate_horizontal(zero);
183  }
184  }
185  // std::cout<<G<<std::endl<<G_row<<std::endl;
186  G = G.concatenate_vertical(G_row);
187  }
188 
189  // std::cout<<G<<std::endl<<"debug"<<std::endl;
190  G=G.get_submatrix(1,1,G.rows()-1,G.cols()-1);
191  //std::cout<<G<<std::endl;
192  return G;
193 }
194 
195 std::string common::NumberToString(int pNumber) //not used anywhere.
196 {
197  std::ostringstream o;
198  o<<pNumber;
199  return o.str();
200 }
201 
202 itpp::GF2mat common::append_vector(itpp::GF2mat G,itpp::bvec b){
203  //append a row vector to the GF2mat G
204  //used when saving error vectors for decoding
205  G.set_size(G.rows()+1,G.cols(),true);
206  G.set_row(G.rows()-1,b);
207  return G;
208 }
209 
210 itpp::GF2mat common::get_GF2mat(char * filename_prefix, char * filename_suffix){
211  char filename[255];
212  sprintf(filename,"%s%s",filename_prefix,filename_suffix);
213  itpp::GF2mat E=MM_to_GF2mat(filename);
214  return E;
215 }
216 
217 itpp::GF2mat common::get_GF2mat(char * parent_folder, char * folder, char *filename){
218  char filename_full[255];
219  sprintf(filename_full,"%s/%s/%s",parent_folder, folder, filename);
220  itpp::GF2mat E=MM_to_GF2mat(filename_full);
221  return E;
222 }
223 
224 double common::get_error_density(itpp::GF2mat E){
225  //fisrt row is zero
226  //remove first row and return density of the remained submatrix
227  double w;
228  if(E.rows()==1){
229  w=0;
230  }else{
231  w=E.get_submatrix(1,0,E.rows()-1,E.cols()-1).density();
232  }
233  return w;
234 }
235 
236 //save mat into gnuplot data file, with a custom comment as header
237 int common::mat2gnudata(itpp::mat data, std::string filename, std::string header){
238  FILE *fout;//file to save the data
239  //char filename_out[255];
240  //sprintf(filename_out,"%s/gnuplot/rate_versus_p_size_%d_weight_%d.gnudat",error_folder,size,weight);
241  fout =fopen(filename.c_str(),"w");
242  fprintf(fout,"%s\n",header.c_str());
243  int row=data.rows(), col=data.cols();
244  for (int i =0;i<row;i++){
245  for (int j=0;j<col;j++){
246  fprintf(fout,"%f\t",data.get(i,j));
247  }
248  fprintf(fout,"\n");
249  }
250  fclose(fout);
251  return 0;
252 }
253 
254 
lib.h
lib for general functions, defined within namespace common
common::matPrint
int matPrint(itpp::mat G, char *name=(char *) " ")
Definition: lib.cpp:153
common::get_time
int get_time(int mode=1)
Definition: lib.cpp:53
common::get_GF2mat
itpp::GF2mat get_GF2mat(char *filename_prefix, char *filename_suffix)
Definition: lib.cpp:210
common::blue_text
std::string blue_text(std::string str)
Definition: lib.cpp:42
MM_to_GF2mat
itpp::GF2mat MM_to_GF2mat(std::string file_name)
Definition: mm_read.cpp:36
common::red_text
std::string red_text(std::string str)
Definition: lib.cpp:37
common::kron
itpp::GF2mat kron(itpp::GF2mat A, itpp::GF2mat B)
Definition: lib.cpp:162
common::mat2gnudata
int mat2gnudata(itpp::mat data, std::string filename, std::string header)
Definition: lib.cpp:237
common::NumberToString
std::string NumberToString(int pNumber)
Definition: lib.cpp:195
common::make_it_full_rank
itpp::GF2mat make_it_full_rank(itpp::GF2mat fat)
Definition: lib.cpp:123
mm_read.h
common::color_text
std::string color_text(std::string str)
Definition: lib.cpp:32
common::append_vector
itpp::GF2mat append_vector(itpp::GF2mat G, itpp::bvec b)
Definition: lib.cpp:202
common::get_error_density
double get_error_density(itpp::GF2mat E)
Definition: lib.cpp:224
common::GF2matPrint
int GF2matPrint(itpp::GF2mat &G, std::string name)
Definition: lib.cpp:144
common::is_quantum_code
bool is_quantum_code(itpp::GF2mat &G_x, itpp::GF2mat &G_z)
Definition: lib.cpp:77