From 2349826f649b41ecd75154982ab80f9e853b7d8c Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 09:27:19 +0200 Subject: [PATCH 1/9] promote TimeDependentSum in schroedinger_dynamics --- src/schroedinger.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/schroedinger.jl b/src/schroedinger.jl index ede39534..fffc12b1 100644 --- a/src/schroedinger.jl +++ b/src/schroedinger.jl @@ -56,7 +56,9 @@ end function schroedinger_dynamic(tspan, psi0::T, H::AbstractTimeDependentOperator; kwargs...) where {B,Bp,T<:Union{AbstractOperator{B,Bp},StateVector{B}}} - schroedinger_dynamic(tspan, psi0, schroedinger_dynamic_function(H); kwargs...) + tspan, psi0 = _promote_time_and_state(psi0, H, tspan) + Ht = TimeDependentSum(H.coefficients, H.static_op; init_time=zero(promote_type(eltype(tspan), typeof(current_time(H))))) + schroedinger_dynamic(tspan, psi0, schroedinger_dynamic_function(Ht); kwargs...) end """ From ec2e6b95dc6d411682ac9ab791daa558de5c6283 Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 09:46:11 +0200 Subject: [PATCH 2/9] test autodiff with tdop --- test/test_ForwardDiff.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/test_ForwardDiff.jl b/test/test_ForwardDiff.jl index 01d8135c..876f56c6 100644 --- a/test/test_ForwardDiff.jl +++ b/test/test_ForwardDiff.jl @@ -50,3 +50,24 @@ for u0 = (psi, psi', psi⊗psi') # test all methods of `rebuild` end end # testset + +@testset "ForwardDiff with schroedinger using TimeDependentSum" begin + +base=SpinBasis(1/2) +ψi = spinup(base) +ψt = spindown(base) +function Ftdop(q) + H = TimeDependentSum([q, abs2∘sinpi], [sigmaz(base), sigmax(base)]) + _, ψf = schroedinger_dynamic(range(0,1,2), ψi, H) + abs2(ψt'last(ψf)) +end +@test FD.derivative(Ftdop, 1.0) isa Any + +function Ftdop(q) + H = TimeDependentSum([1, abs2∘sinpi], [sigmaz(base), q*sigmax(base)]) + _, ψf = schroedinger_dynamic(range(0,1,2), ψi, H) + abs2(ψt'last(ψf)) +end +@test FD.derivative(Ftdop, 1.0) isa Any + +end # testset From 0c123dc9b59892e1c55830fd8db0173235221a7e Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 10:05:58 +0200 Subject: [PATCH 3/9] small fix needs to promote LazySum factors --- src/schroedinger.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schroedinger.jl b/src/schroedinger.jl index fffc12b1..db497dc2 100644 --- a/src/schroedinger.jl +++ b/src/schroedinger.jl @@ -57,7 +57,7 @@ end function schroedinger_dynamic(tspan, psi0::T, H::AbstractTimeDependentOperator; kwargs...) where {B,Bp,T<:Union{AbstractOperator{B,Bp},StateVector{B}}} tspan, psi0 = _promote_time_and_state(psi0, H, tspan) - Ht = TimeDependentSum(H.coefficients, H.static_op; init_time=zero(promote_type(eltype(tspan), typeof(current_time(H))))) + Ht = TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(tspan), typeof(current_time(H))))) schroedinger_dynamic(tspan, psi0, schroedinger_dynamic_function(Ht); kwargs...) end From d2e1875a5c4c8aafc60fc1803c8b394e0dc72a2f Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 11:18:34 +0200 Subject: [PATCH 4/9] Update test_ForwardDiff.jl --- test/test_ForwardDiff.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_ForwardDiff.jl b/test/test_ForwardDiff.jl index 876f56c6..8c34804e 100644 --- a/test/test_ForwardDiff.jl +++ b/test/test_ForwardDiff.jl @@ -61,13 +61,13 @@ function Ftdop(q) _, ψf = schroedinger_dynamic(range(0,1,2), ψi, H) abs2(ψt'last(ψf)) end -@test FD.derivative(Ftdop, 1.0) isa Any +@test ForwardDiff.derivative(Ftdop, 1.0) isa Any function Ftdop(q) H = TimeDependentSum([1, abs2∘sinpi], [sigmaz(base), q*sigmax(base)]) _, ψf = schroedinger_dynamic(range(0,1,2), ψi, H) abs2(ψt'last(ψf)) end -@test FD.derivative(Ftdop, 1.0) isa Any +@test ForwardDiff.derivative(Ftdop, 1.0) isa Any end # testset From a47801031f978fb347f5286639c0e834b22839af Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 14:36:12 +0200 Subject: [PATCH 5/9] Update test_ForwardDiff.jl --- test/test_ForwardDiff.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_ForwardDiff.jl b/test/test_ForwardDiff.jl index 8c34804e..91b889fc 100644 --- a/test/test_ForwardDiff.jl +++ b/test/test_ForwardDiff.jl @@ -58,16 +58,18 @@ base=SpinBasis(1/2) ψt = spindown(base) function Ftdop(q) H = TimeDependentSum([q, abs2∘sinpi], [sigmaz(base), sigmax(base)]) - _, ψf = schroedinger_dynamic(range(0,1,2), ψi, H) + _, ψf = timeevolution.schroedinger_dynamic(range(0,1,2), ψi, H) abs2(ψt'last(ψf)) end +Ftdop(1.0) @test ForwardDiff.derivative(Ftdop, 1.0) isa Any function Ftdop(q) H = TimeDependentSum([1, abs2∘sinpi], [sigmaz(base), q*sigmax(base)]) - _, ψf = schroedinger_dynamic(range(0,1,2), ψi, H) + _, ψf = timeevolution.schroedinger_dynamic(range(0,1,2), ψi, H) abs2(ψt'last(ψf)) end +Ftdop(1.0) @test ForwardDiff.derivative(Ftdop, 1.0) isa Any end # testset From dc52f9f9c62f7a3e1932f55a3ba0b59a4c2cf552 Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 16:39:30 +0200 Subject: [PATCH 6/9] promote only if Dual --- src/schroedinger.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/schroedinger.jl b/src/schroedinger.jl index db497dc2..317b0d36 100644 --- a/src/schroedinger.jl +++ b/src/schroedinger.jl @@ -56,9 +56,13 @@ end function schroedinger_dynamic(tspan, psi0::T, H::AbstractTimeDependentOperator; kwargs...) where {B,Bp,T<:Union{AbstractOperator{B,Bp},StateVector{B}}} - tspan, psi0 = _promote_time_and_state(psi0, H, tspan) - Ht = TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(tspan), typeof(current_time(H))))) - schroedinger_dynamic(tspan, psi0, schroedinger_dynamic_function(Ht); kwargs...) + promoted_tspan, psi0 = _promote_time_and_state(psi0, H, tspan) + if promoted_tspan !== tspan # promote H + promoted_H = schroedinger_dynamic_function( TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(tspan), typeof(current_time(H))))) ) + return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(promoted_H); kwargs...) + else + return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(H); kwargs...) + end end """ From 587d598319b38579f985f566541b77928d1bbd6c Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 16:41:45 +0200 Subject: [PATCH 7/9] Update schroedinger.jl --- src/schroedinger.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schroedinger.jl b/src/schroedinger.jl index 317b0d36..b03f15a2 100644 --- a/src/schroedinger.jl +++ b/src/schroedinger.jl @@ -58,7 +58,7 @@ function schroedinger_dynamic(tspan, psi0::T, H::AbstractTimeDependentOperator; kwargs...) where {B,Bp,T<:Union{AbstractOperator{B,Bp},StateVector{B}}} promoted_tspan, psi0 = _promote_time_and_state(psi0, H, tspan) if promoted_tspan !== tspan # promote H - promoted_H = schroedinger_dynamic_function( TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(tspan), typeof(current_time(H))))) ) + promoted_H = schroedinger_dynamic_function( TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(promoted_tspan), typeof(current_time(H))))) ) return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(promoted_H); kwargs...) else return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(H); kwargs...) From 0ae999fba72f116ee0f48d9ba9a2b8b8409bb7b4 Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Sun, 24 Dec 2023 19:50:13 +0200 Subject: [PATCH 8/9] Update schroedinger.jl --- src/schroedinger.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schroedinger.jl b/src/schroedinger.jl index b03f15a2..b4d5bfed 100644 --- a/src/schroedinger.jl +++ b/src/schroedinger.jl @@ -58,7 +58,7 @@ function schroedinger_dynamic(tspan, psi0::T, H::AbstractTimeDependentOperator; kwargs...) where {B,Bp,T<:Union{AbstractOperator{B,Bp},StateVector{B}}} promoted_tspan, psi0 = _promote_time_and_state(psi0, H, tspan) if promoted_tspan !== tspan # promote H - promoted_H = schroedinger_dynamic_function( TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(promoted_tspan), typeof(current_time(H))))) ) + promoted_H = TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(promoted_tspan), typeof(current_time(H))))) return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(promoted_H); kwargs...) else return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(H); kwargs...) From 52e0dc76490893980ff96a8a1e0206e4fd7318e1 Mon Sep 17 00:00:00 2001 From: Amit Rotem <66641519+AmitRotem@users.noreply.github.com> Date: Thu, 28 Dec 2023 02:04:36 +0200 Subject: [PATCH 9/9] Update schroedinger.jl --- src/schroedinger.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schroedinger.jl b/src/schroedinger.jl index b4d5bfed..63978d09 100644 --- a/src/schroedinger.jl +++ b/src/schroedinger.jl @@ -58,7 +58,7 @@ function schroedinger_dynamic(tspan, psi0::T, H::AbstractTimeDependentOperator; kwargs...) where {B,Bp,T<:Union{AbstractOperator{B,Bp},StateVector{B}}} promoted_tspan, psi0 = _promote_time_and_state(psi0, H, tspan) if promoted_tspan !== tspan # promote H - promoted_H = TimeDependentSum(H.coefficients, H.static_op.operators; init_time=zero(promote_type(eltype(promoted_tspan), typeof(current_time(H))))) + promoted_H = TimeDependentSum(H.coefficients, H.static_op.operators; init_time=first(promoted_tspan)) return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(promoted_H); kwargs...) else return schroedinger_dynamic(promoted_tspan, psi0, schroedinger_dynamic_function(H); kwargs...)