Skip to content

Commit

Permalink
Format.
Browse files Browse the repository at this point in the history
  • Loading branch information
b-kloss committed Jan 14, 2024
1 parent a03b4a7 commit 4135a4f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 65 deletions.
4 changes: 1 addition & 3 deletions src/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for n in 1:(N - 1)
os_dn .+= -t, "Cdagdn", n + 1, "Cdn", n
end
=#
function tight_binding(g::AbstractGraph;t=1.0, tp=0.0, h::Union{<:Real,Vector{<:Real}}=0)
function tight_binding(g::AbstractGraph; t=1.0, tp=0.0, h::Union{<:Real,Vector{<:Real}}=0)
h = _maybe_fill(h, nv(g))
= OpSum()
if !iszero(t)
Expand Down Expand Up @@ -44,8 +44,6 @@ function tight_binding(g::AbstractGraph;t=1.0, tp=0.0, h::Union{<:Real,Vector{<:
return
end



"""
t-t' Hubbard Model
"""
Expand Down
23 changes: 11 additions & 12 deletions src/treetensornetworks/opsum_to_ttn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Utility methods
#


# linear ordering of vertices in tree graph relative to chosen root, chosen outward from root
function find_index_in_tree(site, g::AbstractGraph, root_vertex)
ordering = reverse(post_order_dfs_vertices(g, root_vertex))
Expand Down Expand Up @@ -145,7 +144,7 @@ function ttn_svd(
not_incoming_qn = calc_qn(not_incoming)
outgoing_qns = Dict(e => calc_qn(outgoing[e]) for e in edges_out)
site_qn = calc_qn(onsite)

# initialize QNArrayElement indices and quantum numbers
T_inds = MVector{degrees[v]}(fill(-1, degrees[v]))
T_qns = MVector{degrees[v]}(fill(QN(), degrees[v]))
Expand All @@ -155,9 +154,9 @@ function ttn_svd(
if !isempty(incoming)
# get the correct map from edge=>QN to term and channel
# this checks if term exists on edge=>QN ( otherwise insert it) and returns it's index
coutmap = get!(outmaps, edge_in => not_incoming_qn , Dict{Vector{Op},Int}())
coutmap = get!(outmaps, edge_in => not_incoming_qn, Dict{Vector{Op},Int}())
cinmap = get!(inmaps, edge_in => -incoming_qn, Dict{Vector{Op},Int}())

bond_row = ITensors.posInLink!(cinmap, incoming)
bond_col = ITensors.posInLink!(coutmap, not_incoming) # get incoming channel
bond_coef = convert(coefficient_type, ITensors.coefficient(term))
Expand All @@ -166,7 +165,7 @@ function ttn_svd(
)
push!(q_inbond_coefs, ITensors.MatElem(bond_row, bond_col, bond_coef))
T_inds[dim_in] = bond_col
T_qns[dim_in] = -incoming_qn
T_qns[dim_in] = -incoming_qn
end
for dout in dims_out
coutmap = get!(
Expand Down Expand Up @@ -269,10 +268,10 @@ function ttn_svd(

# set non-trivial helper inds
for d in normal_dims
block_helper_inds[d] = qnblock(linkinds[d], T_qns[d])
block_helper_inds[d] = qnblock(linkinds[d], T_qns[d])
end
@assert all((-1), block_helper_inds)# check that all block indices are set

# make and fill Block
theblock = Block(Tuple(block_helper_inds))
if isempty(normal_dims)
Expand All @@ -281,7 +280,7 @@ function ttn_svd(
M[] += ct
else
M = get!(blocks, (theblock, terms(t)), zero_arr())
dim_ranges = Tuple(size(Vv[d][T_qns[d]], 2) for d in normal_dims)
dim_ranges = Tuple(size(Vv[d][T_qns[d]], 2) for d in normal_dims)
for c in CartesianIndices(dim_ranges) # applies isometries in a element-wise manner
z = ct
temp_inds = copy(T_inds)
Expand Down Expand Up @@ -310,13 +309,13 @@ function ttn_svd(
end
sq = flux(Op)
if !isnothing(sq)
rq =(b[1]==1 ? Hflux : first(space(linkinds[1])[b[1]])) # get row (dim_in) QN
rq = (b[1] == 1 ? Hflux : first(space(linkinds[1])[b[1]])) # get row (dim_in) QN
cq = rq - sq # get column (out_dims) QN
if ITensors.using_auto_fermion()
# we need to account for the direct product below ordering the physical indices as the last indices
# although they are in between incoming and outgoing indices in the canonical site-ordering
perm=(1,3,2)
if ITensors.compute_permfactor(perm,rq,sq,cq) == -1
perm = (1, 3, 2)
if ITensors.compute_permfactor(perm, rq, sq, cq) == -1
Op .*= -1
end
end
Expand All @@ -327,7 +326,7 @@ function ttn_svd(
if !thishasqns
iT = removeqns(iT)
end

if is_internal[v]
H[v] += iT
else
Expand Down
50 changes: 26 additions & 24 deletions test/test_opsum_to_ttn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,31 +130,31 @@ using Test

@testset "OpSum to TTN Fermions" begin
# small comb tree
auto_fermion_enabled=ITensors.using_auto_fermion()
auto_fermion_enabled = ITensors.using_auto_fermion()
if !auto_fermion_enabled
ITensors.enable_auto_fermion()
end
tooth_lengths = fill(2, 3)
c = named_comb_tree(tooth_lengths)
is = siteinds("Fermion", c; conserve_nf=true)
#is = siteinds("Electron", c; conserve_nf=true,conserve_sz=true)

# linearized version
#linear_order = [4, 1, 2, 5, 3, 6]

# test with next-to-nearest-neighbor Ising Hamiltonian
t=1.0
t = 1.0
tp = 0.4
U = 0.0
h=0.5
h = 0.5
H = ITensorNetworks.tight_binding(c; t=t, tp=tp, h=h)
#H = ITensorNetworks.hubbard(c; t=t, tp=tp, h=h)

# add combination of longer range interactions
Hlr = copy(H)
#Hlr += 5, "Cdag", (1, 2), "C", (3, 2)#, "Z", (3,2)
#Hlr += 5, "Cdag", (3, 2), "C", (1, 2)#, "Z", (3,2)

#Hlr += -4, "N", (1, 1), "N", (2, 2)
#Hlr += 2.0, "Sz", (2, 2), "Sz", (3, 2)
#Hlr += -1.0, "Sz", (1, 2), "Sz", (3, 1)
Expand All @@ -166,7 +166,7 @@ using Test
# get TTN Hamiltonian directly
Hsvd = TTN(H, is; root_vertex=root_vertex, cutoff=1e-10)
# get corresponding MPO Hamiltonian
sites=only.([is[v] for v in reverse(post_order_dfs_vertices(c, root_vertex))])
sites = only.([is[v] for v in reverse(post_order_dfs_vertices(c, root_vertex))])
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
Expand All @@ -177,25 +177,28 @@ using Test
end
# Tmpo ≈ Tttno seems to be broken for fermionic tensors
# thus matricize tensors by hand and convert to dense Matrix to compare element by element
cTmpo1=combiner(inds(Tmpo)[findall((plev.(inds(Tmpo))).==0)])
cTmpo2=combiner(inds(Tmpo)[findall((plev.(inds(Tmpo))).==1)])
Tmm=(Tmpo*cTmpo1)*cTmpo2
cTttno1=combiner(inds(Tttno)[findall((plev.(inds(Tttno))).==0)])
cTttno2=combiner(inds(Tttno)[findall((plev.(inds(Tttno))).==1)])
Ttm=(Tttno*cTttno1)*cTttno2
Ttm=replaceinds(Ttm, inds(Ttm)[1]=>inds(Tmm)[1])
Ttm=replaceinds(Ttm, inds(Ttm)[2]=>inds(Tmm)[2])
cTmpo1 = combiner(inds(Tmpo)[findall((plev.(inds(Tmpo))) .== 0)])
cTmpo2 = combiner(inds(Tmpo)[findall((plev.(inds(Tmpo))) .== 1)])
Tmm = (Tmpo * cTmpo1) * cTmpo2
cTttno1 = combiner(inds(Tttno)[findall((plev.(inds(Tttno))) .== 0)])
cTttno2 = combiner(inds(Tttno)[findall((plev.(inds(Tttno))) .== 1)])
Ttm = (Tttno * cTttno1) * cTttno2
Ttm = replaceinds(Ttm, inds(Ttm)[1] => inds(Tmm)[1])
Ttm = replaceinds(Ttm, inds(Ttm)[2] => inds(Tmm)[2])

@test norm(Tmpo) norm(Tttno) rtol = 1e-6
@test any(Matrix(dense(Tmm-Ttm),inds(Tmm)[1],inds(Tmm)[2]) .> 1e-14)
@test any(Matrix(dense(Tmm),inds(Tmm)[1],inds(Tmm)[2]) - Matrix(dense(Ttm),inds(Tmm)[1],inds(Tmm)[2]) .> 1e-14)

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

#@@@test Tmm ≈ Ttm atol = 1e-8
#@test all(Matrix(dense(Tmm-Ttm),inds(Tmm)[1],inds(Tmm)[2]) .≈ 0.0 atol = 1e-8)

#@test Tmm ≈ Ttm
#@test Tmpo ≈ Tttno rtol = 1e-6

# this breaks for longer range interactions ###not anymore
#=
Hsvd_lr = TTN(Hlr, is; root_vertex=root_vertex, algorithm="svd", cutoff=1e-10)
Expand All @@ -211,7 +214,7 @@ using Test
ITensors.disable_auto_fermion()
end
end

@testset "OpSum to TTN QN missing" begin
# small comb tree
tooth_lengths = fill(2, 3)
Expand Down Expand Up @@ -277,5 +280,4 @@ using Test
@test Tttno_lr Tmpo_lr rtol = 1e-6
end
end

end
2 changes: 1 addition & 1 deletion test/test_tebd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ ITensors.disable_warn_order()
)
E2 = expect(ℋ, ψ; sequence=inner_sequence)
@show E0, E1, E2, E_dmrg
@test (((abs((E2-E1)/E2)<1e-4) && (E1 < E0)) || (E2 < E1 < E0))
@test (((abs((E2 - E1) / E2) < 1e-4) && (E1 < E0)) || (E2 < E1 < E0))
@test E2 E_dmrg rtol = 1e-4
end
49 changes: 24 additions & 25 deletions test/test_treetensornetworks/test_solvers/test_dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,21 @@ end

tooth_lengths = fill(2, 3)
c = named_comb_tree(tooth_lengths)
@testset "Svd approach" for use_qns in [false,true]
s = siteinds("S=1/2", c,conserve_qns=use_qns)

@testset "Svd approach" for use_qns in [false, true]
s = siteinds("S=1/2", c; conserve_qns=use_qns)

os = ITensorNetworks.heisenberg(c)

H = TTN(os, s)

# make init_state
d=Dict()
for (i,v) in enumerate(vertices(s))
d = Dict()
for (i, v) in enumerate(vertices(s))
d[v] = isodd(i) ? "Up" : "Dn"
end
states=v -> d[v]
psi = TTN(s,states)
states = v -> d[v]
psi = TTN(s, states)

# psi = random_ttn(s; link_space=20) #FIXME: random_ttn broken for QN conserving case

Expand All @@ -144,39 +144,39 @@ end
vmap = Dictionary(vertices(s)[linear_order], 1:length(linear_order))
sline = only.(collect(vertex_data(s)))[linear_order]
Hline = MPO(relabel_sites(os, vmap), sline)
psiline = randomMPS(sline, i->isodd(i) ? "Up" : "Dn"; linkdims=20)
psiline = randomMPS(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20)
e2, psi2 = dmrg(Hline, psiline, sweeps; outputlevel=0)

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

@testset "Tree DMRG for Fermions" for nsite in [2]
auto_fermion_enabled=ITensors.using_auto_fermion()
auto_fermion_enabled = ITensors.using_auto_fermion()
if !auto_fermion_enabled
ITensors.enable_auto_fermion()
end

cutoff = 1e-12
tooth_lengths = fill(2, 3)
c = named_comb_tree(tooth_lengths)

@testset "Svd approach" for use_qns in [true]
s = siteinds("Electron", c,conserve_qns=use_qns)
U=2.0
t=1.3
tp=0.6
os = ITensorNetworks.hubbard(c;U=U,t=t,tp=tp)
s = siteinds("Electron", c; conserve_qns=use_qns)
U = 2.0
t = 1.3
tp = 0.6
os = ITensorNetworks.hubbard(c; U=U, t=t, tp=tp)
H = TTN(os, s)

# make init_state
d=Dict()
for (i,v) in enumerate(vertices(s))
d = Dict()
for (i, v) in enumerate(vertices(s))
d[v] = isodd(i) ? "Up" : "Dn"
end
states=v -> d[v]
psi = TTN(s,states)
states = v -> d[v]
psi = TTN(s, states)

nsweeps = 10
maxdim = [10, 20, 40, 100]
sweeps = Sweeps(nsweeps) # number of sweeps is 5
Expand All @@ -190,7 +190,7 @@ end
sline = only.(collect(vertex_data(s)))[linear_order]
#@show sline
Hline = MPO(relabel_sites(os, vmap), sline)
psiline = randomMPS(sline,i->isodd(i) ? "Up" : "Dn"; linkdims=20)
psiline = randomMPS(sline, i -> isodd(i) ? "Up" : "Dn"; linkdims=20)
e2, psi2 = dmrg(Hline, psiline, sweeps; outputlevel=0)
@test inner(psi', H, psi) inner(psi2', Hline, psi2) atol = 1e-5
end
Expand All @@ -199,7 +199,6 @@ end
end
end


@testset "Regression test: tree truncation" begin
maxdim = 4
nsite = 2
Expand Down

0 comments on commit 4135a4f

Please sign in to comment.