Skip to content

Commit

Permalink
Remove :table from CompositeGenerator.
Browse files Browse the repository at this point in the history
  • Loading branch information
waltergu committed May 24, 2024
1 parent 9ba17c7 commit b982ca2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
53 changes: 23 additions & 30 deletions src/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Printf: @printf, @sprintf
using RecipesBase: RecipesBase, @recipe
using Serialization: serialize
using TimerOutputs: TimerOutput, TimerOutputs, @timeit
using ..DegreesOfFreedom: plain, Boundary, Hilbert, Table, Term
using ..DegreesOfFreedom: plain, Boundary, Hilbert, Term
using ..QuantumOperators: OperatorPack, Operators, OperatorSet, OperatorSum, LinearFunction, LinearTransformation, Transformation, identity, optype
using ..Spatials: AbstractLattice, Bond, Neighbors, bonds!, isintracell
using ..Toolkit: atol, efficientoperations, rtol, decimaltostr
Expand Down Expand Up @@ -410,45 +410,43 @@ Get the complete set of parameters of an entry of (representations of) quantum o
@inline Parameters(entry::Entry) = merge(entry.parameters, Parameters(entry.boundary))

"""
CompositeGenerator{E<:Entry, T<:Union{Table, Nothing}} <: RepresentationGenerator
CompositeGenerator{E<:Entry} <: RepresentationGenerator
Abstract type for a composite representation generator of a quantum lattice system.
By protocol, it must have the following predefined contents:
* `operators::E`: the entry for the generated (representations of) quantum operators
* `table::T`: the index-sequence table if it is not nothing
"""
abstract type CompositeGenerator{E<:Entry, T<:Union{Table, Nothing}} <: RepresentationGenerator end
abstract type CompositeGenerator{E<:Entry} <: RepresentationGenerator end
@inline ExpansionStyle(::Type{<:CompositeGenerator{E}}) where {E<:Entry} = ExpansionStyle(E)
@inline contentnames(::Type{<:CompositeGenerator}) = (:operators, :table)
@inline contentnames(::Type{<:CompositeGenerator}) = (:operators,)
@inline Base.valtype(::Type{<:CompositeGenerator{E}}) where {E<:Entry} = valtype(E)
@inline Base.eltype(::Type{<:CompositeGenerator{E}}) where {E<:Entry} = eltype(E)
@inline expand(gen::CompositeGenerator, ::Lazy) = expand(getcontent(gen, :operators), lazy)
@inline Parameters(gen::CompositeGenerator) = Parameters(getcontent(gen, :operators))
@inline Entry(gen::CompositeGenerator) = getcontent(gen, :operators)

"""
OperatorGenerator{E<:Entry{<:Operators}, TS<:Tuple{Vararg{Term}}, B<:Bond, H<:Hilbert, T<:Union{Table, Nothing}} <: CompositeGenerator{E, T}
OperatorGenerator{E<:Entry{<:Operators}, TS<:Tuple{Vararg{Term}}, B<:Bond, H<:Hilbert} <: CompositeGenerator{E}
A generator of operators based on the terms, bonds and Hilbert space of a quantum lattice system.
"""
struct OperatorGenerator{E<:Entry{<:Operators}, TS<:Tuple{Vararg{Term}}, B<:Bond, H<:Hilbert, T<:Union{Table, Nothing}} <: CompositeGenerator{E, T}
struct OperatorGenerator{E<:Entry{<:Operators}, TS<:Tuple{Vararg{Term}}, B<:Bond, H<:Hilbert} <: CompositeGenerator{E}
operators::E
terms::TS
bonds::Vector{B}
hilbert::H
half::Bool
table::T
end
@inline contentnames(::Type{<:OperatorGenerator}) = (:operators, :terms, :bonds, :hilbert, :half, :table)
@inline contentnames(::Type{<:OperatorGenerator}) = (:operators, :terms, :bonds, :hilbert, :half)

"""
OperatorGenerator(terms::Tuple{Vararg{Term}}, bonds::Vector{<:Bond}, hilbert::Hilbert, boundary::Boundary=plain, table::Union{Table,Nothing}=nothing, style::ExpansionStyle=eager; half::Bool=false)
OperatorGenerator(terms::Tuple{Vararg{Term}}, bonds::Vector{<:Bond}, hilbert::Hilbert, boundary::Boundary=plain, style::ExpansionStyle=eager; half::Bool=false)
Construct a generator of operators.
"""
@inline function OperatorGenerator(terms::Tuple{Vararg{Term}}, bonds::Vector{<:Bond}, hilbert::Hilbert, boundary::Boundary=plain, table::Union{Table,Nothing}=nothing, style::ExpansionStyle=eager; half::Bool=false)
return OperatorGenerator(Entry(terms, bonds, hilbert, boundary, style; half=half), terms, bonds, hilbert, half, table)
@inline function OperatorGenerator(terms::Tuple{Vararg{Term}}, bonds::Vector{<:Bond}, hilbert::Hilbert, boundary::Boundary=plain, style::ExpansionStyle=eager; half::Bool=false)
return OperatorGenerator(Entry(terms, bonds, hilbert, boundary, style; half=half), terms, bonds, hilbert, half)
end

"""
Expand All @@ -469,12 +467,11 @@ end
Get an empty copy of or empty an operator generator.
"""
@inline function Base.empty(gen::OperatorGenerator)
return OperatorGenerator(empty(gen.operators), gen.terms, empty(gen.bonds), empty(gen.hilbert), gen.half, isnothing(gen.table) ? nothing : empty(gen.table))
return OperatorGenerator(empty(gen.operators), gen.terms, empty(gen.bonds), empty(gen.hilbert), gen.half)
end
function Base.empty!(gen::OperatorGenerator)
empty!(gen.bonds)
empty!(gen.hilbert)
isnothing(gen.table) || empty!(gen.table)
empty!(gen.operators)
return gen
end
Expand All @@ -488,7 +485,6 @@ function reset!(gen::OperatorGenerator, lattice::AbstractLattice, hilbert::Hilbe
isa(neighbors, Neighbors) || (neighbors = Neighbors(lattice, neighbors))
bonds!(empty!(gen.bonds), lattice, neighbors)
merge!(empty!(gen.hilbert), hilbert)
isnothing(gen.table) || reset!(gen.table, gen.hilbert)
reset!(gen.operators, gen.terms, gen.bonds, gen.hilbert, replace(gen.operators.boundary; vectors=lattice.vectors); half=gen.half)
return gen
end
Expand Down Expand Up @@ -541,25 +537,24 @@ end
end

"""
Image{E<:Entry, H<:Transformation, T<:Union{Table, Nothing}} <: CompositeGenerator{E, T}
Image{E<:Entry, H<:Transformation} <: CompositeGenerator{E}
The image of a transformation applied to a representation of a quantum lattice system.
"""
mutable struct Image{E<:Entry, H<:Transformation, T<:Union{Table, Nothing}} <: CompositeGenerator{E, T}
mutable struct Image{E<:Entry, H<:Transformation} <: CompositeGenerator{E}
const operators::E
transformation::H
const table::T
const sourceid::UInt
end
@inline contentnames(::Type{<:Image}) = (:operators, :transformation, :table, :sourceid)
@inline contentnames(::Type{<:Image}) = (:operators, :transformation, :sourceid)

"""
(transformation::Transformation)(gen::RepresentationGenerator, table::Union{Table, Nothing}=nothing; kwargs...) -> Image
(transformation::Transformation)(gen::RepresentationGenerator; kwargs...) -> Image
Get the image of a transformation applied to a representation of a quantum lattice system.
"""
@inline function (transformation::Transformation)(gen::RepresentationGenerator, table::Union{Table, Nothing}=nothing; kwargs...)
return Image(transformation(Entry(gen); kwargs...), transformation, table, objectid(gen))
@inline function (transformation::Transformation)(gen::RepresentationGenerator; kwargs...)
return Image(transformation(Entry(gen); kwargs...), transformation, objectid(gen))
end

"""
Expand All @@ -568,10 +563,9 @@ end
Get an empty copy of or empty the image of a transformation applied to a representation.
"""
@inline Base.empty(gen::Image) = Image(empty(gen.operators), gen.transformation, isnothing(gen.table) ? nothing : empty(gen.table), gen.sourceid)
@inline Base.empty(gen::Image) = Image(empty(gen.operators), gen.transformation, gen.sourceid)
@inline function Base.empty!(gen::Image)
empty!(gen.operators)
isnothing(gen.table) || empty!(gen.table)
return gen
end

Expand All @@ -597,19 +591,18 @@ Update the parameters of the image based on its source representation.
end

"""
reset!(gen::Image, transformation::Transformation, source::CompositeGenerator, table::Union{Table, Nothing}=getcontent(source, :table); kwargs...) -> Image
reset!(gen::Image, transformation::Transformation, source::RepresentationGenerator, table::Union{Table, Nothing}=gen.table; kwargs...) -> Image
reset!(gen::Image, transformation::Transformation, source::CompositeGenerator; kwargs...) -> Image
reset!(gen::Image, transformation::Transformation, source::RepresentationGenerator; kwargs...) -> Image
Reset the image of a transformation applied to a representation.
"""
@inline function reset!(gen::Image, transformation::Transformation, source::CompositeGenerator, table::Union{Table, Nothing}=getcontent(source, :table); kwargs...)
return invoke(reset!, Tuple{Image, Transformation, RepresentationGenerator, Union{Table, Nothing}}, gen, transformation, source, table; kwargs...)
@inline function reset!(gen::Image, transformation::Transformation, source::CompositeGenerator; kwargs...)
return invoke(reset!, Tuple{Image, Transformation, RepresentationGenerator}, gen, transformation, source; kwargs...)
end
@inline function reset!(gen::Image, transformation::Transformation, source::RepresentationGenerator, table::Union{Table, Nothing}=gen.table; kwargs...)
@inline function reset!(gen::Image, transformation::Transformation, source::RepresentationGenerator; kwargs...)
@assert gen.sourceid==objectid(source) "reset! error: mismatched image, transformation and source representation."
reset!(gen.operators, transformation, Entry(source); kwargs...)
gen.transformation = transformation
isnothing(table) || gen.table===table || merge!(empty!(gen.table), table)
return gen
end

Expand Down
31 changes: 15 additions & 16 deletions 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: plain, Boundary, CompositeIndex, Coupling, Hilbert, IIDSpace, Index, OperatorUnitToTuple, SimpleIID, SimpleInternal, Table, Term
using QuantumLattices.DegreesOfFreedom: plain, Boundary, CompositeIndex, Coupling, Hilbert, IIDSpace, Index, OperatorUnitToTuple, SimpleIID, SimpleInternal, 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 @@ -61,14 +61,13 @@ end
end

@testset "RepresentationGenerator" begin
@test contentnames(CompositeGenerator) == (:operators, :table)
@test contentnames(OperatorGenerator) == (:operators, :terms, :bonds, :hilbert, :half, :table)
@test contentnames(Image) == (:operators, :transformation, :table, :sourceid)
@test contentnames(CompositeGenerator) == (:operators,)
@test contentnames(OperatorGenerator) == (:operators, :terms, :bonds, :hilbert, :half)
@test contentnames(Image) == (:operators, :transformation, :sourceid)

lattice = Lattice([0.0], [0.5]; vectors=[[1.0]])
bs = bonds(lattice, 1)
hilbert = Hilbert(site=>FFock(2) for site=1:length(lattice))
table = Table(hilbert, OperatorUnitToTuple(:site))
boundary = Boundary{(:θ,)}([0.1], lattice.vectors)

t = Term{:Hp}(:t, 2.0, 1, Coupling(1.0, (1, 2), FID, (2, 1)), false; ismodulatable=false)
Expand Down Expand Up @@ -117,7 +116,7 @@ end
@test reset!(deepcopy(another), (t, μ), bs, hilbert, boundary; half=true) == entry
@test reset!(deepcopy(another), i, entry) == entry

cgen = OperatorGenerator((t, μ), bs, hilbert, boundary, table; half=true)
cgen = OperatorGenerator((t, μ), bs, hilbert, boundary; half=true)
@test cgen == deepcopy(cgen) && isequal(cgen, deepcopy(cgen))
@test cgen|>eltype == cgen|>typeof|>eltype == optp
@test cgen|>valtype == cgen|>typeof|>valtype == Operators{optp, idtype(optp)}
Expand All @@ -130,18 +129,18 @@ end
@test expand(cgen, , 1)+expand(cgen, , 2) μops
@test expand(cgen, :t, 3) tops₁
@test expand(cgen, :t, 4) tops₂*2.0
@test empty!(deepcopy(cgen)) == OperatorGenerator((t, μ), empty(bs), empty(hilbert), boundary, empty(table); half=true) == empty(cgen)
@test empty!(deepcopy(cgen)) == OperatorGenerator((t, μ), empty(bs), empty(hilbert), boundary; half=true) == empty(cgen)
@test reset!(empty(cgen), lattice, hilbert) == cgen
@test update!(cgen, μ=1.5)|>expand tops₁+tops₂*2.0+μops*1.5

sgen = i(cgen, table)
@test sgen == Image(cgen.operators, i, table, objectid(cgen))
sgen = i(cgen)
@test sgen == Image(cgen.operators, i, objectid(cgen))
@test Parameters(sgen) == (t=2.0, μ=1.5, θ=0.1)
@test expand!(Operators{optp}(), sgen) == expand(sgen) tops₁+tops₂*2.0+μops*1.5
@test empty!(deepcopy(sgen)) == Image(empty(cgen.operators), i, empty(table), objectid(cgen)) == empty(sgen)
@test empty!(deepcopy(sgen)) == Image(empty(cgen.operators), i, objectid(cgen)) == empty(sgen)
@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) == sgen
@test reset!(empty(sgen), i, cgen) == sgen

t = Term{:Hp}(:t, 2.0, 1, Coupling(1.0, (1, 2), FID, (2, 1)), false; ismodulatable=false)
μ = Term{:Mu}(, 1.0, 0, Coupling(1.0, (1, 1), FID, (2, 1)), true)
Expand All @@ -164,7 +163,7 @@ end
@test reset!(deepcopy(another), (t, μ), bs, hilbert, plain; half=true) == entry
@test reset!(deepcopy(another), i, entry) == entry

cgen = OperatorGenerator((t, μ), bs, hilbert, plain, table; half=true)
cgen = OperatorGenerator((t, μ), bs, hilbert, plain; half=true)
@test expand(cgen) tops + μops
@test expand(cgen, :t) tops
@test expand(cgen, ) μops
Expand All @@ -174,14 +173,14 @@ end
@test reset!(empty(cgen), lattice, hilbert) == cgen
@test update!(cgen, μ=1.5)|>expand tops+μops*1.5

sgen = i(cgen, table)
@test sgen == Image(cgen.operators, i, table, objectid(cgen))
sgen = i(cgen)
@test sgen == Image(cgen.operators, i, 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 empty!(deepcopy(sgen)) == Image(empty(cgen.operators), i, 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) == sgen
@test reset!(empty(sgen), i, cgen) == sgen
end

mutable struct VCA <: Frontend
Expand Down

0 comments on commit b982ca2

Please sign in to comment.