package QKey; use strict; use Exporter; use Math::MatrixReal; use QStuff qw(entangle); our @ISA = qw(Exporter); our @EXPORT_OK = qw(randseq quantumencode analyze ynseq bobkey alicekey %ms); our %ms; # seq of measurements used sub randseq { map{int rand(2)} 1..($_[0]) } # Alice sub quantumencode { # Alice map { $_ ? entangle(1/sqrt(2), 0, 1/sqrt(2), 1) : entangle(1, 0, 0, 1) } @_ } # measurements(@quantumEncode): seq of measurements gonna use sub _measurements { # Bob my $ms = {}; $ms->{ms} = [ entangle(1/sqrt(2), 0, -1/sqrt(2), 1), entangle(0, 0, 1, 1) ]; $ms->{seq} = [ map{int rand(2)} 0..$#_ ]; return $ms; } # analyze(@quantumEncode): return measurement results sub analyze { # Bob %ms = %{_measurements(@_)}; map{ $_[$_]->proj($ms{ms}[$ms{seq}[$_]]) } 0..$#_; } # ynseq(@analyze): return y/n seq of "results" sub ynseq { # Bob map{ $_->cmpquanta($ms{ms}[0]) or $_->cmpquanta($ms{ms}[1]) ? 1 : 0 } @_; } # bobkey(@analyze) sub bobkey { # Bob my @key; for (@_) { push(@key, 0) if $_->cmpquanta($ms{ms}[0]); push(@key, 1) if $_->cmpquanta($ms{ms}[1]); } return @key; } # alicekey(\@ynseq, \@randseq) sub alicekey { # Alice my @key; for(0..$#{$_[0]}){ push(@key, ${$_[1]}[$_]) if ${$_[0]}[$_] } return @key; } 1;