Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Quantum::Superpositions prob

by tommycahir (Acolyte)
on Mar 25, 2004 at 14:37 UTC ( #339747=perlquestion: print w/replies, xml ) Need Help??
tommycahir has asked for the wisdom of the Perl Monks concerning the following question:

just a quick question when i run the code below it gives the 1st elemnt in the array last and the rest in their proper order is this a side effect of the operation or have i made a mistake with my code.i just using it to remove all instances of the contents of @a1 from @a2..
#! /usr/bin/perl -w use strict; use Quantum::Superpositions; my @a2 = qw/1 2 2 3 3 3 4 4 4 4 5 6 7/;#list of to_visit my @a1 = qw/1 2 3 4/;#list of visited @a2 = eigenstates(any(@a2) != all(@a1)); print "list of to visit\n"; print_arrays(\@a2); print "\nlist visited\n"; print_arrays(\@a1); sub print_arrays { my($x) = @_; for my $item1 ( @$x ) { print $item1,"\n"; } print "\n"; }
it returns
list of to visit 6 7 5 list visited 1 2 3 4

Replies are listed 'Best First'.
Re: Quantum::Superpositions prob
by Limbic~Region (Chancellor) on Mar 25, 2004 at 14:57 UTC
    tommycahir,
    I think you were just banking on something that Quantum::Superpositions doesn't promise. The eigenstates function returns a list of states in the superposition, but does not say in order. I peeked under the hood and discovered that it generates a unique list by using a hash and we all know that hash order is not array order. There are ways to still use the module and maintain order - but it requires more work.

    Hope this helps - L~R

      how would i go about this, is there much work involved.
      What i really am aiming to do is
      1.to have an array of routers visited and an array of routers to_visit,
      2.then then remove the routers visited from the to_visit array
      3.continue to visit each one of these routers in turn finding out what other routers they connected to and adding them to the to_visit array
      4.repeat from step 2 till no more left in the to_visit array.
        tommychahir,
        Of course this can be done without Quantum::Superpositions, but since this sounded like fun:
        #! /usr/bin/perl use strict; use warnings; use Quantum::Superpositions; my @array_1 = (1 .. 4); my @array_2 = qw{1 2 2 3 3 3 4 4 4 4 5 6 7}; my @array_3 = eigenstates( any(@array_2) != all(@array_1) ); my @array_4 = Quantum::Superpositions::eigenstates( any(@array_2) != a +ll(@array_1) ); print "Ordered:\n"; print "$_\n" for @array_3; print "Hash:\n"; print "$_\n" for @array_4; { no warnings 'redefine'; *eigenstates = *main::eigenstates; sub eigenstates ($) { my @uniq; my %seen; for ( Quantum::Superpositions::collapse( $_[0] ) ) { push @uniq , $_ if ! $seen{$_}; $seen{$_}++; } return @uniq; } }
        Cheers - L~R
        tommycahir,
        Ok, since I have already provided a way to do it using Quantum::Superpositions, I should point out that there certainly are simpler solutions. Since your goal is just to visit all routers, order really should not matter:
        my @visited = (1 .. 5); my %unvisited = map { $_ => undef } 1 .. 9; while ( %unvisited ) { delete @{unvisited}{@visited}; for ( keys %unvisited ) { visit( $_, \%unvisited ); # Adds routers to %unvisited push @visited , $_; } }
        Now if for some reason order is important to you, you can reverse the array/hash for the stack.
        my %visited = map { $_ => undef } (1 .. 4); my @unvisited = (1 .. 9); while ( @unvisited ) { my $router = shift @unvisited; next if exists $visited{$router}; visit ( $router, \@unvisited ); # Adds routers to @unvisited $visited{$router} = undef; }
        I hope these solutions are helpful.

        Cheers - L~R

Re: Quantum::Superpositions prob
by kvale (Monsignor) on Mar 25, 2004 at 15:03 UTC
    Here is a non-quantum solution:
    use warnings; use strict; my @a2 = qw/1 2 2 3 3 3 4 4 4 4 5 6 7/;#list of to_visit my @a1 = qw/1 2 3 4/;#list of visited my %visited; @visited{ @a1 } = 1; @a2 = grep {not exists $visited{ $_ }} @a2; print "list of to visit\n"; print_arrays(\@a2); print "\nlist visited\n"; print_arrays(\@a1); sub print_arrays { my $x = shift; for my $item1 ( @$x ) { print $item1,"\n"; } print "\n"; }
    For the quantum solution, just sort the array.

    -Mark

      i want to try to keep them in the original order that they were entered on to the list, thats why i thought that Quantum::superpositions might just remove the duplicate value
        Then go with my solution. The grep function preserves the array order.

        -Mark

Re: Quantum::Superpositions prob
by flyingmoose (Priest) on Mar 25, 2004 at 18:41 UTC
    Side-question: This is a very cool module, and I like the idea of being able to use any() and all() (which are also cool functional programming ideas), and it poses the theoretical questions of what would be possible with Quantum computing. My question is this -- is anyone using this for real world programs (business apps? phyics apps?). I'd love to hear what they are being used for. I'd use it more myself if it's approved for such usage! (Any drawbacks?)

    I also like the built in Heisenberg Uncertainty Principle in Quantum::Entanglement. Very very cool. That doesn't do what (I'm confused -- never mind -- but it's still cool!)

      Well, I don't know if you count this as a real-world app yet, but Perl 6 will support superpositions (known there as "junctions") out of the box. So you'll probably see lots of real programs eventually that say things like:
      if $x == 1 | 2 | 4 | 8 {...}
      without realizing that they've just generated and used an any(). (And TheDamian's forthcoming Perl6::Junctions module is just a reworking of Quantum::Superpositions without all the hifalutin' physics jargon, and with the lofalutin' infix junctional operators.)

      And before you ask, yes, we renamed the Perl 5 bitops to something else just so that we could steal them for junctions. That's how important we think they are.

        So that is apparently an 'any', so is 'all' still supported with Perl6 core? Can we still do cool stuff like if (any($h,$i,$j) < all ($x,$y,$z)) ? How does that look? I am supposing the junctions would not support arithmetic operations between junctions? (If there is an article or a usenet thread I should read, feel free to point me to it if this is complicated)

        Anyhow, color me excited!

      is anyone using this for real world programs

      It's useful for operations like calculating intersections and differences between sets. However, if you are crippled by a classical computer (I once built a quantum computer, but then I looked at it wrong and it turned into a cheeseburger) it can be VERY slow when dealing with anything beyond trivial amounts of data. It's certainly useful for prototyping, but I don't often let it go through into production code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://339747]
Approved by broquaint
Front-paged by broquaint
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-05-20 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?