File Coverage

blib/lib/Quantum/Superpositions/Lazy/ComputedState.pm
Criterion Covered Total %
statement 22 26 84.6
branch 0 4 0.0
condition n/a
subroutine 8 9 88.8
pod 2 2 100.0
total 32 41 78.0


line stmt bran cond sub pod time code
1             package Quantum::Superpositions::Lazy::ComputedState;
2              
3             our $VERSION = '1.11';
4              
5 15     15   184 use v5.24;
  15         48  
6 15     15   72 use warnings;
  15         31  
  15         351  
7 15     15   68 use Moo;
  15         29  
  15         88  
8 15     15   4338 use Quantum::Superpositions::Lazy::Role::Operation;
  15         33  
  15         645  
9 15     15   157 use Types::Standard qw(ConsumerOf ArrayRef);
  15         39  
  15         123  
10 15     15   8853 use Carp qw(croak);
  15         43  
  15         696  
11              
12 15     15   90 use namespace::clean;
  15         35  
  15         140  
13              
14             extends "Quantum::Superpositions::Lazy::State";
15              
16             has "source" => (
17             is => "ro",
18             isa => ArrayRef,
19             required => 1,
20             );
21              
22             has "operation" => (
23             is => "ro",
24             isa => ConsumerOf ["Quantum::Superpositions::Lazy::Role::Operation"],
25             required => 1,
26             );
27              
28             sub clone
29             {
30 3     3 1 9 my ($self) = @_;
31              
32             return $self->new(
33 3         61 $self->%{qw(value weight source operation)}
34             );
35             }
36              
37             # TODO: allow merging with regular states
38             sub merge
39             {
40 0     0 1   my ($self, $with) = @_;
41              
42 0 0         croak "cannot merge a state: values mismatch"
43             if $self->value ne $with->value;
44 0 0         croak "cannot merge a state: operation mismatch"
45             if $self->operation->sign ne $with->operation->sign;
46              
47 0           return $self->new(
48             weight => $self->weight + $with->weight,
49             operation => $self->operation,
50             value => $self->value,
51             source => [$self->source->@*, $with->source->@*],
52             );
53             }
54              
55             1;
56              
57             =head1 NAME
58              
59             Quantum::Superpositions::Lazy::ComputedState - a weighted state implementation
60             with the source of the computation
61              
62             =head1 DESCRIPTION
63              
64             This is a subclass of L with extra fields
65             that allow tracking of the computation sources that produced the state. Objects
66             of this class are produced inside the
67             L block.
68              
69             =head1 METHODS
70              
71             All of the methods available in L, plus:
72              
73             =head2 operation
74              
75             Instance of a class consuming the
76             L role. This can be helpful to
77             determine what kind of operation was performed to obtain the state.
78              
79             =head2 source
80              
81             An array reference of state values that were used in the operation (in order).
82             The number of elements in the arrayref will depend of the operation type.