Skip to content

Commit 0b58b84

Browse files
AFeuerpfeillkdvos
andauthored
Refactor entropy function to also use spectrum directly (#377)
* Refactor entropy function to also use spectrum directly * specialize to SectorVector and add test * add infinite test * update docstring --------- Co-authored-by: Lukas Devos <ldevos98@gmail.com>
1 parent 2220cd1 commit 0b58b84

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

src/algorithms/toolbox.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
"""
22
entropy(state, [site::Int])
3+
entropy(spectrum::SectorVector)
34
4-
Calculate the von Neumann entanglement entropy of a given MPS. If an integer `site` is
5-
given, the entropy is across the entanglement cut to the right of site `site`. Otherwise, a
6-
vector of entropies is returned, one for each site.
5+
Calculate the von Neumann entanglement entropy. The entropy can be computed from either an
6+
MPS state or directly from an entanglement spectrum as obtained from
7+
[`entanglement_spectrum`](@ref).
8+
9+
When called on an MPS with an integer `site`, the entropy is computed across the
10+
entanglement cut to the right of site `site`. For `InfiniteMPS`, omitting `site` returns a
11+
vector of entropies, one for each site. For `FiniteMPS` and `WindowMPS`, `site` is
12+
required.
713
"""
814
entropy(state::InfiniteMPS) = map(Base.Fix1(entropy, state), 1:length(state))
915
function entropy(state::Union{FiniteMPS, WindowMPS, InfiniteMPS}, loc::Int)
10-
S = zero(real(scalartype(state)))
11-
tol = eps(typeof(S))
12-
for (c, b) in pairs(entanglement_spectrum(state, loc))
16+
return entropy(entanglement_spectrum(state, loc))
17+
end
18+
function entropy(spectrum::TensorKit.SectorVector{T}) where {T}
19+
S = zero(T)
20+
tol = eps(T)
21+
for (c, b) in pairs(spectrum)
1322
s = zero(S)
1423
for x in b
1524
x < tol && break

test/states/finitemps.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,34 @@ end
127127
@test (ismissing(mps1.Cs[end]) && ismissing(mps2.Cs[end])) ||
128128
mps1.Cs[end] !== mps2.Cs[end]
129129
end
130+
131+
@testset "FiniteMPS entropy ($(sectortype(D)), $elt)" for (D, d, elt) in [
132+
(ℙ^10, ℙ^2, ComplexF64),
133+
(
134+
Rep[U₁](-1 => 3, 0 => 3, 1 => 3),
135+
Rep[U₁](-1 => 1, 0 => 1, 1 => 1),
136+
ComplexF64,
137+
),
138+
]
139+
L = 6
140+
ψ = FiniteMPS(rand, elt, L, d, D)
141+
142+
# entropy is non-negative at all sites
143+
for site in 1:L
144+
@test real(entropy(ψ, site)) >= 0
145+
end
146+
147+
# entropy is consistent with entanglement_spectrum
148+
for site in 1:L
149+
@test entropy(ψ, site) entropy(entanglement_spectrum(ψ, site))
150+
end
151+
152+
# entropy is zero at the right boundary (trivial bond)
153+
@test entropy(ψ, L) 0 atol = 1.0e-10
154+
155+
# product state has zero entropy everywhere
156+
ψ_product = FiniteMPS(rand, elt, L, d, oneunit(D))
157+
for site in 1:L
158+
@test entropy(ψ_product, site) 0 atol = 1.0e-10
159+
end
160+
end

test/states/infinitemps.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,25 @@ end
7070
@test mps1.AC[1] !== mps2.AC[1]
7171
@test mps1.C[1] !== mps2.C[1]
7272
end
73+
74+
@testset "InfiniteMPS entropy ($(sectortype(D)), $elt)" for (D, d, elt) in
75+
[(ℙ^10, ℙ^2, ComplexF64), (Rep[U₁](1 => 3), Rep[U₁](0 => 1), ComplexF64)]
76+
ψ = InfiniteMPS([d, d], [D, D])
77+
78+
# entropy(ψ) returns one value per site, all non-negative
79+
Ss = entropy(ψ)
80+
@test length(Ss) == length(ψ)
81+
@test all(isreal, Ss)
82+
@test all(>=(0), Ss)
83+
84+
# entropy(ψ, site) is non-negative and consistent with entropy(ψ)
85+
for site in 1:length(ψ)
86+
@test entropy(ψ, site) Ss[site]
87+
@test entropy(ψ, site) entropy(entanglement_spectrum(ψ, site))
88+
end
89+
90+
# product state has zero entropy everywhere
91+
ψ_product = InfiniteMPS([d, d], [oneunit(D), oneunit(D)])
92+
Ss_product = entropy(ψ_product)
93+
@test all(S -> isapprox(S, 0; atol = 1.0e-10), Ss_product)
94+
end

0 commit comments

Comments
 (0)