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