Skip to content

Commit

Permalink
Add update! and reset! for a CategorizedGenerator by a `Trasnsf…
Browse files Browse the repository at this point in the history
…ormation` and an `OperatorGenerator`.
  • Loading branch information
waltergu committed Dec 19, 2024
1 parent 4dc8c30 commit 46d1937
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
25 changes: 21 additions & 4 deletions src/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,14 @@ function update!(cat::CategorizedGenerator{<:OperatorSum}; parameters...)
end

"""
update!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator{<:Operators}; kwargs...) -> CategorizedGenerator
update!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator; kwargs...) -> CategorizedGenerator
Update the parameters (including the boundary parameters) of a categorized generator based on its source categorized generator of (representations of) quantum operators and the corresponding linear transformation.
!!! Note
The coefficients of `boundops` are also updated due to the change of the boundary parameters.
"""
function update!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator{<:Operators}; kwargs...)
function update!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator; kwargs...)
cat.parameters = update(cat.parameters; source.parameters...)
if !match(Parameters(cat.boundary), Parameters(source.boundary))
update!(cat.boundary; Parameters(source.boundary)...)
Expand All @@ -456,11 +456,11 @@ function update!(cat::CategorizedGenerator, transformation::LinearTransformation
end

"""
reset!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator{<:Operators}; kwargs...)
reset!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator; kwargs...)
Reset a categorized generator by its source categorized generator of (representations of) quantum operators and the corresponding linear transformation.
"""
function reset!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator{<:Operators}; kwargs...)
function reset!(cat::CategorizedGenerator, transformation::LinearTransformation, source::CategorizedGenerator; kwargs...)
add!(empty!(cat.constops), transformation, source.constops; kwargs...)
map((dest, ops)->add!(empty!(dest), transformation, ops; kwargs...), values(cat.alterops), values(source.alterops))
map((dest, ops)->add!(empty!(dest), transformation, ops; kwargs...), values(cat.boundops), values(source.boundops))
Expand Down Expand Up @@ -649,6 +649,23 @@ Get the transformation applied to a generator of quantum operators.
"""
@inline (transformation::Transformation)(gen::OperatorGenerator; kwargs...) = transformation(gen.operators; kwargs...)

"""
update!(cat::CategorizedGenerator, transformation::LinearTransformation, source::OperatorGenerator; kwargs...) -> CategorizedGenerator
Update the parameters (including the boundary parameters) of a categorized generator based on its source operator generator of (representations of) quantum operators and the corresponding linear transformation.
!!! Note
The coefficients of `boundops` are also updated due to the change of the boundary parameters.
"""
@inline update!(cat::CategorizedGenerator, transformation::LinearTransformation, source::OperatorGenerator; kwargs...) = update!(cat, transformation, source.operators; kwargs...)

"""
reset!(cat::CategorizedGenerator, transformation::LinearTransformation, source::OperatorGenerator; kwargs...)
Reset a categorized generator by its source operator generator of (representations of) quantum operators and the corresponding linear transformation.
"""
@inline reset!(cat::CategorizedGenerator, transformation::LinearTransformation, source::OperatorGenerator; kwargs...) = reset!(cat, transformation, source.operators; kwargs...)

"""
Frontend
Expand Down
8 changes: 6 additions & 2 deletions test/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ end
@test empty!(deepcopy(cgen)) == OperatorGenerator(empty(bs), empty(hilbert), (t, μ), boundary; half=true) == empty(cgen)
@test !isempty(cgen) && isempty(empty(cgen))
@test reset!(empty(cgen), bs, hilbert; vectors=lattice.vectors) == cgen
@test update!(cgen, μ=1.5)|>expand tops₁ + tops₂*2.0 + μops*1.5
@test update!(deepcopy(cgen), μ=1.5)|>expand tops₁ + tops₂*2.0 + μops*1.5
@test LinearFunction(identity)(cgen) == cgen.operators
@test reset!(empty(cat), LinearFunction(identity), cgen) == cat
@test update!(deepcopy(cat), LinearFunction(identity), update!(deepcopy(cgen), μ=1.5))|>expand tops₁ + tops₂*2.0 + μops*1.5
end

@testset "OperatorGenerator plain" begin
Expand All @@ -207,8 +209,10 @@ end
@test expand(cgen, , 1) + expand(cgen, , 2) μops
@test expand(cgen, :t, 3) + expand(cgen, :t, 4) tops
@test reset!(empty(cgen), bs, hilbert; vectors=lattice.vectors) == cgen
@test update!(cgen, μ=1.5)|>expand tops + μops*1.5
@test update!(deepcopy(cgen), μ=1.5)|>expand tops + μops*1.5
@test LinearFunction(identity)(cgen) == cgen.operators
@test reset!(empty(cat), LinearFunction(identity), cgen) == cat
@test update!(deepcopy(cat), LinearFunction(identity), update!(deepcopy(cgen), μ=1.5))|>expand tops + μops*1.5
end

mutable struct VCA <: Frontend
Expand Down

0 comments on commit 46d1937

Please sign in to comment.