Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Problems with Quantum::Entanglement

by pernod (Chaplain)
on Jun 20, 2003 at 16:02 UTC ( #267630=perlquestion: print w/ replies, xml ) Need Help??
pernod has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

After reading about the Quantum::Entanglement module, I thought I should give it a try. It might just do exactly what I need for some noisemaking, but my first attempt at using it for anything ended in dismal failure.

I used ppm to install the module on my ActiveState installation on a Win XP box. I tried to make a simple example, taken from the above mentioned article. Here's what I got:

#! /usr/bin/perl use strict; use Quantum::Entanglement; my $die = entangle( 1=>1, 1=>2, 1=>3, 1=>4, 1=>5, 1=>6 ); foreach my $i ( 1 .. 5 ) { print "$die "; } print "\n";

My first run of this gave me: 1, 1, 1, 1, 1, which surprised me. I tried again, and got 3, 3, 3, 3, 3

I had expected $die to collapse to different values each time I observed it, but it seems to me that it is only collapsed once during each run. What am I missing here?

pernod
--
Mischief. Mayhem. Soap.

Comment on Problems with Quantum::Entanglement
Select or Download Code
Re: Problems with Quantum::Entanglement
by zakzebrowski (Curate) on Jun 20, 2003 at 16:18 UTC
    Correct me if I'm wrong, but aren't you only allowed to look at a dice once?

    ----
    Zak
    Pluralitas non est ponenda sine neccesitate - mysql's philosphy
      no you can look at them as many times as you want, if they change between times you look at them though... it's time to look for the cheater...
Re: Problems with Quantum::Entanglement
by tedrek (Pilgrim) on Jun 20, 2003 at 16:21 UTC

    The thing you appear to be missing is this line from the docs:

    Observing a particle in this way is said to collapse the wave-function, or superposition of values, into a single value, which it will retain from then onwards.

    So the behavior you are observing seems to be what is expected by the module author. You may be able to look into the internals and fiddle with a few bits to get the behavior you want though :)

Re: Problems with Quantum::Entanglement
by Mr. Muskrat (Abbot) on Jun 20, 2003 at 16:23 UTC

    If you observe it, it will collapse. So for this, you should move the entangle inside the loop.

    #! /usr/bin/perl -w use strict; use Quantum::Entanglement; foreach my $i ( 1 .. 5 ) { my $die = entangle( 1=>1, 1=>2, 1=>3, 1=>4, 1=>5, 1=>6 ); print "$die "; } print "\n";

Re: Problems with Quantum::Entanglement
by bunnyman (Hermit) on Jun 20, 2003 at 16:55 UTC

    I'd think carefully about what you're trying to do. Quantum::Entanglement seems to just be a proof-of-concept module, not intended for use in "real" programs. It will only serve to confuse the logic of the code for no real benefit, as Perl does not run on quantum computers yet.

    If you're only playing around, then go forth with Perl, brother.

Re: Problems with Quantum::Entanglement
by hossman (Prior) on Jun 21, 2003 at 02:33 UTC
Re: Problems with Quantum::Entanglement
by chunlou (Curate) on Jun 21, 2003 at 03:09 UTC
    A slightly longer answer more or less equivalent to Mr. Muskrat's reply in a somewhat more programmatic way.

    Note that the module has "save_state" and "restore_state" methods. So you can do:
    my $die = entangle( 1=>1, 1=>2, 1=>3, 1=>4, 1=>5, 1=>6 ); my $save_state = $die->save_state; for (1..5) { print "$die "; $die = $save_state->restore_state; } # print: "2 1 3 2 5" or whatever
    And observe that (of course, the "observation" below will be different each time):
    #!/usr/bin/perl -w use Quantum::Entanglement; my $die = entangle( 1=>1, 1=>2, 1=>3, 1=>4, 1=>5, 1=>6 ); print "Pre-observation: ".$die->show_states; print "Observation: $die\n"; print "Post-observation: ".$die->show_states; # print: # # Pre-observation: 1|1> 1|2> 1|3> 1|4> 1|5> 1|6> # Observation: 3 # Post-observation: 1|3>
    To see how/why the module collapsed the states (besides being philosophically compliant with quantum mechanics), you could use the Aspect module to track call flow:
    #!/usr/bin/perl -w use strict; # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - use Aspect qw(advice calls returns around) ; my $aspect = advice( calls(qr/^(Quantum|main)::(.*)/), sub { printf "calling -> %s\n", $::thisjp->sub } ); $aspect->enable; # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - use Quantum::Entanglement; sub foo { my $die = entangle( 1=>1, 1=>2, 1=>3, 1=>4, 1=>5, 1=>6 ); print $die ; print "\n" ; } foo(); # print: # # calling -> main::foo # calling -> main::entangle # calling -> Quantum::Entanglement::_new # calling -> Quantum::Entanglement::("" # calling -> Quantum::Entanglement::_normalise # 5 # calling -> Quantum::Entanglement::DESTROY # calling -> Quantum::Entanglement::_rationalise_states # calling -> Quantum::Entanglement::_unravel # foo();
    The ("" thing is (I assume) the double-quote operator, which is overloaded by "str_ent" method in the module (which in turn called "_normalise" ). In str_ent, there is a comment that says:
    # set all non retained states to zero probability, leave others alone
    So, I guess, each time you print $die, double-quote operator will be used, which uses str_end, which always collapses the states.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://267630]
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-12-26 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls