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

Generalize ProjTTNSum, replace ProjTTNApply with ProjOuterProdTTN #132

Merged
merged 28 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
830fdc4
Generalize ProjTTNSum to vector of concrete subtype of AbstractProjTT…
Jan 31, 2024
4983fa4
Format.
Feb 1, 2024
c86bf86
Implement missing methods for generalized ProjTTNSum and add test .
b-kloss Feb 1, 2024
4a75a4d
Rename field and add its accessor for ProjTTNSum.
b-kloss Feb 1, 2024
afa6982
Add TTN test.
b-kloss Feb 1, 2024
788d788
Format.
b-kloss Feb 1, 2024
616ccae
Deleted some comments and unnecessary type restrictions.
b-kloss Feb 1, 2024
6fdc0e6
Replace access by field by accessor function.
b-kloss Feb 1, 2024
1202049
Rename ttnos to operators in constructor.
b-kloss Feb 1, 2024
aa464bf
refactor contract
b-kloss Feb 1, 2024
468cace
Merge remote-tracking branch 'upstream/generalize-projttnsum' into ge…
b-kloss Feb 1, 2024
b6a8f32
Fix priming behaviours of apply and remove default for init.
b-kloss Feb 1, 2024
80ccddc
Remove prime-level logic from apply, and use contract when primelevel…
b-kloss Feb 1, 2024
bd69886
Add the primelevel logic back in as check in apply.
b-kloss Feb 1, 2024
0cec308
Rename ProjTTNApply to ProjOuterProdTTN and redefine its functionalit…
b-kloss Feb 2, 2024
ac96ce5
Rename init_state to internal_state for ProjOuterProdTTN.
b-kloss Feb 2, 2024
1d5f722
Rename file.
b-kloss Feb 2, 2024
572932a
Remove remaining type parametrization on output.
b-kloss Feb 2, 2024
e43b016
Remove more return type parametrizations.
b-kloss Feb 2, 2024
a9d75d3
Format and removed another return type parametrization.
b-kloss Feb 2, 2024
7acb0d0
Change filename in includes.
b-kloss Feb 2, 2024
a90bb03
Access position in copy via accessor function.
b-kloss Feb 2, 2024
88351d7
Access vertextype by function instead of via where in type definition.
b-kloss Feb 2, 2024
59cbf79
Fix DMRG variational apply test and type signature in type definition.
b-kloss Feb 2, 2024
559c948
Fix test.
b-kloss Feb 2, 2024
cb8a78f
Fix TimeDependentSum.
b-kloss Feb 2, 2024
e42ef5a
Add prefactors to ProjTTNSum, optimize ProjOuterProdTTN
b-kloss Feb 2, 2024
1ce884d
Fix factors and edit tests.
b-kloss Feb 2, 2024
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
2 changes: 1 addition & 1 deletion src/solvers/contract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ function contract_updater(
updater_kwargs,
)
P = projected_operator![]
return contract_ket(P, ITensor(true)), (;)
return contract_ket(P, ITensor(one(Bool))), (;)
end
3 changes: 3 additions & 0 deletions src/treetensornetworks/projttns/abstractprojttn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ function Base.eltype(P::AbstractProjTTN)::Type
return ElType
end

vertextype(::Type{<:AbstractProjTTN{V}}) where {V} = V
vertextype(p::AbstractProjTTN) = typeof(p)
mtfishman marked this conversation as resolved.
Show resolved Hide resolved

function Base.size(P::AbstractProjTTN)::Tuple{Int,Int}
d = 1
for e in incident_edges(P)
Expand Down
6 changes: 3 additions & 3 deletions src/treetensornetworks/projttns/projouterprodttn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ function contract_ket(P::ProjOuterProdTTN, v::ITensor)
return v
end

# ToDo: debug this, not yet working
# probably
# ToDo: verify conjugation etc. with complex AbstractTTN
function contract(P::ProjOuterProdTTN, x::ITensor)
return conj(contract_ket(P, dag(x))) * contract_ket(P, ITensor(true))
ket = contract_ket(P, ITensor(one(Bool)))
return (dag(ket) * x) * ket
end
27 changes: 20 additions & 7 deletions src/treetensornetworks/projttns/projttnsum.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
"""
ProjTTNSum
"""
struct ProjTTNSum{V} <: AbstractProjTTN{V}
terms::Vector{T} where {T<:AbstractProjTTN{V}}
function ProjTTNSum(terms::Vector{T}) where {V,T<:AbstractProjTTN{V}}
return new{V}(terms)
struct ProjTTNSum{V,T<:AbstractProjTTN{V},Z<:Number} <: AbstractProjTTN{V}
terms::Vector{T}
factors::Vector{Z}
function ProjTTNSum(terms::Vector{<:AbstractProjTTN}, factors::Vector{<:Number})
return new{vertextype(eltype(terms)),eltype(terms),eltype(factors)}(terms, factors)
end
end

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

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

ProjTTNSum(operators::Vector{<:AbstractTTN}) = ProjTTNSum(ProjTTN.(operators))
function ProjTTNSum(operators::Vector{<:AbstractProjTTN})
return ProjTTNSum(operators, fill(1, length(operators)))
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
end
function ProjTTNSum(operators::Vector{<:AbstractTTN})
return ProjTTNSum(ProjTTN.(operators), fill(1, length(operators)))
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
end

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

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

product(P::ProjTTNSum, v::ITensor) = noprime(contract(P, v))
b-kloss marked this conversation as resolved.
Show resolved Hide resolved

contract(P::ProjTTNSum, v::ITensor) = sum(p -> contract(p, v), terms(P))
contract(P::ProjTTNSum, v::ITensor) =
mapreduce(+, zip(factors(P), terms(P))) do (f, p)
f * contract(p, v)
end
mtfishman marked this conversation as resolved.
Show resolved Hide resolved

contract_ket(P::ProjTTNSum, v::ITensor) = sum(p -> contract_ket(p, v), terms(P))
contract_ket(P::ProjTTNSum, v::ITensor) =
mapreduce(+, zip(factors(P), terms(P))) do (f, p)
f * contract_ket(p, v)
end
mtfishman marked this conversation as resolved.
Show resolved Hide resolved

function Base.eltype(P::ProjTTNSum)
return mapreduce(eltype, promote_type, terms(P))
Expand Down
13 changes: 11 additions & 2 deletions test/test_treetensornetworks/test_solvers/test_contract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ using Test
Hfit = ProjOuterProdTTN(psi', H)
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,))
@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)
Hpsi = ITensorNetworks.sum_apply(
[(H, psi), (minus_identity, copy(psi))]; alg="fit", init=psi, nsweeps=1
[(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

#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
mtfishman marked this conversation as resolved.
Show resolved Hide resolved

#
# Change "top" indices of MPO to be a different set
#
Expand Down Expand Up @@ -84,7 +93,7 @@ end
os_id += -1, "Id", vertices(s)[1], "Id", vertices(s)[1]
minus_identity = TTN(os_id, s)
Hpsi = ITensorNetworks.sum_apply(
[(H, psi), (minus_identity, copy(psi))]; alg="fit", init=psi, nsweeps=1
[(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

Expand Down
Loading