-
Notifications
You must be signed in to change notification settings - Fork 31
Stochastic Schrödinger equation #210
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
4a1dbd5
ssesolve problem
lgravina1997 42f4a21
Merge remote-tracking branch 'upstream/main' into SSE
lgravina1997 0515244
fix operator_sum
lgravina1997 eef4ce9
fixes
lgravina1997 7b43c7e
improved diffusion and added comments
lgravina1997 06cd067
ensemble problem
lgravina1997 3e344ca
Fix instabilities
albertomercurio 90ad6a1
Improve WienerProcesses
albertomercurio e95ea5d
Fix type instabilities
albertomercurio 34327c0
docs
lgravina1997 139c017
api
lgravina1997 5730f8c
update project
lgravina1997 a15e236
add tests
lgravina1997 5588ef2
fix TimeEvolutionSSESol
lgravina1997 c31c665
Fix white space
albertomercurio 81885b4
[Docs] Basic Operations on Quantum Objects (#145)
ytdHuang fc5bb6b
Add Unitary Fund Badge
albertomercurio cbae37c
Improve ProgressBar thread safety (#212)
albertomercurio 27263d2
Set Version v0.12.1
albertomercurio a0f6324
Update buildkite token (#214)
ytdHuang 8039910
Normalize states for `mcsolve` solution (#213)
albertomercurio 57e6e55
Improve c_ops handling (#216)
albertomercurio 1f487d6
Change the type of `dims` for `QuantumObject` to `SVector` for type s…
albertomercurio 70f5962
Drop `Julia v1.9` (#218)
ytdHuang 8bf642c
fix typo for `versioninfo` (#220)
ytdHuang 87495c0
Fix type instabilities for almost all functions (#221)
albertomercurio de271d5
bump version to `0.13.1`
ytdHuang 6afd966
Add CUDA.allowscalar(false)
albertomercurio c2381fc
Avoid scalar indexing in runtests and fix buildkite pipeline (#222)
ytdHuang 41a194c
update README
ytdHuang 095eba4
extended methods for `expect` and `variance`
ytdHuang ae20ad7
add new section to docs
ytdHuang 25140a6
format files
ytdHuang e895dc1
change section title in docs
ytdHuang ad6c2b0
extended methods for `tensor` and `kron`
ytdHuang a54a8f8
add new section to docs
ytdHuang b283ef5
add `hat` to all docs and make `fcreate` and `fdestroy` compat with `…
ytdHuang ac0e7c0
Fix `ptrace` (#225)
ytdHuang d7bc352
Change version to v0.14.0
albertomercurio 30f9167
fix type conversion of `tlist` in time evolution
ytdHuang 4625d35
fix typo
ytdHuang 99dcb09
format files
ytdHuang 18524a6
introduce inner function `_convert_u0`
ytdHuang 78bfcbd
fix element type for `steadystate`
ytdHuang 42543a7
add comments
ytdHuang c199e46
minor change
ytdHuang 72f3974
re-organize runtests directory (#228)
ytdHuang 108bdf9
handle type of `u0` with `sparse_to_dense`
ytdHuang e03b9d9
fix typo
ytdHuang 1bae876
fix `rand_unitary` (#230)
ytdHuang 1d0437a
fix CI config (#232)
ytdHuang 08551b7
Add pipeline to run core and code quality tests under `Julia nightly`…
ytdHuang bc0074b
Introduce `Makefile` to make development more convenient (#234)
ytdHuang 0827792
Automatically convert TimeDependentOperatorSum to liouvillian
albertomercurio e94dc3a
Change version to v0.14.1
albertomercurio de1babc
add command `all` to Makefile (#236)
ytdHuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| export ssesolveProblem, ssesolveEnsembleProblem, ssesolve | ||
|
|
||
| #TODO: Check if works in GPU | ||
| function _ssesolve_update_coefficients!(ψ, coefficients, c_ops) | ||
| _get_en = op -> real(dot(ψ, op, ψ)) #this is en/2: <Sn + Sn'>/2 = Re<Sn> | ||
| @. coefficients[2:end-1] = _get_en(c_ops) #coefficients of the OperatorSum: Σ Sn * en/2 | ||
| coefficients[end] = -sum(x -> x^2, coefficients[2:end-1]) / 2 #this last coefficient is -Σen^2/8 | ||
| return nothing | ||
| end | ||
|
|
||
| function ssesolve_drift!(du, u, p, t) | ||
| _ssesolve_update_coefficients!(u, p.K.coefficients, p.c_ops) | ||
|
|
||
| mul!(du, p.K, u) | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
| function ssesolve_diffusion!(du, u, p, t) | ||
| @inbounds en = @view(p.K.coefficients[2:end-1]) | ||
|
|
||
| # du:(H,W). du_reshaped:(H*W,). | ||
| # H:Hilbert space dimension, W: number of c_ops | ||
| du_reshaped = reshape(du, :) | ||
| mul!(du_reshaped, p.D, u) #du[:,i] = D[i] * u | ||
|
|
||
| du .-= u .* reshape(en, 1, :) #du[:,i] -= en[i] * u | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
| function _ssesolve_prob_func(prob, i, repeat) | ||
| internal_params = prob.p | ||
|
|
||
| noise = RealWienerProcess!(prob.tspan[1], zeros(length(internal_params.c_ops)), zeros(length(internal_params.c_ops)), save_everystep = false) | ||
|
|
||
| prm = merge( | ||
| internal_params, | ||
| ( | ||
| expvals = similar(internal_params.expvals), | ||
| progr = ProgressBar(size(internal_params.expvals, 2), enable = false), | ||
| ), | ||
| ) | ||
|
|
||
| return remake(prob, p = prm, noise = noise) | ||
| end | ||
|
|
||
| _ssesolve_output_func(sol, i) = (sol, false) | ||
|
|
||
| function _ssesolve_generate_statistics!(sol, i, states, expvals_all) | ||
| sol_i = sol[:, i] | ||
| !isempty(sol_i.prob.kwargs[:saveat]) ? | ||
| states[i] = [QuantumObject(sol_i.u[i], dims = sol_i.prob.p.Hdims) for i in 1:length(sol_i.u)] : nothing | ||
|
|
||
| copyto!(view(expvals_all, i, :, :), sol_i.prob.p.expvals) | ||
| return nothing | ||
| end | ||
|
|
||
| function ssesolveProblem( | ||
| H::QuantumObject{MT1,OperatorQuantumObject}, | ||
| ψ0::QuantumObject{<:AbstractArray{T2},KetQuantumObject}, | ||
| tlist::AbstractVector, | ||
| c_ops::Vector{QuantumObject{Tc,OperatorQuantumObject}} = QuantumObject{MT1,OperatorQuantumObject}[]; | ||
| alg::StochasticDiffEq.StochasticDiffEqAlgorithm = EM(), | ||
| e_ops::Union{Nothing,AbstractVector} = nothing, | ||
| H_t::Union{Nothing,Function,TimeDependentOperatorSum} = nothing, | ||
| params::NamedTuple = NamedTuple(), | ||
| progress_bar::Union{Val,Bool} = Val(true), | ||
| kwargs..., | ||
| ) where {MT1<:AbstractMatrix,T2,Tc<:AbstractMatrix} | ||
| H.dims != ψ0.dims && throw(DimensionMismatch("The two quantum objects are not of the same Hilbert dimension.")) | ||
|
|
||
| haskey(kwargs, :save_idxs) && | ||
| throw(ArgumentError("The keyword argument \"save_idxs\" is not supported in QuantumToolbox.")) | ||
|
|
||
| !(H_t isa Nothing) && throw(ArgumentError("Time-dependent Hamiltonians are not currently supported in ssesolve.")) | ||
|
|
||
| progress_bar_val = makeVal(progress_bar) | ||
|
|
||
| t_l = convert(Vector{Float64}, tlist) # Convert it into Float64 to avoid type instabilities for OrdinaryDiffEq.jl | ||
|
|
||
| ϕ0 = get_data(ψ0) | ||
|
|
||
| H_eff = get_data(H - T2(0.5im) * mapreduce(op -> op' * op, +, c_ops)) | ||
| c_ops2 = get_data.(c_ops) | ||
|
|
||
| coefficients = [1.0, fill(0.0, length(c_ops) + 1)...] | ||
| operators = [-1im * H_eff, c_ops2..., I(prod(H.dims))] | ||
| K = OperatorSum(coefficients, operators) | ||
| _ssesolve_update_coefficients!(ϕ0, K.coefficients, c_ops2) | ||
|
|
||
| D = vcat(c_ops2...) | ||
|
|
||
| progr = ProgressBar(length(t_l), enable = getVal(progress_bar_val)) | ||
|
|
||
| if e_ops isa Nothing | ||
| expvals = Array{ComplexF64}(undef, 0, length(t_l)) | ||
| e_ops2 = MT1[] | ||
| is_empty_e_ops = true | ||
| else | ||
| expvals = Array{ComplexF64}(undef, length(e_ops), length(t_l)) | ||
| e_ops2 = get_data.(e_ops) | ||
| is_empty_e_ops = isempty(e_ops) | ||
| end | ||
|
|
||
| p = ( | ||
| K = K, | ||
| D = D, | ||
| e_ops = e_ops2, | ||
| c_ops = c_ops2, | ||
| expvals = expvals, | ||
| progr = progr, | ||
| Hdims = H.dims, | ||
| H_t = H_t, | ||
| is_empty_e_ops = is_empty_e_ops, | ||
| params..., | ||
| ) | ||
|
|
||
| saveat = e_ops isa Nothing ? t_l : [t_l[end]] | ||
| default_values = (DEFAULT_SDE_SOLVER_OPTIONS..., saveat = saveat) | ||
| kwargs2 = merge(default_values, kwargs) | ||
| kwargs3 = _generate_sesolve_kwargs(e_ops, progress_bar_val, t_l, kwargs2) | ||
|
|
||
| tspan = (t_l[1], t_l[end]) | ||
| noise = RealWienerProcess!(t_l[1], zeros(length(c_ops)), zeros(length(c_ops)), save_everystep = false) | ||
| noise_rate_prototype = similar(ϕ0, length(ϕ0), length(c_ops)) | ||
| return SDEProblem{true}( | ||
| ssesolve_drift!, | ||
| ssesolve_diffusion!, | ||
| ϕ0, | ||
| tspan, | ||
| p; | ||
| noise_rate_prototype = noise_rate_prototype, | ||
| noise = noise, | ||
| kwargs3..., | ||
| ) | ||
| end | ||
|
|
||
| function ssesolveEnsembleProblem( | ||
| H::QuantumObject{MT1,OperatorQuantumObject}, | ||
| ψ0::QuantumObject{<:AbstractArray{T2},KetQuantumObject}, | ||
| tlist::AbstractVector, | ||
| c_ops::Vector{QuantumObject{Tc,OperatorQuantumObject}} = QuantumObject{MT1,OperatorQuantumObject}[]; | ||
| alg::StochasticDiffEq.StochasticDiffEqAlgorithm = EM(), | ||
| e_ops::Union{Nothing,AbstractVector} = nothing, | ||
| H_t::Union{Nothing,Function,TimeDependentOperatorSum} = nothing, | ||
| params::NamedTuple = NamedTuple(), | ||
| prob_func::Function = _ssesolve_prob_func, | ||
| output_func::Function = _ssesolve_output_func, | ||
| kwargs..., | ||
| ) where {MT1<:AbstractMatrix,T2,Tc<:AbstractMatrix} | ||
| prob_sse = ssesolveProblem(H, ψ0, tlist, c_ops; alg = alg, e_ops = e_ops, H_t = H_t, params = params, kwargs...) | ||
|
|
||
| ensemble_prob = EnsembleProblem(prob_sse, prob_func = prob_func, output_func = output_func) | ||
|
|
||
| return ensemble_prob | ||
| end | ||
|
|
||
| function ssesolve( | ||
| H::QuantumObject{MT1,OperatorQuantumObject}, | ||
| ψ0::QuantumObject{<:AbstractArray{T2},KetQuantumObject}, | ||
| tlist::AbstractVector, | ||
| c_ops::Vector{QuantumObject{Tc,OperatorQuantumObject}} = QuantumObject{MT1,OperatorQuantumObject}[]; | ||
| alg::StochasticDiffEq.StochasticDiffEqAlgorithm = EM(), | ||
| e_ops::Union{Nothing,AbstractVector} = nothing, | ||
| H_t::Union{Nothing,Function,TimeDependentOperatorSum} = nothing, | ||
| params::NamedTuple = NamedTuple(), | ||
| n_traj::Int = 1, | ||
| ensemble_method = EnsembleThreads(), | ||
| prob_func::Function = _ssesolve_prob_func, | ||
| output_func::Function = _ssesolve_output_func, | ||
| kwargs..., | ||
| ) where {MT1<:AbstractMatrix,T2,Tc<:AbstractMatrix} | ||
| ens_prob = ssesolveEnsembleProblem( | ||
| H, | ||
| ψ0, | ||
| tlist, | ||
| c_ops; | ||
| alg = alg, | ||
| e_ops = e_ops, | ||
| H_t = H_t, | ||
| params = params, | ||
| prob_func = prob_func, | ||
| output_func = output_func, | ||
| kwargs..., | ||
| ) | ||
|
|
||
| return ssesolve(ens_prob; alg = alg, n_traj = n_traj, ensemble_method = ensemble_method) | ||
| end | ||
|
|
||
| function ssesolve( | ||
| ens_prob::EnsembleProblem; | ||
| alg::StochasticDiffEq.StochasticDiffEqAlgorithm = EM(), | ||
| n_traj::Int = 1, | ||
| ensemble_method = EnsembleThreads(), | ||
| ) | ||
| sol = solve(ens_prob, alg, ensemble_method, trajectories = n_traj) | ||
| _sol_1 = sol[:, 1] | ||
|
|
||
| expvals_all = Array{ComplexF64}(undef, length(sol), size(_sol_1.prob.p.expvals)...) | ||
| states = | ||
| isempty(_sol_1.prob.kwargs[:saveat]) ? fill(QuantumObject[], length(sol)) : | ||
| Vector{Vector{QuantumObject}}(undef, length(sol)) | ||
|
|
||
| foreach(i -> _ssesolve_generate_statistics!(sol, i, states, expvals_all), eachindex(sol)) | ||
| expvals = dropdims(sum(expvals_all, dims = 1), dims = 1) ./ length(sol) | ||
|
|
||
| return TimeEvolutionSSESol( | ||
| n_traj, | ||
| _sol_1.t, | ||
| states, | ||
| expvals, | ||
| expvals_all, | ||
| sol.converged, | ||
| _sol_1.alg, | ||
| _sol_1.prob.kwargs[:abstol], | ||
| _sol_1.prob.kwargs[:reltol], | ||
| ) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.