Skip to content

Commit

Permalink
Format.
Browse files Browse the repository at this point in the history
  • Loading branch information
b-kloss committed Jan 21, 2024
1 parent f6a43e4 commit fe8be5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
35 changes: 17 additions & 18 deletions test/test_opsum_to_ttn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ function _fermionic_to_dense_matrix(A)
return (A * cA1) * cA2
end

function _make_inds_same(A,B)
function _make_inds_same(A, B)
B = replaceinds(B, inds(B)[1] => inds(A)[1])
B = replaceinds(B, inds(B)[2] => inds(A)[2])
return A,B
return A, B
end

@testset "OpSum to TTN converter" begin
Expand Down Expand Up @@ -150,14 +150,14 @@ end
tooth_lengths = fill(2, 3)
c = named_comb_tree(tooth_lengths)
is = siteinds("Fermion", c; conserve_nf=true)

# test with next-nearest neighbor tight-binding model
t = 1.0
tp = 0.4
U = 0.0
h = 0.5
H = ITensorNetworks.tight_binding(c; t=t, tp=tp, h=h)

# add combination of longer range interactions
Hlr = copy(H)

Expand All @@ -169,32 +169,31 @@ end
vmap = Dictionary(reverse(post_order_dfs_vertices(c, root_vertex)), 1:length(sites))
Hline = ITensors.MPO(relabel_sites(H, vmap), sites)
# compare resulting sparse Hamiltonians
Hmat_sp=hopping_hamiltonian(relabel_sites(H, vmap))
Hmat_sp = hopping_hamiltonian(relabel_sites(H, vmap))
@disable_warn_order begin
Tmpo = prod(Hline)
Tttno = contract(Hsvd)
end

# verify that the norm isn't 0 and thus the same (which would indicate a problem with the autofermion system
@test norm(Tmpo) > 0
@test norm(Tttno) > 0
@test norm(Tmpo) norm(Tttno) rtol = 1e-6

@test_broken Tmpo Tttno # ToDo fix comparison for fermionic tensors
# In the meantime: matricize tensors and convert to dense Matrix to compare element by element
Tmm = _fermionic_to_dense_matrix(Tmpo)
Ttm = _fermionic_to_dense_matrix(Tttno)
Tmm,Ttm=_make_inds_same(Tmm,Ttm)

dTmm=Matrix(dense(Tmm), inds(Tmm)[1], inds(Tmm)[2])
dTtm=Matrix(dense(Ttm), inds(Tmm)[1], inds(Tmm)[2])
@test any( dTmm - dTtm .> 1e-14 )
Tmm, Ttm = _make_inds_same(Tmm, Ttm)

# also compare with energies obtained from single-particle Hamiltonian
GS_mb,_,_=eigsolve(dTtm,1, :SR,eltype(dTtm))
spectrum_sp=eigvals(Hmat_sp)
@test minimum(cumsum(spectrum_sp)) GS_mb[1] atol=1e-8
dTmm = Matrix(dense(Tmm), inds(Tmm)[1], inds(Tmm)[2])
dTtm = Matrix(dense(Ttm), inds(Tmm)[1], inds(Tmm)[2])
@test any(dTmm - dTtm .> 1e-14)

# also compare with energies obtained from single-particle Hamiltonian
GS_mb, _, _ = eigsolve(dTtm, 1, :SR, eltype(dTtm))
spectrum_sp = eigvals(Hmat_sp)
@test minimum(cumsum(spectrum_sp)) GS_mb[1] atol = 1e-8
end
if !auto_fermion_enabled
ITensors.disable_auto_fermion()
Expand All @@ -214,7 +213,7 @@ end
add_edge!(c2, (-1, 1) => (2, 1))
rem_edge!(c2, (2, 1) => (2, 2))
rem_edge!(c2, (2, 1) => (3, 1))

is = siteinds("S=1/2", c; conserve_qns=true)
is_missing_site = siteinds("S=1/2", c2; conserve_qns=true)
is_missing_site[(-1, 1)] = Vector{Index}[]
Expand All @@ -228,7 +227,7 @@ end
J2 = 2
h = 0.5
# connectivity of the Hamiltonian is that of the original comb graph
H = ITensorNetworks.heisenberg(c; J1=J1, J2=J2, h=h)
H = ITensorNetworks.heisenberg(c; J1=J1, J2=J2, h=h)

# add combination of longer range interactions
Hlr = copy(H)
Expand Down
22 changes: 12 additions & 10 deletions test/test_treetensornetworks/test_solvers/test_dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ end
maxdim = [10, 20, 40, 100]
@show use_qns
psi = dmrg(
H, psi; nsweeps, maxdim, cutoff, nsites, updater_kwargs=(; krylovdim=3, maxiter=1)
)
H, psi; nsweeps, maxdim, cutoff, nsites, updater_kwargs=(; krylovdim=3, maxiter=1)
)

# Compare to `ITensors.MPO` version of `dmrg`
linear_order = [4, 1, 2, 5, 3, 6]
Expand All @@ -156,11 +156,11 @@ end

@testset "Tree DMRG for Fermions" for nsites in [2] #ToDo: change to [1,2] when random_ttn works with QNs
auto_fermion_enabled = ITensors.using_auto_fermion()
use_qns=true
use_qns = true
cutoff = 1e-12
nsweeps = 10
maxdim = [10, 20, 40, 100]

# setup model
tooth_lengths = fill(2, 3)
c = named_comb_tree(tooth_lengths)
Expand All @@ -174,14 +174,14 @@ end
linear_order = [4, 1, 2, 5, 3, 6]
vmap = Dictionary(vertices(s)[linear_order], 1:length(linear_order))
sline = only.(collect(vertex_data(s)))[linear_order]

# get MPS / MPO with JW string result
ITensors.disable_auto_fermion()
Hline = MPO(relabel_sites(os, vmap), sline)
psiline = randomMPS(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20)
e_jw, psi_jw = dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0)
ITensors.enable_auto_fermion()

# now get auto-fermion results
H = TTN(os, s)
# make init_state
Expand All @@ -191,17 +191,19 @@ end
end
states = v -> d[v]
psi = TTN(s, states)
psi = dmrg(H, psi; nsweeps, maxdim, cutoff, nsites, updater_kwargs=(; krylovdim=3, maxiter=1))
psi = dmrg(
H, psi; nsweeps, maxdim, cutoff, nsites, updater_kwargs=(; krylovdim=3, maxiter=1)
)

# Compare to `ITensors.MPO` version of `dmrg`
Hline = MPO(relabel_sites(os, vmap), sline)
psiline = randomMPS(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20)
e2, psi2 = dmrg(Hline, psiline; nsweeps, maxdim, cutoff, outputlevel=0)

@test inner(psi', H, psi) inner(psi2', Hline, psi2) atol = 1e-5
@test e2 e_jw atol = 1e-5
@test inner(psi2', Hline, psi2) e_jw atol = 1e-5

if !auto_fermion_enabled
ITensors.disable_auto_fermion()
end
Expand All @@ -220,6 +222,6 @@ end
psi = dmrg(H, psi; nsweeps, maxdim, nsites)

@test all(edge_data(linkdims(psi)) .<= maxdim)
end
end

nothing

0 comments on commit fe8be5c

Please sign in to comment.