Skip to content

Commit 05651fe

Browse files
authored
Fix MPOHamiltonian constructors for adding multiple terms on the same site (#235)
* Use `+=` in MPOHamiltonian constructors * Add testcase * Bump v0.12.1 * tolerance change
1 parent 7df0cba commit 05651fe

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MPSKit"
22
uuid = "bb1c41ca-d63c-52ed-829e-0820dda26502"
33
authors = "Lukas Devos, Maarten Van Damme and contributors"
4-
version = "0.12.0"
4+
version = "0.12.1"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"

src/operators/mpohamiltonian.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function FiniteMPOHamiltonian(lattice::AbstractArray{<:VectorSpace},
177177
# Fill it
178178
for ((key_L, key_R), o) in zip(nonzero_keys[site], nonzero_opps[site])
179179
key_R′ = key_R == 0 ? length(virtualspaces[site + 1]) : key_R
180-
O[key_L, 1, 1, key_R′] = if o isa Number
180+
O[key_L, 1, 1, key_R′] += if o isa Number
181181
iszero(o) && continue
182182
τ = BraidingTensor{E}(eachspace(O)[key_L, 1, 1, key_R′])
183183
isone(o) ? τ : τ * o
@@ -295,7 +295,7 @@ function InfiniteMPOHamiltonian(lattice′::AbstractArray{<:VectorSpace},
295295
# Fill it
296296
for ((key_L, key_R′), o) in zip(nonzero_keys[site], nonzero_opps[site])
297297
key_R = key_R′ == 0 ? length(virtualspaces[site]) : key_R′
298-
O[key_L, 1, 1, key_R] = if o isa Number
298+
O[key_L, 1, 1, key_R] += if o isa Number
299299
iszero(o) && continue
300300
τ = BraidingTensor{E}(eachspace(O)[key_L, 1, 1, key_R])
301301
isone(o) ? τ : τ * o

test/operators.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ end
8282
@test convert(TensorMap, H3)
8383
O₁ E E + E O₂ + permute(O₂ E, ((1, 3, 2), (4, 6, 5)))
8484

85+
# check if adding terms on the same site works
86+
single_terms = Iterators.flatten(Iterators.repeated((i => O₁ / 2 for i in 1:L), 2))
87+
H4 = FiniteMPOHamiltonian(lattice, single_terms)
88+
@test H4 H1 atol = 1e-6
89+
double_terms = Iterators.flatten(Iterators.repeated(((i, i + 1) => O₂ / 2
90+
for i in 1:(L - 1)), 2))
91+
H5 = FiniteMPOHamiltonian(lattice, double_terms)
92+
@test H5 H2 atol = 1e-6
93+
8594
# test linear algebra
8695
@test H1
8796
FiniteMPOHamiltonian(lattice, 1 => O₁) +

0 commit comments

Comments
 (0)