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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |