Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mutation behavior of position #131

Merged
merged 15 commits into from
Jan 31, 2024
8 changes: 4 additions & 4 deletions src/treetensornetworks/projttns/abstractprojttn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ function position(
) where {V}
b-kloss marked this conversation as resolved.
Show resolved Hide resolved
# shift position
P = shift_position(P, pos)
# invalidate environments corresponding to internal edges
for e in internal_edges(P)
unset!(P.environments, e)
end
# remove internal edges (out of place)
ie = internal_edges(P)
newenvskeys = filter(!in(ie), keys(P.environments))
b-kloss marked this conversation as resolved.
Show resolved Hide resolved
P = ProjTTN(pos, P.H, getindices(P.environments, newenvskeys))
b-kloss marked this conversation as resolved.
Show resolved Hide resolved
# make all environments surrounding new position
for e in incident_edges(P)
make_environment!(P, psi, e)
b-kloss marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
43 changes: 43 additions & 0 deletions test/test_treetensornetworks/test_position.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using ITensors
using ITensorNetworks
using ITensorNetworks: position
using Test

@testset "ProjTTN position copy-safe" begin
# make a nontrivial TTN state and TTN operator

auto_fermion_enabled = ITensors.using_auto_fermion()
use_qns = true
cutoff = 1e-12

tooth_lengths = fill(2, 3)
c = named_comb_tree(tooth_lengths)
if use_qns # test whether autofermion breaks things when using non-fermionic QNs
ITensors.enable_auto_fermion()
else # when using no QNs, autofermion breaks # ToDo reference Issue in ITensors
ITensors.disable_auto_fermion()
end
s = siteinds("S=1/2", c; conserve_qns=use_qns)

os = ITensorNetworks.heisenberg(c)

H = TTN(os, s)

d = Dict()
for (i, v) in enumerate(vertices(s))
d[v] = isodd(i) ? "Up" : "Dn"
end
states = v -> d[v]
psi = TTN(s, states)

# actual test, verifies that position is copy safe
vs = vertices(s)
PH0 = ProjTTN(H)
PH0 = position(PH0, psi, [vs[2]])
PH = copy(PH0)
PH = position(PH, psi, [vs[2], vs[5]])
@test keys(PH.environments) != keys(PH0.environments)
if !auto_fermion_enabled
ITensors.disable_auto_fermion()
end
end
Loading