#!/usr/local/bin/perl -w # gf4hunt.p # hunts for a square matrix that is cyclic and has full rank # and satisfies the commutation rules $invertibilitycheck = 1; $N = 5 ; $q = 4 ; # which field to do $omega = 2 ; # what to multiply by to get the extra rows $verbose = 0 ; $exhaustive = 0 ; $randomcode=0; $checksumnonzero = 1 ; # check row sum is non zero $allow_odd_commute_with_self = 0 ; # THIS IS A SILLY IDEA, do not switch to 1. $cyclic = 1 ; # whether to study cyclic codes only $keepgoing=0 ;# whether to keep going after a success found $checkrowsandcols = 1 ; # example usage # to hunt over all linear 3x3 matrices # gf4hunt.p N=3 verbose=0 cyclic=0 keepgoing=1 | m $Try = -1 ; # How to override with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $Q = $q-1 ; if ($Try < 0 ) { print "setting number of attempts to " ; if ($cyclic ) { $Try = $q**($N-1) -1 ; } else { $Try = $q**( ($N*$N) - 1 ) - 1 ; } print "$Try\n" ; } RANDLOOP: for ($try=0;$try<=$Try;$try ++ ) { print "try $try - " if ($verbose>0) ; print "\n" if ($verbose>2) ; if ($cyclic) { # Make a random vector to make a cyclic code if ($randomcode) { print " Make a random vector, attempt $try - " ; print "\n" if ($verbose>2) ; $code[0] = 1; # for ( $a = 1 ; $a < $N ; $a ++ ) { $code[$a] = int(rand($q)) ; } } else { $code[0] = 1; $t = $try ; for ( $a = 1 ; $a < $N ; $a ++ ) { $remainder = $t % $q ; $t = int ( $t / $q ) ; $code[$a] = $remainder ; } } $sum = 0 ; for ( $a = 0 ; $a < $N ; $a ++ ) { $sum = &gfa( $sum , $code[$a] ) ; } if ($verbose>2) { for ( $a = 0 ; $a < $N ; $a ++ ) { print $code[$a] ,"\n" ; } } if ( $checksumnonzero ) { if ( $sum == 0 ) { print "Sum is zero!\n" if ($verbose>0) ; next RANDLOOP ; } } print " Forming all rows\n" if ($verbose>2) ; for ( $a = 0 ; $a < $N ; $a ++ ) { $aa = $N-$a ; for ( $b = 0 ; $b < $N ; $b ++ ) { if ($aa >= $N ) { $aa = 0 ; } $M{$a,$b} = $code[$aa]; $aa ++ ; } } } else { # noncyclic code. $t = $try ; $M{0,0} = 1 ; for ( $a = 0 ; $a < $N ; $a ++ ) { for ( $b = 0 ; $b < $N ; $b ++ ) { if ( ($a==0) && ($b==0) ) { $b ++ ; } if ($randomcode) { $M{$a,$b} = int(rand($q)) ; } else { $remainder = $t % $q ; $t = int ( $t / $q ) ; $M{$a,$b} = $remainder ; } } } if ( $checkrowsandcols ) { # check that all cols have a nonzero and that all rows do too. # = a crude invertibliy filter. for ( $a = 0 ; $a < $N ; $a ++ ) { $sum = 0 ; for ( $b = 0 ; $b < $N ; $b ++ ) { $sum += $M{$a,$b} ; } if ( $sum == 0 ) { print "A row is all zero\n" if ($verbose>0) ; next RANDLOOP ; } $sum = 0 ; for ( $b = 0 ; $b < $N ; $b ++ ) { $sum += $M{$b,$a} ; } if ( $sum == 0 ) { print "A col is all zero\n" if ($verbose>0) ; next RANDLOOP ; } } } } if ($verbose > 2 ) { &printoutM ; } print " Forming all extra stabilizer rows\n" if ($verbose>2) ; for ( $a = $N ; $a < 2 * $N ; $a ++ ) { $am = $a - $N ; for ( $b = 0 ; $b < $N ; $b ++ ) { $M{$a,$b} = &gfm( $M{$am,$b} ,$omega); print $M{$a,$b} ," " if ($verbose>2) ; } print "\n" if ($verbose>2) ; } print "Check commutation of rows\n" if ($verbose>2) ; $dead = 0 ; # only need to check one row against the others, if cyclic. $Amax = $cyclic ? 1 : $N ; for ( $a = 0 ; $a < $Amax ; $a ++ ) { for ( $aa = 0 ; $aa < 2 * $N ; $aa ++ ) { if ( $allow_odd_commute_with_self ) { # don't bother checking the self-row if ($aa == $a + $N ) { $aa ++ ; } } $sum = 0 ; for ( $b = 0 ; $b < $N ; $b ++ ) { $outcome = &commute( $M{$a,$b} , $M{$aa,$b} ) ; print "$outcome " if ($verbose>2) ; $sum += $outcome ; } print "rows $a and $aa have $sum anti-commuters\n" if ($verbose ) ; if ($sum % 2 ) { print " that is odd $a, $aa\n" if ($verbose>0) ; $dead = 1 ; } else { print "that is even\n" if($verbose>2) ; } if ($dead) { print "escaping 1\n" if ($verbose>2) ; last ; } } if ($dead) { print "escaping 2\n" if ($verbose>2) ; last ; } } # next attempt! if($dead==0) { print "try $try\n" ; print " partial Success at try $try - @code \n" ; if ($cyclic==0) { &printoutM ; } print "try $try\n" ; if ($invertibilitycheck) { $dependencies = &binaryInverseCheck($N) ; if ( $dependencies == 0 ) { print "YES! YES! YES! YES! YES! YES! YES! YES! YES! YES! invertible!\n" ; last ; } else { print "not invertible\n" ; } } if ($keepgoing==0) { last ; } } } print "done, $try = $Try\n" ; sub printoutM { for ( $a = 0 ; $a < $N ; $a ++ ) { for ( $b = 0 ; $b < $N ; $b ++ ) { print $M{$a,$b} ," " ; } print "\n" ; } } sub commute { # returns 0 if they commute local ( $a,$b ) = @_; if ( (( $a * $b ) == 0 ) || ( $a==$b ) ) { return 0; # commute } else { return 1; # anticommute; } } # exit(0); sub tablecheck { for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { printf "+ %2d" , &gfa($a,$b ) ; } print "\n" ; } print "\n" ; for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { printf "* %2d" , &gfm($a,$b ) ; } print "\n" ; } print "\n" ; } sub vtablecheck { for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { print " $a + $b = " , &gfa($a,$b ) , " : " ; } print "\n" ; } print "\n" ; for ( $a = 0 ; $a <= $Q ; $a ++ ) { for ( $b = 0 ; $b <= $Q ; $b ++ ) { print " $a * $b = " , &gfm($a,$b ), " : " ; } print "\n" ; } print "\n" ; } if ( $verbose ) { &vtablecheck() ; } else { &tablecheck() ; } &set_fq(); for ($A=1;$A<=$Q;$A++) { print "==== all A are $A ====\n" ; &doit() ; if ( $qary ) { exit ; } } if ( $q > 2 ) { for ( $k =1 ; $k <= $Q ; $k ++ ) { $A = $k ; print "==== A goes 1,2,..Q,1 e.g. 1,2,3,1,2,3 === " , $k , "\n" ; &doitA() ; } print "==== A goes Q-1,Q,Q-1,Q e.g. 3,2,3,2 ====\n" ; $A = $Q ; &doitA3() ; if ( $q > 4 ) { print "==== A pseudorandom ====\n" ; $A = $Q ; &doitA8() ; print "==== A goes 1,2,4,8.. ====\n" ; $A = 1 ; &doitA2() ; } } if ( $exhaustive ) { $Max = 0.0 ; $done = 0 ; print "==== exhaustive enumerate\n" ; for ( $k = ($exhaustive==1)?0:$exhaustive ; (!$done) && $k <= $Q**($tr-1) - 1 ; $k ++ ) { &initialize(); $report="" ; $kc = $k ; for ( $t=1; $t<=$tr ; $t++ ) { $kd = int ( $kc / $Q ) ; $A = $kc - $kd * $Q + 1; $kc = $kd ; &iterate(); $report .= sprintf "%d ", $A , $t, $H; } &entropy() ; &scaleH(); if ( $verbose || ( $H > $Max * 0.9999 ) ) { print $report ; printf "Hscaled =%8g", $H; print "\n" ; } if ( $H > $Max ) { $Max = $H ; $kmax = $k } if ( $exhaustive != 1 ) { $done = 1 ; } } print "max was $Max , achieved by $kmax \n" ; } &say_channel() ; ##############################################################3 sub scaleH { if ( $q == 2 ) { $H = 2.0 * $H; } elsif ( $q == 8 ) { $H = 2.0 * $H / 3.0 ; } elsif ( $q == 16 ) { $H = 0.5 * $H ; } } sub sayH { if ( $q == 2 ) { printf " 2 * "; $H = 2.0 * $H; } elsif ( $q == 8 ) { printf " 2/3 * "; $H = 2.0 * $H / 3.0 ; } elsif ( $q == 16 ) { printf " 1/2 * "; $H = 0.5 * $H ; } printf "H=%8g", $H; print "\n" ; } sub doit { &initialize(); for ( $t=1; $t<=$tr ; $t++ ) { &iterate_chat() ; } } sub doitA3 { &initialize(); for ( $t=1; $t<=$tr ; $t++ ) { &iterate_chat() ; $A ++ ; if ( $A > $Q ) { $A = $Q-1 ; } } } sub doitA8 { &initialize(); for ( $t=1; $t<=$tr ; $t++ ) { &iterate_chat() ; $A ++ ; $A *= 2 ; while ( $A > $Q ) { $A -= $Q-2 ; } } } sub doitA2 { &initialize(); for ( $t=1; $t<=$tr ; $t++ ) { &iterate_chat() ; $A *= 2 ; if ( $A > $Q ) { $A = 1 ; } } } sub doitA { &initialize(); for ( $t=1; $t<=$tr ; $t++ ) { &iterate_chat() ; $A ++ ; if ( $A > $Q ) { $A = 1 ; } } } sub iterate_chat { &iterate(); &entropy() ; printf "A %d col %d H=%8g", $A , $t, $H; if ( $q == 2 ) { printf " 2H=%8g", 2.0 * $H; } elsif ( $q == 8 ) { printf " H*2/3=%8g", 2.0 * $H / 3.0 ; } elsif ( $q == 16 ) { printf " H/2=%8g", 0.5 * $H ; } print "\n" ;} sub iterate { for ( $a = 0 ; $a <= $Q ; $a ++ ) { $sum = 0.0 ; for ( $b = 0 ; $b <= $Q ; $b ++ ) { $sum += $p[&gfa(&gfm($b,$A),$a)] * $fq[$b] ; } $pnew[$a] = $sum ; } for ( $a = 0 ; $a <= $Q ; $a ++ ) { $p[$a] = $pnew[$a] ; } } sub gfa { local ( $a , $b ) = @_ ; return $a ^ $b ; # if ( $a == 0 ) { return $b ; } # elsif ($a } sub gfm { local ( $a , $b ) = @_ ; if ( $a == 0 || $b == 0 ) { return 0 ; } elsif ( $a == 1 ) { return $b ; } elsif ( $b == 1 ) { return $a ; } elsif ( $q == 4 ) { if ( $a == 2 ) { if ( $b == 2 ) { return 3 ; } elsif ( $b == 3 ) { return 1 ; } else { print "ERR" ; } } elsif ( $a == 3 ) { if ( $b == 2 ) { return 1 ; } elsif ( $b == 3 ) { return 2 ; } else { print "ERR" ; } } else { print "ERR" ; } } elsif ( $q == 8 ) { if ( $a == 2 ) { if ( $b == 2 ) { return 4 ; } elsif ( $b == 3 ) { return 6 ; } elsif ( $b == 4 ) { return 3 ; } elsif ( $b == 5 ) { return 1 ; } elsif ( $b == 6 ) { return 7 ; } elsif ( $b == 7 ) { return 5 ; } else { print "ERR" ; } } elsif ( $a == 3 ) { if ( $b == 2 ) { return 6; } elsif ( $b == 3 ) { return 5; } elsif ( $b == 4 ) { return 7; } elsif ( $b == 5 ) { return 4; } elsif ( $b == 6 ) { return 1; } elsif ( $b == 7 ) { return 2; } else { print "ERR" ; } } elsif ( $a == 4 ) { if ( $b == 2 ) { return 3; } elsif ( $b == 3 ) { return 7; } elsif ( $b == 4 ) { return 6; } elsif ( $b == 5 ) { return 2; } elsif ( $b == 6 ) { return 5; } elsif ( $b == 7 ) { return 1; } else { print "ERR" ; } } elsif ( $a == 5 ) { if ( $b == 2 ) { return 1 ; } elsif ( $b == 3 ) { return 4 ; } elsif ( $b == 4 ) { return 2 ; } elsif ( $b == 5 ) { return 7 ; } elsif ( $b == 6 ) { return 3 ; } elsif ( $b == 7 ) { return 6 ; } else { print "ERR" ; } } elsif ( $a == 6 ) { if ( $b == 2 ) { return 7 ; } elsif ( $b == 3 ) { return 1 ; } elsif ( $b == 4 ) { return 5 ; } elsif ( $b == 5 ) { return 3 ; } elsif ( $b == 6 ) { return 2 ; } elsif ( $b == 7 ) { return 4 ; } else { print "ERR" ; } } elsif ( $a == 7 ) { if ( $b == 2 ) { return 5 ; } elsif ( $b == 3 ) { return 2 ; } elsif ( $b == 4 ) { return 1 ; } elsif ( $b == 5 ) { return 6 ; } elsif ( $b == 6 ) { return 4 ; } elsif ( $b == 7 ) { return 3 ; } else { print "ERR" ; } } else { print "ERR" ; } } else { print "ERR" ; } return 0 ; } sub initialize { for ( $a = 0 ; $a <= $Q ; $a ++ ) { $p[$a] = 0.0 ; } $p[0] = 1.0 ; } sub entropy { $H = 0.0 ; for ( $a = 0 ; $a <= $Q ; $a ++ ) { $H -= &plgp( $p[$a] ) ; } } sub H2 { # binary entropy in natural units # usage ( $f ) local ( $f ) = @_ ; local ( $ans ) = - ( $f * &safelog ( $f ) + (1.0 - $f) * &safelog (1.0 - $f ) ) ; return $ans ; } sub set_fq { if ( $qary ) { # uniform noise $qf = $f / $Q ; for($i=1;$i<=$Q;$i++){ $fq[$i] = $qf ; } $fq[0] = 1 - $f ; } else { if ( $q == 8 ) { $fq[0] = (1-$f)*(1-$f)*(1-$f) ; $fq[1] = $f*(1-$f)*(1-$f) ; $fq[2] = (1-$f)*$f*(1-$f) ; $fq[3] = ($f*$f)*(1-$f) ; $fq[4] = (1-$f)*(1-$f)*($f) ; $fq[5] = $f*(1-$f)*($f) ; $fq[6] = (1-$f)*$f*($f) ; $fq[7] = ($f*$f)*($f) ; } elsif ( $q == 16 ) { $fq[0] = (1-$f)*(1-$f)*(1-$f)*(1-$f) ; $fq[1] = $f*(1-$f)*(1-$f)*(1-$f) ; $fq[2] = (1-$f)*$f*(1-$f)*(1-$f) ; $fq[3] = ($f*$f)*(1-$f)*(1-$f) ; $fq[4] = (1-$f)*(1-$f)*($f)*(1-$f) ; $fq[5] = $f*(1-$f)*($f)*(1-$f) ; $fq[6] = (1-$f)*$f*($f)*(1-$f) ; $fq[7] = ($f*$f)*($f)*(1-$f) ; $fq[8] = (1-$f)*(1-$f)*(1-$f)*($f) ; $fq[9] = $f*(1-$f)*(1-$f)*($f) ; $fq[10] = (1-$f)*$f*(1-$f)*($f) ; $fq[11] = ($f*$f)*(1-$f)*($f) ; $fq[12] = (1-$f)*(1-$f)*($f)*($f) ; $fq[13] = $f*(1-$f)*($f)*($f) ; $fq[14] = (1-$f)*$f*($f)*($f) ; $fq[15] = ($f*$f)*($f)*($f) ; } elsif ( $q == 4 ) { $fq[0] = (1-$f)*(1-$f) ; $fq[1] = $f*(1-$f) ; $fq[2] = (1-$f)*$f ; $fq[3] = ($f*$f) ; } elsif ( $q == 2 ) { $fq[0] = 1-$f ; $fq[1] = $f ; } } &say_channel() ; } sub say_channel { # report entropy of channel for ( $i = 0 ; $i <= $Q ; $i ++ ) { $p[$i] = $fq[$i] ; } &entropy(); print "noise has entropy: " ; &sayH(); } sub plgp { local ($x) = @_ ; return $x * safelog($x) / log(2.0) ; } sub safelog { local ($f) = @_ ; if ( $f < 0 ) { print STDERR "Can't take log of $f\n" ; } return ( $f > 0 ? log ( $f) : 0.0 ) ; } # pass $N to this # checks whether a square matrix is of full rank sub binaryInverseCheck { # check whether matrix is invertible local( $Norig ) = @_ ; $i = 0 ; for ( $a = 0 ; $a < $N ; $a ++ ) { for ( $b = 0 ; $b < $N ; $b ++ ) { $G[$i] = ( $M{$a,$b} == 1 ) || ( $M{$a,$b} == 3 ) ? 1 : 0 ; $i ++ ; $G[$i] = ( $M{$a,$b} >= 2 ) ? 1 : 0; $i ++ ; } } for ( $a = 0 ; $a < $N ; $a ++ ) { for ( $b = 0 ; $b < $N ; $b ++ ) { $G[$i] = ( $M{$a,$b} >= 2 ) ? 1 : 0; $i ++ ; $G[$i] = ( $M{$a,$b} == 1 ) || ( $M{$a,$b} == 2 ) ? 1 : 0 ; $i ++ ; } } local($K) = 2*$Norig ; local($N) = $K; $Mactual = 0 ; if ( $verbose ) { print "K = $K, N = $N\n" ; } for ( $n = 1 ; $n <= $N ; $n ++ ) { $taken[$n] = 0 ; } # $i $k are reserved symbols, so is $l , $h , $e $i = 0 ; $iorig = 0 ; for ( $k = 1 ; $k <= $K ; $k ++ ) { # run through rows if ( $verbose ) { # if ( !($k%50) ) { print "\n$k" ; } print "." ; } # do { # repeatedly read G[k], modifying it until its first 1 is cool. $leftspan = $N+1 ; for ( $n = 1 ; $n <= $N ; $n ++ ) { # read in the new codeword k $g[$n] = $G[$iorig] ; $iorig ++ ; if ( $g[$n] ) { if ( $n < $leftspan ) { $leftspan = $n ; } } } $n = $leftspan ; if ( $leftspan > $N ) { print " zero weight row $k ($Kactual)\n" if ( $verbose ) ; $Mactual ++ ; $repeat = 0 ; } elsif ( $taken[$n] ) { # we have a clash and need to do a subtraction $repeat = 1 ; # $kkill = $taken[$n] ; $ikill = ($kkill-1) * $N ; $i = $iorig - $N ; # cut out the old guy from this new guy. print " row $k first 1-element $n clashes with $kkill\n" if ( $verbose > 2 ) ; for ( $n = 1 ; $n <= $N ; $n ++ ) { $G[$i] = ( $G[$ikill] + $G[$i] ) % 2; # modify G $ikill ++ ; $i++ ; } # $iorig -= $N ; # } else { # print " row $k first 1-element $n\n" if ( $verbose > 2 ) ; $repeat = 0 ; # $taken[$n] = $k ; # $k2n[$k] = $n ; $Kactual ++ ; # } } while ( $repeat ) ; } # G is now in trellis-oriented form on its left but not its right. # print "\n" if ( $verbose ) ; &reportM ; return ( $N - $Kactual) ; } # sub reportM { # $Kactual = $N - $Mactual ; # print "N: $N\t \tM: $Mactual\tK: $Kactual\n" ; } #