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

Fix broken tests #8

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions test/test_cucontract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using CUDA
using ITensorGPU
using ITensors
using LinearAlgebra: tr
using Random: Random
using Test: @test, @testset

@testset "cuITensor $T Contractions" for T in (Float64, ComplexF64)
Expand Down Expand Up @@ -211,8 +212,8 @@ using Test: @test, @testset
end
@test Ccpu ≈ cpu(C)
for shuffles in 1:1 # too many permutations to test all
inds_a = shuffle(vcat(a_only_inds, shared_inds))
inds_b = shuffle(vcat(b_only_inds, shared_inds))
inds_a = Random.shuffle(vcat(a_only_inds, shared_inds))
inds_b = Random.shuffle(vcat(b_only_inds, shared_inds))
cA_ = permute(cA, inds_a...)
cB_ = permute(cB, inds_b...)
@disable_warn_order begin
Expand Down
3 changes: 2 additions & 1 deletion test/test_cuitensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using CUDA
using ITensorGPU
using ITensors
using LinearAlgebra: tr
using Random: Random
using Test: @test, @testset

# gpu tests!
Expand Down Expand Up @@ -40,7 +41,7 @@ using Test: @test, @testset
A = randomITensor(SType, (inds))
CA = cuITensor(A)
for shuffle_count in 1:20
perm_inds = shuffle(inds)
perm_inds = Random.shuffle(inds)
permCA = permute(CA, perm_inds...)
permA = cpu(permCA)
pA = permute(A, perm_inds...)
Expand Down
188 changes: 7 additions & 181 deletions test/test_dmrg.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ITensorGPU
using ITensorMPS
using ITensors
using Random: Random
using Test: @test, @testset

function heisenberg(n)
Expand All @@ -18,66 +19,27 @@ end
N = 10
sites = siteinds("S=1", N)
H = cuMPO(MPO(heisenberg(N), sites))

psi = randomCuMPS(sites)

sweeps = Sweeps(3)
@test length(sweeps) == 3
maxdim!(sweeps, 10, 20, 40)
mindim!(sweeps, 1, 10)
cutoff!(sweeps, 1E-11)
noise!(sweeps, 1E-10)
str = split(sprint(show, sweeps), '\n')
@test length(str) > 1
energy, psi = dmrg(H, psi, sweeps; outputlevel=0)
dmrg_kwargs = (;
nsweeps=3, maxdim=[10, 20, 40], mindim=[1, 10], cutoff=1e-11, noise=1e-11
)
energy, psi = dmrg(H, psi; outputlevel=0, dmrg_kwargs...)
@test energy < -12.0
end

#=@testset "QN-conserving Spin-one Heisenberg" begin
N = 10
sites = siteinds("S=1",N; conserve_qns=true)

opsum = OpSum()
for j=1:N-1
add!(opsum,"Sz",j,"Sz",j+1)
add!(opsum,0.5,"S+",j,"S-",j+1)
add!(opsum,0.5,"S-",j,"S+",j+1)
end
H = cuMPO(MPO(opsum,sites))

state = [isodd(n) ? "Up" : "Dn" for n in 1:N]
psi = randomCuMPS(sites,state,4)

sweeps = Sweeps(3)
@test length(sweeps) == 3
maxdim!(sweeps,10,20,40)
mindim!(sweeps,1,10)
cutoff!(sweeps,1E-11)
noise!(sweeps,1E-10)
str = split(sprint(show, sweeps), '\n')
@test length(str) > 1
energy,psi = dmrg(H, psi, sweeps; outputlevel=0)
@test energy < -12.0
end=#

@testset "Transverse field Ising" begin
N = 32
sites = siteinds("S=1/2", N)
Random.seed!(432)
psi0 = randomCuMPS(sites)

opsum = OpSum()
for j in 1:N
j < N && add!(opsum, -1.0, "Sz", j, "Sz", j + 1)
add!(opsum, -0.5, "Sx", j)
end
H = cuMPO(MPO(opsum, sites))

sweeps = Sweeps(5)
maxdim!(sweeps, 10, 20)
cutoff!(sweeps, 1E-12)
noise!(sweeps, 1E-10)
energy, psi = dmrg(H, psi0, sweeps; outputlevel=0)
dmrg_kwargs = (; nsweeps=5, maxdim=[10, 20], cutoff=1e-12, noise=1e-10)
energy, psi = dmrg(H, psi0; outputlevel=0, dmrg_kwargs...)

# Exact energy for transverse field Ising model
# with open boundary conditions at criticality
Expand All @@ -100,140 +62,4 @@ end
10 * sqrt(eps(real(ITensors.scalartype(ψ0))))
@test correlation_matrix(ψ, "Z", "Z") ≈ correlation_matrix(cpu(ψ), "Z", "Z")
end

#=@testset "DMRGObserver" begin
N = 10
sites = siteinds("S=1/2",N)
Random.seed!(42)
psi0 = randomCuMPS(sites)

opsum = OpSum()
for j = 1:N-1
add!(opsum,-1.0,"Sz",j,"Sz",j+1)
end
for j = 1:N
add!(opsum,-0.2,"Sx",j)
end
H = cuMPO(MPO(opsum,sites))

sweeps = Sweeps(3)
maxdim!(sweeps,10)
cutoff!(sweeps,1E-12)

observer = DMRGObserver(["Sz","Sx"], sites)

E,psi = dmrg(H,psi0,sweeps,observer=observer,outputlevel=0)
@test length(measurements(observer)["Sz"])==3
@test length(measurements(observer)["Sx"])==3
@test all(length.(measurements(observer)["Sz"]) .== N)
@test all(length.(measurements(observer)["Sx"]) .== N)
@test length(energies(observer))==3
@test length(truncerrors(observer))==3
@test energies(observer)[end]==E
@test all(truncerrors(observer) .< 1E-9)

orthogonalize!(psi,1)
m = scalar(dag(psi[1])*noprime(op(sites, "Sz", 1)*psi[1]))
@test measurements(observer)["Sz"][end][1] ≈ m
end

@testset "Sum of MPOs (ProjMPOSum)" begin
N = 10
sites = siteinds("S=1",N)

ampoZ = OpSum()
for j=1:N-1
add!(opsumZ,"Sz",j,"Sz",j+1)
end
HZ = MPO(opsumZ,sites)

ampoXY = OpSum()
for j=1:N-1
add!(opsumXY,0.5,"S+",j,"S-",j+1)
add!(opsumXY,0.5,"S-",j,"S+",j+1)
end
HXY = MPO(opsumXY,sites)

psi = randomMPS(sites)

sweeps = Sweeps(3)
maxdim!(sweeps,10,20,40)
mindim!(sweeps,1,10,10)
cutoff!(sweeps,1E-11)
noise!(sweeps,1E-10)
energy,psi = dmrg([HZ,HXY], psi, sweeps; outputlevel=0)
@test energy < -12.0
end=#

#=@testset "Excited-state DMRG" begin
N = 10
weight = 15.0

sites = siteinds("S=1",N)
sites[1] = Index(2,"S=1/2,n=1,Site")
sites[N] = Index(2,"S=1/2,n=$N,Site")

opsum = OpSum()
for j=1:N-1
add!(opsum,"Sz",j,"Sz",j+1)
add!(opsum,0.5,"S+",j,"S-",j+1)
add!(opsum,0.5,"S-",j,"S+",j+1)
end
H = cuMPO(MPO(opsum,sites))

psi0i = randomCuMPS(sites,10)

sweeps = Sweeps(4)
maxdim!(sweeps, 10,20,100,100)
cutoff!(sweeps, 1E-11)
noise!(sweeps,1E-10)

energy0, psi0 = dmrg(H,psi0i, sweeps; outputlevel=0)
@test energy0 < -11.5

psi1i = randomCuMPS(sites,10)
energy1,psi1 = dmrg(H,[psi0],psi1i,sweeps;outputlevel=0,weight=weight)

@test energy1 > energy0
@test energy1 < -11.1

@test inner(psi1,psi0) < 1E-6
end=#

#=@testset "Fermionic Hamiltonian" begin
N = 10
t1 = 1.0
t2 = 0.5
V = 0.2
s = siteinds("Fermion", N; conserve_qns = true)

state = fill(1,N)
state[1] = 2
state[3] = 2
state[5] = 2
state[7] = 2
psi0 = productMPS(s,state)

opsum = OpSum()
for j=1:N-1
opsum += (-t1, "Cdag", j, "C", j+1)
opsum += (-t1, "Cdag", j+1, "C", j)
opsum += ( V, "N", j, "N", j+1)
end
for j=1:N-2
opsum += (-t2, "Cdag", j, "C", j+2)
opsum += (-t2, "Cdag", j+2, "C", j)
end
H = MPO(opsum, s)

sweeps = Sweeps(5)
maxdim!(sweeps, 10, 20, 100, 100, 200)
cutoff!(sweeps, 1E-8)
noise!(sweeps, 1E-10)

energy, psi = dmrg(H, psi0, sweeps; outputlevel = 0)
@test (-6.5 < energy < -6.4)
end=#
end

nothing
Loading