/
abstractmps.jl
2414 lines (1975 loc) · 64.1 KB
/
abstractmps.jl
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
using IsApprox: Approx, IsApprox
using ..ITensors: ITensors
using NDTensors: NDTensors, using_auto_fermion, scalartype, tensor
using ..QuantumNumbers: QuantumNumbers, removeqn
using ..SiteTypes: SiteTypes, siteinds
using ..TagSets: TagSets
abstract type AbstractMPS end
"""
length(::MPS/MPO)
The number of sites of an MPS/MPO.
"""
Base.length(m::AbstractMPS) = length(data(m))
"""
size(::MPS/MPO)
The number of sites of an MPS/MPO, in a tuple.
"""
Base.size(m::AbstractMPS) = size(data(m))
Base.ndims(m::AbstractMPS) = ndims(data(m))
function promote_itensor_eltype(m::Vector{ITensor})
T = isassigned(m, 1) ? eltype(m[1]) : Number
for n in 2:length(m)
Tn = isassigned(m, n) ? eltype(m[n]) : Number
T = promote_type(T, Tn)
end
return T
end
function LinearAlgebra.promote_leaf_eltypes(m::Vector{ITensor})
return promote_itensor_eltype(m)
end
function LinearAlgebra.promote_leaf_eltypes(m::AbstractMPS)
return LinearAlgebra.promote_leaf_eltypes(data(m))
end
"""
promote_itensor_eltype(m::MPS)
promote_itensor_eltype(m::MPO)
Return the promotion of the elements type of the
tensors in the MPS or MPO. For example,
if all tensors have type `Float64` then
return `Float64`. But if one or more tensors
have type `ComplexF64`, return `ComplexF64`.
"""
promote_itensor_eltype(m::AbstractMPS) = LinearAlgebra.promote_leaf_eltypes(m)
NDTensors.scalartype(m::AbstractMPS) = LinearAlgebra.promote_leaf_eltypes(m)
NDTensors.scalartype(m::Array{ITensor}) = LinearAlgebra.promote_leaf_eltypes(m)
NDTensors.scalartype(m::Array{<:Array{ITensor}}) = LinearAlgebra.promote_leaf_eltypes(m)
"""
eltype(m::MPS)
eltype(m::MPO)
The element type of the MPS/MPO. Always returns `ITensor`.
For the element type of the ITensors of the MPS/MPO,
use `promote_itensor_eltype`.
"""
Base.eltype(::AbstractMPS) = ITensor
Base.complex(ψ::AbstractMPS) = complex.(ψ)
Base.real(ψ::AbstractMPS) = real.(ψ)
Base.imag(ψ::AbstractMPS) = imag.(ψ)
Base.conj(ψ::AbstractMPS) = conj.(ψ)
function convert_leaf_eltype(eltype::Type, ψ::AbstractMPS)
return map(ψᵢ -> convert_leaf_eltype(eltype, ψᵢ), ψ; set_limits=false)
end
"""
ITensors.data(::MPS/MPO)
Returns a view of the Vector storage of an MPS/MPO.
This is not exported and mostly for internal usage, please let us
know if there is functionality not available for MPS/MPO you would like.
"""
data(m::AbstractMPS) = m.data
ITensors.contract(ψ::AbstractMPS) = contract(data(ψ))
leftlim(m::AbstractMPS) = m.llim
rightlim(m::AbstractMPS) = m.rlim
function setleftlim!(m::AbstractMPS, new_ll::Integer)
return m.llim = new_ll
end
function setrightlim!(m::AbstractMPS, new_rl::Integer)
return m.rlim = new_rl
end
"""
ortho_lims(::MPS/MPO)
Returns the range of sites of the orthogonality center of the MPS/MPO.
# Examples
```julia
s = siteinds("S=½", 5)
ψ = randomMPS(s)
ψ = orthogonalize(ψ, 3)
# ortho_lims(ψ) = 3:3
@show ortho_lims(ψ)
ψ[2] = randomITensor(inds(ψ[2]))
# ortho_lims(ψ) = 2:3
@show ortho_lims(ψ)
```
"""
function ortho_lims(ψ::AbstractMPS)
return (leftlim(ψ) + 1):(rightlim(ψ) - 1)
end
"""
ITensors.set_ortho_lims!(::MPS/MPO, r::UnitRange{Int})
Sets the range of sites of the orthogonality center of the MPS/MPO.
This is not exported and is an advanced feature that should be used with
care. Setting the orthogonality limits wrong can lead to incorrect results
when using ITensor MPS/MPO functions.
If you are modifying an MPS/MPO and want the orthogonality limits to be
preserved, please see the `@preserve_ortho` macro.
"""
function set_ortho_lims!(ψ::AbstractMPS, r::UnitRange{Int})
setleftlim!(ψ, first(r) - 1)
setrightlim!(ψ, last(r) + 1)
return ψ
end
function set_ortho_lims(ψ::AbstractMPS, r::UnitRange{Int})
return set_ortho_lims!(copy(ψ), r)
end
reset_ortho_lims!(ψ::AbstractMPS) = set_ortho_lims!(ψ, 1:length(ψ))
isortho(m::AbstractMPS) = leftlim(m) + 1 == rightlim(m) - 1
# Could also define as `only(ortho_lims)`
function orthocenter(m::AbstractMPS)
!isortho(m) && error(
"$(typeof(m)) has no well-defined orthogonality center, orthogonality center is on the range $(ortho_lims(m)).",
)
return leftlim(m) + 1
end
getindex(M::AbstractMPS, n) = getindex(data(M), n)
isassigned(M::AbstractMPS, n) = isassigned(data(M), n)
lastindex(M::AbstractMPS) = lastindex(data(M))
"""
@preserve_ortho
Specify that a block of code preserves the orthogonality limits of
an MPS/MPO that is being modified in-place. The first input is either
a single MPS/MPO or a tuple of the MPS/MPO whose orthogonality limits
should be preserved.
# Examples
```julia
s = siteinds("S=1/2", 4)
# Make random MPS with bond dimension 2
ψ₁ = randomMPS(s, "↑", 2)
ψ₂ = randomMPS(s, "↑", 2)
ψ₁ = orthogonalize(ψ₁, 1)
ψ₂ = orthogonalize(ψ₂, 1)
# ortho_lims(ψ₁) = 1:1
@show ortho_lims(ψ₁)
# ortho_lims(ψ₂) = 1:1
@show ortho_lims(ψ₂)
@preserve_ortho (ψ₁, ψ₂) begin
ψ₁ .= addtags.(ψ₁, "x₁"; tags = "Link")
ψ₂ .= addtags.(ψ₂, "x₂"; tags = "Link")
end
# ortho_lims(ψ₁) = 1:1
@show ortho_lims(ψ₁)
# ortho_lims(ψ₂) = 1:1
@show ortho_lims(ψ₂)
```
"""
macro preserve_ortho(ψ, block)
quote
if $(esc(ψ)) isa AbstractMPS
local ortho_limsψ = ortho_lims($(esc(ψ)))
else
local ortho_limsψ = ortho_lims.($(esc(ψ)))
end
r = $(esc(block))
if $(esc(ψ)) isa AbstractMPS
set_ortho_lims!($(esc(ψ)), ortho_limsψ)
else
set_ortho_lims!.($(esc(ψ)), ortho_limsψ)
end
r
end
end
function setindex!(M::AbstractMPS, T::ITensor, n::Integer; set_limits::Bool=true)
if set_limits
(n <= leftlim(M)) && setleftlim!(M, n - 1)
(n >= rightlim(M)) && setrightlim!(M, n + 1)
end
data(M)[n] = T
return M
end
function setindex!(M::MPST, v::MPST, ::Colon) where {MPST<:AbstractMPS}
setleftlim!(M, leftlim(v))
setrightlim!(M, rightlim(v))
data(M)[:] = data(v)
return M
end
setindex!(M::AbstractMPS, v::Vector{<:ITensor}, ::Colon) = setindex!(M, MPS(v), :)
"""
copy(::MPS)
copy(::MPO)
Make a shallow copy of an MPS or MPO. By shallow copy, it means that a new MPS/MPO
is returned, but the data of the tensors are still shared between the returned MPS/MPO
and the original MPS/MPO.
Therefore, replacing an entire tensor of the returned MPS/MPO will not modify the input MPS/MPO,
but modifying the data of the returned MPS/MPO will modify the input MPS/MPO.
Use [`deepcopy`](@ref) for an alternative that copies the ITensors as well.
# Examples
```julia
julia> using ITensors
julia> s = siteinds("S=1/2", 3);
julia> M1 = randomMPS(s; linkdims=3);
julia> norm(M1)
0.9999999999999999
julia> M2 = copy(M1);
julia> M2[1] *= 2;
julia> norm(M1)
0.9999999999999999
julia> norm(M2)
1.9999999999999998
julia> M3 = copy(M1);
julia> M3[1] .*= 3; # Modifies the tensor data
julia> norm(M1)
3.0000000000000004
julia> norm(M3)
3.0000000000000004
```
"""
Base.copy(m::AbstractMPS) = typeof(m)(copy(data(m)), leftlim(m), rightlim(m))
Base.similar(m::AbstractMPS) = typeof(m)(similar(data(m)), 0, length(m))
"""
deepcopy(::MPS)
deepcopy(::MPO)
Make a deep copy of an MPS or MPO. By deep copy, it means that a new MPS/MPO
is returned that doesn't share any data with the input MPS/MPO.
Therefore, modifying the resulting MPS/MPO will note modify the original MPS/MPO.
Use [`copy`](@ref) for an alternative that performs a shallow copy that avoids
copying the ITensor data.
# Examples
```julia
julia> using ITensors
julia> s = siteinds("S=1/2", 3);
julia> M1 = randomMPS(s; linkdims=3);
julia> norm(M1)
1.0
julia> M2 = deepcopy(M1);
julia> M2[1] .*= 2; # Modifies the tensor data
julia> norm(M1)
1.0
julia> norm(M2)
2.0
julia> M3 = copy(M1);
julia> M3[1] .*= 3; # Modifies the tensor data
julia> norm(M1)
3.0
julia> norm(M3)
3.0
```
"""
deepcopy(m::AbstractMPS) = typeof(m)(copy.(data(m)), leftlim(m), rightlim(m))
eachindex(m::AbstractMPS) = 1:length(m)
iterate(M::AbstractMPS) = iterate(data(M))
iterate(M::AbstractMPS, state) = iterate(data(M), state)
"""
linkind(M::MPS, j::Integer)
linkind(M::MPO, j::Integer)
Get the link or bond Index connecting the
MPS or MPO tensor on site j to site j+1.
If there is no link Index, return `nothing`.
"""
function linkind(M::AbstractMPS, j::Integer)
N = length(M)
(j ≥ length(M) || j < 1) && return nothing
return commonind(M[j], M[j + 1])
end
"""
linkinds(M::MPS, j::Integer)
linkinds(M::MPO, j::Integer)
Get all of the link or bond Indices connecting the
MPS or MPO tensor on site j to site j+1.
"""
function linkinds(M::AbstractMPS, j::Integer)
N = length(M)
(j ≥ length(M) || j < 1) && return IndexSet()
return commoninds(M[j], M[j + 1])
end
linkinds(ψ::AbstractMPS) = [linkind(ψ, b) for b in 1:(length(ψ) - 1)]
function linkinds(::typeof(all), ψ::AbstractMPS)
return IndexSet[linkinds(ψ, b) for b in 1:(length(ψ) - 1)]
end
#
# Internal tools for checking for default link tags
#
"""
ITensors.defaultlinktags(b::Integer)
Default link tags for link index connecting sites `b` to `b+1`.
"""
defaultlinktags(b::Integer) = TagSet("Link,l=$b")
"""
ITensors.hasdefaultlinktags(ψ::MPS/MPO)
Return true if the MPS/MPO has default link tags.
"""
function hasdefaultlinktags(ψ::AbstractMPS)
ls = linkinds(all, ψ)
for (b, lb) in enumerate(ls)
if length(lb) ≠ 1 || tags(only(lb)) ≠ defaultlinktags(b)
return false
end
end
return true
end
"""
ITensors.eachlinkinds(ψ::MPS/MPO)
Return an iterator over each of the sets of link indices of the MPS/MPO.
"""
eachlinkinds(ψ::AbstractMPS) = (linkinds(ψ, n) for n in eachindex(ψ)[1:(end - 1)])
"""
ITensors.eachsiteinds(ψ::MPS/MPO)
Return an iterator over each of the sets of site indices of the MPS/MPO.
"""
eachsiteinds(ψ::AbstractMPS) = (siteinds(ψ, n) for n in eachindex(ψ))
"""
ITensors.hasnolinkinds(ψ::MPS/MPO)
Return true if the MPS/MPO has no link indices.
"""
function hasnolinkinds(ψ::AbstractMPS)
for l in eachlinkinds(ψ)
if length(l) > 0
return false
end
end
return true
end
"""
ITensors.insertlinkinds(ψ::MPS/MPO)
If any link indices are missing, insert default ones.
"""
function insertlinkinds(ψ::AbstractMPS)
ψ = copy(ψ)
space = hasqns(ψ) ? [QN() => 1] : 1
linkind(b::Integer) = Index(space; tags=defaultlinktags(b))
for b in 1:(length(ψ) - 1)
if length(linkinds(ψ, b)) == 0
lb = ITensor(1, linkind(b))
@preserve_ortho ψ begin
ψ[b] = ψ[b] * lb
ψ[b + 1] = ψ[b + 1] * dag(lb)
end
end
end
return ψ
end
"""
dense(::MPS/MPO)
Given an MPS (or MPO), return a new MPS (or MPO)
having called `dense` on each ITensor to convert each
tensor to use dense storage and remove any QN or other
sparse structure information, if it is not dense already.
"""
function dense(ψ::AbstractMPS)
ψ = copy(ψ)
@preserve_ortho ψ ψ .= dense.(ψ)
return ψ
end
"""
siteinds(uniqueinds, A::MPO, B::MPS, j::Integer; kwargs...)
siteinds(uniqueind, A::MPO, B::MPS, j::Integer; kwargs...)
Get the site index (or indices) of MPO `A` that is unique to `A` (not shared with MPS/MPO `B`).
"""
function SiteTypes.siteinds(
f::Union{typeof(uniqueinds),typeof(uniqueind)},
A::AbstractMPS,
B::AbstractMPS,
j::Integer;
kwargs...,
)
N = length(A)
N == 1 && return f(A[j], B[j]; kwargs...)
j == 1 && return f(A[j], A[j + 1], B[j]; kwargs...)
j == N && return f(A[j], A[j - 1], B[j]; kwargs...)
return f(A[j], A[j - 1], A[j + 1], B[j]; kwargs...)
end
"""
siteinds(uniqueinds, A::MPO, B::MPS)
siteinds(uniqueind, A::MPO, B::MPO)
Get the site indices of MPO `A` that are unique to `A` (not shared with MPS/MPO `B`), as a `Vector{<:Index}`.
"""
function SiteTypes.siteinds(
f::Union{typeof(uniqueinds),typeof(uniqueind)}, A::AbstractMPS, B::AbstractMPS; kwargs...
)
return [siteinds(f, A, B, j; kwargs...) for j in eachindex(A)]
end
"""
siteinds(commoninds, A::MPO, B::MPS, j::Integer; kwargs...)
siteinds(commonind, A::MPO, B::MPO, j::Integer; kwargs...)
Get the site index (or indices) of the `j`th MPO tensor of `A` that is shared with MPS/MPO `B`.
"""
function SiteTypes.siteinds(
f::Union{typeof(commoninds),typeof(commonind)},
A::AbstractMPS,
B::AbstractMPS,
j::Integer;
kwargs...,
)
return f(A[j], B[j]; kwargs...)
end
"""
siteinds(commoninds, A::MPO, B::MPS; kwargs...)
siteinds(commonind, A::MPO, B::MPO; kwargs...)
Get a vector of the site index (or indices) of MPO `A` that is shared with MPS/MPO `B`.
"""
function SiteTypes.siteinds(
f::Union{typeof(commoninds),typeof(commonind)}, A::AbstractMPS, B::AbstractMPS; kwargs...
)
return [siteinds(f, A, B, j) for j in eachindex(A)]
end
keys(ψ::AbstractMPS) = keys(data(ψ))
#
# Find sites of an MPS or MPO
#
# TODO: accept a keyword argument sitedict that
# is a dictionary from the site indices to the site.
"""
findsite(M::Union{MPS, MPO}, is)
Return the first site of the MPS or MPO that has at least one
Index in common with the Index or collection of indices `is`.
To find all sites with common indices with `is`, use the
`findsites` function.
# Examples
```julia
s = siteinds("S=1/2", 5)
ψ = randomMPS(s)
findsite(ψ, s[3]) == 3
findsite(ψ, (s[3], s[4])) == 3
M = MPO(s)
findsite(M, s[4]) == 4
findsite(M, s[4]') == 4
findsite(M, (s[4]', s[4])) == 4
findsite(M, (s[4]', s[3])) == 3
```
"""
findsite(ψ::AbstractMPS, is) = findfirst(hascommoninds(is), ψ)
findsite(ψ::AbstractMPS, s::Index) = findsite(ψ, IndexSet(s))
#
# TODO: Maybe make:
# findall(f::Function, siteindsM::Tuple{typeof(siteinds), ::AbstractMPS})
# findall(siteindsM::Tuple{typeof(siteinds), <:AbstractMPS}, is) =
# findall(hassameinds(is), siteindsM)
#
"""
findsites(M::Union{MPS, MPO}, is)
Return the sites of the MPS or MPO that have
indices in common with the collection of site indices
`is`.
# Examples
```julia
s = siteinds("S=1/2", 5)
ψ = randomMPS(s)
findsites(ψ, s[3]) == [3]
findsites(ψ, (s[4], s[1])) == [1, 4]
M = MPO(s)
findsites(M, s[4]) == [4]
findsites(M, s[4]') == [4]
findsites(M, (s[4]', s[4])) == [4]
findsites(M, (s[4]', s[3])) == [3, 4]
```
"""
findsites(ψ::AbstractMPS, is) = findall(hascommoninds(is), ψ)
findsites(ψ::AbstractMPS, s::Index) = findsites(ψ, IndexSet(s))
#
# TODO: Maybe make:
# findfirst(f::Function, siteindsM::Tuple{typeof(siteinds), ::AbstractMPS})
# findfirst(siteindsM::Tuple{typeof(siteinds), <:AbstractMPS}, is) =
# findfirst(hassameinds(is), siteindsM)
#
"""
findfirstsiteind(M::MPS, i::Index)
findfirstsiteind(M::MPO, i::Index)
Return the first site of the MPS or MPO that has the
site index `i`.
"""
findfirstsiteind(ψ::AbstractMPS, s::Index) = findfirst(hasind(s), ψ)
# TODO: depracate in favor of findsite.
"""
findfirstsiteinds(M::MPS, is)
findfirstsiteinds(M::MPO, is)
Return the first site of the MPS or MPO that has the
site indices `is`.
"""
findfirstsiteinds(ψ::AbstractMPS, s) = findfirst(hasinds(s), ψ)
"""
siteind(::typeof(first), M::Union{MPS,MPO}, j::Integer; kwargs...)
Return the first site Index found on the MPS or MPO
(the first Index unique to the `j`th MPS/MPO tensor).
You can choose different filters, like prime level
and tags, with the `kwargs`.
"""
function SiteTypes.siteind(::typeof(first), M::AbstractMPS, j::Integer; kwargs...)
N = length(M)
(N == 1) && return firstind(M[1]; kwargs...)
if j == 1
si = uniqueind(M[j], M[j + 1]; kwargs...)
elseif j == N
si = uniqueind(M[j], M[j - 1]; kwargs...)
else
si = uniqueind(M[j], M[j - 1], M[j + 1]; kwargs...)
end
return si
end
"""
siteinds(M::Union{MPS, MPO}}, j::Integer; kwargs...)
Return the site Indices found of the MPO or MPO
at the site `j` as an IndexSet.
Optionally filter prime tags and prime levels with
keyword arguments like `plev` and `tags`.
"""
function SiteTypes.siteinds(M::AbstractMPS, j::Integer; kwargs...)
N = length(M)
(N == 1) && return inds(M[1]; kwargs...)
if j == 1
si = uniqueinds(M[j], M[j + 1]; kwargs...)
elseif j == N
si = uniqueinds(M[j], M[j - 1]; kwargs...)
else
si = uniqueinds(M[j], M[j - 1], M[j + 1]; kwargs...)
end
return si
end
function SiteTypes.siteinds(::typeof(all), ψ::AbstractMPS, n::Integer; kwargs...)
return siteinds(ψ, n; kwargs...)
end
function SiteTypes.siteinds(::typeof(first), ψ::AbstractMPS; kwargs...)
return [siteind(first, ψ, j; kwargs...) for j in 1:length(ψ)]
end
function SiteTypes.siteinds(::typeof(only), ψ::AbstractMPS; kwargs...)
return [siteind(only, ψ, j; kwargs...) for j in 1:length(ψ)]
end
function SiteTypes.siteinds(::typeof(all), ψ::AbstractMPS; kwargs...)
return [siteinds(ψ, j; kwargs...) for j in 1:length(ψ)]
end
function replaceinds!(::typeof(linkinds), M::AbstractMPS, l̃s::Vector{<:Index})
for i in eachindex(M)[1:(end - 1)]
l = linkind(M, i)
l̃ = l̃s[i]
if !isnothing(l)
@preserve_ortho M begin
M[i] = replaceinds(M[i], l => l̃)
M[i + 1] = replaceinds(M[i + 1], l => l̃)
end
end
end
return M
end
function replaceinds(::typeof(linkinds), M::AbstractMPS, l̃s::Vector{<:Index})
return replaceinds!(linkinds, copy(M), l̃s)
end
# TODO: change kwarg from `set_limits` to `preserve_ortho`
function map!(f::Function, M::AbstractMPS; set_limits::Bool=true)
for i in eachindex(M)
M[i, set_limits=set_limits] = f(M[i])
end
return M
end
# TODO: change kwarg from `set_limits` to `preserve_ortho`
function map(f::Function, M::AbstractMPS; set_limits::Bool=true)
return map!(f, copy(M); set_limits=set_limits)
end
for (fname, fname!) in [
(:(ITensors.dag), :(dag!)),
(:(ITensors.prime), :(ITensors.prime!)),
(:(ITensors.setprime), :(ITensors.setprime!)),
(:(ITensors.noprime), :(ITensors.noprime!)),
(:(ITensors.swapprime), :(ITensors.swapprime!)),
(:(ITensors.replaceprime), :(ITensors.replaceprime!)),
(:(TagSets.addtags), :(ITensors.addtags!)),
(:(TagSets.removetags), :(ITensors.removetags!)),
(:(TagSets.replacetags), :(ITensors.replacetags!)),
(:(ITensors.settags), :(ITensors.settags!)),
]
@eval begin
"""
$($fname)[!](M::MPS, args...; kwargs...)
$($fname)[!](M::MPO, args...; kwargs...)
Apply $($fname) to all ITensors of an MPS/MPO, returning a new MPS/MPO.
The ITensors of the MPS/MPO will be a view of the storage of the original ITensors. Alternatively apply the function in-place.
"""
function $fname(M::AbstractMPS, args...; set_limits::Bool=false, kwargs...)
return map(m -> $fname(m, args...; kwargs...), M; set_limits=set_limits)
end
function $(fname!)(M::AbstractMPS, args...; set_limits::Bool=false, kwargs...)
return map!(m -> $fname(m, args...; kwargs...), M; set_limits=set_limits)
end
end
end
adjoint(M::AbstractMPS) = prime(M)
function hascommoninds(::typeof(siteinds), A::AbstractMPS, B::AbstractMPS)
N = length(A)
for n in 1:N
!hascommoninds(siteinds(A, n), siteinds(B, n)) && return false
end
return true
end
function check_hascommoninds(::typeof(siteinds), A::AbstractMPS, B::AbstractMPS)
N = length(A)
if length(B) ≠ N
throw(
DimensionMismatch(
"$(typeof(A)) and $(typeof(B)) have mismatched lengths $N and $(length(B))."
),
)
end
for n in 1:N
!hascommoninds(siteinds(A, n), siteinds(B, n)) && error(
"$(typeof(A)) A and $(typeof(B)) B must share site indices. On site $n, A has site indices $(siteinds(A, n)) while B has site indices $(siteinds(B, n)).",
)
end
return nothing
end
function map!(f::Function, ::typeof(linkinds), M::AbstractMPS)
for i in eachindex(M)[1:(end - 1)]
l = linkinds(M, i)
if !isempty(l)
l̃ = f(l)
@preserve_ortho M begin
M[i] = replaceinds(M[i], l, l̃)
M[i + 1] = replaceinds(M[i + 1], l, l̃)
end
end
end
return M
end
map(f::Function, ::typeof(linkinds), M::AbstractMPS) = map!(f, linkinds, copy(M))
function map!(f::Function, ::typeof(siteinds), M::AbstractMPS)
for i in eachindex(M)
s = siteinds(M, i)
if !isempty(s)
@preserve_ortho M begin
M[i] = replaceinds(M[i], s, f(s))
end
end
end
return M
end
map(f::Function, ::typeof(siteinds), M::AbstractMPS) = map!(f, siteinds, copy(M))
function map!(
f::Function, ::typeof(siteinds), ::typeof(commoninds), M1::AbstractMPS, M2::AbstractMPS
)
length(M1) != length(M2) && error("MPOs/MPSs must be the same length")
for i in eachindex(M1)
s = siteinds(commoninds, M1, M2, i)
if !isempty(s)
s̃ = f(s)
@preserve_ortho (M1, M2) begin
M1[i] = replaceinds(M1[i], s .=> s̃)
M2[i] = replaceinds(M2[i], s .=> s̃)
end
end
end
return M1, M2
end
function map!(
f::Function, ::typeof(siteinds), ::typeof(uniqueinds), M1::AbstractMPS, M2::AbstractMPS
)
length(M1) != length(M2) && error("MPOs/MPSs must be the same length")
for i in eachindex(M1)
s = siteinds(uniqueinds, M1, M2, i)
if !isempty(s)
@preserve_ortho M1 begin
M1[i] = replaceinds(M1[i], s .=> f(s))
end
end
end
return M1
end
function map(
f::Function,
ffilter::typeof(siteinds),
fsubset::Union{typeof(commoninds),typeof(uniqueinds)},
M1::AbstractMPS,
M2::AbstractMPS,
)
return map!(f, ffilter, fsubset, copy(M1), copy(M2))
end
function hassameinds(::typeof(siteinds), M1::AbstractMPS, M2::AbstractMPS)
length(M1) ≠ length(M2) && return false
for n in 1:length(M1)
!hassameinds(siteinds(all, M1, n), siteinds(all, M2, n)) && return false
end
return true
end
function hassamenuminds(::typeof(siteinds), M1::AbstractMPS, M2::AbstractMPS)
length(M1) ≠ length(M2) && return false
for n in 1:length(M1)
length(siteinds(M1, n)) ≠ length(siteinds(M2, n)) && return false
end
return true
end
for (fname, fname!) in [
(:(NDTensors.sim), :(sim!)),
(:(ITensors.prime), :(ITensors.prime!)),
(:(ITensors.setprime), :(ITensors.setprime!)),
(:(ITensors.noprime), :(ITensors.noprime!)),
(:(TagSets.addtags), :(ITensors.addtags!)),
(:(TagSets.removetags), :(ITensors.removetags!)),
(:(TagSets.replacetags), :(ITensors.replacetags!)),
(:(ITensors.settags), :(ITensors.settags!)),
]
@eval begin
"""
$($fname)[!](linkinds, M::MPS, args...; kwargs...)
$($fname)[!](linkinds, M::MPO, args...; kwargs...)
Apply $($fname) to all link indices of an MPS/MPO, returning a new MPS/MPO.
The ITensors of the MPS/MPO will be a view of the storage of the original ITensors.
"""
function $fname(ffilter::typeof(linkinds), M::AbstractMPS, args...; kwargs...)
return map(i -> $fname(i, args...; kwargs...), ffilter, M)
end
function $(fname!)(ffilter::typeof(linkinds), M::AbstractMPS, args...; kwargs...)
return map!(i -> $fname(i, args...; kwargs...), ffilter, M)
end
"""
$($fname)[!](siteinds, M::MPS, args...; kwargs...)
$($fname)[!](siteinds, M::MPO, args...; kwargs...)
Apply $($fname) to all site indices of an MPS/MPO, returning a new MPS/MPO.
The ITensors of the MPS/MPO will be a view of the storage of the original ITensors.
"""
function $fname(ffilter::typeof(siteinds), M::AbstractMPS, args...; kwargs...)
return map(i -> $fname(i, args...; kwargs...), ffilter, M)
end
function $(fname!)(ffilter::typeof(siteinds), M::AbstractMPS, args...; kwargs...)
return map!(i -> $fname(i, args...; kwargs...), ffilter, M)
end
"""
$($fname)[!](siteinds, commoninds, M1::MPO, M2::MPS, args...; kwargs...)
$($fname)[!](siteinds, commoninds, M1::MPO, M2::MPO, args...; kwargs...)
Apply $($fname) to the site indices that are shared by `M1` and `M2`.
Returns new MPSs/MPOs. The ITensors of the MPSs/MPOs will be a view of the storage of the original ITensors.
"""
function $fname(
ffilter::typeof(siteinds),
fsubset::typeof(commoninds),
M1::AbstractMPS,
M2::AbstractMPS,
args...;
kwargs...,
)
return map(i -> $fname(i, args...; kwargs...), ffilter, fsubset, M1, M2)
end
function $(fname!)(
ffilter::typeof(siteinds),
fsubset::typeof(commoninds),
M1::AbstractMPS,
M2::AbstractMPS,
args...;
kwargs...,
)
return map!(i -> $fname(i, args...; kwargs...), ffilter, fsubset, M1, M2)
end
"""
$($fname)[!](siteinds, uniqueinds, M1::MPO, M2::MPS, args...; kwargs...)
Apply $($fname) to the site indices of `M1` that are not shared with `M2`. Returns new MPSs/MPOs.
The ITensors of the MPSs/MPOs will be a view of the storage of the original ITensors.
"""
function $fname(
ffilter::typeof(siteinds),
fsubset::typeof(uniqueinds),
M1::AbstractMPS,
M2::AbstractMPS,
args...;
kwargs...,
)
return map(i -> $fname(i, args...; kwargs...), ffilter, fsubset, M1, M2)
end
function $(fname!)(
ffilter::typeof(siteinds),
fsubset::typeof(uniqueinds),
M1::AbstractMPS,
M2::AbstractMPS,
args...;
kwargs...,
)
return map!(i -> $fname(i, args...; kwargs...), ffilter, fsubset, M1, M2)
end
end
end
"""
maxlinkdim(M::MPS)
maxlinkdim(M::MPO)
Get the maximum link dimension of the MPS or MPO.
The minimum this will return is `1`, even if there
are no link indices.
"""
function maxlinkdim(M::AbstractMPS)
md = 1
for b in eachindex(M)[1:(end - 1)]
l = linkind(M, b)
linkdim = isnothing(l) ? 1 : dim(l)
md = max(md, linkdim)
end
return md
end
"""
linkdim(M::MPS, j::Integer)
linkdim(M::MPO, j::Integer)
Get the dimension of the link or bond connecting the
MPS or MPO tensor on site j to site j+1.
If there is no link Index, return `nothing`.
"""
function linkdim(ψ::AbstractMPS, b::Integer)
l = linkind(ψ, b)
isnothing(l) && return nothing
return dim(l)
end
linkdims(ψ::AbstractMPS) = [linkdim(ψ, b) for b in 1:(length(ψ) - 1)]
function inner_mps_mps_deprecation_warning()
return """
Calling `inner(x::MPS, y::MPS)` where the site indices of the `MPS` `x` and `y`
don't match is deprecated as of ITensor v0.3 and will result in an error in ITensor
v0.4. Likely you are attempting to take the inner product of MPS that have site indices
with mismatched prime levels. The most common cause of this is something like the following:
```julia
s = siteinds("S=1/2")