Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Nested Quantum::Superpositions

by welchavw (Pilgrim)
on Nov 06, 2003 at 20:24 UTC ( #305142=perlquestion: print w/replies, xml ) Need Help??
welchavw has asked for the wisdom of the Perl Monks concerning the following question:

Could someone please explain the output to a new user of Quantum::Superpositions...thanks...

(all(1) == any(all(1), all(2), all(3))) and print "y1\n"; (all(1,4) == any(all(1,4), all(2), all(3))) and print "y2\n"; __OUTPUT__ y1


Replies are listed 'Best First'.
Re: Nested Quantum::Superpositions
by gjb (Vicar) on Nov 06, 2003 at 20:53 UTC

    any(all(1,4)) evaluates to any() since in scalar context there's nothing that can evaluate to all(1,4), i.e. to 1 and 4 simulataneously (any is conjunctive).

    I've just tried out the example in de docs, and is doesn't seem to work:

    my @features = ("tall", "handsome", "rich"); my $ideal = any(all("tall", "handsome", "rich" all("rich", "old"), all("smart", "rich", "Australian")); if (any(@features) eq $ideal) { print "success\n"; } else { print "failure\n"; } print any(@features), "\n"; print $ideal, "\n"; __END__ failure any(handsome,tall,rich) any()
    Changing any to all for @features doesn't help matters.
    Something's rotten in the Kingdom of Denmark...

    I think we'll need Damian on this, -gjb-

    Update: Removed the strike-through of the explanation since it seems to be correct after all. The docs are probably wrong.

      Thanks for the reply. I'm not sure my grok meter is full yet, though.

      Here's a snippet from the docs...

      More interestingly, since the individual states of a superposition are scalar values and a superposition is itself a scalar value, a superposition may have states that are themselves superpositions:

      $ideal = any( all("tall", "rich", "handsome"), all("rich", "old"), all("smart","Australian","rich") );

      Operations involving such a composite superposition operate recursively and in parallel on each its states individually and then recompose the result. For example:

      while (@features = get_description) { if (any(@features) eq $ideal) { print "True love"; } }

      The any(@features) eq $ideal equality is true if the input characteristics collectively match any of the three superimposed conjunctive superpositions. That is, if the characteristics collectively equate to each of "tall" and "rich" and "handsome", or to both "rich" and "old", or to all three of "smart" and "Australian" and "rich".

      So...I guess I thought that the outer any would distribute across the results of the inner alls without affecting their meaning (if that makes "any" sense)? Maybe I just really don't understand the docs example.


      Notice that he never says exactly what goes into @features. You need to put something in which can equal 2 or 3 things at the same time. Interesting, this succeeds:
      if (any(any("tall", "rich")) eq all("tall", "rich")) { print "yes\n"; }
      But this doesn't:
      if (any(any("tall", "rich")) eq any(all("ugly"), all("tall", "rich"))) + { print "yes\n"; }
      Methinks there's a bug lurking in there, but then, you were warned
Re: Nested Quantum::Superpositions
by tilly (Archbishop) on Nov 07, 2003 at 00:36 UTC
    Allow me to clarify runrig's answer in case it didn't make sense to you. all(1,4) == all(1,4) would be true if 1 == all(1,4) and 4 == all(1,4).

    For the first statement to be true we must have 1 == 1 and 1 == 4 Since 1 is not 4, the first one is false.

    The second one is false for the same reason. A fact that we didn't actually need to check since the failure of either the first or the second statement is enough to make the overall comparison fail.

    Therefore in quantum logic, all(1,4) != all(1,4), no matter how much that violates your classical prejudices.

    And that is why the match that you expected to find in the more complex example, wasn't found.

    UPDATE Here is a simple example to make you think that the way that Quantum::Superpositions works is reasonable. Just read the statement all(1, 2, 3) < all(4, 5, 6) as plain English. All of (1, 2, 3) are less than all of (4, 5, 6). Which is true. So the code evaluates to true. By contrast all(1, 2, 4) < all(3, 5, 6) says that All of (1, 2, 4) are less than all of (3, 5, 6). Which is false because 4 is not less than 3. And so the code evaluates to false.

    And this is exactly how Quantum::Superpositions works. When you read all(1,4) == all(1,4), don't think of all(1,4) as a "thing" in and of itself. It isn't. Think of the whole statement as being a sentence asserting something, and think about whether that assertion is correct.

    Yes, at some level all(1,4) has to be a single Perl scalar. But the illusion is that it isn't, and with TheDamian doing the illusioning, you can bet that it is a pretty darned good illusion!

      Ok. Realizing that all(1,4)!= all(1,4) helps me a bunch - thanks to all posters. As a followup, it seems to me that the docs must be in error when they imply that the following code could ever work...

      $ideal = any( all("tall", "rich", "handsome"), all("rich", "old"), all("smart","Australian","rich") ); while (@features = get_description) { if (any(@features) eq $ideal) { print "True love"; } }

      After all, I think that it has been illustrated that an "n-all" where n>1 cannot eq anything. Am I wrong?

      update: Of course, I am wrong about this. I must have had those Friday-morning blinders on. An "n-all" cannot eq an "n-all" of the same order (update: except if ni=C for 0<=i<=ordern-1 (e.g. all(1,1,1) == all(1,1,1)), but that doesn't mean that an any can't match "n-all" of order > 1. My bad.

      upupdate: Fantastic! Now, I actually understand the Q::S operators. Many thanks to Tilly (and also for reporting the docs bug). I really had to struggle to get used to distributing the operator joining each composite superposition across the cross-products (or something like that!). Then, everything clicked (here's to hoping it stays clicked). Q::S is really a neat package, IMHO, but a bit of a mind-bender initially.


        Q::S is really a neat package, IMHO, but a bit of a mind-bender initially.
        Obviously for their inventor too, since he apparently couldn't even get a simple example right in the docs! ;-)

        I'm sure the module's new maintainer will update them accordingly for the next release.

        Meanwile I do appreciate the bug report. One of the projects I'm currently working on is finalizing the semantics of Perl 6 junctions, which are an evolution of quantum superpositions. One of my goals is to simplify those semantics to make them a little more intuitive/predictable, and a lot less mind-bending.

        Being able to watch your learning process in this thread has been of real value to me in doing that. Thank-you.

        And thanks to tilly for the usual masterful explanations, for the bug report, and especially for drawing my (all too thinly spread) attention to this thread.

        Let's try it.
        use Quantum::Superpositions; my $all = all("smart","Australian","rich"); my $any = any("smart","Australian","rich"); print "Yes 1\n" if $all eq $any; print "Yes 2\n" if $any eq $all; __END__ Yes 1
        Reading this as I suggested above explains the result. Unfortunately the documentation doesn't clarify this important point, and worse yet, gets it backwards in its example. I'll send a bug report to the current maintainer (Steven Lembark).

        UPDATE: Reported. (BTW I find it charming that "True love" is impossible to find...)

Re: Nested Quantum::Superpositions
by runrig (Abbot) on Nov 07, 2003 at 00:02 UTC
    Without ever having used that module extensively, my thinking is that for your second example, you want this:
    (all(1,4) == any(any(1,4), any(2), any(3))) and print "y2\n";
    all(1,4) can not equal all(1,4) because that would mean 1 equals 4, which is obviously not true.

    Update: After rereading your post, I'm thinking that the above is probably not what you want either. I don't think this is correct:

    use Quantum::Superpositions; if ( any("tall", "rich", "handsome") eq all('tall', 'rich', 'handsome' +) ) { print "match1\n"; } if ( all("tall", "rich", "handsome") eq any('tall', 'rich', 'handsome' +) ) { print "match2\n"; } #prints: match2
    Update: I think I understand the example in the docs now(posted by welchav above), and the reason it doesn't seem to work is that you can't use any simple Q::S object to equal any of those feature sets. No one simple thing can equal 'all' of "tall", "rich", and "handsome". You need some special object which can equal all of those things at once. And so I'm not sure if the results above are correct, or a bug. I don't know if 'eq' in Q::S is supposed to be commutative.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-06-26 23:27 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (595 votes). Check out past polls.