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

Efficient implementation of expect for AbstractTTN #129

Merged
merged 9 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/treetensornetworks/abstracttreetensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,25 @@ function inner(
end
return O[]
end

function expect(
operator::String,
state::AbstractTTN;
vertices=vertices(state),
root_vertex=default_root_vertex(siteinds(state)), #ToDo: verify that this is a sane default
)
# ToDo: for performance it may be beneficial to also implement expect! and change the orthogonality center in place
# assuming that the next algorithmic step can make use of the orthogonality center being moved to a different vertex
sites = siteinds(state)
ordered_vertices = reverse(post_order_dfs_vertices(sites, root_vertex)) #ToDo: Verify that this is indeed the correct order for performance
ElT = promote_itensor_eltype(state)
b-kloss marked this conversation as resolved.
Show resolved Hide resolved
res = Dictionary(vertices, Vector{ElT}(undef, length(vertices)))
for v in ordered_vertices
!(v in vertices) && continue #only compute expectation values for required vertices
state = orthogonalize(state, v)
@assert isone(length(sites[v]))
Op = ITensors.op(operator, only(sites[v])) #ToDo: Add compatibility with more than a single index per vertex
b-kloss marked this conversation as resolved.
Show resolved Hide resolved
res[v] = scalar(dag(state[v]) * apply(Op, state[v]))
end
return res
end
32 changes: 32 additions & 0 deletions test/test_treetensornetworks/test_expect.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ITensors
using ITensorNetworks
using Test

@testset "MPS expect comparison with ITensors" begin
N = 25
s = siteinds("S=1/2", N)
a = random_mps(s; internal_inds_space=100)
b = MPS([a[v] for v in vertices(a)])
res_a = expect("Sz", a)
res_b = expect(b, "Sz")
res_a = [res_a[v] for v in vertices(a)]
@test res_a ≈ res_b rtol = 1e-6
end

@testset "TTN expect" begin
tooth_lengths = fill(5, 6)
c = named_comb_tree(tooth_lengths)
s = siteinds("S=1/2", c)
d = Dict()
magnetization = Dict()
for (i, v) in enumerate(vertices(s))
d[v] = isodd(i) ? "Up" : "Dn"
magnetization[v] = isodd(i) ? 0.5 : -0.5
end
states = v -> d[v]
state = TTN(s, states)
res = expect("Sz", state) # just testing that this doesn't go via expect for arbitrary tensor networks.
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
@test all([isapprox(res[v], magnetization[v]; atol=1e-8) for v in vertices(s)])
end

nothing
Loading