Esteemed Monks,

Following an earlier question on Quantum::Entanglement, I was pointed to Quantum::Superpositions. I downloaded the module, read the documentation and tested it, but I'm still missing something, methinks.

What I want to do is to create a variable that will return a different value every time I access it. The value shall be selected from a distribution (for example: normal, chi squared), and this distribution will be a parameter for the variable constructor. I know there are tons of ways to do this, but my requirement has been that it should use a Quantum module, because they are cool :o) Efficiency is not really an issue here, as the program is neither too big, nor complex.

Anyhow, Quantum::Entanglement fits the bill rather nicely. Except that at the first observation of the variable, it collapses to one value and stays that way for the rest of its existence. Quantum::Superpositions has the eigenstates method, that will return the list of all the possible states of the variable. The problem here is that it lacks the functionality to add a distribution. This can be remedied by using Math::Random, but that takes away som of the magic that the Quantum modules provides.

In the end, I created a tied scalar that encapsulates a Quantum::Entanglement object, that uses the save_state and restore_state methods to produce a different value each time, given the distribution of the tied Quantum::Entanglement-object.

I would appreciate comments on the tied scalar given below, as this is both my first tied class and a solution that pleases me. I would appreciate pointers to alternative solutions using other techniques and modules, and solutions not using Quantum are interesting too. I have thought of using Math::Random and methods like random_normal, is this a good way to go forward, perhaps? Thoughts? Comments? Flames for posting a narrow question that only accepts the answer I want?

package Quaint; use strict; require Tie::Scalar; use Carp; use Quantum::Entanglement; sub TIESCALAR { my ( $pck, @distribution ) = @_; croak "No distribution given." unless @distribution; my $variable = entangle( @distribution ); my $state = $variable -> save_state(); return bless ( { "data" => $variable, "state" => $state }, $pck ); } sub FETCH { my $self = shift; my $variable = $self -> { "data" }; $self -> { "data" } = $self -> { "state" } -> restore_state(); return $variable; } sub STORE { my ( $self, $distref ) = @_; croak "No distribution given." unless ref $distref eq "ARRAY"; my $variable = entangle( @{ $distref } ); $self -> { "state" } = $variable -> save_state(); $self -> { "data" } = $variable; } 1;

Usage:

tie my $duration, "Quaint", ( 1 => "qn", 1 => "en", 2 => "sn", 4 => "den", 3 => "tsn" ); print $duration, "\n"; print $duration, "\n"; print $duration, "\n";

Have a marvelous day.

pernod
--
Mischief. Mayhem. Soap.


In reply to Tied and Entangled, Quantum fun. by pernod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.