Skip to content

Commit

Permalink
small changes and comment resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Dec 21, 2023
1 parent 99ed650 commit 2df89bc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
29 changes: 13 additions & 16 deletions src/operators/multipliedoperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,37 @@ end
- An function f that gives the time-dependence according to op(t) = f(t)*op
"""
const TimedOperator{O} = MultipliedOperator{O,<:Function}
TimedOperator(x) = MultipliedOperator(x, t -> One())

"""
Structure representing a time-independent operator that will be multiplied with a constant coefficient. Consists of
- An operator (MPO, Hamiltonian, ...)
- A number f that gets multiplied with the operator
"""
const UntimedOperator{O} = MultipliedOperator{O,<:Union{Real,One}}

#constructors for (un)TimedOperator
TimedOperator(x::O, f::F) where {F<:Function,O} = MultipliedOperator(x, f)
UntimedOperator(x::O, c::C) where {C<:Union{Real,One},O} = MultipliedOperator(x, c)

TimedOperator(x) = TimedOperator(x, t -> One())
UntimedOperator(x) = UntimedOperator(x, One())
const UntimedOperator{O} = MultipliedOperator{O,<:Number}
UntimedOperator(x) = MultipliedOperator(x, One())

# Holy traits
TimeDependence(x::TimedOperator) = TimeDependent()
TimeDependence(::TimedOperator) = TimeDependent()

# For internal use only
_eval_at(x::UntimedOperator) = x.f * x.op
_eval_at(x::UntimedOperator, ::Number) = x #_eval_at(x)
_eval_at(x::TimedOperator, t::Number) = UntimedOperator(x.op, x.f(t))
_eval_at(x::UntimedOperator, ::Number) = x
_eval_at(x::TimedOperator, t::Number) = MultipliedOperator(x.op, x.f(t))

# For users
(x::UntimedOperator)() = _eval_at(x)
(x::TimedOperator)(t::Number) = _eval_at(x, t)

# what to do when we multiply by a scalar
Base.:*(op::UntimedOperator, b::Number) = UntimedOperator(op.op, b * op.f)
Base.:*(op::TimedOperator, b::Number) = TimedOperator(op.op, t -> b * op.f(t))
Base.:*(b::Number, op::MultipliedOperator) = op * b
Base.:*(op::UntimedOperator, b::Number) = MultipliedOperator(op.op, b * op.f)
Base.:*(op::TimedOperator, b::Number) = MultipliedOperator(op.op, t -> b * op.f(t))
Base.:*(b, op::MultipliedOperator) = op * b

# slightly dangerous
Base.:*(op::TimedOperator, g::Function) = MultipliedOperator(op.op, t -> g(t) * op.f(t))
Base.:*(op::UntimedOperator, g::Function) = MultipliedOperator(op.op, t -> g(t) * op.f)

Base.:*(op::TimedOperator, g::Function) = TimedOperator(op.op, t -> g(t) * op.f(t)) #slightly dangerous
Base.:*(g::Function, op::TimedOperator) = op * g

# don't know a better place to put this
# environment for MultipliedOperator
Expand Down
5 changes: 3 additions & 2 deletions src/operators/timedependence.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Holy traits to differentiate between time dependent and time independent types

struct TimeDependent end
struct NotTimeDependent end
abstract type TimeDependence end
struct TimeDependent <: TimeDependence end
struct NotTimeDependent <: TimeDependence end

TimeDependence(x) = NotTimeDependent()
istimed(::TimeDependent) = true
Expand Down
4 changes: 2 additions & 2 deletions test/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ end
@test (3 + 1.55 - 0.1) * sum(E₀) sum(E) atol = 1e-2
end

Ht = TimedOperator(H, t -> 4) + UntimedOperator(H, 1.45)
Ht = MultipliedOperator(H, t -> 4) + MultipliedOperator(H, 1.45)

@testset "Finite TimeDependent LazySum $(alg isa TDVP ? "TDVP" : "TDVP2")" for alg in
algs
Expand Down Expand Up @@ -139,7 +139,7 @@ end
@test (3 + 1.55 - 0.1) * sum(E₀) sum(E) atol = 1e-2
end

Ht = TimedOperator(H, t -> 4) + UntimedOperator(H, 1.45)
Ht = MultipliedOperator(H, t -> 4) + MultipliedOperator(H, 1.45)

@testset "Infinite TimeDependent LazySum" begin
ψ, envs = timestep(ψ₀, Ht(1.0), 0.0, dt, TDVP())
Expand Down
2 changes: 1 addition & 1 deletion test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ vspaces = (ℙ^10, Rep[U₁]((0 => 20)), Rep[SU₂](1 => 10, 3 => 5, 5 => 1))
end

fs = [t -> 3t, 2, 1]
Hts = [TimedOperator(H1, fs[1]), UntimedOperator(H2, fs[2]), H3]
Hts = [MultipliedOperator(H1, fs[1]), MultipliedOperator(H2, fs[2]), H3]
summedH = LazySum(Hts)
t = 1.1
summedH_at = summedH(t)
Expand Down

0 comments on commit 2df89bc

Please sign in to comment.