pernod has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Tied and Entangled, Quantum fun.
by dragonchild (Archbishop) on Jul 25, 2003 at 15:17 UTC
    I think that you have done exactly what should've been done in that situation and written the smallest amount of code that you needed to write to solve the job within the requirements (additional developer requirements aside *grins*).

    Are you considering uploading this? If you are, I would suggest renaming it to Tie::Scalar::[Something Useful Here] so that it follows the standard naming convention(s).

    ++!

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Tied and Entangled, Quantum fun.
by Zaxo (Archbishop) on Jul 25, 2003 at 16:22 UTC

    It's too bad about losing Quantum::Cool, but Math::Random does exactly what you want. You can import random_normal(), random_chi_square(), and many more distribution functions.

    After Compline,
    Zaxo

Re: Tied and Entangled, Quantum fun.
by diotalevi (Canon) on Jul 25, 2003 at 16:52 UTC

    What are you buying with Quantum:: that I don't get with my plain $distribution->[ rand @$distribution ]? Outside of having a really bloated module?

    package Quaint; use strict; use Tie::Scalar; use Carp; use UNIVERSAL 'isa'; sub TIESCALAR { my ( $pck, @distribution ) = @_; croak "No distribution given." unless @distribution; bless { data => \ @distribution, state => $state }, $pck; } sub FETCH { my $self = shift; my $distribution = $self->{distribution}; $distribution->[ rand @$distribution ]; } sub STORE { my ( $self, $distref ) = @_; croak "No distribution given." unless isa($distref, "ARRAY"); my $orig_dist = $self->{'distribution'}; push @$orig_dist, @$distref; }
      What are you buying with Quantum:: that I don't get with my plain $distribution-> rand @$distribution ? Outside of having a really bloated module?

      Nothing, really. But does your version provide any other distributions than uniform? At first glance, it seems to me that your solution lacks the possibility to provide the frequency of the different elements. That can be worked around using the following (untested) code, though.

      My version:

      tie my $variable, "Quaint", ( 1 => "one", 2 => "two", 1 => "three" );

      Your version:

      tie my $variable, "Quaint",( "one", "two", "two", "three" );

      I happen to prefer the first notation, but that is merely a matter of taste. On the other hand, I think you loose the possibility to specify frequencies using complex number. I may be wrong on this one though, so I agree that the only thing I loose with your version is Quantum::Cool.

      Come to think of it, I can't use complex numbers to specify frequencies right now. Thank you for pointing it out for me :o)

      pernod
      --
      Mischief. Mayhem. Soap.

Re: Tied and Entangled, Quantum fun.
by halley (Prior) on Jul 25, 2003 at 19:15 UTC
    I haven't done this in Perl, and your code looks like a good implementation of a class I wrote (once upon a time) in Java. I called my class a "Loaded Die." Some games require dice with unusual numbers of faces, and some games require non-uniform affine probabilities. It had a set of possible face values, and an associated probability for each face.

    I agree, if you submit this as a full-fledged module to CPAN, call it something useful. I'd suggest Quantum::Quaint if you want to demonstrate its similarity to the other Quantum::* modules, or Tie::Scalar::Random or something that people would find with a search. CPAN's searches are disappointing but well-chosen names go a long way.

    --
    [ e d @ h a l l e y . c c ]

Re: Tied and Entangled, Quantum fun.
by chunlou (Curate) on Jul 25, 2003 at 17:28 UTC
    Is it possible that when we add or multiple two tied Quaint variables, it operates on the states, not the data? Right now, any operation will call FETCH and therefore will operate on the data, not the state. If we want to use it for modelling qubits, we need to operate on the states.

      I hadn't thought about that, but I'm not quite sure whether it really is the point of the module. It's function after all is to return random variables, not model qubits. I will think about it and see if it makes sense to change the requirements and add this functionality. I'll post any changes I come up with. Thank you very much for your interesting suggestion.

      My timezone has brought me to an afternoon tapas-party right now, so I can't follow up on this thread any more at the moment. Have a nice weekend, and thank you all for your valuable and thoughtful comments. Have a nice weekend :o)

      pernod
      --
      Mischief. Mayhem. Soap.