Skip to content

Commit

Permalink
Namespace improvements in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Feb 16, 2024
1 parent ea597ce commit e3e2f5d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 137 deletions.
10 changes: 6 additions & 4 deletions test/test_contract_mpo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using ITensors:
truncate!
using ITensorTDVP: ITensorTDVP
using Test: @test, @testset
@testset "Contract MPO (eltype=$elt)" for elt in (
@testset "Contract MPO (eltype=$elt, conserve_qns=$conserve_qns)" for elt in (
Float32, Float64, Complex{Float32}, Complex{Float64}
),
conserve_qns in [false, true]
Expand Down Expand Up @@ -52,11 +52,13 @@ using Test: @test, @testset
truncate!(psi_guess; maxdim=2)
Hpsi = apply(H, psi; alg="fit", nsweeps=4, init_mps=psi_guess)
@test ITensors.scalartype(Hpsi) == elt
@test inner(psit, Hpsi) inner(psit, H, psi) atol = 1e-5
@test inner(psit, Hpsi) inner(psit, H, psi) rtol = eps(real(elt))
# Test with nsite=1
Hpsi_guess = apply(H, psi; alg="naive", cutoff=1E-4)
Hpsi_guess = apply(H, psi; alg="naive", cutoff=1e-4)
Hpsi = apply(H, psi; alg="fit", init_mps=Hpsi_guess, nsite=1, nsweeps=2)
@test ITensors.scalartype(Hpsi) == elt
@test inner(psit, Hpsi) inner(psit, H, psi) atol = 1e-4
scale(::Type{Float32}) = 10^2
scale(::Type{Float64}) = 10^6
@test inner(psit, Hpsi) inner(psit, H, psi) rtol = eps(real(elt)) * scale(real(elt))
end
end
2 changes: 1 addition & 1 deletion test/test_dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using ITensors: ITensors, MPO, OpSum, inner, randomMPS, siteinds
using ITensorTDVP: ITensorTDVP
using Test: @test, @testset
@testset "DMRG (eltype=$elt, nsite=$nsite)" for elt in (
@testset "DMRG (eltype=$elt, nsite=$nsite, conserve_qns=$conserve_qns)" for elt in (
Float32, Float64, Complex{Float32}, Complex{Float64}
),
nsite in [1, 2],
Expand Down
18 changes: 6 additions & 12 deletions test/test_dmrg_x.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ITensors
using ITensorTDVP
using Random
using Test

@eval module $(gensym())
using ITensors: ITensors, MPO, MPS, OpSum, ProjMPO, inner, siteinds
using ITensorTDVP: dmrg_x
using Random: Random
using Test: @test, @testset
@testset "DMRG-X (eltype=$elt, conserve_qns=$conserve_qns)" for elt in (
Float32, Float64, Complex{Float32}, Complex{Float64}
),
Expand Down Expand Up @@ -35,24 +35,18 @@ using Test
nsweeps=20, reverse_step=false, normalize=true, maxdim=20, cutoff=1e-10, outputlevel=0
)
ϕ = dmrg_x(ProjMPO(H), ψ; nsite=2, dmrg_x_kwargs...)

@test ITensors.scalartype(ϕ) == elt
@test inner(H, ψ, H, ψ) inner', H, ψ)^2 rtol = eps(real(elt))
@test inner', H, ψ) / inner(ψ, ψ) inner', H, ϕ) / inner(ϕ, ϕ) rtol = 1e-1
@test inner(H, ϕ, H, ϕ) inner', H, ϕ)^2 rtol = eps(real(elt))

ϕ̃ = dmrg_x(ProjMPO(H), ϕ; nsite=1, dmrg_x_kwargs...)

@test ITensors.scalartype(ϕ̃) == elt
@test inner', H, ψ) / inner(ψ, ψ) inner(ϕ̃', H, ϕ̃) / inner(ϕ̃, ϕ̃) rtol = 1e-1

scale(::Type{Float32}) = 10^2
scale(::Type{Float64}) = 10^5

@test inner(H, ϕ̃, H, ϕ̃) inner(ϕ̃', H, ϕ̃)^2 rtol =
(eps(real(elt))) * scale(real(elt))
# Sometimes broken, sometimes not
# @test abs(loginner(ϕ̃, ϕ) / n) ≈ 0.0 atol = 1e-6
end

nothing
end
49 changes: 20 additions & 29 deletions test/test_linsolve.jl
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
using ITensors
using ITensorTDVP
using Test
using Random

@testset "linsolve with conserve_qns=$conserve_qns and eltype=$eltype" for conserve_qns in
(false, true),
eltype in (Float32, Float64, ComplexF32, ComplexF64)
@eval module $(gensym())
using ITensors: ITensors, MPO, OpSum, apply, randomMPS, siteinds
using ITensorTDVP: ITensorTDVP, dmrg
using KrylovKit: linsolve
using LinearAlgebra: norm
using Test: @test, @testset
using Random: Random
@testset "linsolve (eltype=$elt, conserve_qns=$conserve_qns)" for elt in (
Float32, Float64, Complex{Float32}, Complex{Float64}
),
conserve_qns in [false, true]

N = 6
s = siteinds("S=1/2", N; conserve_qns)

os = OpSum()
for j in 1:(N - 1)
os += 0.5, "S+", j, "S-", j + 1
os += 0.5, "S-", j, "S+", j + 1
os += "Sz", j, "Sz", j + 1
end
H = ITensors.convert_leaf_eltype(eltype, MPO(os, s))

H = ITensors.convert_leaf_eltype(elt, MPO(os, s))
state = [isodd(n) ? "Up" : "Dn" for n in 1:N]

Random.seed!(1234)
x_c = randomMPS(eltype, s, state; linkdims=2)
e, x_c = dmrg(H, x_c; nsweeps=10, cutoff=1e-6, maxdim=20, outputlevel=0)

@test ITensors.scalartype(x_c) == eltype

x_c = randomMPS(elt, s, state; linkdims=2)
x_c = dmrg(H, x_c; nsweeps=10, cutoff=1e-6, maxdim=20, outputlevel=0)
@test ITensors.scalartype(x_c) == elt
# Compute `b = H * x_c`
b = apply(H, x_c; cutoff=1e-8)

@test ITensors.scalartype(b) == eltype

@test ITensors.scalartype(b) == elt
# Starting guess
x0 = x_c + eltype(0.05) * randomMPS(eltype, s, state; linkdims=2)

@test ITensors.scalartype(x0) == eltype

x0 = x_c + elt(0.05) * randomMPS(elt, s, state; linkdims=2)
@test ITensors.scalartype(x0) == elt
nsweeps = 10
cutoff = 1e-5
maxdim = 20
solver_kwargs = (; tol=1e-4, maxiter=20, krylovdim=30, ishermitian=true)
x = linsolve(H, b, x0; nsweeps, cutoff, maxdim, solver_kwargs)

@test ITensors.scalartype(x) == eltype

@test ITensors.scalartype(x) == elt
@test norm(x - x_c) < 1e-2
end

nothing
end
Loading

0 comments on commit e3e2f5d

Please sign in to comment.