| 1 | package Quantum::Superpositions::Lazy::Superposition; | ||||||||||||||||||||||||||||
| 2 | 15 | $Quantum::Superpositions::Lazy::Superposition::VERSION = '1.12'; | |||||||||||||||||||||||||||
| 3 | 15 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Quantum::Superpositions::Lazy::State; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Quantum::Superpositions::Lazy::Computation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Quantum::Superpositions::Lazy::Util qw(get_rand); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Types::Standard qw(ArrayRef InstanceOf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use List::Util qw(sum0); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 15 | with "Quantum::Superpositions::Lazy::Role::Collapsible"; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 15 | has "_collapsed_state" => ( | |||||||||||||||||||||||||||
| 17 | is => "ro", | ||||||||||||||||||||||||||||
| 18 | lazy => 1, | ||||||||||||||||||||||||||||
| 19 | builder => "_observe", | ||||||||||||||||||||||||||||
| 20 | clearer => "_reset", | ||||||||||||||||||||||||||||
| 21 | predicate => "_is_collapsed", | ||||||||||||||||||||||||||||
| 22 | init_arg => undef, | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 15 | has "_states" => ( | |||||||||||||||||||||||||||
| 26 | is => "ro", | ||||||||||||||||||||||||||||
| 27 | isa => ArrayRef [ | ||||||||||||||||||||||||||||
| 28 | (InstanceOf ["Quantum::Superpositions::Lazy::State"]) | ||||||||||||||||||||||||||||
| 29 | ->plus_coercions( | ||||||||||||||||||||||||||||
| 30 | ArrayRef->where(q{@$_ == 2}), | ||||||||||||||||||||||||||||
| 31 | q{ Quantum::Superpositions::Lazy::State->new(weight => shift @$_, value => shift @$_) }, | ||||||||||||||||||||||||||||
| 32 | ~InstanceOf ["Quantum::Superpositions::Lazy::State"], | ||||||||||||||||||||||||||||
| 33 | q{ Quantum::Superpositions::Lazy::State->new(value => $_) }, | ||||||||||||||||||||||||||||
| 34 | ) | ||||||||||||||||||||||||||||
| 35 | ], | ||||||||||||||||||||||||||||
| 36 | coerce => 1, | ||||||||||||||||||||||||||||
| 37 | required => 1, | ||||||||||||||||||||||||||||
| 38 | init_arg => "states", | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has "_weight_sum" => ( | ||||||||||||||||||||||||||||
| 42 | is => "ro", | ||||||||||||||||||||||||||||
| 43 | lazy => 1, | ||||||||||||||||||||||||||||
| 44 | 60 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 60 | sum0 map { $_->weight } | |||||||||||||||||||||||||||
| 46 | shift->_states->@*; | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | 15 | init_arg => undef, | |||||||||||||||||||||||||||
| 49 | clearer => 1, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub collapse | ||||||||||||||||||||||||||||
| 53 | 194 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
collapse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 194 | my ($self) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 194 | return $self->_collapsed_state; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub is_collapsed | ||||||||||||||||||||||||||||
| 60 | 209 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_collapsed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 209 | my ($self) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 209 | return $self->_is_collapsed; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub weight_sum | ||||||||||||||||||||||||||||
| 67 | 122 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
weight_sum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 122 | my ($self) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 122 | return $self->_weight_sum; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub reset | ||||||||||||||||||||||||||||
| 74 | 131 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 131 | my ($self) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 131 | ▶ | if (!$self->{_collapsible}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 21 | $self->{_collapsible} = [grep { $_->collapsible } $self->_states->@*]; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 131 | foreach my $state ($self->{_collapsible}->@*) { | |||||||||||||||||||||||||||
| 82 | 1 | $state->reset; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 131 | $self->_reset; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 131 | return $self; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub _observe | ||||||||||||||||||||||||||||
| 91 | 154 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_observe: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||
| 92 | 154 | my ($self) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 154 | ▶ | if (!$self->{_lookup}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 28 | my @positions = $self->_states->@*; | |||||||||||||||||||||||||||
| 96 | 28 | my $sum = $self->weight_sum; | |||||||||||||||||||||||||||
| 97 | 28 | my @weights; | |||||||||||||||||||||||||||
| 98 | 28 | my $current = 0; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 28 | for my $state (@positions) { | |||||||||||||||||||||||||||
| 101 | 344 | push @weights, $current; | |||||||||||||||||||||||||||
| 102 | 344 | $current += $state->weight / $sum; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 28 | $self->{_lookup} = \@weights; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 154 | my $prob = get_rand; | |||||||||||||||||||||||||||
| 109 | 154 | my @cache = $self->{_lookup}->@*; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 154 | ▶ | return undef if !@cache; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 152 | my $current = int(@cache / 2); | |||||||||||||||||||||||||||
| 114 | 152 | ▶ | my $step = int(@cache / 4) || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
int @cache / 4 || 1
MC/DC100%1/1
int @cache / 4 || 1
int @cache / 4
Truth table100%2/2
A
A= int @cache / 4
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # warn "rand: $prob, all: [@cache], step: $step"; | ||||||||||||||||||||||||||||
| 117 | 152 | ▶ | while ($step > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 432 | ▶ | if ($cache[$current] < $prob) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | # warn "going up: $current + $step"; | ||||||||||||||||||||||||||||
| 120 | 213 | $current += $step; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 213 | ▶ | $current = @cache - 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | if $current >= @cache; | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||||
| 127 | # warn "going down $current - $step"; | ||||||||||||||||||||||||||||
| 128 | 219 | $current -= $step; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 219 | ▶ | $current = 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | if $current < 0; | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 432 | $step = int($step / 2); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 152 | ▶ | while ($current < $#cache && $cache[$current] < $prob) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$current < $#cache and $cache[$current] < $prob
MC/DC100%2/2
$current < $#cache and $cache[$current] < $prob
$current < $#cache
$cache[$current] < $prob
Truth table100%3/3
A and B
A= $current < $#cacheB= $cache[$current] < $prob
| |||||||||||||||||||||||||||||
| 138 | 126 | $current += 1 | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 152 | ▶ | while ($current > 0 && $cache[$current] > $prob) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$current > 0 and $cache[$current] > $prob
MC/DC100%2/2
$current > 0 and $cache[$current] > $prob
$current > 0
$cache[$current] > $prob
Truth table100%3/3
A and B
A= $current > 0B= $cache[$current] > $prob
| |||||||||||||||||||||||||||||
| 142 | 203 | $current -= 1 | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # warn "selected: $current ($cache[$current] < $prob)"; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 152 | my $state = $self->_states->[$current]; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 152 | ▶ | return $state->collapsible | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | ? $state->value->collapse | ||||||||||||||||||||||||||||
| 151 | : $state->value; | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub _build_complete_states | ||||||||||||||||||||||||||||
| 155 | 66 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_complete_states: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 156 | 66 | my ($self) = @_; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 66 | my %states; | |||||||||||||||||||||||||||
| 159 | 66 | for my $state ($self->_states->@*) { | |||||||||||||||||||||||||||
| 160 | 16149 | my @local_states; | |||||||||||||||||||||||||||
| 161 | 16149 | my $coeff = 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 16149 | my $value = $state->value; | |||||||||||||||||||||||||||
| 164 | 16149 | ▶ | if ($state->collapsible) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # all values from this state must have their weights multiplied by $coeff | ||||||||||||||||||||||||||||
| 167 | # this way the weight sum will stay the same | ||||||||||||||||||||||||||||
| 168 | 1 | $coeff = $state->weight / $value->weight_sum; | |||||||||||||||||||||||||||
| 169 | 1 | @local_states = $value->states->@*; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||
| 172 | 16148 | @local_states = $state; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 16149 | foreach my $value (@local_states) { | |||||||||||||||||||||||||||
| 176 | 16153 | my $result = $value->value; | |||||||||||||||||||||||||||
| 177 | 16153 | ▶ | my $copied = $value->clone_with(weight => sub { shift() * $coeff }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 16153 | ▶ | if (exists $states{$result}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 1 | $states{$result} = $states{$result}->merge($copied); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | else { | ||||||||||||||||||||||||||||
| 183 | 16152 | $states{$result} = $copied; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 66 | return [values %states]; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 15 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||