-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mutation behavior of
position
(#131)
- Loading branch information
b-kloss
authored
Jan 31, 2024
1 parent
3fc272e
commit cf59738
Showing
6 changed files
with
154 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,73 @@ | ||
struct ProjTTNApply{V} <: AbstractProjTTN{V} | ||
pos::Union{Vector{<:V},NamedEdge{V}} | ||
psi0::TTN{V} | ||
H::TTN{V} | ||
init_state::TTN{V} | ||
operator::TTN{V} | ||
environments::Dictionary{NamedEdge{V},ITensor} | ||
end | ||
|
||
function ProjTTNApply(psi0::TTN, H::TTN) | ||
return ProjTTNApply(vertextype(H)[], psi0, H, Dictionary{edgetype(H),ITensor}()) | ||
environments(p::ProjTTNApply) = p.environments | ||
operator(p::ProjTTNApply) = p.operator | ||
underlying_graph(p::ProjTTNApply) = underlying_graph(operator(p)) | ||
pos(p::ProjTTNApply) = p.pos | ||
init_state(p::ProjTTNApply) = p.init_state | ||
|
||
function ProjTTNApply(init_state::AbstractTTN, operator::AbstractTTN) | ||
return ProjTTNApply( | ||
vertextype(operator)[], init_state, operator, Dictionary{edgetype(operator),ITensor}() | ||
) | ||
end | ||
|
||
function copy(P::ProjTTNApply) | ||
return ProjTTNApply(P.pos, copy(P.psi0), copy(P.H), copy(P.environments)) | ||
return ProjTTNApply(P.pos, copy(init_state(P)), copy(operator(P)), copy(environments(P))) | ||
end | ||
|
||
function set_nsite(P::ProjTTNApply, nsite) | ||
return P | ||
end | ||
|
||
function shift_position(P::ProjTTNApply, pos) | ||
return ProjTTNApply(pos, P.psi0, P.H, P.environments) | ||
return ProjTTNApply(pos, init_state(P), operator(P), environments(P)) | ||
end | ||
|
||
function set_environments(p::ProjTTNApply, environments) | ||
return ProjTTNApply(pos(p), init_state(p), operator(p), environments) | ||
end | ||
|
||
function make_environment!( | ||
P::ProjTTNApply{V}, psi::TTN{V}, e::NamedEdge{V} | ||
)::ITensor where {V} | ||
set_environment(p::ProjTTNApply, edge, env) = set_environment!(copy(p), edge, env) | ||
function set_environment!(p::ProjTTNApply, edge, env) | ||
set!(environments(p), edge, env) | ||
return p | ||
end | ||
|
||
function make_environment(P::ProjTTNApply, state::AbstractTTN, e::AbstractEdge) | ||
# invalidate environment for opposite edge direction if necessary | ||
reverse(e) ∈ incident_edges(P) || unset!(P.environments, reverse(e)) | ||
reverse(e) ∈ incident_edges(P) || (P = invalidate_environment(P, reverse(e))) | ||
# do nothing if valid environment already present | ||
if haskey(P.environments, e) | ||
env = environment(P, e) | ||
else | ||
if !haskey(environments(P), e) | ||
if is_leaf(underlying_graph(P), src(e)) | ||
# leaves are easy | ||
env = P.psi0[src(e)] * P.H[src(e)] * dag(psi[src(e)]) | ||
env = init_state(P)[src(e)] * operator(P)[src(e)] * dag(state[src(e)]) | ||
else | ||
# construct by contracting neighbors | ||
neighbor_envs = ITensor[] | ||
for n in setdiff(neighbors(underlying_graph(P), src(e)), [dst(e)]) | ||
push!(neighbor_envs, make_environment!(P, psi, edgetype(P)(n, src(e)))) | ||
P = make_environment(P, state, edgetype(P)(n, src(e))) | ||
push!(neighbor_envs, environment(P, edgetype(P)(n, src(e)))) | ||
end | ||
# manually heuristic for contraction order: two environments, site tensors, then | ||
# other environments | ||
frst, scnd, rst = _separate_first_two(neighbor_envs) | ||
itensor_map = vcat(P.psi0[src(e)], frst, scnd, P.H[src(e)], dag(psi[src(e)]), rst) | ||
itensor_map = vcat( | ||
init_state(P)[src(e)], frst, scnd, operator(P)[src(e)], dag(state[src(e)]), rst | ||
) # no prime here in comparison to the same routine for Projttn | ||
# TODO: actually use optimal contraction sequence here | ||
env = reduce(*, itensor_map) | ||
end | ||
# cache | ||
set!(P.environments, e, env) | ||
P = set_environment(P, e, env) | ||
end | ||
@assert( | ||
hascommoninds(environment(P, e), psi[src(e)]), | ||
hascommoninds(environment(P, e), state[src(e)]), | ||
"Something went wrong, probably re-orthogonalized this edge in the same direction twice!" | ||
) | ||
return env | ||
return P | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using ITensors | ||
using ITensorNetworks | ||
using ITensorNetworks: position, environments | ||
using Test | ||
|
||
@testset "ProjTTN position" 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 out of place | ||
vs = vertices(s) | ||
PH = ProjTTN(H) | ||
PH = position(PH, psi, [vs[2]]) | ||
original_keys = deepcopy(keys(environments(PH))) | ||
# test out-of-placeness of position | ||
PHc = position(PH, psi, [vs[2], vs[5]]) | ||
@test keys(environments(PH)) == original_keys | ||
@test keys(environments(PHc)) != original_keys | ||
|
||
if !auto_fermion_enabled | ||
ITensors.disable_auto_fermion() | ||
end | ||
end | ||
nothing |