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.11';
4              
5 15     15   7599 use v5.24;
  15         51  
6 15     15   75 use warnings;
  15         32  
  15         381  
7 15     15   67 use Quantum::Superpositions::Lazy::Operation::Computational;
  15         30  
  15         282  
8 15     15   5840 use Quantum::Superpositions::Lazy::Operation::Logical;
  15         46  
  15         489  
9 15     15   102 use Quantum::Superpositions::Lazy::Computation;
  15         24  
  15         297  
10 15     15   69 use Quantum::Superpositions::Lazy::State;
  15         29  
  15         245  
11 15     15   6856 use Quantum::Superpositions::Lazy::Statistics;
  15         49  
  15         548  
12 15     15   114 use Types::Standard qw(ArrayRef InstanceOf);
  15         33  
  15         125  
13 15     15   8697 use List::Util qw(reduce);
  15         37  
  15         943  
14 15     15   93 use Carp qw(croak);
  15         35  
  15         704  
15              
16 15     15   84 use Moo::Role;
  15         28  
  15         111  
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   31147 my (@args) = @_;
53              
54 151         290 my $type = pop @args;
55              
56 151         278 my $self = shift @args;
57 151         395 return $self->operate($type, @args);
58             }
59              
60 15     15   8709 use namespace::clean;
  15         45  
  15         130  
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 279     279 0 33400 my ($self) = @_;
98              
99 279         4635 return $self->_complete_states;
100             }
101              
102             sub stringify
103             {
104 1     1 0 113 my ($self) = @_;
105 1         6 return $self->collapse;
106             }
107              
108             sub operate
109             {
110 158     158 0 387 my ($self, $type, @args) = @_;
111              
112 158         321 unshift @args, $self;
113 158         267 my $order = pop @args;
114 158 100       387 @args = reverse @args
115             if $order;
116              
117 158 100       547 if ($mathematical{$type}) {
    50          
118 65         158 return create_computation $type, @args;
119             }
120              
121             elsif ($logical{$type}) {
122 93         211 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 350 my ($self, $coderef, @more) = @_;
133              
134 3         16 return $self->operate("_transform", $coderef, @more, undef);
135             }
136              
137             sub compare
138             {
139 4     4 0 23 my ($self, $coderef, @more) = @_;
140              
141 4         12 return $self->operate("_compare", $coderef, @more, undef);
142             }
143              
144             sub to_ket_notation
145             {
146 3     3 0 393 my ($self) = @_;
147              
148             return join " + ", map {
149 3         12 ($_->weight / $self->weight_sum) . "|" .
  6         176  
150             $_->value . ">"
151             } $self->states->@*;
152             }
153              
154             use overload
155             q{nomethod} => \&_operate,
156             q{fallback} => 0,
157              
158 13     13   2887 q{=} => sub { shift },
159 15         152 q{""} => \&stringify,
160 15     15   11653 ;
  15         47  
161              
162             1;