File Coverage

blib/lib/Quantum/Superpositions/Lazy/Role/Collapsible.pm
Criterion Covered Total %
statement 61 62 98.3
branch 5 6 83.3
condition n/a
subroutine 21 21 100.0
pod 0 6 0.0
total 87 95 91.5


line stmt bran cond sub pod time code
1             package Quantum::Superpositions::Lazy::Role::Collapsible;
2             $Quantum::Superpositions::Lazy::Role::Collapsible::VERSION = '1.12';
3 15     15   7515 use v5.24;
  15         50  
4 15     15   74 use warnings;
  15         42  
  15         387  
5 15     15   72 use Quantum::Superpositions::Lazy::Operation::Computational;
  15         27  
  15         252  
6 15     15   5801 use Quantum::Superpositions::Lazy::Operation::Logical;
  15         46  
  15         496  
7 15     15   102 use Quantum::Superpositions::Lazy::Computation;
  15         31  
  15         302  
8 15     15   76 use Quantum::Superpositions::Lazy::State;
  15         23  
  15         266  
9 15     15   6644 use Quantum::Superpositions::Lazy::Statistics;
  15         50  
  15         545  
10 15     15   118 use Types::Standard qw(ArrayRef InstanceOf);
  15         32  
  15         133  
11 15     15   8805 use List::Util qw(reduce);
  15         31  
  15         897  
12 15     15   87 use Carp qw(croak);
  15         29  
  15         638  
13              
14 15     15   89 use Moo::Role;
  15         29  
  15         108  
15              
16             my %mathematical = map { $_ => 1 }
17             Quantum::Superpositions::Lazy::Operation::Computational->supported_types;
18              
19             my %logical = map { $_ => 1 }
20             Quantum::Superpositions::Lazy::Operation::Logical->supported_types;
21              
22             sub create_computation
23             {
24             my ($type, @args) = @_;
25              
26             return Quantum::Superpositions::Lazy::Computation->new(
27             operation => $type,
28             values => [@args],
29             );
30             }
31              
32             sub create_logic
33             {
34             my ($type, @args) = @_;
35              
36             my $op = Quantum::Superpositions::Lazy::Operation::Logical->new(
37             sign => $type,
38             );
39              
40             if ($Quantum::Superpositions::Lazy::global_compare_bool) {
41             return $op->run(@args);
42             }
43             else {
44             return $op->valid_states(@args);
45             }
46             }
47              
48             sub _operate
49             {
50 151     151   31278 my (@args) = @_;
51              
52 151         294 my $type = pop @args;
53              
54 151         251 my $self = shift @args;
55 151         399 return $self->operate($type, @args);
56             }
57              
58 15     15   8842 use namespace::clean;
  15         42  
  15         120  
59              
60             requires qw(
61             collapse
62             is_collapsed
63             _build_complete_states
64             weight_sum
65             reset
66             );
67              
68             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             default => sub { $Quantum::Superpositions::Lazy::Statistics::implementation->new(parent => shift) },
89             init_arg => undef,
90             clearer => "_clear_stats",
91             );
92              
93             sub states
94             {
95 279     279 0 29075 my ($self) = @_;
96              
97 279         4600 return $self->_complete_states;
98             }
99              
100             sub stringify
101             {
102 1     1 0 84 my ($self) = @_;
103 1         4 return $self->collapse;
104             }
105              
106             sub operate
107             {
108 158     158 0 368 my ($self, $type, @args) = @_;
109              
110 158         325 unshift @args, $self;
111 158         256 my $order = pop @args;
112 158 100       401 @args = reverse @args
113             if $order;
114              
115 158 100       512 if ($mathematical{$type}) {
    50          
116 65         165 return create_computation $type, @args;
117             }
118              
119             elsif ($logical{$type}) {
120 93         206 return create_logic $type, @args;
121             }
122              
123             else {
124 0         0 croak "quantum operator $type is not supported";
125             }
126             }
127              
128             sub transform
129             {
130 3     3 0 296 my ($self, $coderef, @more) = @_;
131              
132 3         11 return $self->operate("_transform", $coderef, @more, undef);
133             }
134              
135             sub compare
136             {
137 4     4 0 21 my ($self, $coderef, @more) = @_;
138              
139 4         11 return $self->operate("_compare", $coderef, @more, undef);
140             }
141              
142             sub to_ket_notation
143             {
144 3     3 0 398 my ($self) = @_;
145              
146             return join " + ", map {
147 3         14 ($_->weight / $self->weight_sum) . "|" .
  6         189  
148             $_->value . ">"
149             } $self->states->@*;
150             }
151              
152             use overload
153             q{nomethod} => \&_operate,
154             q{fallback} => 0,
155              
156 13     13   2796 q{=} => sub { shift },
157 15         135 q{""} => \&stringify,
158 15     15   11459 ;
  15         44  
159              
160             1;
161