| 1 | package Quantum::Superpositions::Lazy::Role::Collapsible; | ||||||||||||||
| 2 | 15 | $Quantum::Superpositions::Lazy::Role::Collapsible::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 Quantum::Superpositions::Lazy::Operation::Computational; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 15 | ▶ | use Quantum::Superpositions::Lazy::Operation::Logical; | ||||||||||||
|
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::State; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 15 | ▶ | use Quantum::Superpositions::Lazy::Statistics; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 15 | ▶ | use Types::Standard qw(ArrayRef InstanceOf); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 15 | ▶ | use List::Util qw(reduce); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 15 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 15 | ▶ | use Moo::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 15 | my %mathematical = map { $_ => 1 } | |||||||||||||
| 17 | Quantum::Superpositions::Lazy::Operation::Computational->supported_types; | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 15 | my %logical = map { $_ => 1 } | |||||||||||||
| 20 | Quantum::Superpositions::Lazy::Operation::Logical->supported_types; | ||||||||||||||
| 21 | |||||||||||||||
| 22 | sub create_computation | ||||||||||||||
| 23 | 158 | ▶ | { | ||||||||||||
|
Subroutine
create_computation: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 158 | my ($type, @args) = @_; | |||||||||||||
| 25 | |||||||||||||||
| 26 | 158 | return Quantum::Superpositions::Lazy::Computation->new( | |||||||||||||
| 27 | operation => $type, | ||||||||||||||
| 28 | values => [@args], | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | sub create_logic | ||||||||||||||
| 33 | { | ||||||||||||||
| 34 | 93 | my ($type, @args) = @_; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 80 | my $op = Quantum::Superpositions::Lazy::Operation::Logical->new( | |||||||||||||
| 37 | sign => $type, | ||||||||||||||
| 38 | ); | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 13 | ▶ | if ($Quantum::Superpositions::Lazy::global_compare_bool) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | return $op->run(@args); | ||||||||||||||
| 42 | } | ||||||||||||||
| 43 | else { | ||||||||||||||
| 44 | return $op->valid_states(@args); | ||||||||||||||
| 45 | } | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | sub _operate | ||||||||||||||
| 49 | 151 | ▶ | { | ||||||||||||
|
Subroutine
_operate: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | 151 | my (@args) = @_; | |||||||||||||
| 51 | |||||||||||||||
| 52 | 151 | my $type = pop @args; | |||||||||||||
| 53 | |||||||||||||||
| 54 | 151 | my $self = shift @args; | |||||||||||||
| 55 | 151 | return $self->operate($type, @args); | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 15 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 59 | |||||||||||||||
| 60 | 15 | requires qw( | |||||||||||||
| 61 | collapse | ||||||||||||||
| 62 | is_collapsed | ||||||||||||||
| 63 | _build_complete_states | ||||||||||||||
| 64 | weight_sum | ||||||||||||||
| 65 | reset | ||||||||||||||
| 66 | ); | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 15 | has "_complete_states" => ( | |||||||||||||
| 69 | is => "ro", | ||||||||||||||
| 70 | isa => ArrayRef [ | ||||||||||||||
| 71 | (InstanceOf ["Quantum::Superpositions::Lazy::State"]) | ||||||||||||||
| 72 | ->plus_coercions( | ||||||||||||||
| 73 | ArrayRef->where(q{@$_ == 2}), | ||||||||||||||
| 74 | q{ Quantum::Superpositions::Lazy::State->new(weight => shift @$_, value => shift @$_) }, | ||||||||||||||
| 75 | ) | ||||||||||||||
| 76 | ], | ||||||||||||||
| 77 | lazy => 1, | ||||||||||||||
| 78 | coerce => 1, | ||||||||||||||
| 79 | builder => "_build_complete_states", | ||||||||||||||
| 80 | clearer => "clear_states", | ||||||||||||||
| 81 | init_arg => undef, | ||||||||||||||
| 82 | ); | ||||||||||||||
| 83 | |||||||||||||||
| 84 | has "stats" => ( | ||||||||||||||
| 85 | is => "ro", | ||||||||||||||
| 86 | isa => InstanceOf ["Quantum::Superpositions::Lazy::Statistics"], | ||||||||||||||
| 87 | lazy => 1, | ||||||||||||||
| 88 | 6 | ▶ | default => sub { $Quantum::Superpositions::Lazy::Statistics::implementation->new(parent => shift) }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 89 | 15 | init_arg => undef, | |||||||||||||
| 90 | clearer => "_clear_stats", | ||||||||||||||
| 91 | ); | ||||||||||||||
| 92 | |||||||||||||||
| 93 | sub states | ||||||||||||||
| 94 | 279 | ▶ | { | ||||||||||||
|
Subroutine
states: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 95 | 279 | my ($self) = @_; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 279 | return $self->_complete_states; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | sub stringify | ||||||||||||||
| 101 | 1 | ▶ | { | ||||||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 102 | 1 | my ($self) = @_; | |||||||||||||
| 103 | 1 | return $self->collapse; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | sub operate | ||||||||||||||
| 107 | 158 | ▶ | { | ||||||||||||
|
Subroutine
operate: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||
| 108 | 158 | my ($self, $type, @args) = @_; | |||||||||||||
| 109 | |||||||||||||||
| 110 | 158 | unshift @args, $self; | |||||||||||||
| 111 | 158 | my $order = pop @args; | |||||||||||||
| 112 | 158 | ▶ | @args = reverse @args | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | if $order; | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 158 | ▶ | if ($mathematical{$type}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 116 | 65 | return create_computation $type, @args; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | elsif ($logical{$type}) { | ||||||||||||||
| 120 | 93 | return create_logic $type, @args; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | else { | ||||||||||||||
| 124 | 0 | croak "quantum operator $type is not supported"; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | sub transform | ||||||||||||||
| 129 | 3 | ▶ | { | ||||||||||||
|
Subroutine
transform: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 130 | 3 | my ($self, $coderef, @more) = @_; | |||||||||||||
| 131 | |||||||||||||||
| 132 | 3 | return $self->operate("_transform", $coderef, @more, undef); | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | sub compare | ||||||||||||||
| 136 | 4 | ▶ | { | ||||||||||||
|
Subroutine
compare: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 137 | 4 | my ($self, $coderef, @more) = @_; | |||||||||||||
| 138 | |||||||||||||||
| 139 | 4 | return $self->operate("_compare", $coderef, @more, undef); | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | sub to_ket_notation | ||||||||||||||
| 143 | 3 | ▶ | { | ||||||||||||
|
Subroutine
to_ket_notation: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 144 | 3 | my ($self) = @_; | |||||||||||||
| 145 | |||||||||||||||
| 146 | return join " + ", map { | ||||||||||||||
| 147 | 3 | ($_->weight / $self->weight_sum) . "|" . | |||||||||||||
| 148 | $_->value . ">" | ||||||||||||||
| 149 | } $self->states->@*; | ||||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | use overload | ||||||||||||||
| 153 | 15 | ▶ | q{nomethod} => \&_operate, | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 154 | q{fallback} => 0, | ||||||||||||||
| 155 | |||||||||||||||
| 156 | 13 | ▶ | q{=} => sub { shift }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 157 | 15 | q{""} => \&stringify, | |||||||||||||
| 158 | 15 | ; | |||||||||||||
| 159 | |||||||||||||||
| 160 | 15 | 1; | |||||||||||||
| 161 | |||||||||||||||