Skip to content

Commit

Permalink
Fix factors and edit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
b-kloss committed Feb 2, 2024
1 parent e42ef5a commit 1ce884d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/treetensornetworks/projttns/abstractprojttn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function Base.eltype(P::AbstractProjTTN)::Type
end

vertextype(::Type{<:AbstractProjTTN{V}}) where {V} = V
vertextype(p::AbstractProjTTN) = typeof(p)
vertextype(p::AbstractProjTTN) = vertextype(typeof(p))

function Base.size(P::AbstractProjTTN)::Tuple{Int,Int}
d = 1
Expand Down
25 changes: 16 additions & 9 deletions src/treetensornetworks/projttns/projttnsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ end
terms(P::ProjTTNSum) = P.terms
factors(P::ProjTTNSum) = P.factors

copy(P::ProjTTNSum) = ProjTTNSum(copy.(terms(P)))
copy(P::ProjTTNSum) = ProjTTNSum(copy.(terms(P)), copy(factors(P)))

function ProjTTNSum(operators::Vector{<:AbstractProjTTN})
return ProjTTNSum(operators, fill(1, length(operators)))
return ProjTTNSum(operators, fill(one(Bool), length(operators)))
end
function ProjTTNSum(operators::Vector{<:AbstractTTN})
return ProjTTNSum(ProjTTN.(operators), fill(1, length(operators)))
return ProjTTNSum(ProjTTN.(operators))
end

on_edge(P::ProjTTNSum) = on_edge(terms(P)[1])

nsite(P::ProjTTNSum) = nsite(terms(P)[1])

function set_nsite(Ps::ProjTTNSum, nsite)
return ProjTTNSum(map(p -> set_nsite(p, nsite), terms(Ps)))
return ProjTTNSum(map(p -> set_nsite(p, nsite), terms(Ps)), factors(Ps))
end

underlying_graph(P::ProjTTNSum) = underlying_graph(terms(P)[1])
Expand All @@ -41,15 +41,19 @@ internal_edges(P::ProjTTNSum) = internal_edges(terms(P)[1])

product(P::ProjTTNSum, v::ITensor) = noprime(contract(P, v))

contract(P::ProjTTNSum, v::ITensor) =
mapreduce(+, zip(factors(P), terms(P))) do (f, p)
function contract(P::ProjTTNSum, v::ITensor)
res = mapreduce(+, zip(factors(P), terms(P))) do (f, p)
f * contract(p, v)
end
return res
end

contract_ket(P::ProjTTNSum, v::ITensor) =
mapreduce(+, zip(factors(P), terms(P))) do (f, p)
function contract_ket(P::ProjTTNSum, v::ITensor)
res = mapreduce(+, zip(factors(P), terms(P))) do (f, p)
f * contract_ket(p, v)
end
return res
end

function Base.eltype(P::ProjTTNSum)
return mapreduce(eltype, promote_type, terms(P))
Expand All @@ -60,5 +64,8 @@ end
Base.size(P::ProjTTNSum) = size(terms(P)[1])

function position(P::ProjTTNSum, psi::AbstractTTN, pos)
return ProjTTNSum(map(M -> position(M, psi, pos), terms(P)))
theterms = map(M -> position(M, psi, pos), terms(P))
#@show typeof(theterms)
#@show factors(P)
return ProjTTNSum(theterms, factors(P))
end
21 changes: 13 additions & 8 deletions test/test_treetensornetworks/test_solvers/test_contract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,29 @@ using Test
Hpsi_via_dmrg = dmrg(Hfit, psi; updater_kwargs=(; which_eigval=:LR,), nsweeps=1)
@test abs(inner(Hpsi_via_dmrg, Hpsi / norm(Hpsi))) 1 atol = 1E-4
# Test whether the interface works for ProjTTNSum with factors
Hfit = ProjTTNSum([ProjOuterProdTTN(psi', H), ProjOuterProdTTN(psi', H)], [0.5, 0.5])
Hpsi_via_dmrg = dmrg(Hfit, psi; nsweeps=1, updater_kwargs=(; which_eigval=:LR,))
Hfit = ProjTTNSum([ProjOuterProdTTN(psi', H), ProjOuterProdTTN(psi', H)], [-0.2, -0.8])
Hpsi_via_dmrg = dmrg(Hfit, psi; nsweeps=1, updater_kwargs=(; which_eigval=:SR,))
@test abs(inner(Hpsi_via_dmrg, Hpsi / norm(Hpsi))) 1 atol = 1E-4

# Test basic usage for use with multiple ProjOuterProdTTN with default parameters
# BLAS.axpy-like test
os_id = OpSum()
os_id += -1, "Id", 1, "Id", 2
minus_identity = mpo(os_id, s)
os_id = OpSum()
os_id += +1, "Id", 1, "Id", 2
identity = mpo(os_id, s)
Hpsi = ITensorNetworks.sum_apply(
[(H, psi), (minus_identity, psi)]; alg="fit", init=psi, nsweeps=1
[(H, psi), (minus_identity, psi)]; alg="fit", init=psi, nsweeps=3
)
@test inner(psi, Hpsi) (inner(psi', H, psi) - norm(psi)^2) atol = 1E-5

#Hpsi = ITensorNetworks.dmrg(
# [(H, psi), (minus_identity, psi)]; alg="fit", init=psi, nsweeps=1
#)
#@test inner(psi, Hpsi) ≈ (inner(psi', H, psi) - norm(psi)^2) atol = 1E-5
# Test the above via DMRG
# ToDo: Investigate why this is broken
Hfit = ProjTTNSum([ProjOuterProdTTN(psi', H), ProjOuterProdTTN(psi', identity)], [-1, 1])
Hpsi_normalized = ITensorNetworks.dmrg(
Hfit, psi; nsweeps=3, updater_kwargs=(; which_eigval=:SR)
)
@test_broken abs(inner(Hpsi, (Hpsi_normalized) / norm(Hpsi))) 1 atol = 1E-5

#
# Change "top" indices of MPO to be a different set
Expand Down

0 comments on commit 1ce884d

Please sign in to comment.