Skip to content

Commit

Permalink
Optimize Entry when boundary is plain.
Browse files Browse the repository at this point in the history
  • Loading branch information
waltergu committed Sep 28, 2023
1 parent e97e411 commit d8b6e1d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
33 changes: 21 additions & 12 deletions src/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ Construct an entry of quantum operators based on the input terms, bonds, Hilbert
"""
function Entry(terms::Tuple{Vararg{Term}}, bonds::Vector{<:Bond}, hilbert::Hilbert; half::Bool=false, boundary::Boundary=plain)
constterms, alterterms, choosedterms = termclassifier(terms)
innerbonds = filter(bond->isintracell(bond), bonds)
boundbonds = filter(bond->!isintracell(bond), bonds)
if boundary === plain
innerbonds = bonds
boundbonds = eltype(bonds)[]
else
innerbonds = filter(bond->isintracell(bond), bonds)
boundbonds = filter(bond->!isintracell(bond), bonds)
end
constops = Operators{mapreduce(term->optype(typeof(term), typeof(hilbert), eltype(bonds)), promote_type, choosedterms)}()
map(term->expand!(constops, term, innerbonds, hilbert; half=half), constterms)
alterops = NamedTuple{map(id, alterterms)}(map(term->expand(one(term), innerbonds, hilbert; half=half), alterterms))
Expand Down Expand Up @@ -305,8 +310,13 @@ Reset an entry of quantum operators by the new terms, bonds, Hilbert space and (
function reset!(entry::Entry{<:Operators}, terms::Tuple{Vararg{Term}}, bonds::Vector{<:Bond}, hilbert::Hilbert; half::Bool=false, boundary::Boundary=entry.boundary)
empty!(entry)
constterms, alterterms, _ = termclassifier(terms)
innerbonds = filter(bond->isintracell(bond), bonds)
boundbonds = filter(bond->!isintracell(bond), bonds)
if boundary === plain
innerbonds = bonds
boundbonds = eltype(bonds)[]
else
innerbonds = filter(bond->isintracell(bond), bonds)
boundbonds = filter(bond->!isintracell(bond), bonds)
end
map(term->expand!(entry.constops, term, innerbonds, hilbert; half=half), constterms)
map(term->expand!(getfield(entry.alterops, id(term)), one(term), innerbonds, hilbert; half=half), alterterms)
map(term->map!(boundary, expand!(getfield(entry.boundops, id(term)), one(term), boundbonds, hilbert; half=half)), terms)
Expand Down Expand Up @@ -433,16 +443,15 @@ Expand an operator generator to get:
function expand(gen::OperatorGenerator, name::Symbol)
result = zero(valtype(gen))
term = get(gen.terms, Val(name))
if !ismodulatable(term)
expand!(result, term, filter(bond->isintracell(bond), gen.bonds), gen.hilbert; half=gen.half)
else
for opt in getfield(gen.operators.alterops, name)
add!(result, opt*term.value)
for bond in gen.bonds
if isintracell(bond)
expand!(result, term, bond, gen.hilbert; half=gen.half)
else
for opt in expand(term, bond, gen.hilbert; half=gen.half)
add!(result, gen.operators.boundary(opt))
end
end
end
for opt in getfield(gen.operators.boundops, name)
add!(result, opt*term.value)
end
return result
end
function expand(gen::OperatorGenerator, i::Int)
Expand Down
2 changes: 1 addition & 1 deletion src/Spatials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using ..QuantumNumbers: Momenta, Momentum, periods
using ..Toolkit: atol, rtol, efficientoperations, CompositeDict, Float, SimpleNamedVectorSpace, Segment, VectorSpaceCartesian, VectorSpaceDirectSummed, VectorSpaceStyle, getcontent

import StaticArrays: SArray
import ..QuantumLattices: decompose, dimension, dtype, expand, kind, reset!
import ..QuantumLattices: decompose, dimension, dtype, expand, kind
import ..QuantumNumbers: Momentum₁, Momentum₂, Momentum₃
import ..Toolkit: contentnames, shape

Expand Down
42 changes: 41 additions & 1 deletion test/Frameworks.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using LinearAlgebra: dot, tr
using QuantumLattices: add!, expand, expand!, reset!, update
using QuantumLattices.DegreesOfFreedom: Boundary, CompositeIndex, Coupling, Hilbert, IIDSpace, Index, OperatorUnitToTuple, SimpleIID, SimpleInternal, Table, Term
using QuantumLattices.DegreesOfFreedom: plain, Boundary, CompositeIndex, Coupling, Hilbert, IIDSpace, Index, OperatorUnitToTuple, SimpleIID, SimpleInternal, Table, Term
using QuantumLattices.Frameworks
using QuantumLattices.QuantumOperators: ID, Identity, Operator, Operators, id, idtype
using QuantumLattices.Spatials: Lattice, Point, bonds, decompose, isintracell
Expand Down Expand Up @@ -144,6 +144,46 @@ end
@test update!(sgen, μ=3.5)|>expand tops₁+tops₂*2.0+μops*3.5
@test update!(sgen, cgen)|>expand tops₁+tops₂*2.0+μops*1.5
@test reset!(empty(sgen), i, cgen; table=table) == sgen

t = Term{:Hp}(:t, 2.0, 1, Coupling(1.0, (1, 2), FID, (2, 1)), false; modulate=false)
μ = Term{:Mu}(, 1.0, 0, Coupling(1.0, (1, 1), FID, (2, 1)), true)
tops = expand(t, bs, hilbert; half=true)
μops = expand(one(μ), bs, hilbert; half=true)
optp = Operator{Complex{Float}, ID{CompositeIndex{Index{Int, FID{Int}}, SVector{1, Float}}, 2}}
entry = Entry(tops, (μ=μops,), (t=Operators{optp}(), μ=Operators{optp}()), (t=2.0, μ=1.0), plain)
@test entry == Entry((t, μ), bs, hilbert; half=true, boundary=plain)
@test isequal(entry, i(entry))
@test Parameters(entry) == (t=2.0, μ=1.0)
@test entry+entry == entry*2 == 2*entry == Entry(tops*2, (μ=μops,), (t=Operators{optp}(), μ=Operators{optp}()), (t=4.0, μ=2.0), plain)
@test expand(entry) == expand!(Operators{optp}(), entry) tops+μops

entry₁ = Entry(tops, NamedTuple(), (t=Operators{optp}(),), (t=2.0,), plain)
entry₂ = Entry(zero(tops), (μ=μops,), (μ=Operators{optp}(),), (μ=1.0,), plain)
@test entry₁+entry₂ == Entry(tops, (μ=μops,), (t=Operators{optp}(), μ=Operators{optp}()), (t=2.0, μ=1.0), plain)

another = Entry(empty(tops), (μ=empty(μops),), (t=empty(tops), μ=Operators{optp}()), (t=2.0, μ=1.0), plain)
@test empty(entry) == empty!(deepcopy(entry)) == another
@test reset!(deepcopy(another), (t, μ), bs, hilbert; half=true, boundary=plain) == entry
@test reset!(deepcopy(another), i, entry) == entry

cgen = OperatorGenerator((t, μ), bs, hilbert; half=true, boundary=plain, table=table)
@test expand(cgen) tops + μops
@test expand(cgen, :t) tops
@test expand(cgen, ) μops
@test expand(cgen, 1)+expand(cgen, 2)+expand(cgen, 3)+expand(cgen, 4) expand(cgen)
@test expand(cgen, , 1)+expand(cgen, , 2) μops
@test expand(cgen, :t, 3)+expand(cgen, :t, 4) tops
@test reset!(empty(cgen), lattice, hilbert) == cgen
@test update!(cgen, μ=1.5)|>expand tops+μops*1.5

sgen = i(cgen; table=table)
@test sgen == Image(cgen.operators, i, table, objectid(cgen))
@test Parameters(sgen) == (t=2.0, μ=1.5)
@test expand!(Operators{optp}(), sgen) == expand(sgen) tops+μops*1.5
@test empty!(deepcopy(sgen)) == Image(empty(cgen.operators), i, empty(table), objectid(cgen)) == empty(sgen)
@test update!(sgen, μ=3.5)|>expand tops+μops*3.5
@test update!(sgen, cgen)|>expand tops+μops*1.5
@test reset!(empty(sgen), i, cgen; table=table) == sgen
end

mutable struct VCA <: Frontend
Expand Down

0 comments on commit d8b6e1d

Please sign in to comment.