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