7 #include <itpp/itbase.h>
8 #include <itpp/itcomm.h>
40 std::cout<<
"Classical code: n = "<<
n<<std::endl
41 <<
"codeword generating matrix G"<<
G
42 <<
"parity check matrix H"<<
H<<std::endl;
58 G = itpp::GF2mat(itpp::ones_b(
n),
false);
130 if ( col ==0 )
return G.get_submatrix(0,1,G.rows()-1,G.cols()-1);
131 if ( col == n-1 )
return G.get_submatrix(0,0,G.rows()-1,G.cols()-2);
132 return G.get_submatrix(0,0,G.rows()-1,col-1).concatenate_horizontal(
133 G.get_submatrix(0,col+1,G.rows()-1,G.cols()-1)
141 itpp::bvec to_remove(n);
142 for (
int i=0;i<n;i++){
143 if (itpp::GF2mat(Gx.get_col(i)).is_zero()) to_remove.set(i,1);
144 if (itpp::GF2mat(Gz.get_col(i)).is_zero()) to_remove.set(i,1);
146 for (
int i=0;i<n;i++){
147 if ( to_remove(n-i-1) ){
154 std::cout<<
"singleton removed"<<std::endl;
158 int getRandomQuantumCode(
int n,
int Gx_row,
int Gz_row, itpp::GF2mat &Gx,itpp::GF2mat &Gz, itpp::GF2mat &Cx,itpp::GF2mat &Cz){
160 Gx = itpp::GF2mat(Gx_row,n);
161 Gz = itpp::GF2mat(Gz_row,n);
162 for (
int i =0;i<Gx_row;i++){
163 Gx.set_row(i,itpp::randb(n));
167 itpp::GF2mat T,U; itpp::ivec P;
168 int rank_of_Gx = Gx.transpose().T_fact(T,U,P);
169 itpp::GF2mat Q=T.get_submatrix(rank_of_Gx,0,n-1,n-1);
172 itpp::GF2mat alpha(Gz_row,Q.rows());
173 for (
int i=0;i<Gz_row;i++){
174 alpha.set_row(i,itpp::randb(Q.rows()));
184 int getGoodQuantumCode(
int n,
int Gx_row,
int Gz_row, itpp::GF2mat &Gx,itpp::GF2mat &Gz, itpp::GF2mat &Cx,itpp::GF2mat &Cz,
int debug){
187 itpp::GF2mat Gx_temp, Gz_temp,Cx_temp,Cz_temp;
188 int search_trial=1000;
189 int flag_find_good_code=0;
190 for (
int i =0; i<search_trial; i++){
198 flag_find_good_code=1;
206 Gx = Gx_temp; Gz = Gz_temp; Cx = Cx_temp; Cz = Cz_temp;
209 if ( debug ) std::cout<<
"Gx 1st row:"<<Gx.get_row(0)<<std::endl;
211 if ( Gx.row_rank() < Gx.rows() ) {
212 if (debug ) std::cout<<
"getGoodQuantumCode: Gx not full rank. now make it full rank"<<std::endl;
215 if ( Gz.row_rank() < Gz.rows() ) {
216 if (debug) std::cout<<
"getGoodQuantumCode: Gz not full rank. now make it full rank"<<std::endl;
220 if ( debug)
if ( ! flag_find_good_code ) std::cout<<
common::color_text(
"didn't find good code after ")<<search_trial<<
" trials"<<std::endl;
228 for (
int i =0 ; i < sub.rows(); i ++)
229 for (
int j = 0; j< sub.cols(); j++ ){
230 G.set(i+row, j+col, sub.get(i,j));
241 for (
int i = 0; i<alpha_Gaz.cols();i++){
243 for (
int j = alpha_Gaz.rows()-1; j > -1; j--){
244 if (alpha_Gaz.get(j,i)){
247 if (debug) std::cout<<
"get one twice in that column i="<<i<<std::endl;
251 if (j <= position_one){
253 if (debug) std::cout<<
"break the inner for loop for dependent column i = "<<i<<std::endl;
265 if ( columns_one == alpha_Gaz.rows() ){
269 if ( columns_one < alpha_Gaz.rows() ){
270 if (debug) std::cout<<
"columns_one:"<<columns_one<<
" is not full rank"<<alpha_Gaz.rows()<<std::endl;
278 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){
279 if (debug) std::cout<<na<<
","<<Gax_row<<
","<<Gaz_row<<std::endl;
281 if (Gaz_row+Gax_row > na-1){
282 std::cout<<
"generate_code: no logical qubit"<<std::endl;
285 const int id_Gax_MAX = (int) pow(2, Gax_row * (na-Gax_row) ) -1 ;
286 if ( id_Gax <1 || id_Gax > id_Gax_MAX ) {
287 std::cout<<
"illegal id_Gax: "<<id_Gax<<
", id_Gax_MAX = "<<id_Gax_MAX<<std::endl;
290 const int id_Gaz_MAX = (int) pow(2, Gaz_row*(na - Gax_row)) - 1;
291 if ( id_Gaz < 1 || id_Gaz > id_Gaz_MAX ){
292 std::cout<<
"illegal id_Gaz: "<<id_Gaz<<
", id_Gaz_MAX = "<<id_Gaz_MAX<<std::endl;
297 itpp::GF2mat beta_Gaz = itpp::GF2mat(itpp::dec2bin(Gaz_row*(na-Gax_row),id_Gaz),
false);
298 itpp::GF2mat alpha_Gaz(Gaz_row, na-Gax_row);
299 if ( debug ) std::cout<<
"beta_Gaz = "<<beta_Gaz<<std::endl;
300 for (
int i =0;i<Gaz_row;i++){
301 if (debug) std::cout<<
"set submatrix i = "<<i<<std::endl<<beta_Gaz.get_submatrix(0,i*(na-Gax_row),0, (i+1)*(na-Gax_row)-1)<<std::endl;
302 set_submatrix(alpha_Gaz, beta_Gaz.get_submatrix(0,i*(na-Gax_row),0, (i+1)*(na-Gax_row)-1), i,0);
304 if (debug) std::cout<<
"alpha_Gaz"<<alpha_Gaz<<std::endl;
321 Gax = itpp::GF2mat(Gax_row,na);
327 itpp::GF2mat alpha_Gax = itpp::GF2mat( itpp::dec2bin(Gax_row*(na-Gax_row), id_Gax),
false);
328 if (debug) std::cout<<
"alpha_Gax give the right part of Gax"<<std::endl<<alpha_Gax<<std::endl;
329 for (
int i = 0 ; i < Gax_row; i++){
330 set_submatrix(Gax,alpha_Gax.get_submatrix(0, i*(na-Gax_row), 0, (i+1)*(na-Gax_row)-1), i, Gax_row);
332 if (debug) std::cout<<
"Gax"<<Gax<<std::endl;
336 for (
int i =0;i<Gax_row-1;i++){
337 if ( itpp::bin2dec(Gax.get_submatrix(0,Gax_row,Gax_row-1,na-1).get_row(i))
338 < itpp::bin2dec(Gax.get_submatrix(0,Gax_row,Gax_row-1,na-1).get_row(i+1)) ){
340 if (debug) std::cout<<
"duplicate Gax with this id_Gax. no calculation needed. id_Gax must be in decreasing order. zero allowed"<<std::endl;
345 itpp::bvec bvec_zero = itpp::zeros_b(na);
346 for (
int i = 0; i < Gax_row; i++){
347 if ( itpp::BERC::count_errors(bvec_zero, Gax.get_row(i)) == 1){
354 if (debug) std::cout<<
"nullSpace: H"<<H<<std::endl;
357 for (
int i = 0; i < na - Gax_row; i++){
358 if ( itpp::BERC::count_errors(bvec_zero, H.get_row(i)) == 1){
372 if (debug) std::cout<<
"alpha_Gaz"<<alpha_Gaz<<std::endl;
379 itpp::bvec bvec_zero_col=itpp::zeros_b(Gaz.rows());
380 for (
int i = 0; i < Gaz.cols(); i++){
381 if ( itpp::BERC::count_errors(bvec_zero_col, Gaz.get_col(i)) == 0){
388 if (debug) std::cout<<
"Gaz"<<Gaz<<std::endl;
404 int product(itpp::GF2mat Gax, itpp::GF2mat Gaz, itpp::GF2mat Gbx, itpp::GF2mat Gbz,
int ddax,
int ddaz,
int ddbx,
int ddbz,
int debug,
int mode){
406 int na=Gax.cols(),nb=Gbx.cols();
412 itpp::GF2mat Gcx,Gcz;
426 .concatenate_vertical(
440 Gcx=
common::kron(Gaz.transpose(), itpp::gf2dense_eye(Gbx.rows()))
441 .concatenate_horizontal(
common::kron(itpp::gf2dense_eye(Gax.cols()),Gbx))
442 .concatenate_horizontal(
common::kron(itpp::GF2mat(Gax.cols(),Gax.rows()),itpp::GF2mat(Gbx.rows(),Gbz.rows())));
444 .concatenate_vertical(
445 common::kron(itpp::GF2mat(Gax.rows(),Gaz.rows()),itpp::GF2mat(Gbx.cols(),Gbx.rows()))
446 .concatenate_horizontal(
common::kron(Gax, itpp::gf2dense_eye(Gbx.cols())))
447 .concatenate_horizontal(
common::kron(itpp::gf2dense_eye(Gax.rows()),Gbz.transpose()))
449 Gcz=
common::kron(itpp::gf2dense_eye(Gaz.rows()), Gbx.transpose())
450 .concatenate_horizontal(
common::kron(Gaz,itpp::gf2dense_eye(Gbx.cols())))
451 .concatenate_horizontal(
common::kron(itpp::GF2mat(Gaz.rows(),Gax.rows()),itpp::GF2mat(Gbz.cols(),Gbz.rows())));
453 .concatenate_vertical(
454 common::kron(itpp::GF2mat( Gaz.cols(),Gaz.rows() ), itpp::GF2mat( Gbz.rows(),Gbx.rows() ))
455 .concatenate_horizontal(
common::kron(itpp::gf2dense_eye(Gaz.cols()),Gbz))
456 .concatenate_horizontal(
common::kron(Gax.transpose(),itpp::gf2dense_eye(Gbz.rows())))
465 int flag_dist_flip=1;
482 std::cout<<
"mode (3)"<<std::endl;
485 std::cout<<
"mode (4)"<<std::endl;
491 if ( ! (Gcx*Gcz.transpose()).is_zero() ){
492 std::cout<<
"concatenation_lib: not a quantum code "<<std::endl;
493 throw "not a quantum code";
495 if ( debug) std::cout<<
"mode ("<<mode<<
") is quantum code"<<std::endl;
508 switch ( flag_dist_flip ){
513 int dax=ddax,dbx=ddbx;
515 if (debug) std::cout<<
"dax,daz,dbx,dbz = "<<ddax<<
","<<ddaz<<
","<<ddbx<<
","<<ddbz<<
","<<std::endl;
517 if (debug) std::cout<<
"dcx = dax*dbx = "<<dcx<<std::endl;
520 if (debug) std::cout<<
"dcx = "<<dcx<<
", dax = "<<dax<<
", dbx = "<<dbx<<std::endl;
523 if (dcx > dax*dbx) std::cout<<
"PSEUDO ";
524 std::cout<<
common::red_text(
"CASE:")<<
" mode ("<<mode<<
") dax*dbx="<<dax*dbx<<
", dcx="<<dcx;
525 std::cout<<
". dax,daz,dbx,dbz = "<<ddax<<
","<<ddaz<<
","<<ddbx<<
","<<ddbz<<
";";
526 std::cout<<
"na,nb,nc,"<<Gax.cols()<<
","<<Gbx.cols()<<
","<<Gcx.cols()<<
";";
527 std::cout<<
"ka,kb="<<Cax.rows()<<
","<<Cbx.rows()<<
";";
536 int daz=ddaz,dbz=ddbz;
538 if (debug) std::cout<<
"dax,daz,dbx,dbz = "<<ddax<<
","<<ddaz<<
","<<ddbx<<
","<<ddbz<<
","<<std::endl;
540 if (debug) std::cout<<
"dcz = daz*dbz = "<<dcz<<std::endl;
543 if (debug) std::cout<<
"dcz = "<<dcz<<
", daz = "<<daz<<
", dbz = "<<dbz<<std::endl;
546 if (dcz > daz*dbz) std::cout<<
"PSEUDO ";
547 std::cout<<
common::red_text(
"CASE:")<<
" mode ("<<mode<<
") daz*dbz="<<daz*dbz<<
", dcz="<<dcz;
548 std::cout<<
". dax,daz,dbx,dbz = "<<ddax<<
","<<ddaz<<
","<<ddbx<<
","<<ddbz<<
";";
549 std::cout<<
"na,nb,nc="<<Gax.cols()<<
","<<Gbx.cols()<<
","<<Gcx.cols()<<
";";
550 std::cout<<
"ka,kb="<<Cax.rows()<<
","<<Cbx.rows()<<
";";