From 7ee14bd82d4d63caeae99e7a03e7884f0d31fdcd Mon Sep 17 00:00:00 2001 From: Benedikt Kloss Date: Tue, 19 Mar 2024 11:59:41 -0400 Subject: [PATCH] Move tests to appropriate location. --- test/test_forms.jl | 33 +----------- .../test_treetensornetwork.jl | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 test/test_treetensornetworks/test_treetensornetwork.jl diff --git a/test/test_forms.jl b/test/test_forms.jl index d0615c0a..028c2366 100644 --- a/test/test_forms.jl +++ b/test/test_forms.jl @@ -12,9 +12,7 @@ using ITensorNetworks: dual_index_map, bra_network, ket_network, - operator_network, - contract_with_BP, - group + operator_network using Test using Random @@ -52,33 +50,4 @@ using Random @test underlying_graph(ket_network(qf)) == underlying_graph(ψket) @test underlying_graph(operator_network(qf)) == underlying_graph(A) end - -@testset "FormNetworks for TTN" begin - Random.seed!(1234) - Lx, Ly = 3, 3 - χ = 2 - g = named_comb_tree((Lx, Ly)) - s = siteinds("S=1/2", g) - y = TTN(randomITensorNetwork(ComplexF64, s; link_space=χ)) - x = TTN(randomITensorNetwork(ComplexF64, s; link_space=χ)) - - A = TTN(ITensorNetworks.heisenberg(s), s) - #First lets do it with the flattened version of the network - xy = inner_network(x, y; combine_linkinds=true) - xy_scalar = contract(xy)[] - xy_scalar_bp = contract_with_BP(xy) - - @test_broken xy_scalar ≈ xy_scalar_bp - - #Now lets keep it unflattened and do Block BP to keep the partitioned graph as a tree - xy = inner_network(x, y; combine_linkinds=false) - xy_scalar = contract(xy)[] - xy_scalar_bp = contract_with_BP(xy; partitioning=group(v -> first(v), vertices(xy))) - - @test xy_scalar ≈ xy_scalar_bp - # test contraction of three layers for expectation values - # for TTN inner with this signature passes via contract_with_BP - @test inner(prime(x), A, y) ≈ - inner(x, apply(A, y; nsweeps=10, maxdim=16, cutoff=1e-10, init=y)) rtol = 1e-6 -end nothing diff --git a/test/test_treetensornetworks/test_treetensornetwork.jl b/test/test_treetensornetworks/test_treetensornetwork.jl new file mode 100644 index 00000000..13ebf9eb --- /dev/null +++ b/test/test_treetensornetworks/test_treetensornetwork.jl @@ -0,0 +1,53 @@ +using ITensors +using ITensors: contract +using ITensorNetworks +using ITensorNetworks: contract_with_BP, group +using Test + +@testset "TTN constructor defaulting to link_space=1" begin + tooth_lengths = fill(5, 6) + c = named_comb_tree(tooth_lengths) + s = siteinds("S=1/2", c) + d = Dict() + for (i, v) in enumerate(vertices(s)) + d[v] = isodd(i) ? "Up" : "Dn" + end + states = v -> d[v] + #test a few signatures + state = TTN(s, states) + lds = edge_data(linkdims(state)) + @test all([isone(lds[k]) for k in keys(lds)]) + state = TTN(s) + lds = edge_data(linkdims(state)) + @test all([isone(lds[k]) for k in keys(lds)]) +end + +@testset "Inner products for TTN via BP" begin + Random.seed!(1234) + Lx, Ly = 3, 3 + χ = 2 + g = named_comb_tree((Lx, Ly)) + s = siteinds("S=1/2", g) + y = TTN(randomITensorNetwork(ComplexF64, s; link_space=χ)) + x = TTN(randomITensorNetwork(ComplexF64, s; link_space=χ)) + + A = TTN(ITensorNetworks.heisenberg(s), s) + #First lets do it with the flattened version of the network + xy = inner_network(x, y; combine_linkinds=true) + xy_scalar = contract(xy)[] + xy_scalar_bp = contract_with_BP(xy) + + @test_broken xy_scalar ≈ xy_scalar_bp + + #Now lets keep it unflattened and do Block BP to keep the partitioned graph as a tree + xy = inner_network(x, y; combine_linkinds=false) + xy_scalar = contract(xy)[] + xy_scalar_bp = contract_with_BP(xy; partitioning=group(v -> first(v), vertices(xy))) + + @test xy_scalar ≈ xy_scalar_bp + # test contraction of three layers for expectation values + # for TTN inner with this signature passes via contract_with_BP + @test inner(prime(x), A, y) ≈ + inner(x, apply(A, y; nsweeps=10, maxdim=16, cutoff=1e-10, init=y)) rtol = 1e-6 +end +nothing