-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetObservable.m
1677 lines (1517 loc) · 59.1 KB
/
getObservable.m
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function out = getObservable(type,mps,Vmat,para)
%% Calculates the following Observables:
% SBM: 'spin', 'occupation', 'current', 'shift', 'rdm', 'staroccupation', 'starpolaron',
% 'energy', 'displacement', 'bath2correlators'
% MLSBM: 'participation', 'tunnelenergy', 'staroccupation', 'starpolaron', 'energy'
%
% Use as: first argument is cell, with descriptor as type{1}
% all other type{n} are options defined in each function
% getObservable({'spin'} ,mps,Vmat,para)
% getObservable({'occupation'} ,mps,Vmat,para)
% getObservable({'current',AnAm} ,mps,Vmat,para)
% getObservable({'shift'} ,mps,Vmat,para)
% getObservable({'rdm',2} ,mps,Vmat,para)
% getObservable({'tunnelenergy',op} ,mps,Vmat,para)
% getObservable({'displacement'} ,mps,Vmat,para)
% getObservable({'bath2correlators'} ,mps,Vmat,para)
% getObservable({'starpolaron'} ,mps,Vmat,para)
% getObservable({'staroccupation',AnAm} ,mps,Vmat,para)
% getObservable({'energy',op} ,mps,Vmat,para)
% getObservable({'hshi',op} ,mps,Vmat,para) % op is optional
% getObservable({'stateproject',i,j} ,mps,Vmat,para) % i,j optional
% getObservable({'sysheff',i} ,mps,Vmat,para) % i optional; possibly only TreeMPS operational
%
% In 'current' and 'staroccupation': AnAm optional!
%
% The output 'out' can be of different form:
% 'spin': struct, fields: sx, sy, sz
% 'occupation': array (L x NC)
% 'shift': array
% 'rdm': matrix
% 'participation': number
% 'tunnelenergy': number
% 'displacement': vector (L x 1)
% 'bath2correlators': matrix (L x L x NC)
% 'staroccupation': array (3 x X x NC)
% 'current': array (NC x L-1)
% 'starpolaron': array (3 x X x NC)
% 'energy' scalar
% 'hshi' scalar
% 'coherence' not implemented yet
% 'stateproject' scalar
% 'state' matrix (dk x dk)
%
% Created 03/06/2014 by Florian Schroeder @ University of Cambridge
% TODO: - Implement Boson Site Shift to export the routine from optimizesite.m. Only get a single shift value.
%
% Modified:
% - 21/12/14 FS: replaced OBB contractions with faster matrix products.
% - 21/08/15 FS: added Multi-Chain Vmat / Vtens capability
% - 28/02/16 FS: added TreeMPS support
switch type{1}
case 'spin'
% applicable for spin-boson model and for folded SBM2
out = calSpin(mps,Vmat,para);
case 'occupation'
% applicable to all single stranded chains. Extensible to 2-chains
if para.useTreeMPS == 1
out = calBosonOcc_Tree(mps,para); % (L x NC)
return;
elseif para.foldedChain == 0
if para.nChains == 1 && ~para.useStarMPS
out = calBosonOcc(mps,Vmat,para);
else % Multi-chain with Vmat / V-tensor-network
out = calBosonOcc_MC(mps,Vmat,para); % (NC x L)
end
elseif para.foldedChain == 1
out(1,:) = calBosonOcc(mps,Vmat,para,1);
out(2,:) = calBosonOcc(mps,Vmat,para,2);
end
if size(out,1) > 1
out = out.'; % (L x NC) for compatibility with tresults
end
case 'shift'
% applicable to all single stranded chains. Extendable to 2-chains
out = calBosonShift(mps,Vmat,para);
case '1siteshift'
% intended for use in optimizesite. Only for focused single-site
% mps and Vmat are single-site matrices
out = cal1siteShift(mps,Vmat,para);
case 'rdm'
% applicable to all systems.
if type{2} <= para.L
if para.useTreeMPS
out = calRDM(mps.mps,mps.Vmat,para,1); % only for site 1 for now.
else
out = calRDM(mps,Vmat,para,type{2});
end
else
out = [];
end
case 'rdm_adiabatic'
% applicable to site 1 for now only!
if type{2} ~= 1
error('rdm_adiabatic only available for site 1');
end
if para.nChains > 1
error('rdm_adiabatic only for single chains!');
end
assert( type{3} <= para.dk(1,1), 'Need valid number for state selection');
k = type{3}; % the (adiabatic) bond state to construct the (adiabatic) RDM from
% SVD of mps{1} and delete unwanted bond states!
A = mps{1};
[D1, D2, d] = size(A); % d is site dimension
A = permute(A, [3, 1, 2]);
A = reshape(A, [d * D1, D2]); % reshapes to (a1 d),a2
[B, S, U] = svd2(A); % Could also use QR decomposition if nargin !=5
DB = size(S,1);
B = B* sparse(k,k,1,DB,DB) *U;
B = reshape(B, [d, D1, D2]);
mps{1} = permute(B, [2, 3, 1]); % now only contains the desired bond state
out = calRDM(mps,Vmat,para,type{2});
case 'rdm_adiabatic2'
% applicable to site 1 for now only!
% A different way of selecting the bond-state, without SVD.
% Seems to be equivalent to 'rdm_adiabatic'
if type{2} ~= 1
error('rdm_adiabatic only available for site 1');
end
if para.nChains > 1
error('rdm_adiabatic only for single chains!');
end
assert( type{3} <= para.dk(1,1), 'Need valid number for state selection');
k = type{3}; % the (adiabatic) bond state to construct the (adiabatic) RDM from
[~, D2, ~] = size(mps{1}); % d is site dimension
for ii = 1:D2
if ii ~= k
mps{1}(1,ii,:) = 0; % delete 2 for k=1 or 1 for k=2
end
end
mps{1} = mps{1}./norm(reshape(mps{1},[],1)); % normalise the state!
out = calRDM(mps,Vmat,para,type{2});
case 'state'
% applicable to site 1 for now only!
% saves the MPS of the site with diabatic & adiabatic information!
% closely related to 'rdm_adiabatic'
% {'state',sitej}
if type{2} ~= 1
error('VMPS:getObservable:state','state only available for site 1');
end
if para.nChains > 1 || para.useTreeMPS
if para.useStarMPS
d = size(mps{1});
A = reshape(mps{1},[],d(end)); % ... x dk
[U,S,~] = svd2(A.'); % transpose to benefit from speedup in svd2
elseif para.useTreeMPS
d = size(mps.mps{1});
[~,V] = prepare_onesiteAmat(reshape(mps.mps{1},d(1),[],d(end)),para,1); % A_(1,D(1)*..*D(NC),dOBB') * V_(dOBB',dk)
if diff(size(V)) ~= 0 % if dOBB < dk then expand
V(d(end),d(end)) = 0;
end
out = V';
return;
else
error('VMPS:getObservable:NotImplemented','Needs to be implemented!')
end
else
d = size(mps{1});
A = reshape(mps{1},[],d(end)); % ... x dk
[U,S,~] = svd2(A.'); % transpose to benefit from speedup in svd2
if size(S,1) < d(end)
% add zeros
U(d(end),d(end)) = 0;
S(d(end),d(end)) = 0;
end
end
out = U*S; % should be dk x D(1:dk) for D > dk
case 'sys-env-state'
% saves the MPS of site 1 & 2 containing all diabatic & adiabatic information!
% decompose also 1st site to have adiabatic information available
% returns out: struct
% {'sys-env-state'}
if para.nChains > 1
if para.useStarMPS
d = size(mps{1});
A = reshape(mps{1},[],d(end)); % ... x dk
[U,S,~] = svd2(A.'); % transpose to benefit from speedup in svd2
else
error('VMPS:getObservable:NotImplemented','Needs to be implemented!')
end
else
d = size(mps{1});
A = reshape(mps{1},[],d(end)); % ... x dk
[U,S,~] = svd2(A.'); % transpose to benefit from speedup in svd2
if size(S,1) < d(end)
% add zeros
U(d(end),d(end)) = 0;
S(d(end),d(end)) = 0;
end
end
Vmat{1} = U*S; % should be dk x D(1:dk) for D > dk; sets focus on Vmat!! This is simpler for StarMPS
% save the system and reaction coord state
out.mps = mps([1,2]);
out.Vmat = Vmat([1,2]);
case 'participation'
% applicable to all systems, only for first site.
out = calParticipation(calRDM(mps,Vmat,para,1));
case 'tunnelenergy'
% useful for MLSBM, applicable to all
% needs type{2} = op
if length(type) == 2
out = calTunnelingEnergy(mps,Vmat,para,type{2});
else
out = calTunnelingEnergy(mps,Vmat,para);
end
case 'bath1correlators'
% = [state projected] chain observable
% returns a L [x dk(1,1)] x nChains Vector
%
% obs: ['bp'|'x'|'bp^2'|'x^2'|'n'] selects observable to calculate
%
% by default:
% {'bath1correlators',obs} : no projection - TODO!
% {'bath1correlators',obs,'diabatic'} : projects onto diabatic states
% {'bath1correlators',obs,'adiabatic'}: projects onto adiabatic states
NC = para.nChains;
if para.useTreeMPS && isstruct(mps)
if length(type) == 2
out = calBath1SiteCorrelators_Tree(mps,para,type{2}); % L x NC
else
out = zeros(para.L, mps.dk(1), NC);
% One SVD to dk to separate diabatic or adiabatic basis!
% Assume, Vmat of node = eye; mps == treeMPS
assert(all(all(mps.Vmat{1} == eye(mps.dk(1)))),'Please implement for Vmat ~= eye, if this happens!');
d = size(mps.mps{1});
[A,V] = prepare_onesiteAmat(reshape(mps.mps{1},d(1),[],d(end)),para,1); % A_(1,D(1)*..*D(NC),dOBB') * V_(dOBB',dk)
A = reshape(A,[],size(A,3));
% Now dOBB is adiabatic basis, dk is diabatic. V carries probabilities for each state, which will be removed later!
for kk = 1:mps.dk(1)
newV = zeros(size(V));
if strcmpi(type{3},'diabatic')
% Project onto root nodes' diabatic states
newV(:,kk) = V(:,kk);
elseif strcmpi(type{3},'adiabatic')
% Project onto root nodes' adiabatic states
newV(kk,:) = V(kk,:);
elseif strcmpi(type{3},'lettcoherence') % get |TT><LE+| mode-mediated coherence terms
newV = V;
projOp = zeros(size(V,2));
projOp(1,2) = 1;% projOp(2,1) = 1;
end
if norm(newV) ~= 0
newV = newV./norm(newV); % remove weight by /norm()
end
mps.mps{1} = reshape(A*newV,d);
if strcmpi(type{3},'lettcoherence')
out(:,kk,:) = calBath1SiteCorrelators_Tree(mps,para,type{2},projOp); % projection needed for cross terms
break; % only do once!
else
out(:,kk,:) = calBath1SiteCorrelators_Tree(mps,para,type{2}); % no projection needed anymore inside calBath1SiteCorrelators
end
end
end
return;
end
if length(type) == 3
out = zeros(para.L, para.dk(1,1), NC);
elseif length(type) == 2
out = zeros(para.L,NC);
else
error('VMPS:getObservable:bath1correlators','Please call with correct input');
end
if para.useStarMPS
for mc = 1:NC
% extract MPS for each chain and calculate <anam> separately
cL = para.chain{mc}.L;
mpsC = [mps{1}, cellfun(@(x) x{mc},mps(2:cL),'UniformOutput',false)];
VmatC = [Vmat{1}, cellfun(@(x) x{mc},Vmat(2:cL),'UniformOutput',false)];
mpsC{1} = permute(mpsC{1}, [1:mc,mc+2:NC+1, mc+1, NC+2]);
mpsC{1} = reshape(mpsC{1}, [],para.D(mc,1),para.dk(1,1));
paraC = para;
paraC.nChains = 1; paraC.L = para.chain{mc}.L;
paraC.dk = para.dk(mc,:); paraC.dk(1) = para.dk(1,1); % need to preserve system dk
paraC.shift = para.shift(mc,:);
% calculate <anam>
if length(type) == 2
out(1:paraC.L,mc) = calBath1SiteCorrelatorsProject(mpsC,VmatC,paraC,type{2},[],[]);
elseif strcmpi(type{3},'diabatic')
% diabatic projections (H0 eigenstates)
for kk = 1:paraC.dk(1,1)
stateProj = zeros(paraC.dk(1,1)); stateProj(kk,kk) = 1;
bondProj = [];
out(1:paraC.L,kk,mc) = calBath1SiteCorrelatorsProject(mpsC,VmatC,paraC,type{2},stateProj,bondProj);
end
elseif strcmpi(type{3},'adiabatic')
% adiabatic states
for kk = 1:para.D(mc,1)
stateProj = [];
bondProj = zeros(para.D(mc,1)); bondProj(kk,kk) = 1;
out(1:paraC.L,kk,mc) = calBath1SiteCorrelatorsProject(mpsC,VmatC,paraC,type{2},stateProj,bondProj);
end
end
end
else
if length(type) == 2
out(1:para.L,:) = calBath1SiteCorrelatorsProject(mps,Vmat,para,type{2},[],[]);
elseif strcmpi(type{3},'diabatic')
% diabatic projections (H0 eigenstates)
for kk = 1:para.dk(1,1)
stateProj = zeros(para.dk(1,1)); stateProj(kk,kk) = 1;
bondProj = [];
out(1:para.L,kk,:) = calBath1SiteCorrelatorsProject(mps,Vmat,para,type{2},stateProj,bondProj);
end
elseif strcmpi(type{3},'adiabatic')
% adiabatic states
for kk = 1:para.dk(1,1)
stateProj = [];
bondProj = zeros(para.dk(1,1)); bondProj(kk,kk) = 1;
out(1:para.L,kk,:) = calBath1SiteCorrelatorsProject(mps,Vmat,para,type{2},stateProj,bondProj);
end
end
% out(:,1,:) = calBath1SiteCorrelators_MC(mps,Vmat,para,[1,0;0,0]); % spin up
% out(:,2,:) = calBath1SiteCorrelators_MC(mps,Vmat,para,[0,0;0,1]); % spin down
end
case 'bath2correlators'
% needed for mapping from chain to star
% returns triangular L x L Matrix
if para.useStarMPS
NC = para.nChains;
out = zeros(para.L, para.L, NC);
for mc = 1:NC
% extract MPS for each chain and calculate <anam> separately
cL = para.chain{mc}.L;
mpsC = [mps{1}, cellfun(@(x) x{mc},mps(2:cL),'UniformOutput',false)];
VmatC = [Vmat{1}, cellfun(@(x) x{mc},Vmat(2:cL),'UniformOutput',false)];
mpsC{1} = permute(mpsC{1}, [1:mc,mc+2:NC+1, mc+1, NC+2]);
mpsC{1} = reshape(mpsC{1}, [],para.D(mc,1),para.dk(1,1));
paraC = para;
paraC.nChains = 1; paraC.L = para.chain{mc}.L;
paraC.dk = para.dk(mc,:); paraC.dk(1) = para.dk(1,1); % need to preserve system dk
paraC.shift = para.shift(mc,:);
% calculate <anam>
out(1:paraC.L,1:paraC.L,mc) = calBath2SiteCorrelators_MC(mpsC,VmatC,paraC);
end
else
out = calBath2SiteCorrelators_MC(mps,Vmat,para);
end
case 'staroccupation'
%% does mapping chain -> star
% uses bath2correlators
if para.foldedChain == 1, error('Not yet implemented'); end
% Get the correlator
if length(type) == 2
AmAn = real(type{2});
else
AmAn = real(getObservable({'bath2correlators'},mps,Vmat,para)); % L x L x nChains
end
% preallocate output array
[x,~,~] = getStarMapping(para,1);
out = zeros(2,length(x),para.nChains);
for mc = 1:para.nChains
[x,hsquared,pxn] = getStarMapping(para,mc);
if strcmp(para.chain{mc}.mapping, 'LanczosTriDiag')
result = diag(pxn * (AmAn(:,:,mc) + triu(AmAn(:,:,mc),1).') * pxn.'); % pxn == U map -> no need for hsquared
out(:,1:length(x),mc) = [x,result(2:end)].';
else
% out(:,1:length(x),mc) = [x, hsquared.*((pxn.^2)*diag(AmAn(:,:,mc)) + 2.* diag(pxn*(AmAn(:,:,mc)-diag(diag(AmAn(:,:,mc))))*pxn.') )]';
out(:,1:length(x),mc) = [x, hsquared.*diag(pxn*(AmAn(:,:,mc)+triu(AmAn(:,:,mc),1).')*pxn.')].';
end
end
case 'current'
%% gets current through each bond.
% works with Single and Multi-Chain Vmat / Vtens
L = para.L; NC = para.nChains;
if length(type) == 2
AmAn = imag(type{2}); % (L x L x NC)
else
AmAn = imag(getObservable({'bath2correlators'},mps,Vmat,para)); % tridiagonal
end
out = zeros(L-1,NC);
for mc = 1:NC
out(:,mc) = (para.chain{mc}.t.*diag(AmAn(1:end-1,2:end,mc))); % t(n)*a(n)*a(n+1)^+
end
case 'displacement'
% = state projected chain polaron
% needed for mapping from chain to star, starpolaron
% returns a L x dk(1,1) x nChains Vector
%
% by default:
% {'displacement'} : no projection - TODO!
% {'displacement','diabatic'} : projects onto diabatic states
% {'displacement','adiabatic'}: projects onto adiabatic states
NC = para.nChains;
out = zeros(para.L, para.dk(1,1), NC);
if para.useStarMPS
for mc = 1:NC
% extract MPS for each chain and calculate <anam> separately
cL = para.chain{mc}.L;
mpsC = [mps{1}, cellfun(@(x) x{mc},mps(2:cL),'UniformOutput',false)];
VmatC = [Vmat{1}, cellfun(@(x) x{mc},Vmat(2:cL),'UniformOutput',false)];
mpsC{1} = permute(mpsC{1}, [1:mc,mc+2:NC+1, mc+1, NC+2]);
mpsC{1} = reshape(mpsC{1}, [],para.D(mc,1),para.dk(1,1));
paraC = para;
paraC.nChains = 1; paraC.L = para.chain{mc}.L;
paraC.dk = para.dk(mc,:); paraC.dk(1) = para.dk(1,1); % need to preserve system dk
paraC.shift = para.shift(mc,:);
% calculate <anam>
if length(type) == 1 || strcmpi(type{2},'diabatic')
% diabatic projections (H0 eigenstates)
for kk = 1:paraC.dk(1,1)
stateProj = zeros(paraC.dk(1,1)); stateProj(kk,kk) = 1;
bondProj = [];
out(1:paraC.L,kk,mc) = calBath1SiteCorrelatorsProject(mpsC,VmatC,paraC,'x',stateProj,bondProj);
end
elseif strcmpi(type{2},'adiabatic')
% adiabatic states
for kk = 1:para.D(mc,1)
stateProj = [];
bondProj = zeros(para.D(mc,1)); bondProj(kk,kk) = 1;
out(1:paraC.L,kk,mc) = calBath1SiteCorrelatorsProject(mpsC,VmatC,paraC,'x',stateProj,bondProj);
end
end
end
else
if length(type) == 1 || strcmpi(type{2},'diabatic')
% diabatic projections (H0 eigenstates)
for kk = 1:para.dk(1,1)
stateProj = zeros(para.dk(1,1)); stateProj(kk,kk) = 1;
bondProj = [];
out(1:para.L,kk,:) = calBath1SiteCorrelatorsProject(mps,Vmat,para,'x',stateProj,bondProj);
end
elseif strcmpi(type{2},'adiabatic')
% adiabatic states
for kk = 1:para.dk(1,1)
stateProj = [];
bondProj = zeros(para.dk(1,1)); bondProj(kk,kk) = 1;
out(1:para.L,kk,:) = calBath1SiteCorrelatorsProject(mps,Vmat,para,'x',stateProj,bondProj);
end
end
% out(:,1,:) = calBath1SiteCorrelators_MC(mps,Vmat,para,[1,0;0,0]); % spin up
% out(:,2,:) = calBath1SiteCorrelators_MC(mps,Vmat,para,[0,0;0,1]); % spin down
end
case 'starpolaron'
%% does mapping chain -> star
% uses displacement
% x stands for continuous variable (momentum k)
% Does state projection!
% Works with Single and Multi Chain Vmat / Vtens
%
%
if para.foldedChain == 1, error('FoldedChain not yet implemented for polaron'); end
NC = para.nChains;
% Get correlators
if length(type) == 1 || strcmpi(type{2},'diabatic')
An = real(getObservable({'displacement','diabatic'},mps,Vmat,para)); % L x states x nChains
elseif strcmpi(type{2},'adiabatic')
An = real(getObservable({'displacement','adiabatic'},mps,Vmat,para)); % L x states x nChains
end
% AnUp = real(calBath1SiteCorrelators_MC(mps,Vmat,para,1)); % L x nChains
% AnDown = real(calBath1SiteCorrelators_MC(mps,Vmat,para,-1)); % L x nChains
[x,~,~] = getStarMapping(para,1);
out = zeros(size(An,2)+1, length(x), NC);
for mc = 1:NC % for each chain:
[x,h,pxn] = getStarMapping(para,mc);
if strcmp(para.chain{mc}.mapping, 'LanczosTriDiag')
result = 2.* pxn * An(:,:,mc); % pxn == U map -> no need for hsquared
out(:,1:length(x),mc) = [x,result(2:end,:)].';
else
% out(:,:,mc) = [x, h.*(pxn*An(:,1,mc)),h.*(pxn*An(:,2,mc))]';
out(:,:,mc) = [x, (h*ones(1,size(An,2))).*(pxn*An(:,:,mc))]';
end
end
case 'energy'
% Calculates the entire energy of the chain
% only usable for focus on spin site!
% needs type{2} = op
if length(type) == 2
out = calEnergy(mps,Vmat,para,type{2});
else
error('Need op as 2nd argument');
end
case 'coherence'
% only for spin in SBM
case 'hshi'
% Calculates the entire energy of the system hamiltonian and first interaction terms
% needs type{2} = op
if length(type) == 2
out = calHsHi(mps,Vmat,para,type{2});
else
op.h1term = cell(1,2); % One body terms of the Hamiltonian
op.h2term = cell(para.M, 2,2); % default: nearest neighbor interaction
for s=1:2
op=genh1h2term_onesite(para,op,s);
end
out = calHsHi(mps,Vmat,para,op);
end
case 'stateproject'
% Calculates the amplitude of the projection <P|Psi> of the MPS
% Here |P> is a simple state like |1>|0000000> (System-Bath)
%
% by default:
% {'stateproject'} : projects onto |2>|0000000>
% {'stateproject',i} : projects onto |i>|0000000>
% {'stateproject',i,j} : projects onto |i>|jjjjjjj>
% default settings
systemState = 2; % take by default state 2
envState = 1; % the state on each boson to project on
if length(type) >= 2
systemState = type{2};
end
if length(type) == 3
envState = type{3};
end
out = calStateProject(mps,Vmat,para,systemState,envState);
case 'sysheff'
% Calculates the effective Hamiltonian representing the adiabatic total energy surfaces (TES)
% on which the system evolves.
% For now: optimised for treeMPS
%
% {'sysheff'} : returns (dk x dk)^2 operator and C
% {'sysheff',i} : returns (dk x i)^2, operator with the i strongest adiabats
% out = {Heff,sysState} returns the effective hamiltonian together with |psi> mapping from diabatic to the adiabatic basis
out = cell(1,3);
if para.useTreeMPS
[out{1},out{2},out{3}] = calSysHeff(mps,para);
else
error('Needs to be implemented');
end
case 'syspes'
% Calculates the effective potenital representing the adiabatic potential energy surfaces (PES)
% on which the system evolves.
out = cell(1,3);
if para.useTreeMPS
[out{1},out{2},out{3}] = calSysPES(mps,para,type{2});
else
error('Needs to be implemented');
end
end
end
function spin = calSpin(mps,Vmat,para)
% Calculate the spin expectation value
% Has to be modified if site 1 changes dimension!!
% Modified:
% FS 24/05/2014: excluded MLSpinBoson. Use with try-catch block
% FS 19/08/2015: - Can be used with Multi-Chain Vmat and Vtens code
%
% TODO: Needs extension to use information about dimension of spin site.
%
if para.useVtens || para.useStarMPS % only Quick Fix for Vtens code, Could be used as Standard code!
McOp = cell(1,1,3); % L x NC x N
[McOp{1}, McOp{2}, McOp{3}] = spinop(para.spinbase);
if para.useVtens
spinVal = real(expectation_allsites_MC(McOp,mps,Vmat,para));
else
spinVal = real(expectation_allsites_StarMPS(McOp,mps,Vmat,para));
end
spin.sx = spinVal(1);
spin.sy = spinVal(2);
spin.sz = spinVal(3);
return;
end
if para.useTreeMPS
McOp = cell(1,1,3); % L x NC x N
[McOp{1}, McOp{2}, McOp{3}] = spinop(para.spinbase);
error('VMPS:getObservable:NotImplementedYet','Please implement this feature!')
end
N=para.L;
assert(N==length(mps) && N==length(Vmat));
assert(~strcmp(para.model,'MLSpinBoson'),'not possible for MLSBM');
ndset=cell(1,N);
for j=1:N
ndset{1,j}=eye(size(Vmat{j},1));
end
sx=ndset;sy=sx;sz=sy;
%debug:
% sx{1,1}
[sigmaX,sigmaY,sigmaZ]=spinop(para.spinbase);
sx{para.spinposition(end)}=sigmaX;
sy{para.spinposition(end)}=sigmaY;
sz{para.spinposition(end)}=sigmaZ;
if strcmp(para.model,'2SpinPhononModel')
sx{para.spinposition}=kron(sigmaZ,eye(2)); %measures excitation of site1
sz{para.spinposition}=kron(eye(2),sigmaZ); %measures excitation of site2
%try to find a good way to measure this!
sy{para.spinposition}=kron(sigmaY,eye(2)); %measures only sy of site1
end
spin.sx=expectationvalue(sx,mps,Vmat,mps,Vmat);
spin.sy=expectationvalue(sy,mps,Vmat,mps,Vmat);
spin.sz=expectationvalue(sz,mps,Vmat,mps,Vmat);
spin.sx=real(spin.sx);
spin.sy=real(spin.sy);
spin.sz=real(spin.sz);
end
function nx = calBosonOcc(mps,Vmat,para,varargin)
% Calculate the boson occupation on x chain
% The operator on the spin site is set to zero
% left or right chain occupation via varargin{1} = 1 or 2
%
% Modified:
% FS 23/01/2014: - Introduced '~' to ignore unused returned values
% - support for folded Chain models
% FS 10/03/2014: - Using correlator_allsites which is more general
% FS 04/06/2014: - updated for spinposition array.
% FS 17/07/2015: - varargin = {1,2} to calculate nx/nz for folded chain
n_op = cell(1,para.L);
for j=1:para.L
if prod(j~=para.spinposition)
if para.foldedChain == 0
% 1-chain SBM
[~,~,n_op{j}] = bosonop(para.dk(j),para.shift(j),para.parity);
%Modification for 2chain model!! Not perfect or right yet!
elseif para.foldedChain == 1
% only kron(n,1) chain occupation calculated.
if para.parity == 'n'
[~,~,n] = bosonop(sqrt(para.dk(j)),para.shift(j),para.parity);
idm = eye(size(n));
if varargin{1} == 1
nr = kron(n,idm);
elseif varargin{1} == 2
nr = kron(idm,n);
end
else
[bp,~,~] = bosonop(para.dk(j),para.shift(j),para.parity); % Why without sqrt??
[~,~,nr,~,~,~]=paritykron(bp,para.bosonparity);
end
n_op{j} = nr;
else
disp('Not Implemented: getObservable, calBosonOcc, foldedchain >1');
end
else
n_op{j}=zeros(para.dk(j)); % don't measure spin.
end
end
%
nx = expectation_allsites(n_op,mps,Vmat);
nx = real(nx); % imag(nx) = eps -> neglect
end
function n = calBosonOcc_MC(mps,Vtens,para)
%% calculate the boson occupation for Single / Multi-Chain + Vmat / V-tensor-network
% Zero operator for spin site
% calculate for all chains simultaneously
%
% Created 18/08/2015 by FS
assert(para.foldedChain == 0, 'not for folded chains');
L = para.L; NC = para.nChains;
% create Operator for expectationvalue: need nc^2 each (i,:,j) is one operator [] x [] x n x [] x []
McOp = cell(L, NC, NC);
for mc = 1:NC
for j = 1:para.chain{mc}.L
if j ~= para.spinposition % works with array
[~,~,McOp{j,mc,mc}] = bosonop(para.dk(mc,j),para.shift(mc,j),para.parity);
else
McOp{j,mc,mc} = zeros(para.dk(1,j)); % don't measure spin.
end
end
end
% if para.useStarMPS
% n = real(expectation_allsites_StarMPS(McOp,mps,Vtens,para));
% else
n = real(expectation_allsites_MC(McOp,mps,Vtens,para)); % (NC x L)
% end
end
function n = calBosonOcc_Tree(treeMPS,para)
%% calculate the boson occupation for treeMPS
% Zero operator for spin site
% calculate for all chains simultaneously
%
% Created by FS 28/02/2016
%
% L = para.L; NC = para.nChains; % total number of site and chains
% Implement recursively for now!
if treeMPS.height == 0
% this is leaf / chain
% create Operator for expectationvalue: need nc^2 each (i,:,j) is one operator [] x [] x n x [] x []
Lc = treeMPS.L; % length of chain
Op = cell(1,Lc);
for j = 1:Lc
if isempty(treeMPS.spinposition) || j ~= treeMPS.spinposition % works with array
[~,~,Op{1,j}] = bosonop(treeMPS.dk(1,j),treeMPS.shift(1,j),para.parity);
else
Op{1,j} = zeros(treeMPS.dk(1,j)); % don't measure spin.
end
end
n = real(expectation_allsites(Op,treeMPS.mps,treeMPS.Vmat,treeMPS.BondCenter)); % (NC x L)
n = reshape(n,[],1); % L x 1
else
% this is node -> more recursive calls
% assume, Focused on MPS!
% save n into temp cell array, since total number of chains at this node is unknown
nc = treeMPS.degree; % number of subchains at node
nTemp = cell(1,nc);
Atemp = contracttensors(treeMPS.BondCenter,2,2,treeMPS.mps{1},nc+2,1);
for ii = 1:nc
treeMPS.child(ii).BondCenter = contracttensors(conj(treeMPS.mps{1}),nc+2,[1:ii,ii+2:nc+2],Atemp,nc+2,[1:ii,ii+2:nc+2]); % contract all except D_(ii+1)
nTemp{ii} = calBosonOcc_Tree(treeMPS.child(ii),para); % L x nc(child)
end
lengths = cellfun(@(x) size(x,1),nTemp);
nChains = cellfun(@(x) size(x,2),nTemp);
n = zeros(max(lengths)+1,sum(nChains)); % L x nc_max
mc = 0;
for ii = 1:nc
n(1+(1:lengths(ii)),mc+(1:nChains(ii))) = nTemp{ii}; % copy all together
mc = mc + nChains(ii);
end
end
end
function bosonshift = calBosonShift(mps,Vmat,para)
% Calculate boson shift x, x^2, var(x)
% The operator on the spin site is set to zero
% Modified:
% FS 22/01/2014: - changed to using para.foldedChain.
% FS 04/06/2014: - updated for spinposition array
%
L = para.L;
x_opx = cell(1,L);
x2_opx = cell(1,L);
for j = 1:L
if j ~= para.spinposition
if para.foldedChain == 1
% Constructs Supersite Operators
% Only measures kron(x,1) chain part
[bp,~,n] = bosonop(sqrt(para.dk(j)),para.shift(j),para.parity);
idm = eye(size(n));
bpx = kron(bp,idm); bmx = bpx'; %nx = kron(n,idm); unused
x_opx{j} = sqrt(2)/2.*(bpx+bmx);
x2_opx{j} = x_opx{j}*x_opx{j};
elseif para.foldedChain == 0
[bp,bm,~] = bosonop(para.dk(1,j),para.shift(1,j),para.parity);
x_opx{j} = sqrt(2)/2.*(bp+bm);
x2_opx{j} = x_opx{j}*x_opx{j};
end
else
x_opx{j} = zeros(para.dk(j));
x2_opx{j} = zeros(para.dk(j));
end
end
bosonshift.x = expectation_allsites(x_opx,mps,Vmat);
bosonshift.xsquare = expectation_allsites(x2_opx,mps,Vmat);
bosonshift.xvariant = sqrt(bosonshift.xsquare-bosonshift.x.^2);
bosonshift.xerror = mean(abs(para.shift-bosonshift.x));
end
function shift = cal1siteShift(mps,Vmat,para)
%% Calculates shift for a single site, para.sitej!
% for use of shifting procedure in optimizesite.m
% mps and Vmat are single-site matrices
% mps has to be focused
NC = para.nChains; j = para.sitej;
xOp_OBB = cell(1,NC); % one x-OBB operator per chain
shift = zeros(1,NC); % one value per chain
% Generate operators and contract with OBB
if para.foldedChain == 1
switch para.model
case 'SpinDoubleBoson' % calculates shift only for x chain!
bp = bosonop(sqrt(para.dk(j)),para.shift(j),para.parity);
if para.parity == 'n'
idm = eye(size(bp));
bpx = kron(bp,idm); bmx = bpx';
else
[bpx,bmx,~,~,~,~]=paritykron(bp,para.bosonparity);
end
x = sqrt(2)/2*(bpx+bmx);
case '2SpinPhononModel'
bp = bosonop(sqrt(para.dk(j)),para.shift(j),para.parity);
if para.parity=='n'
idm=eye(size(bp));
bpr=kron(bp,idm); bmr=bpr'; % right chain
% bpl=kron(idm,bp); bml=bpl'; % left chain
else
[bpr,bmr,~,~,~,~]=paritykron(bp,para.bosonparity);
end
x = sqrt(2)/2*(bpr+bmr); % why only evaluate it for right part?
end
if para.useVmat
xOp_OBB{1} = Vmat' * x * Vmat;
else
xOp_OBB{1} = x;
end
else
for mc = 1:NC
x = cell(1,NC);
[bp,bm,~] = bosonop(para.dk(mc,j),para.shift(mc,j),para.parity);
x{mc} = sqrt(2)/2*(bp+bm);
xOp_OBB{mc} = contractMultiChainOBB(Vmat, x, para); % _(n~',n~)
end
end
temp = contracttensors(conj(mps),3,[1,2],mps,3,[1,2]); % _(n~',n~)
for mc = 1:NC
shift(mc) = real(sum(sum(temp.*xOp_OBB{mc}))); % fast tr(A * B.')
end
end
function reducedDensity = calRDM(mps,Vmat,para,k)
% calculates the reduced density matrix of any single site, mps{k} for size(mps{k})=a_{k-1} x a_k x n_k
%
% created 24/05/2014 by Florian Schroeder @ University of Cambridge
%
%
% copied from prepare.m:
% does l -> r sweep to create state in local picture of k
para.sweepto = 'r';
if length(k) == 1 % single-site RDM
for i = 1:k-1
if para.useVmat==1
[Vmat{i},V] = prepare_onesiteVmat(Vmat{i},para); % Vmat = U * S * V' ; Vmat := U; V:= S * V'
mps{i} = contracttensors(mps{i},3,3,V,2,2); % = Ai_{l,r,n} * V'_{p,n}; This contraction is defined differently to the paper.
end
[mps{i}, U] = prepare_onesite(mps{i}, para,i); % SVD(Ai_(l,r,n)) = Ai_(l,m,n) * U_(m,r)
mps{i+1} = contracttensors(U,2,2,mps{i+1},3,1); % U_(m,l) * A(i+1)_(l,r,n)
para=gennonzeroindex(mps,Vmat,para,i); % only if parity not 'n'
para=gennonzeroindex(mps,Vmat,para,i+1); % only if parity not 'n'
end
% now in form: Al{1}...Al{k-1} M{k} Ar{k+1}...Ar{L}
% with Al = left-normalized, Ar: right-normalized.
n = ndims(mps{k});
reducedDensity = contracttensors(mps{k},n,1:n-1,conj(mps{k}),n,1:n-1); % contract rD_nm = Mk_abn Mk*_abm
% reducedDensity = Vmat{k} * (reducedDensity * Vmat{k}');
reducedDensity = contracttensors(reducedDensity,2,2,conj(Vmat{k}),2,2); % contract rD_nj = rD_nm Vmat*_jm
reducedDensity = contracttensors(Vmat{k},2,2,reducedDensity,2,1); % contract rD_ij = Vmat_in rd_nj
elseif length(k) == 2 && k(2) == k(1) + 1
% 2-site RDM of nearest neighbours!
% move to k(1)
for i = 1:k(1)-1
if para.useVmat==1
[Vmat{i},V] = prepare_onesiteVmat(Vmat{i},para); % Vmat = U * S * V' ; Vmat := U; V:= S * V'
mps{i} = contracttensors(mps{i},3,3,V,2,2); % = Ai_{l,r,n} * V'_{p,n}; This contraction is defined differently to the paper.
end
[mps{i}, U] = prepare_onesite(mps{i}, para,i); % SVD(Ai_(l,r,n)) = Ai_(l,m,n) * U_(m,r)
mps{i+1} = contracttensors(U,2,2,mps{i+1},3,1); % U_(m,l) * A(i+1)_(l,r,n)
para=gennonzeroindex(mps,Vmat,para,i); % only if parity not 'n'
para=gennonzeroindex(mps,Vmat,para,i+1); % only if parity not 'n'
end
% use ncon, since easy and not performance-critical!
reducedDensity = ncon({mps{k(1)}, mps{k(2)}, conj(mps{k(1)}), conj(mps{k(2)}), Vmat{k(1)}, Vmat{k(2)}, conj(Vmat{k(1)}), conj(Vmat{k(2)})},...
{[1,2,3], [2,4,5], [1,6,7], [6,4,8], [-2,3], [-1,5], [-4,7], [-3,8]});
end
end
function participation = calParticipation(rdm)
% rdm: a reduced density matrix of a single site
% e.g. rdm = calRDM(mps,Vmat,para,k)
%
% takes a rdm and calculates the participation ratio
participation = 1/sum(diag(rdm).^2);
end
function tunnelE = calTunnelingEnergy(mps,Vmat,para,op)
%% calculates the tunneling energy in MLSBM of the PPC system
% <Psi|H0-diag(H0)|Psi>
%
% could be applied to any system.
% Interacting System Hamiltonian: HI
HI = cell(1);
if ~exist('op','var') || ~isfield(op,'h1term')
disp('guessing H0 from para for MLSBM');
op.h1term{1,1} = Hamiltonian_PPC(para);
end
HI{1} = op.h1term{1,1}-diag(diag(op.h1term{1,1}));
tunnelE = expectationvalue(HI,mps,Vmat,mps,Vmat);
end
function An = calBath1SiteCorrelatorsProject(mps,Vmat,para,opSelect,stateProj,bondProj)
% calculates the (projected) single-site expectation value
% output is matrix containing all values for all chains (L x nChains)
% custom made solution for biggest speedup, exact solution!
% opSelect: ['bp'|'x'|'bp^2'|'x^2'|'n']
% stateProj: equals state-projection operator, normalised!
% []: no selection, take all
% bondProj: equals bond-projection operator, normalised!
% []: no selection, take entire bond
%
% supports Multi-Chain with Vmat and Vtens, Works with single chain!
% does NOT support StarMPS or TreeMPS input! This needs pre-processing
assert(para.foldedChain == 0, 'Please use single-chain code for folded Chains');
An = zeros(para.L, para.nChains); % initialize results array
op_OBB = cell( para.L, para.nChains);
%% generate all operators & contract with OBB Vmat / Vtens:
for j = 1:para.L
para.sitej = j;
for mc = 1:para.nChains
op = cell(1, para.nChains); % containing single-site, single-chain operator
if j ~= para.spinposition
switch opSelect
case 'bp'
op{mc} = bosonop(para.dk(mc,j),para.shift(mc,j),para.parity);
case 'x'
bp = bosonop(para.dk(mc,j),para.shift(mc,j),para.parity);
op{mc} = (bp+bp')/2;
case 'bp^2'
bp = bosonop(para.dk(mc,j),para.shift(mc,j),para.parity);
op{mc} = bp^2;
case 'x^2'
bp = bosonop(para.dk(mc,j),para.shift(mc,j),para.parity);
op{mc} = (bp+bp')^2/4;
case 'n'
[~,~,op{mc}] = bosonop(para.dk(mc,j),para.shift(mc,j),para.parity);
end
op_OBB{j, mc} = contractMultiChainOBB(Vmat{j}, op, para);
else % Spin & Multi-Level System
if ~isempty(stateProj)
op_OBB{j,mc} = Vmat{j}' * stateProj * Vmat{j}; % Spin-site Vmat, project onto system state!
else
op_OBB{j,mc} = Vmat{j}' * Vmat{j};
end
end
end
end
if isempty(stateProj) && ~isempty(bondProj)
% do SVD from site 1 to 2 in order to get projections onto the dominant system states!
% otherwise have projection onto dominant Bath states (from previous sweep to left)
A = mps{1};
[D1, D2, d] = size(A); % d is site dimension
A = permute(A, [3, 1, 2]);
A = reshape(A, [d * D1, D2]); % reshapes to (a1 d),a2
[B, S, U] = svd2(A); % Could also use QR decomposition if nargin !=5
DB = size(S, 1); % new a2 dimension of B should == d
B = reshape(B, [d, D1, DB]);
mps{1} = permute(B, [2, 3, 1]); % not used anymore!
% do not contract S into mps{2}, to avoid the displacement to be contaminated with the state amplitude!
mps{2} = contracttensors(U,2,2, mps{2},3,1);
end
%% compute all partial contractions
% such that (1+sz/4) gets updated to the right in row 1;
% Trace(contraction for result) = Am(j)
opContract = cell(para.L, 1); % same for all chains! since only 1 common MPS backbone
for j = 1:para.L
for mc = 1:para.nChains
if j ~= 1
An(j,mc) = trace(updateCleft(opContract{j-1,1},mps{j},[],op_OBB{j,mc},mps{j},[])); % this is the result!
end
end
if j ~=1