Skip to content

Commit

Permalink
Add tests for expect function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Kloss committed Jan 24, 2024
1 parent 1f888df commit 3aa639f
Showing 1 changed file with 32 additions and 0 deletions.
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

Check warning on line 13 in test/test_treetensornetworks/test_expect.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/test_treetensornetworks/test_expect.jl:13:- @test res_a ≈ res_b rtol=1e-6 test/test_treetensornetworks/test_expect.jl:13:+ @test res_a ≈ res_b rtol = 1e-6
end

@testset "TTN expect" begin
tooth_lengths = fill(5, 6)

Check warning on line 17 in test/test_treetensornetworks/test_expect.jl

View workflow job for this annotation

GitHub Actions / format

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: test/test_treetensornetworks/test_expect.jl:17:- tooth_lengths = fill(5, 6) test/test_treetensornetworks/test_expect.jl:18:- c = named_comb_tree(tooth_lengths) test/test_treetensornetworks/test_expect.jl:19:- s = siteinds("S=1/2", c) test/test_treetensornetworks/test_expect.jl:20:- d = Dict() test/test_treetensornetworks/test_expect.jl:21:- magnetization = Dict() test/test_treetensornetworks/test_expect.jl:22:- for (i, v) in enumerate(vertices(s)) test/test_treetensornetworks/test_expect.jl:23:- d[v] = isodd(i) ? "Up" : "Dn" test/test_treetensornetworks/test_expect.jl:24:- magnetization[v] = isodd(i) ? 0.5 : -0.5 test/test_treetensornetworks/test_expect.jl:25:- end test/test_treetensornetworks/test_expect.jl:26:- states = v -> d[v] test/test_treetensornetworks/test_expect.jl:27:- state = TTN(s, states) test/test_treetensornetworks/test_expect.jl:28:- res=expect("Sz", state) # just testing that this doesn't go via expect for arbitrary tensor networks. test/test_treetensornetworks/test_expect.jl:29:- @test all([isapprox(res[v], magnetization[v],atol=1e-8) for v in vertices(s)]) test/test_treetensornetworks/test_expect.jl:17:+ tooth_lengths = fill(5, 6) test/test_treetensornetworks/test_expect.jl:18:+ c = named_comb_tree(tooth_lengths) test/test_treetensornetworks/test_expect.jl:19:+ s = siteinds("S=1/2", c) test/test_treetensornetworks/test_expect.jl:20:+ d = Dict() test/test_treetensornetworks/test_expect.jl:21:+ magnetization = Dict() test/test_treetensornetworks/test_expect.jl:22:+ for (i, v) in enumerate(vertices(s)) test/test_treetensornetworks/test_expect.jl:23:+ d[v] = isodd(i) ? "Up" : "Dn" test/test_treetensornetworks/test_expect.jl:24:+ magnetization[v] = isodd(i) ? 0.5 : -0.5 test/test_treetensornetworks/test_expect.jl:25:+ end test/test_treetensornetworks/test_expect.jl:26:+ states = v -> d[v] test/test_treetensornetworks/test_expect.jl:27:+ state = TTN(s, states) test/test_treetensornetworks/test_expect.jl:28:+ res = expect("Sz", state) # just testing that this doesn't go via expect for arbitrary tensor networks. test/test_treetensornetworks/test_expect.jl:29:+ @test all([isapprox(res[v], magnetization[v]; atol=1e-8) for v in vertices(s)])
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

0 comments on commit 3aa639f

Please sign in to comment.