Skip to content

Commit

Permalink
Merge e052500 into 4f7e1a8
Browse files Browse the repository at this point in the history
  • Loading branch information
david-pl committed Sep 10, 2018
2 parents 4f7e1a8 + e052500 commit 3998102
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 61 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ os:
- linux
julia:
- 0.7
- 1.0
- nightly
matrix:
allow_failures:
Expand All @@ -12,7 +13,7 @@ notifications:
email: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone(pwd()); Pkg.build("QuantumOptics"); Pkg.test("QuantumOptics"; coverage=true)';
- julia -e 'import Pkg; Pkg.clone(pwd()); Pkg.build("QuantumOptics"); Pkg.test("QuantumOptics"; coverage=true)';
after_success:
- julia -e 'cd(Pkg.dir("QuantumOptics")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
- julia -e 'cd(Pkg.dir("QuantumOptics")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())';
- julia -e 'import Pkg; cd(Pkg.dir("QuantumOptics")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
- julia -e 'import Pkg; cd(Pkg.dir("QuantumOptics")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())';
31 changes: 13 additions & 18 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
- julia_version: 0.7
- julia_version: 1.0 # latest 1.0.x
- julia_version: nightly

platform:
- x86 # 32-bit
- x64 # 64-bit

matrix:
allow_failures:
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
- julia_version: nightly

branches:
only:
Expand All @@ -22,19 +24,12 @@ notifications:
on_build_status_changed: false

install:
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
# Download most recent Julia Windows binary
- ps: (new-object net.webclient).DownloadFile(
$env:JULIA_URL,
"C:\projects\julia-binary.exe")
# Run installer silently, output to C:\projects\julia
- C:\projects\julia-binary.exe /S /D=C:\projects\julia
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))

build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
Pkg.clone(pwd(), \"QuantumOptics\"); Pkg.build(\"QuantumOptics\")"
- echo "%JL_BUILD_SCRIPT%"
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"

test_script:
- C:\projects\julia\bin\julia --check-bounds=yes -e "Pkg.test(\"QuantumOptics\")"
- echo "%JL_TEST_SCRIPT%"
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"
14 changes: 7 additions & 7 deletions src/semiclassical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ operators.expect(op, state::State) = expect(op, state.quantum)
operators.variance(op, state::State) = variance(op, state.quantum)
operators.ptrace(state::State, indices::Vector{Int}) = State{DenseOperator}(ptrace(state.quantum, indices), state.classical)

operators_dense.dm(x::State{Ket}) = State{DenseOperator}(dm(x.quantum), x.classical)
operators_dense.dm(x::State{T}) where T<:Ket = State{DenseOperator}(dm(x.quantum), x.classical)


"""
Expand All @@ -57,11 +57,11 @@ Integrate time-dependent Schrödinger equation coupled to a classical system.
normalized nor permanent!
* `kwargs...`: Further arguments are passed on to the ode solver.
"""
function schroedinger_dynamic(tspan, state0::State{Ket}, fquantum::Function, fclassical::Function;
function schroedinger_dynamic(tspan, state0::State{T}, fquantum::Function, fclassical::Function;
fout::Union{Function,Nothing}=nothing,
kwargs...)
kwargs...) where T <: Ket
tspan_ = convert(Vector{Float64}, tspan)
dschroedinger_(t, state::State{Ket}, dstate::State{Ket}) = dschroedinger_dynamic(t, state, fquantum, fclassical, dstate)
dschroedinger_(t, state::State{T}, dstate::State{T}) = dschroedinger_dynamic(t, state, fquantum, fclassical, dstate)
x0 = Vector{ComplexF64}(undef, length(state0))
recast!(state0, x0)
state = copy(state0)
Expand Down Expand Up @@ -104,7 +104,7 @@ function master_dynamic(tspan, state0::State{DenseOperator}, fquantum, fclassica
integrate(tspan_, dmaster_, x0, state, dstate, fout; kwargs...)
end

function master_dynamic(tspan, state0::State{Ket}, fquantum, fclassical; kwargs...)
function master_dynamic(tspan, state0::State{T}, fquantum, fclassical; kwargs...) where T<:Ket
master_dynamic(tspan, dm(state0), fquantum, fclassical; kwargs...)
end

Expand All @@ -122,8 +122,8 @@ function recast!(x::Vector{ComplexF64}, state::State)
copyto!(state.classical, 1, x, N+1, length(state.classical))
end

function dschroedinger_dynamic(t::Float64, state::State{Ket}, fquantum::Function,
fclassical::Function, dstate::State{Ket})
function dschroedinger_dynamic(t::Float64, state::State{T}, fquantum::Function,
fclassical::Function, dstate::State{T}) where T<:Ket
fquantum_(t, psi) = fquantum(t, state.quantum, state.classical)
timeevolution.timeevolution_schroedinger.dschroedinger_dynamic(t, state.quantum, fquantum_, dstate.quantum)
fclassical(t, state.quantum, state.classical, dstate.classical)
Expand Down
33 changes: 21 additions & 12 deletions src/states.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ in respect to a certain basis. These coefficients are stored in the
`data` field and the basis is defined in the `basis`
field.
"""
abstract type StateVector end
abstract type StateVector{B<:Basis,T<:Vector{ComplexF64}} end

"""
Bra(b::Basis[, data])
Bra state defined by coefficients in respect to the basis.
"""
mutable struct Bra <: StateVector
basis::Basis
data::Vector{ComplexF64}
function Bra(b::Basis, data)
mutable struct Bra{B<:Basis,T<:Vector{ComplexF64}} <: StateVector{B,T}
basis::B
data::T
function Bra{B,T}(b::B, data::T) where {B<:Basis,T<:Vector{ComplexF64}}
if length(b) != length(data)
throw(DimensionMismatch())
end
Expand All @@ -42,20 +42,26 @@ end
Ket state defined by coefficients in respect to the given basis.
"""
mutable struct Ket <: StateVector
basis::Basis
data::Vector{ComplexF64}
function Ket(b::Basis, data)
mutable struct Ket{B<:Basis,T<:Vector{ComplexF64}} <: StateVector{B,T}
basis::B
data::T
function Ket{B,T}(b::B, data::T) where {B<:Basis,T<:Vector{ComplexF64}}
if length(b) != length(data)
throw(DimensionMismatch())
end
new(b, data)
end
end

Bra(b::B, data::T) where {B<:Basis,T<:Vector{ComplexF64}} = Bra{B,T}(b, data)
Ket(b::B, data::T) where {B<:Basis,T<:Vector{ComplexF64}} = Ket{B,T}(b, data)

Bra(b::Basis) = Bra(b, zeros(ComplexF64, length(b)))
Ket(b::Basis) = Ket(b, zeros(ComplexF64, length(b)))

Ket(b::Basis, data) = Ket(b, convert(Vector{ComplexF64}, data))
Bra(b::Basis, data) = Ket(b, convert(Vector{ComplexF64}, data))

copy(a::T) where {T<:StateVector} = T(a.basis, copy(a.data))
length(a::StateVector) = length(a.basis)::Int
basis(a::StateVector) = a.basis
Expand All @@ -68,7 +74,7 @@ basis(a::StateVector) = a.basis
-(a::T) where {T<:StateVector} = T(a.basis, -a.data)
-(a::T, b::T) where {T<:StateVector} = (check_samebases(a, b); T(a.basis, a.data-b.data))

*(a::Bra, b::Ket) = (check_multiplicable(a, b); sum(a.data.*b.data))
*(a::Bra{B,D}, b::Ket{B,D}) where {B<:Basis,D<:Vector{ComplexF64}} = (check_multiplicable(a, b); sum(a.data.*b.data))
*(a::Number, b::T) where {T<:StateVector} = T(b.basis, a*b.data)
*(a::T, b::Number) where {T<:StateVector} = T(a.basis, b*a.data)

Expand All @@ -88,9 +94,12 @@ dagger(x::Ket) = Bra(x.basis, conj(x.data))
Tensor product ``|x⟩⊗|y⟩⊗|z⟩⊗…`` of the given states.
"""
tensor(a::T, b::T) where {T<:StateVector} = T(tensor(a.basis, b.basis), kron(b.data, a.data))
tensor(a::Ket, b::Ket) = Ket(tensor(a.basis, b.basis), kron(b.data, a.data))
tensor(a::Bra, b::Bra) = Bra(tensor(a.basis, b.basis), kron(b.data, a.data))
tensor(state::StateVector) = state
tensor(states::T...) where {T<:StateVector} = reduce(tensor, states)
tensor(states::Ket...) = reduce(tensor, states)
tensor(states::Bra...) = reduce(tensor, states)
tensor(states::Vector{T}) where T<:StateVector = reduce(tensor, states)

# Normalization functions
"""
Expand Down
29 changes: 15 additions & 14 deletions src/stochastic_semiclassical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ Integrate time-dependent Schrödinger equation coupled to a classical system.
each time step taken by the solver.
* `kwargs...`: Further arguments are passed on to the ode solver.
"""
function schroedinger_semiclassical(tspan, state0::State{Ket}, fquantum::Function,
function schroedinger_semiclassical(tspan, state0::State{T}, fquantum::Function,
fclassical::Function; fstoch_quantum::Union{Nothing, Function}=nothing,
fstoch_classical::Union{Nothing, Function}=nothing,
fout::Union{Function,Nothing}=nothing,
noise_processes::Int=0,
noise_prototype_classical=nothing,
normalize_state::Bool=false,
kwargs...)
kwargs...) where T<:Ket
tspan_ = convert(Vector{Float64}, tspan)
dschroedinger_det(t::Float64, state::State{Ket}, dstate::State{Ket}) = semiclassical.dschroedinger_dynamic(t, state, fquantum, fclassical, dstate)
dschroedinger_det(t::Float64, state::State{T}, dstate::State{T}) where T<:Ket =
semiclassical.dschroedinger_dynamic(t, state, fquantum, fclassical, dstate)

if isa(fstoch_quantum, Nothing) && isa(fstoch_classical, Nothing)
throw(ArgumentError("No stochastic functions provided!"))
Expand Down Expand Up @@ -104,9 +105,9 @@ function schroedinger_semiclassical(tspan, state0::State{Ket}, fquantum::Functio
ncb = nothing
end

dschroedinger_stoch(dx::DiffArray,
t::Float64, state::State{Ket}, dstate::State{Ket}, n::Int) =
dschroedinger_stoch(dx::DiffArray, t::Float64, state::State{T}, dstate::State{T}, n::Int) where T<:Ket =
dschroedinger_stochastic(dx, t, state, fstoch_quantum, fstoch_classical, dstate, n)

integrate_stoch(tspan_, dschroedinger_det, dschroedinger_stoch, x0, state, dstate, fout, n;
noise_prototype_classical = noise_prototype_classical,
ncb=ncb,
Expand Down Expand Up @@ -199,22 +200,22 @@ function master_semiclassical(tspan::Vector{Float64}, rho0::State{DenseOperator}
noise_prototype_classical=noise_prototype_classical,
kwargs...)
end
master_semiclassical(tspan::Vector{Float64}, psi0::State{Ket}, args...; kwargs...) =
master_semiclassical(tspan::Vector{Float64}, psi0::State{T}, args...; kwargs...) where T<:Ket =
master_semiclassical(tspan, dm(psi0), args...; kwargs...)

# Derivative functions
function dschroedinger_stochastic(dx::Vector{ComplexF64}, t::Float64,
state::State{Ket}, fstoch_quantum::Function, fstoch_classical::Nothing,
dstate::State{Ket}, ::Int)
state::State{T}, fstoch_quantum::Function, fstoch_classical::Nothing,
dstate::State{T}, ::Int) where T<:Ket
H = fstoch_quantum(t, state.quantum, state.classical)
recast!(dx, dstate)
QO_CHECKS[] && check_schroedinger(state.quantum, H[1])
dschroedinger(state.quantum, H[1], dstate.quantum)
recast!(dstate, dx)
end
function dschroedinger_stochastic(dx::Array{ComplexF64, 2},
t::Float64, state::State{Ket}, fstoch_quantum::Function,
fstoch_classical::Nothing, dstate::State{Ket}, n::Int)
t::Float64, state::State{T}, fstoch_quantum::Function,
fstoch_classical::Nothing, dstate::State{T}, n::Int) where T<:Ket
H = fstoch_quantum(t, state.quantum, state.classical)
for i=1:n
dx_i = @view dx[:, i]
Expand All @@ -225,13 +226,13 @@ function dschroedinger_stochastic(dx::Array{ComplexF64, 2},
end
end
function dschroedinger_stochastic(dx::DiffArray, t::Float64,
state::State{Ket}, fstoch_quantum::Nothing, fstoch_classical::Function,
dstate::State{Ket}, ::Int)
state::State{T}, fstoch_quantum::Nothing, fstoch_classical::Function,
dstate::State{T}, ::Int) where T<:Ket
dclassical = @view dx[length(state.quantum)+1:end, :]
fstoch_classical(t, state.quantum, state.classical, dclassical)
end
function dschroedinger_stochastic(dx::Array{ComplexF64, 2}, t::Float64, state::State{Ket}, fstoch_quantum::Function,
fstoch_classical::Function, dstate::State{Ket}, n::Int)
function dschroedinger_stochastic(dx::Array{ComplexF64, 2}, t::Float64, state::State{T}, fstoch_quantum::Function,
fstoch_classical::Function, dstate::State{T}, n::Int) where T<:Ket
dschroedinger_stochastic(dx, t, state, fstoch_quantum, nothing, dstate, n)

dx_i = @view dx[length(state.quantum)+1:end, n+1:end]
Expand Down
11 changes: 6 additions & 5 deletions src/subspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ using ..bases, ..states, ..operators, ..operators_dense
A basis describing a subspace embedded a higher dimensional Hilbert space.
"""
mutable struct SubspaceBasis <: Basis
mutable struct SubspaceBasis{B<:Basis,T<:Ket} <: Basis
shape::Vector{Int}
superbasis::Basis
basisstates::Vector{Ket}
superbasis::B
basisstates::Vector{T}
basisstates_hash::UInt

function SubspaceBasis(superbasis::Basis, basisstates::Vector{Ket})
function SubspaceBasis{B,T}(superbasis::B, basisstates::Vector{T}) where {B<:Basis,T<:Ket}
for state = basisstates
if state.basis != superbasis
throw(ArgumentError("The basis of the basisstates has to be the superbasis."))
Expand All @@ -30,7 +30,8 @@ mutable struct SubspaceBasis <: Basis
end
end

SubspaceBasis(basisstates::Vector{Ket}) = SubspaceBasis(basisstates[1].basis, basisstates)
SubspaceBasis(superbasis::B, basisstates::Vector{T}) where {B<:Basis,T<:Ket} = SubspaceBasis{B,T}(superbasis, basisstates)
SubspaceBasis(basisstates::Vector{T}) where T<:Ket = SubspaceBasis(basisstates[1].basis, basisstates)

==(b1::SubspaceBasis, b2::SubspaceBasis) = b1.superbasis==b2.superbasis && b1.basisstates_hash==b2.basisstates_hash

Expand Down
2 changes: 1 addition & 1 deletion test/test_semiclassical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function fclassical(t, quantumstate, u, du)
end

state0 = semiclassical.State(psi0, ComplexF64[complex(2., 3.)])
function f(t, state::semiclassical.State{Ket})
function f(t, state::semiclassical.State{T}) where T<:Ket
@test 1e-5 > norm(state.quantum - U(t)*psi0)
@test 1e-5 > abs(state.classical[1] - state0.classical[1]*exp(-t))
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_states.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ket = Ket(b)
@test_throws DimensionMismatch Ket(b, [1, 2])
@test 0 norm(bra)
@test 0 norm(ket)
@test_throws bases.IncompatibleBases bra*Ket(b1)
@test_throws MethodError bra*Ket(b1)
@test bra == bra
@test length(bra) == length(bra.data) == 15
@test length(ket) == length(ket.data) == 15
Expand Down

0 comments on commit 3998102

Please sign in to comment.