Quantum::Entanglement
There's More Than One World In Which To Do It
by Alex GoughAugust 08, 2001
There Is More Than One World (In Which) To Do It
The Quantum::Entanglement module attempts to port some of the
functionality of the universe into Perl. Variables can be prepared in
a superposition of states, where they take many values at once, and
when observed during the course of a program, will collapse to have a
single value. If variables interact then their fates are linked so
that when one is observed and forced to collapse the others will also
collapse at the moment of observation.
It is quite hard to provide a complete version of quantum mechanics in Perl, so we need to make some simplifications. Instead of solving thousands of equations each time we want to do something, we will forget entirely about eigen-functions, Hermitian operators and other mathematical hurdles. This still leaves us with plenty of ways to make Perl behave in a thoroughly unpredictable fashion.
The entangle() function
The Quantum::Entanglement module adds an entangle()
function to Perl, this takes a list of amplitudes and values and
returns a scalar in a superposition of values; saying
$die = entangle( 1=>1, 1=>2, 1=>3, 1=>4, 1=>5, 1=>6);
creates a superposition of the values 1..6. From now on,
$die acts as if it has every one of those values at the same time
as long as we do not try to find out exactly which one.
Observation and Collapse in Perl
We now need to decide what happens when we observe our variable, and
what we mean by observe? Taking a broad
definition as being ``anything that reveals the values
which a variable has'' seems about right. Perl
provides us with many ways of doing this, there are the obvious acts
of printing out a variable or testing it for truth, but even operators
such as eq or <= tell us something.
How do we decide which way a variable collapses? Well, each possible value has an associated probability amplitude, so all we need to do is build up a list of distinct outcomes, add up the amplitudes for each one, square the result, then use this to bias the value (or values) to which the variable collapses.
As every coefficient of the superposition in $die is equal to 1,
print "You rolled a $die.\n";
will output You rolled a 1. or You rolled a 2. and so on,
each for one sixth of the time.
Entanglement and Simple Complex Logic
Whenever superposed variables interact, or are involved in calculations, the results of these as well as the variables themselves become entangled. This means that they will all collapse at the same time, so as to remain consistent with their history. This emulates the entanglement, or ``spooky action at a distance'', which so worried Einstein.
Complex Amplitudes and Entanglement in Perl
If we can have plain numbers as the coefficients of our
superpositions it seems sensible that we could also use complex
numbers. Although instead of just squaring the
number when working out our probability, we need to square the
size of the number. (eg. |1+2i|**2 == 5 == |1-2i|**2.)
The Quantum::Entanglement module allows subroutines to create new
states (amplitude-value pairs) based on the current set of states by
using the function q_logic. This takes as an argument a subroutine
which is presented each state in turn and must return a new set of
states constructed from these.
Starting our program with:
#!/usr/bin/perl -w use Quantum::Entanglement qw(:DEFAULT :complex); $Quantum::Entanglement::destroy = 0;
so that we have access to the constants defined by Math::Complex and
turn off the memory management performed by the module (as this causes
some information to be lost, which will be important later). We then
define a subroutine to return the value it receives and its logical
negation, their coefficients are those of the original state
multiplied by i/sqrt(2) and 1/sqrt(2) respectively:
sub root_not {
my ($prob, $val) = @_;
return( $prob * i / sqrt(2) , $val,
$prob / sqrt(2) , !$val );
}
We then create a superposition which we know is equal to 0
and feed it through our root_not() once:
my $var = entangle(1 => 0); $var = q_logic(\&root_not, $var);
the variable is now in a superposition of two possible values, 0 and
1, with coefficients of i/sqrt(2) and 1/sqrt(2) respectively. We now
make our variable interact, storing the result in $peek. As
$var is in a superposition, every possible value it has
participates in the calculation and contributes to the result.
my $peek = 12*$var; # $peek and $var become entangled $var = q_logic(\&root_not, $var);
We then feed $var through root_not() one more time and
test it for truth. What will happen and what will be the value of
$peek?
if ($var) { print "\$var is true!\n"; }
else { print "\$var is false\n"; }
print "\$peek is equal to: $peek.\n";
The output is always $var is true! as $var is in a final
superposition of (1/2=0, i/2=>1, -1/2=>0, i/2=>1)>.
You can convince yourself of this by running
through the math. What about $peek? Well, because it
interacted with $var before $<var> collapsed and both
possible values that $var had at that time contributed to its
eventual truthfulness, both values of $peek are still
present, we get 0 or 12 each for half the time.
If we reverse the order in which we examine the variables:
print "\$peek is equal to: $peek.\n";
if ($var) { print "\$var is true!\n"; }
else { print "\$var is false\n"; }
we still see peek being 0 and 12 but as we
collapsed $peek we must also collapse $var at the same time. This
causes $var to be in a superposition of (1/2=0,i/2=>1)> or a
superposition of (-1/2=0,i/2=>1)>, both of which will collapse to 0
half of the time and 1 the other half of the time so that (on average)
we see both phrases printed.
If we try to find the value that $var had while it was `between'
the subroutines we force it to have a single value so that after two
passes though root_not() we get random noise, even if we test
this after the event. If, on the other hand, we leave it alone it
emerges from repeated application of root_not() as the logical
negation of its original value, thus the name of our subroutine.
Beneath the Veil
Although the module is intended to be used as a black box which does the Right Thing (or some close approximation to it), the internals of the code are interesting and reveal many features of Perl which may be useful elsewhere.
Writing entangled behaviour into Perl presents an interesting challenge; a means of representing a superposition is required, as is some way of allowing different variables to know about each other without creating a twisty maze of references which would stand in the way of garbage collection and lead to a certain programming headache. We also need a means to cause collapse, as well as a robust mechanism for dealing with both real and complex numbers. Thankfully Perl provides a rich set of ingredients which can more than satisfy these requirements without making the job so hard that it becomes impossible.
Pages: 1, 2 |

