Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote state and time-span in schroedinger to the type from H(t)*psi #356

Merged
merged 42 commits into from
Jan 25, 2023
Merged
Changes from 8 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
dff8b1a
promote time and state to type of hamiltonian
Dec 21, 2022
2e78b53
innit
Dec 21, 2022
d5097bc
added nansafe_mode=true preference
Dec 21, 2022
fe2e01a
use eltype instead of _get_type
Dec 28, 2022
c00785d
update example
Dec 28, 2022
8932e03
small fix
Dec 28, 2022
73a1684
_promote_state for Bra
Dec 28, 2022
8174cb8
remove example folder
Dec 28, 2022
339da4f
promote only is detect ForwardDiff.Dual
Dec 29, 2022
4a82847
added ForwardDiff.jl
Dec 29, 2022
fa987dd
add check if state of time are Dual. promote only if not Dual
Dec 30, 2022
098ad85
tests
Dec 30, 2022
b38e498
remove DiffEq.jl
Dec 30, 2022
d8b629e
minor
Dec 30, 2022
8e5671b
dual check test, reduce repetition, small fix
Dec 31, 2022
9693133
Promote tspan to ckeck switch. Remove @time
AmitRotem Jan 1, 2023
65d15cf
Delete LocalPreferences.toml
AmitRotem Jan 1, 2023
4f0a4e8
create LocalPreferences.toml file
AmitRotem Jan 1, 2023
430c0dc
Update test_ForwardDiff.jl
AmitRotem Jan 1, 2023
f6039d0
Update test_ForwardDiff.jl
AmitRotem Jan 1, 2023
c346074
Update test_ForwardDiff.jl
AmitRotem Jan 3, 2023
102218d
use promote from DiffEqBase
Jan 3, 2023
5e71135
remove @time
Jan 3, 2023
3f1611f
~
Jan 3, 2023
9ae3ee9
tol change
Jan 3, 2023
2842f68
reduce dt
Jan 4, 2023
66c024a
Merge pull request #1 from AmitRotem/pst
AmitRotem Jan 4, 2023
cffea71
compare vs DiffEq, choose random seed
Jan 8, 2023
c3f9cc5
change range to avoid tol issue
Jan 8, 2023
0965915
Merge pull request #2 from AmitRotem/pst
AmitRotem Jan 8, 2023
b11273b
rebuild state only if promoted
Jan 10, 2023
a719bc3
modify test to impove covrage. remove test. add comments.
Jan 10, 2023
dfe7b95
Merge pull request #3 from AmitRotem/pst
AmitRotem Jan 11, 2023
26aa49a
add DiffEqBase to Project
Jan 21, 2023
447ccd5
Merge pull request #4 from AmitRotem/pst
AmitRotem Jan 21, 2023
1ca17ec
clean up AD test
Jan 22, 2023
b312938
name change
Jan 22, 2023
a128c33
Merge pull request #5 from AmitRotem/pst
AmitRotem Jan 22, 2023
203846f
Improve coverage
AmitRotem Jan 22, 2023
f3e59fc
Update test_ForwardDiff.jl
AmitRotem Jan 22, 2023
9020a11
small fix
Jan 22, 2023
c9ad058
Merge pull request #6 from AmitRotem/pst
AmitRotem Jan 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/schroedinger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function schroedinger(tspan, psi0::T, H::AbstractOperator{B,B};
fout::Union{Function,Nothing}=nothing,
kwargs...) where {B,T<:Union{AbstractOperator{B,B},StateVector{B}}}
dschroedinger_(t, psi, dpsi) = dschroedinger!(dpsi, H, psi)
tspan, psi0 = _promote_time_and_state(tspan, psi0, H) # promote
x0 = psi0.data
state = copy(psi0)
dstate = copy(psi0)
Expand All @@ -41,6 +42,7 @@ function schroedinger_dynamic(tspan, psi0, f;
fout::Union{Function,Nothing}=nothing,
kwargs...)
dschroedinger_(t, psi, dpsi) = dschroedinger_dynamic!(dpsi, f, psi, t)
tspan, psi0 = _promote_time_and_state(tspan, psi0, f) # promote
x0 = psi0.data
state = copy(psi0)
dstate = copy(psi0)
Expand Down Expand Up @@ -102,3 +104,18 @@ function check_schroedinger(psi::Bra, H)
check_multiplicable(psi, H)
check_samebases(H)
end


_promote_time_and_state(tspan, psi0, f) = _promote_time_and_state(tspan, psi0, f(first(tspan), psi0))
function _promote_time_and_state(tspan, psi0, H::AbstractOperator)
# general case is Ts<:Complex, Tt<:Real
Ts = eltype(H)
Tt = real(Ts)
(isconcretetype(Ts) && isconcretetype(Tt)) || @warn "For using `ForwardDiff` on `schroedinger` the element type of `real(H(t,psi)*psi)` must be concrete !!\nGot elements of type $Tt \nTry promoting the Hamiltonian elements based on the parameters you are differentiating by."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what situations would this warning message be seen? I feel it might be confusing as it will show up in front of users that have not touched ForwardDiff or schroedinger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove it.
This was trying to warn about something like

ω0, α = 5.0, -0.2 # not promoting
#...
fun = [fω, fα, fΩ]
H_at_t = LazySum([f(0.0) for f=fun], op)
@show H_at_t.factors #  type is abstract !! Complex{Real}

in the definition of get_Ht.
But It doesn't cover all possible "bad" definitions of get_Ht.

tspan = Tt.(tspan)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine there are situations where no promotion is necessary. Can we shortcircuit those?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll declare type Dual and only promote that case, probably would be safer.

psi0 = _promote_state(Ts, psi0)
return tspan, psi0
end
_promote_state(Ts, psi0::Operator) = Operator(psi0.basis_l, psi0.basis_r, Ts.(psi0.data))
_promote_state(Ts, psi0::Ket) = Ket(psi0.basis, Ts.(psi0.data))
_promote_state(Ts, psi0::Bra) = Bra(psi0.basis, Ts.(psi0.data))