Skip to content

Commit d5a77be

Browse files
committed
Port functions from ITensorInfiniteMPS
1 parent a583788 commit d5a77be

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

src/itensor.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ dirs(A::ITensor, is) = dirs(inds(A), is)
840840

841841
# TODO: add isdiag(::Tensor) to NDTensors
842842
isdiag(T::ITensor)::Bool = (storage(T) isa Diag || storage(T) isa DiagBlockSparse)
843+
LinearAlgebra.isdiag(T::ITensor) = isdiag(T)
843844

844845
diaglength(T::ITensor) = diaglength(tensor(T))
845846

src/lib/ITensorMPS/src/mps.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,3 +1032,5 @@ end
10321032
function expect(psi::MPS, op1::Matrix{<:Number}, ops::Matrix{<:Number}...; kwargs...)
10331033
return expect(psi, (op1, ops...); kwargs...)
10341034
end
1035+
1036+
Base.getindex::MPS, r::UnitRange{Int}) = MPS([ψ[n] for n in r])

src/lib/SmallStrings/src/smallstring.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ end
102102
Base.:(==)(s1::SmallString, s2::SmallString) = (s1.data == s2.data)
103103
Base.isless(s1::SmallString, s2::SmallString) = isless(s1.data, s2.data)
104104

105+
maxlength(s::SmallString) = length(s.data)
106+
107+
function Base.length(s::SmallString)
108+
n = 1
109+
while n <= maxlength(s) && s[n] != zero(eltype(s))
110+
n += 1
111+
end
112+
return n - 1
113+
end
114+
115+
Base.lastindex(s::SmallString) = length(s)
116+
Base.getindex(s::SmallString, r::UnitRange) = SmallString([s[n] for n in r])
117+
118+
119+
# TODO: make this work directly on a Tag, without converting
120+
# to String
121+
function Base.parse(::Type{T}, s::SmallString) where {T<:Integer}
122+
return parse(T, string(s))
123+
end
124+
125+
function Base.startswith(s::SmallString, subtag::SmallString)
126+
for n in 1:length(subtag)
127+
s[n] subtag[n] && return false
128+
end
129+
return true
130+
end
131+
105132
########################################################
106133
# Here are alternative SmallString comparison implementations
107134
#

src/lib/TagSets/src/TagSets.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ data(T::TagSet) = T.data
215215
Base.length(T::TagSet) = T.length
216216
Base.@propagate_inbounds Base.getindex(T::TagSet, n::Integer) = SmallString(data(T)[n])
217217
Base.copy(ts::TagSet) = TagSet(data(ts), length(ts))
218+
Base.keys(ts::TagSet) = Base.OneTo(length(ts))
218219

219220
function Base.:(==)(ts1::TagSet, ts2::TagSet)
220221
l1 = length(ts1)

src/tensor_operations/matrix_decomposition.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,27 @@ function sqrt_decomp(D::ITensor, u::Index, v::Index)
590590
return sqrtDL, prime(δᵤᵥ), sqrtDR
591591
end
592592

593+
# Take the square root of T assuming it is Hermitian
594+
# TODO: add more general index structures
595+
function Base.sqrt(T::ITensor; ishermitian=true, atol=1e-15)
596+
@assert ishermitian
597+
# TODO diagonal version
598+
#if isdiag(T) && order(T) == 2
599+
# return itensor(sqrt(tensor(T)))
600+
#end
601+
D, U = eigen(T; ishermitian=ishermitian)
602+
sqrtD = D
603+
for n in 1:mindim(D)
604+
Dnn = D[n, n]
605+
if Dnn < 0 && abs(Dnn) < atol
606+
sqrtD[n, n] = 0
607+
else
608+
sqrtD[n, n] = sqrt(Dnn)
609+
end
610+
end
611+
return U' * sqrtD * dag(U)
612+
end
613+
593614
function factorize_svd(
594615
A::ITensor,
595616
Linds...;

0 commit comments

Comments
 (0)