-
Notifications
You must be signed in to change notification settings - Fork 55
/
opcreps.cpp
842 lines (688 loc) · 29.2 KB
/
opcreps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
#define NULL 0
#include <iostream>
#include <complex>
#include <assert.h>
#include <algorithm> // std::find
#include "statecreps.h"
#include "opcreps.h"
//#include <pthread.h>
//using namespace std::complex_literals;
//#define DEBUG(x) x
#define DEBUG(x)
namespace CReps_statevec {
/****************************************************************************\
|* OpCRep *|
\****************************************************************************/
OpCRep::OpCRep(INT dim) {
_dim = dim;
}
OpCRep::~OpCRep() { }
StateCRep* OpCRep::acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
return this->acton(state, out_state);
}
StateCRep* OpCRep::adjoint_acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
return this->adjoint_acton(state, out_state);
}
/****************************************************************************\
|* OpCRep_DenseUnitary *|
\****************************************************************************/
OpCRep_DenseUnitary::OpCRep_DenseUnitary(dcomplex* data, INT dim)
:OpCRep(dim)
{
_dataptr = data;
}
OpCRep_DenseUnitary::~OpCRep_DenseUnitary() { }
StateCRep* OpCRep_DenseUnitary::acton(StateCRep* state,
StateCRep* outstate) {
DEBUG(std::cout << "Dense acton called!" << std::endl);
DEBUG(state->print("INPUT"));
INT k;
for(INT i=0; i< _dim; i++) {
outstate->_dataptr[i] = 0.0;
k = i*_dim; // "row" offset into _dataptr, so dataptr[k+j] ~= dataptr[i,j]
for(INT j=0; j< _dim; j++) {
outstate->_dataptr[i] += _dataptr[k+j] * state->_dataptr[j];
}
}
DEBUG(outstate->print("OUTPUT"));
return outstate;
}
StateCRep* OpCRep_DenseUnitary::adjoint_acton(StateCRep* state,
StateCRep* outstate) {
DEBUG(std::cout << "Dense adjoint_acton called!" << std::endl);
DEBUG(state->print("INPUT"));
for(INT i=0; i< _dim; i++) {
outstate->_dataptr[i] = 0.0;
for(INT j=0; j< _dim; j++) {
outstate->_dataptr[i] += std::conj(_dataptr[j*_dim+i]) * state->_dataptr[j];
}
}
DEBUG(outstate->print("OUTPUT"));
return outstate;
}
/****************************************************************************\
|* OpCRep_Embedded *|
\****************************************************************************/
OpCRep_Embedded::OpCRep_Embedded(OpCRep* embedded_gate_crep, INT* noop_incrementers,
INT* numBasisEls_noop_blankaction, INT* baseinds, INT* blocksizes,
INT embedded_dim, INT nComponentsInActiveBlock, INT iActiveBlock,
INT nBlocks, INT dim)
:OpCRep(dim)
{
_embedded_gate_crep = embedded_gate_crep;
_noop_incrementers = noop_incrementers;
_numBasisEls_noop_blankaction = numBasisEls_noop_blankaction;
_baseinds = baseinds;
_blocksizes = blocksizes;
_nComponents = nComponentsInActiveBlock;
_embeddedDim = embedded_dim;
_iActiveBlock = iActiveBlock;
_nBlocks = nBlocks;
}
OpCRep_Embedded::~OpCRep_Embedded() { }
StateCRep* OpCRep_Embedded::acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << " Embedded acton called!" << std::endl);
DEBUG(state->print("INPUT"));
//_fastcalc.embedded_fast_acton_sparse(self.embedded_op.acton,
// output_state, state,
// self.noop_incrementers,
// self.numBasisEls_noop_blankaction,
// self.baseinds)
INT i, j, k, vec_index_noop = 0;
INT nParts = _nComponents;
INT nActionIndices = _embeddedDim;
INT offset;
dcomplex* state_data = state->_dataptr;
dcomplex* outstate_data = out_state->_dataptr;
//zero-out output state initially
for(i=0; i<_dim; i++) outstate_data[i] = 0.0;
INT b[100]; // could alloc dynamically (LATER?)
assert(nParts <= 100); // need to increase size of static arrays above
for(i=0; i<nParts; i++) b[i] = 0;
// Temporary states for acting the embedded gate on a subset of the whole
StateCRep subState1(nActionIndices);
StateCRep subState2(nActionIndices);
while(true) {
// Act with embedded gate on appropriate sub-space of state
// out_state[ inds ] += embedded_gate_acton( state[inds] ) (fancy index notn)
// out_state[inds] += state[inds]
for(k=0; k<nActionIndices; k++)
subState1._dataptr[k] = state_data[ vec_index_noop+_baseinds[k] ];
_embedded_gate_crep->acton(&subState1, &subState2);
for(k=0; k<nActionIndices; k++)
outstate_data[ vec_index_noop+_baseinds[k] ] += subState2._dataptr[k];
// increment b ~ itertools.product & update vec_index_noop = _np.dot(self.multipliers, b)
for(i=nParts-1; i >= 0; i--) {
if(b[i]+1 < _numBasisEls_noop_blankaction[i]) {
b[i] += 1; vec_index_noop += _noop_incrementers[i];
break;
}
else {
b[i] = 0;
}
}
if(i < 0) break; // if didn't break out of loop above, then can't
} // increment anything - break while(true) loop.
//act on other blocks trivially:
if(_nBlocks > 1) { // if there's more than one basis "block" (in direct sum)
offset = 0;
for(i=0; i<_nBlocks; i++) {
if(i != _iActiveBlock) {
for(j=0; j<_blocksizes[i]; j++) // identity op on this block
outstate_data[offset+j] = state_data[offset+j];
offset += _blocksizes[i];
}
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Embedded::adjoint_acton(StateCRep* state, StateCRep* out_state) {
//Note: exactly the same as acton(...) but calls embedded gate's adjoint_acton
DEBUG(std::cout << " Embedded adjoint_acton called!" << std::endl);
DEBUG(state->print("INPUT"));
INT i, j, k, vec_index_noop = 0;
INT nParts = _nComponents;
INT nActionIndices = _embeddedDim;
INT offset;
dcomplex* state_data = state->_dataptr;
dcomplex* outstate_data = out_state->_dataptr;
//zero-out output state initially
for(i=0; i<_dim; i++) outstate_data[i] = 0.0;
INT b[100]; // could alloc dynamically (LATER?)
assert(nParts <= 100); // need to increase size of static arrays above
for(i=0; i<nParts; i++) b[i] = 0;
// Temporary states for acting the embedded gate on a subset of the whole
StateCRep subState1(nActionIndices);
StateCRep subState2(nActionIndices);
while(true) {
// Act with embedded gate on appropriate sub-space of state
// out_state[ inds ] += embedded_gate_acton( state[inds] ) (fancy index notn)
// out_state[inds] += state[inds]
for(k=0; k<nActionIndices; k++)
subState1._dataptr[k] = state_data[ vec_index_noop+_baseinds[k] ];
_embedded_gate_crep->adjoint_acton(&subState1, &subState2);
for(k=0; k<nActionIndices; k++)
outstate_data[ vec_index_noop+_baseinds[k] ] += subState2._dataptr[k];
// increment b ~ itertools.product & update vec_index_noop = _np.dot(self.multipliers, b)
for(i=nParts-1; i >= 0; i--) {
if(b[i]+1 < _numBasisEls_noop_blankaction[i]) {
b[i] += 1; vec_index_noop += _noop_incrementers[i];
break;
}
else {
b[i] = 0;
}
}
if(i < 0) break; // if didn't break out of loop above, then can't
} // increment anything - break while(true) loop.
//act on other blocks trivially:
if(_nBlocks > 1) { // if there's more than one basis "block" (in direct sum)
offset = 0;
for(i=0; i<_nBlocks; i++) {
if(i != _iActiveBlock) {
for(j=0; j<_blocksizes[i]; j++) // identity op on this block
outstate_data[offset+j] = state_data[offset+j];
offset += _blocksizes[i];
}
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Embedded::acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << " Embedded acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
//_fastcalc.embedded_fast_acton_sparse(self.embedded_op.acton,
// output_state, state,
// self.noop_incrementers,
// self.numBasisEls_noop_blankaction,
// self.baseinds)
INT i, j, k, vec_index_noop = 0;
INT nParts = _nComponents;
INT nActionIndices = _embeddedDim;
INT offset;
dcomplex* state_data = state->_dataptr;
dcomplex* outstate_data = out_state->_dataptr;
//zero-out output state initially
for(i=0; i<_dim; i++) outstate_data[i] = 0.0;
INT b[100]; // could alloc dynamically (LATER?)
assert(nParts <= 100); // need to increase size of static arrays above
for(i=0; i<nParts; i++) b[i] = 0;
// Temporary states for acting the embedded gate on a subset of the whole
StateCRep subState1(nActionIndices);
StateCRep subState2(nActionIndices);
while(true) {
// Act with embedded gate on appropriate sub-space of state
// out_state[ inds ] += embedded_gate_acton_random( state[inds] ) (fancy index notn)
// out_state[inds] += state[inds]
for(k=0; k<nActionIndices; k++)
subState1._dataptr[k] = state_data[ vec_index_noop+_baseinds[k] ];
_embedded_gate_crep->acton_random(&subState1, &subState2, rand_state);
for(k=0; k<nActionIndices; k++)
outstate_data[ vec_index_noop+_baseinds[k] ] += subState2._dataptr[k];
// increment b ~ itertools.product & update vec_index_noop = _np.dot(self.multipliers, b)
for(i=nParts-1; i >= 0; i--) {
if(b[i]+1 < _numBasisEls_noop_blankaction[i]) {
b[i] += 1; vec_index_noop += _noop_incrementers[i];
break;
}
else {
b[i] = 0;
}
}
if(i < 0) break; // if didn't break out of loop above, then can't
} // increment anything - break while(true) loop.
//act on other blocks trivially:
if(_nBlocks > 1) { // if there's more than one basis "block" (in direct sum)
offset = 0;
for(i=0; i<_nBlocks; i++) {
if(i != _iActiveBlock) {
for(j=0; j<_blocksizes[i]; j++) // identity op on this block
outstate_data[offset+j] = state_data[offset+j];
offset += _blocksizes[i];
}
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Embedded::adjoint_acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
//Note: exactly the same as acton_random(...) but calls embedded gate's adjoint_acton_random
DEBUG(std::cout << " Embedded adjoint_acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
INT i, j, k, vec_index_noop = 0;
INT nParts = _nComponents;
INT nActionIndices = _embeddedDim;
INT offset;
dcomplex* state_data = state->_dataptr;
dcomplex* outstate_data = out_state->_dataptr;
//zero-out output state initially
for(i=0; i<_dim; i++) outstate_data[i] = 0.0;
INT b[100]; // could alloc dynamically (LATER?)
assert(nParts <= 100); // need to increase size of static arrays above
for(i=0; i<nParts; i++) b[i] = 0;
// Temporary states for acting the embedded gate on a subset of the whole
StateCRep subState1(nActionIndices);
StateCRep subState2(nActionIndices);
while(true) {
// Act with embedded gate on appropriate sub-space of state
// out_state[ inds ] += embedded_gate_acton_random( state[inds] ) (fancy index notn)
// out_state[inds] += state[inds]
for(k=0; k<nActionIndices; k++)
subState1._dataptr[k] = state_data[ vec_index_noop+_baseinds[k] ];
_embedded_gate_crep->adjoint_acton_random(&subState1, &subState2, rand_state);
for(k=0; k<nActionIndices; k++)
outstate_data[ vec_index_noop+_baseinds[k] ] += subState2._dataptr[k];
// increment b ~ itertools.product & update vec_index_noop = _np.dot(self.multipliers, b)
for(i=nParts-1; i >= 0; i--) {
if(b[i]+1 < _numBasisEls_noop_blankaction[i]) {
b[i] += 1; vec_index_noop += _noop_incrementers[i];
break;
}
else {
b[i] = 0;
}
}
if(i < 0) break; // if didn't break out of loop above, then can't
} // increment anything - break while(true) loop.
//act on other blocks trivially:
if(_nBlocks > 1) { // if there's more than one basis "block" (in direct sum)
offset = 0;
for(i=0; i<_nBlocks; i++) {
if(i != _iActiveBlock) {
for(j=0; j<_blocksizes[i]; j++) // identity op on this block
outstate_data[offset+j] = state_data[offset+j];
offset += _blocksizes[i];
}
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
/****************************************************************************\
|* OpCRep_Composed *|
\****************************************************************************/
OpCRep_Composed::OpCRep_Composed(std::vector<OpCRep*> factor_gate_creps, INT dim)
:OpCRep(dim),_factor_gate_creps(factor_gate_creps)
{
}
OpCRep_Composed::~OpCRep_Composed() { }
void OpCRep_Composed::reinit_factor_op_creps(std::vector<OpCRep*> new_factor_gate_creps) {
_factor_gate_creps.clear(); //removes all elements
_factor_gate_creps.insert(_factor_gate_creps.end(),
new_factor_gate_creps.begin(),
new_factor_gate_creps.end()); //inserts contents of new array
}
StateCRep* OpCRep_Composed::acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << "Composed acton called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_gate_creps.size();
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//if length is 0 just copy state --> outstate
if(nfactors == 0) {
out_state->copy_from(state);
return out_state;
}
//Act with first gate: output in tmp1
_factor_gate_creps[0]->acton(state, tmp1);
if(nfactors > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(std::size_t i=1; i < nfactors; i++) {
_factor_gate_creps[i]->acton(tmp1,tmp2);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Composed::adjoint_acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << "Composed adjoint_acton called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_gate_creps.size();
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//Note: same as acton(...) but reverse order of gates and perform adjoint_acton
//Act with last gate: output in tmp1
_factor_gate_creps[nfactors-1]->adjoint_acton(state, tmp1);
if(nfactors > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(INT i=nfactors-2; i >= 0; i--) {
_factor_gate_creps[i]->adjoint_acton(tmp1,tmp2);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Composed::acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << "Composed acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_gate_creps.size();
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//if length is 0 just copy state --> outstate
if(nfactors == 0) {
out_state->copy_from(state);
return out_state;
}
//Act with first gate: output in tmp1
_factor_gate_creps[0]->acton_random(state, tmp1, rand_state);
if(nfactors > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(std::size_t i=1; i < nfactors; i++) {
_factor_gate_creps[i]->acton_random(tmp1, tmp2, rand_state);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Composed::adjoint_acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << "Composed adjoint_acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_gate_creps.size();
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//Note: same as acton_random(...) but reverse order of gates and perform adjoint_acton_random
//Act with last gate: output in tmp1
_factor_gate_creps[nfactors-1]->adjoint_acton_random(state, tmp1, rand_state);
if(nfactors > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(INT i=nfactors-2; i >= 0; i--) {
_factor_gate_creps[i]->adjoint_acton_random(tmp1, tmp2, rand_state);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
/****************************************************************************\
|* OpCRep_Sum *|
\****************************************************************************/
OpCRep_Sum::OpCRep_Sum(std::vector<OpCRep*> factor_creps, INT dim)
:OpCRep(dim),_factor_creps(factor_creps)
{
}
OpCRep_Sum::~OpCRep_Sum() { }
StateCRep* OpCRep_Sum::acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << "Sum acton called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_creps.size();
StateCRep temp_state(_dim);
//zero-out output state
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] = 0.0;
//if length is 0 just return "0" state --> outstate
if(nfactors == 0) return out_state;
//Act with factors and accumulate into out_state
for(std::size_t i=0; i < nfactors; i++) {
_factor_creps[i]->acton(state,&temp_state);
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] += temp_state._dataptr[k];
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Sum::adjoint_acton(StateCRep* state, StateCRep* out_state) {
//Note: same as acton(...) but perform adjoint_acton
DEBUG(std::cout << "Sum adjoint_acton called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_creps.size();
StateCRep temp_state(_dim);
//zero-out output state
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] = 0.0;
//if length is 0 just return "0" state --> outstate
if(nfactors == 0) return out_state;
//Act with factors and accumulate into out_state
for(std::size_t i=0; i < nfactors; i++) {
_factor_creps[i]->adjoint_acton(state,&temp_state);
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] += temp_state._dataptr[k];
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Sum::acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << "Sum acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_creps.size();
StateCRep temp_state(_dim);
//zero-out output state
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] = 0.0;
//if length is 0 just return "0" state --> outstate
if(nfactors == 0) return out_state;
//Act with factors and accumulate into out_state
for(std::size_t i=0; i < nfactors; i++) {
_factor_creps[i]->acton_random(state,&temp_state,rand_state);
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] += temp_state._dataptr[k];
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Sum::adjoint_acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
//Note: same as acton_random(...) but perform adjoint_acton_random
DEBUG(std::cout << "Sum adjoint_acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
std::size_t nfactors = _factor_creps.size();
StateCRep temp_state(_dim);
//zero-out output state
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] = 0.0;
//if length is 0 just return "0" state --> outstate
if(nfactors == 0) return out_state;
//Act with factors and accumulate into out_state
for(std::size_t i=0; i < nfactors; i++) {
_factor_creps[i]->adjoint_acton_random(state,&temp_state,rand_state);
for(INT k=0; k<_dim; k++)
out_state->_dataptr[k] += temp_state._dataptr[k];
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
/****************************************************************************\
|* OpCRep_Repeated *|
\****************************************************************************/
OpCRep_Repeated::OpCRep_Repeated(OpCRep* repeated_crep, INT num_repetitions, INT dim)
:OpCRep(dim)
{
_repeated_crep = repeated_crep;
_num_repetitions = num_repetitions;
}
OpCRep_Repeated::~OpCRep_Repeated() { }
StateCRep* OpCRep_Repeated::acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << "Repeated acton called!" << std::endl);
DEBUG(state->print("INPUT"));
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//if num_repetitions is 0 just copy state --> outstate
if(_num_repetitions == 0) {
out_state->copy_from(state);
return out_state;
}
//Act with first gate: output in tmp1
_repeated_crep->acton(state, tmp1);
if(_num_repetitions > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(INT i=1; i < _num_repetitions; i++) {
_repeated_crep->acton(tmp1,tmp2);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Repeated::adjoint_acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << "Repeated adjoint_acton called!" << std::endl);
DEBUG(state->print("INPUT"));
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//Note: same as acton(...) but perform adjoint_acton
//if num_repetitions is 0 just copy state --> outstate
if(_num_repetitions == 0) {
out_state->copy_from(state);
return out_state;
}
//Act with first gate: output in tmp1
_repeated_crep->adjoint_acton(state, tmp1);
if(_num_repetitions > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(INT i=1; i < _num_repetitions; i++) {
_repeated_crep->adjoint_acton(tmp1,tmp2);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Repeated::acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << "Repeated acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//if num_repetitions is 0 just copy state --> outstate
if(_num_repetitions == 0) {
out_state->copy_from(state);
return out_state;
}
//Act with first gate: output in tmp1
_repeated_crep->acton_random(state, tmp1, rand_state);
if(_num_repetitions > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(INT i=1; i < _num_repetitions; i++) {
_repeated_crep->acton_random(tmp1,tmp2,rand_state);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_Repeated::adjoint_acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << "Repeated adjoint_acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
StateCRep *tmp2, *tmp1 = out_state; //tmp1 already alloc'd
StateCRep* t; // for swapping
//Note: same as acton_random(...) but perform adjoint_acton_random
//if num_repetitions is 0 just copy state --> outstate
if(_num_repetitions == 0) {
out_state->copy_from(state);
return out_state;
}
//Act with first gate: output in tmp1
_repeated_crep->adjoint_acton_random(state, tmp1, rand_state);
if(_num_repetitions > 1) {
StateCRep temp_state(_dim); tmp2 = &temp_state;
//Act with additional gates: tmp1 -> tmp2 then swap, so output in tmp1
for(INT i=1; i < _num_repetitions; i++) {
_repeated_crep->adjoint_acton_random(tmp1,tmp2,rand_state);
t = tmp1; tmp1 = tmp2; tmp2 = t;
}
//tmp1 holds the output state now; if tmp1 == out_state
// we're in luck, otherwise we need to copy it into out_state.
if(tmp1 != out_state) {
out_state->copy_from(tmp1);
}
}
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
/****************************************************************************\
|* OpCRep_RandomUnitary *|
\****************************************************************************/
OpCRep_RandomUnitary::OpCRep_RandomUnitary(PyObject* unitary_rates, std::vector<OpCRep*> unitary_reps,
PyObject* rand_state, INT dim)
:OpCRep(dim)
{
_unitary_rates = unitary_rates;
_unitary_reps = unitary_reps;
_rand_state = rand_state;
}
OpCRep_RandomUnitary::~OpCRep_RandomUnitary() { }
// def acton_random(self, state, rand_state):
// rand_state = rand_state if rand_state is not None else self.rand_state
// rates = self.unitary_rates
// index = rand_state.choice(len(self.unitary_rates), p=rates)
// rep = self.unitary_reps[index]
// return rep.acton(state)
StateCRep* OpCRep_RandomUnitary::acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << "RandomUnitary acton called - and you should never call this!!" << std::endl);
return out_state;
}
StateCRep* OpCRep_RandomUnitary::adjoint_acton(StateCRep* state, StateCRep* out_state) {
DEBUG(std::cout << "RandomUnitary adjoint_acton called - and you should never call this!!" << std::endl);
return out_state;
}
StateCRep* OpCRep_RandomUnitary::acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << "RandomUnitary acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
INT i = 0;
if(rand_state == NULL) rand_state = _rand_state;
//Call rand_state.choice Python function
PyGILState_STATE gstate = PyGILState_Ensure();
PyObject *res = PyObject_CallMethod(rand_state, "choice", "(l,s,O,O)",
_unitary_reps.size(), NULL, Py_True, _unitary_rates);
if( !res ) {
PyErr_Print(); assert(0);
}
else {
i = PyLong_AsUnsignedLong(res);
Py_DECREF(res);
}
PyGILState_Release(gstate);
out_state = _unitary_reps[i]->acton(state, out_state);
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
StateCRep* OpCRep_RandomUnitary::adjoint_acton_random(StateCRep* state, StateCRep* out_state, PyObject* rand_state) {
DEBUG(std::cout << "RandomUnitary adjoint_acton_random called!" << std::endl);
DEBUG(state->print("INPUT"));
DEBUG(out_state->print("OUTPUT"));
return out_state;
}
}