From 1f888df0f7242371a0fb625772b2b9eb7eaf36ed Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Wed, 24 Jan 2024 15:04:09 -0500 Subject: [PATCH 1/9] Added expect function dispatching on AbstractTTN. --- .../abstracttreetensornetwork.jl | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/treetensornetworks/abstracttreetensornetwork.jl b/src/treetensornetworks/abstracttreetensornetwork.jl index 72642687..2eacf33a 100644 --- a/src/treetensornetworks/abstracttreetensornetwork.jl +++ b/src/treetensornetworks/abstracttreetensornetwork.jl @@ -420,3 +420,23 @@ function inner( end return O[] end + +function expect( + operator::String, + state::AbstractTTN; + sites=siteinds(state), + root_vertex=default_root_vertex(sites), #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 + 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) + res = Dictionary(vertices, Vector{ElT}(undef, length(vertices))) + for v in 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 + res[v] = scalar(dag(state[v]) * apply(Op, state[v])) + end + return res +end From 3aa639f19a362b1813ff2a72b2b4bddfc08bc04e Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Wed, 24 Jan 2024 15:04:46 -0500 Subject: [PATCH 2/9] Add tests for expect function. --- test/test_treetensornetworks/test_expect.jl | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 test/test_treetensornetworks/test_expect.jl diff --git a/test/test_treetensornetworks/test_expect.jl b/test/test_treetensornetworks/test_expect.jl new file mode 100644 index 00000000..c8831350 --- /dev/null +++ b/test/test_treetensornetworks/test_expect.jl @@ -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. + @test all([isapprox(res[v], magnetization[v],atol=1e-8) for v in vertices(s)]) +end + +nothing From f96763cac7ad4e290e6e13b1b8e60622da65a009 Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Wed, 24 Jan 2024 15:18:13 -0500 Subject: [PATCH 3/9] Format --- test/test_treetensornetworks/test_expect.jl | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/test_treetensornetworks/test_expect.jl b/test/test_treetensornetworks/test_expect.jl index c8831350..1bd17a93 100644 --- a/test/test_treetensornetworks/test_expect.jl +++ b/test/test_treetensornetworks/test_expect.jl @@ -10,23 +10,23 @@ using Test 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 + @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. - @test all([isapprox(res[v], magnetization[v],atol=1e-8) for v in vertices(s)]) + 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. + @test all([isapprox(res[v], magnetization[v]; atol=1e-8) for v in vertices(s)]) end nothing From 8e37899337bb908502a3350db87de942769bff1e Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Wed, 24 Jan 2024 17:56:34 -0500 Subject: [PATCH 4/9] Fix function signature for expect. --- src/treetensornetworks/abstracttreetensornetwork.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/treetensornetworks/abstracttreetensornetwork.jl b/src/treetensornetworks/abstracttreetensornetwork.jl index 2eacf33a..a7615399 100644 --- a/src/treetensornetworks/abstracttreetensornetwork.jl +++ b/src/treetensornetworks/abstracttreetensornetwork.jl @@ -424,19 +424,21 @@ end function expect( operator::String, state::AbstractTTN; - sites=siteinds(state), - root_vertex=default_root_vertex(sites), #ToDo: verify that this is a sane default + 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 - vertices = reverse(post_order_dfs_vertices(sites, root_vertex)) #ToDo: Verify that this is indeed the correct order for performance + 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) res = Dictionary(vertices, Vector{ElT}(undef, length(vertices))) - for v in 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 res[v] = scalar(dag(state[v]) * apply(Op, state[v])) end return res -end +end \ No newline at end of file From 8a1c87bd316c380598aee1aa47dea2125ebac30e Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Wed, 24 Jan 2024 18:10:03 -0500 Subject: [PATCH 5/9] Format. --- src/treetensornetworks/abstracttreetensornetwork.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/treetensornetworks/abstracttreetensornetwork.jl b/src/treetensornetworks/abstracttreetensornetwork.jl index a7615399..acbd5e31 100644 --- a/src/treetensornetworks/abstracttreetensornetwork.jl +++ b/src/treetensornetworks/abstracttreetensornetwork.jl @@ -429,7 +429,7 @@ function expect( ) # 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) + 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) res = Dictionary(vertices, Vector{ElT}(undef, length(vertices))) @@ -441,4 +441,4 @@ function expect( res[v] = scalar(dag(state[v]) * apply(Op, state[v])) end return res -end \ No newline at end of file +end From 28371ccdcbd022aa11bb4c26f244db3088f55e95 Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Fri, 26 Jan 2024 11:37:17 -0500 Subject: [PATCH 6/9] Fix Number type handling. --- src/treetensornetworks/abstracttreetensornetwork.jl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/treetensornetworks/abstracttreetensornetwork.jl b/src/treetensornetworks/abstracttreetensornetwork.jl index acbd5e31..7d7a1650 100644 --- a/src/treetensornetworks/abstracttreetensornetwork.jl +++ b/src/treetensornetworks/abstracttreetensornetwork.jl @@ -431,14 +431,13 @@ function expect( # 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) - res = Dictionary(vertices, Vector{ElT}(undef, length(vertices))) + res = Dictionary(vertices, undef) for v in ordered_vertices - !(v in vertices) && continue #only compute expectation values for required vertices + !(v in vertices) && continue 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 - res[v] = scalar(dag(state[v]) * apply(Op, state[v])) + op_v = op(operator, only(sites[v])) #ToDo: Add compatibility with more than a single index per vertex + res[v] = scalar(dag(state[v]) * apply(op_v, state[v])) end - return res + return mapreduce(typeof, promote_type, res).(res) end From a03890682d42403e07278ecac70143a1ae2cb562 Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Fri, 26 Jan 2024 11:39:12 -0500 Subject: [PATCH 7/9] Format. --- src/treetensornetworks/abstracttreetensornetwork.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/treetensornetworks/abstracttreetensornetwork.jl b/src/treetensornetworks/abstracttreetensornetwork.jl index 7d7a1650..40a6458b 100644 --- a/src/treetensornetworks/abstracttreetensornetwork.jl +++ b/src/treetensornetworks/abstracttreetensornetwork.jl @@ -425,18 +425,21 @@ 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: verify that this is a sane default + root_vertex=default_root_vertex(siteinds(state)), ) # 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 + # ToDo: Verify that this is indeed the correct order for performance sites = siteinds(state) - ordered_vertices = reverse(post_order_dfs_vertices(sites, root_vertex)) #ToDo: Verify that this is indeed the correct order for performance + ordered_vertices = reverse(post_order_dfs_vertices(sites, root_vertex)) res = Dictionary(vertices, undef) for v in ordered_vertices !(v in vertices) && continue state = orthogonalize(state, v) @assert isone(length(sites[v])) - op_v = op(operator, only(sites[v])) #ToDo: Add compatibility with more than a single index per vertex + #ToDo: Add compatibility with more than a single index per vertex + op_v = op(operator, only(sites[v])) res[v] = scalar(dag(state[v]) * apply(op_v, state[v])) end return mapreduce(typeof, promote_type, res).(res) From 36386b901df3b4a428d19a41ebe290153ce5ac2e Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Fri, 26 Jan 2024 11:44:06 -0500 Subject: [PATCH 8/9] Remove comment --- test/test_treetensornetworks/test_expect.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_treetensornetworks/test_expect.jl b/test/test_treetensornetworks/test_expect.jl index 1bd17a93..37dc9f7c 100644 --- a/test/test_treetensornetworks/test_expect.jl +++ b/test/test_treetensornetworks/test_expect.jl @@ -25,7 +25,7 @@ end 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. + res = expect("Sz", state) @test all([isapprox(res[v], magnetization[v]; atol=1e-8) for v in vertices(s)]) end From 8b78e1dec4a7a1c2b502db6d08c366265f825097 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Fri, 26 Jan 2024 14:13:14 -0500 Subject: [PATCH 9/9] Change `scalar(...)` to `(...)[]` --- src/treetensornetworks/abstracttreetensornetwork.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/treetensornetworks/abstracttreetensornetwork.jl b/src/treetensornetworks/abstracttreetensornetwork.jl index 40a6458b..289449ab 100644 --- a/src/treetensornetworks/abstracttreetensornetwork.jl +++ b/src/treetensornetworks/abstracttreetensornetwork.jl @@ -440,7 +440,7 @@ function expect( @assert isone(length(sites[v])) #ToDo: Add compatibility with more than a single index per vertex op_v = op(operator, only(sites[v])) - res[v] = scalar(dag(state[v]) * apply(op_v, state[v])) + res[v] = (dag(state[v]) * apply(op_v, state[v]))[] end return mapreduce(typeof, promote_type, res).(res) end