« blib/lib/Quantum/Superpositions/Lazy/Statistics.pm
↑ Summary
blib/lib/Quantum/Superpositions/Lazy/Util.pm »
| 1 | package Quantum::Superpositions::Lazy::Computation; | ||||||||||||||||||||||||||||
| 2 | 15 | $Quantum::Superpositions::Lazy::Computation::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::Operation::Computational; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Quantum::Superpositions::Lazy::ComputedState; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Quantum::Superpositions::Lazy::Util qw(is_collapsible get_iterator); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Types::Common::Numeric qw(PositiveNum); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use Types::Standard qw(ConsumerOf ArrayRef Str); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use List::Util qw(product); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 15 | with "Quantum::Superpositions::Lazy::Role::Collapsible"; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 15 | has "operation" => ( | |||||||||||||||||||||||||||
| 18 | is => "ro", | ||||||||||||||||||||||||||||
| 19 | isa => (ConsumerOf ["Quantum::Superpositions::Lazy::Role::Operation"]) | ||||||||||||||||||||||||||||
| 20 | ->plus_coercions(Str, | ||||||||||||||||||||||||||||
| 21 | q{Quantum::Superpositions::Lazy::Operation::Computational->new(sign => $_)} | ||||||||||||||||||||||||||||
| 22 | ), | ||||||||||||||||||||||||||||
| 23 | coerce => 1, | ||||||||||||||||||||||||||||
| 24 | required => 1, | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 15 | has "values" => ( | |||||||||||||||||||||||||||
| 28 | is => "ro", | ||||||||||||||||||||||||||||
| 29 | isa => ArrayRef->where(q{@$_ > 0}), | ||||||||||||||||||||||||||||
| 30 | required => 1, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 379 | ▶ | sub weight_sum { 1 } | ||||||||||||||||||||||||||
|
Subroutine
weight_sum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub collapse | ||||||||||||||||||||||||||||
| 36 | 30 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
collapse: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 30 | my ($self) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | my @members = map { | ||||||||||||||||||||||||||||
| 40 | 30 | ▶ | (is_collapsible $_) ? $_->collapse : $_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | } $self->values->@*; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 30 | return $self->operation->run(@members); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub is_collapsed | ||||||||||||||||||||||||||||
| 47 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_collapsed: called CC=4 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # a single uncollapsed state means that the computation | ||||||||||||||||||||||||||||
| 51 | # is not fully collapsed | ||||||||||||||||||||||||||||
| 52 | 2 | foreach my $member ($self->values->@*) { | |||||||||||||||||||||||||||
| 53 | 2 | ▶ | if (is_collapsible($member) && !$member->is_collapsed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
is_collapsible($member) and not $member->is_collapsed
MC/DC0%0/2
is_collapsible($member) and not $member->is_collapsed
is_collapsible($member)
not $member->is_collapsed
Truth table33%1/3
A and not(B)
A= is_collapsible($member)B= not $member->is_collapsed
| |||||||||||||||||||||||||||||
| 54 | 2 | return 0; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 0 | return 1; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub reset | ||||||||||||||||||||||||||||
| 61 | 11 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 11 | foreach my $member ($self->values->@*) { | |||||||||||||||||||||||||||
| 65 | 22 | ▶ | if (is_collapsible $member) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 22 | $member->reset; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 11 | return $self; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub _cartesian_product | ||||||||||||||||||||||||||||
| 74 | 53 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_cartesian_product: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 75 | 53 | my ($self, $input_states, $sourced) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 53 | my %states; | |||||||||||||||||||||||||||
| 78 | 53 | my $iterator = get_iterator $input_states->@*; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 53 | ▶ | while (my @params = $iterator->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 81065 | my @source = map { $_->[1] } @params; | |||||||||||||||||||||||||||
| 82 | 81065 | my $result = $self->operation->run(@source); | |||||||||||||||||||||||||||
| 83 | 81065 | my $probability = product map { $_->[0] } @params; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 81065 | ▶ | if (exists $states{$result}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 38348 | $states{$result}[0] += $probability; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | else { | ||||||||||||||||||||||||||||
| 89 | 42717 | $states{$result} = [ | |||||||||||||||||||||||||||
| 90 | $probability, | ||||||||||||||||||||||||||||
| 91 | $result, | ||||||||||||||||||||||||||||
| 92 | ]; | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 81065 | ▶ | if ($sourced) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 65104 | push $states{$result}[2]->@*, \@source; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 53 | return [values %states]; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub _build_complete_states | ||||||||||||||||||||||||||||
| 104 | 53 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_complete_states: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 105 | 53 | my ($self) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 53 | my @input_states; | |||||||||||||||||||||||||||
| 108 | 53 | for my $value ($self->values->@*) { | |||||||||||||||||||||||||||
| 109 | 109 | my $local_states; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 109 | ▶ | if (is_collapsible $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 84 | my $total = $value->weight_sum; | |||||||||||||||||||||||||||
| 113 | $local_states = [ | ||||||||||||||||||||||||||||
| 114 | map { | ||||||||||||||||||||||||||||
| 115 | 84 | [$_->weight / $total, $_->value] | |||||||||||||||||||||||||||
| 116 | } $value->states->@* | ||||||||||||||||||||||||||||
| 117 | ]; | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 25 | $local_states = [[1, $value]]; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 109 | push @input_states, $local_states; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 53 | my $sourced = $Quantum::Superpositions::Lazy::global_sourced_calculations; | |||||||||||||||||||||||||||
| 127 | 53 | my $states = $self->_cartesian_product(\@input_states, $sourced); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 53 | ▶ | if ($sourced) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | return [ | ||||||||||||||||||||||||||||
| 131 | map { | ||||||||||||||||||||||||||||
| 132 | 6 | ▶ | Quantum::Superpositions::Lazy::ComputedState->new( | ||||||||||||||||||||||||||
|
Condition33%1/3
$_->[2] // $_->[1]
MC/DC0%0/2
$_->[2] // $_->[1]
$_->[2]
$_->[1]
Truth table33%1/3
A // B
A= $_->[2]B= $_->[1]
| |||||||||||||||||||||||||||||
| 133 | weight => $_->[0], | ||||||||||||||||||||||||||||
| 134 | value => $_->[1], | ||||||||||||||||||||||||||||
| 135 | source => $_->[2] // $_->[1], | ||||||||||||||||||||||||||||
| 136 | operation => $self->operation, | ||||||||||||||||||||||||||||
| 137 | ) | ||||||||||||||||||||||||||||
| 138 | } $states->@* | ||||||||||||||||||||||||||||
| 139 | ]; | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | 47 | return $states; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 15 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||