Wednesday, August 5, 2009

Quantum Superpositions and other Scary Stuff

Quantum::Superpositions, Quantum::Entanglement and Quantum::Usrn, as one may note, are related to quantum mechanics. Those are not packages to solve equations characteristic to quantum mechanics, but to bring quantum ideas and logic into Perl and programming in general. All three packages are interesting and deserve attention but only Quantum::Superpositions achieved wider recognition and become part of Perl 6. For that reason (limited space also plays a role) I would concentrate only on Quantum::Superpositions.

Welcome to Hilbert space and no, you do not have to have quantum computer at home to play with madness. With a little help from your friends (whom you manage to find on Internet) your good old computer will do just fine.

If you are running Ubuntu or Debian, use sudo; if not, just run terminal as root:

sudo perl -MCPAN -e shell

As mentioned, to invoke CPAN shell you must use root. If you never used it then do the update.

cpan[1]> install Bundle::CPAN

That may take some time.

cpan[2]> reload cpan

And finally, why we started all this - installation of Quantum::Superpositions:

cpan[3]> install Quantum::Superpositions

CPAN shell will ask for permission to download dependencies, and when all is done type in q or quit and hit enter to release the lock.

There is a philosophycal background to this package and the author, Damian Conway, gently mentioned it in documentation. Naturally, very few readers have the knowledge of quantum mechanics (that thing is worse than maths) and for the majority we will nicely disect the package through trusty old trial and error aproach. Quantum::Superpositions introduces two operators - any and all. Those operators are mapping a set of scalars to a single scalar variable, the author calls those scalar variables eigenstates. You see how that quantum mechanic may be dangerous and contageous, short exposure to it and you are already appending German words to English ones creating a new language which nobody understands. Without further ado we start with the code:

use Quantum::Superpositions;

use Data::Dumper;

$temp = all(1 .. 5);

print "$temp";

print "\n";

print Dumper($temp);

From the output we may conclude that to string method is overloaded and that $temp is a reference to anonymous array blessed to Quantum::Superpositions::Conj. If we do the same for any we see that $temp is a reference to anonymous array now blessed to Quantum::Superpositions::Disj. If you can't remember that reference to anonymous array, here it is:

$temp = [1, 2, 3]

Beside string conversion, the package overloads all other commonly used operators. Locating Superpositions.pm and inspecting the code may be highly beneficial here. Trivial:

$test++;

will increase all eigenvalues, irrespective of whether we are using any or all. Multiplication and addition becomes real fun:

$temp = any(1 .. 3)*any(4, 5);

print "$temp";

will produce:

any(8,4,10,12,15,5)

We will also note that any ignores duplicate values (or eigenvalues?) and all doesn't. Commutativity is satisfied for any*any or all*all but not for all*any. For example:

any(1 .. 3)*all(4, 5) = any()

but

all(4, 5)*any(1 .. 3) = all(any(8,4,12),any(10,15,5))

The type of the first operand determines the type of the result of multiplication. If we try addition then:

$temp = all(4, 5)+any(1 .. 3);

print "$temp";

print "\n";

print Dumper($temp);

produces a very interesting result:

6 7
$VAR1 = bless( [
                 bless( [
                          5,
                          6,
                          7
                        ], 'Quantum::Superpositions::Disj' ),
                 bless( [
                          6,
                          7,
                          8
                        ], 'Quantum::Superpositions::Disj' )
               ], 'Quantum::Superpositions::Conj' ); 

The result is an array containing common subset. From this (pretending that we did not read help file) we can easily draw the formula for finding common subset:

$temp = all(any(1 .. 5), any(4, 5, 6), any(3, 4, 5));

Small warning here: if one tries @$temp it will not produce expected 4 and 5 but:

any(4,1,3,2,5) any(6,4,5) any(4,3,5)

To pull subset into array properly we must use eigenstates method.

@subset = eigenstates(all(any(1 .. 5), any(4, 5, 6), any(3, 4, 5)));

Now the question do elements of array @b contain all the elements of array @a becomes:

@a = (1, 2, 3);

@b = (1 .. 5);

if(@a == eigenstates(all(any(@a), any(@b)))){

print "Yes @a is subset of @b\n";

}

Which is really a neat way of coding compared to usual nested loops.

If you download the source as well, it contains demo directory where we can see GCD and factoring and generally get some idea on how the package may be used. While the package brings ideas from quantum computing, it doesn't turn your old computer into a quantum one, so some algorithms may still execute in exponential time. If you find this interesting here is a small advertisment for Quantum::Entanglement, its demo directory contains implementation of Shor's algorithm for factoring numbers.



No comments:

Post a Comment