Skip to content

Commit

Permalink
Use dot syntax (broadcasting) where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
bastikr committed Apr 20, 2017
1 parent 51f47ae commit 565ebf2
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/fock.jl
Expand Up @@ -33,7 +33,7 @@ end
Number operator for the specified Fock space.
"""
function number(b::FockBasis)
diag = Complex128[complex(x) for x=0:b.N]
diag = complex.(0.:b.N)
data = spdiagm(diag, 0, b.N+1, b.N+1)
SparseOperator(b, data)
end
Expand All @@ -44,7 +44,7 @@ end
Annihilation operator for the specified Fock space.
"""
function destroy(b::FockBasis)
diag = Complex128[complex(sqrt(x)) for x=1:b.N]
diag = complex.(sqrt.(1.:b.N))
data = spdiagm(diag, 1, b.N+1, b.N+1)
SparseOperator(b, data)
end
Expand All @@ -55,7 +55,7 @@ end
Creation operator for the specified Fock space.
"""
function create(b::FockBasis)
diag = Complex128[complex(sqrt(x)) for x=1:b.N]
diag = complex.(sqrt.(1.:b.N))
data = spdiagm(diag, -1, b.N+1, b.N+1)
SparseOperator(b, data)
end
Expand Down
8 changes: 4 additions & 4 deletions src/master.jl
Expand Up @@ -175,7 +175,7 @@ Further information can be found at [`master`](@ref).
"""
function master_h(tspan, rho0::DenseOperator, H::Operator, J::Vector;
Gamma::Union{Vector{Float64}, Matrix{Float64}}=ones(Float64, length(J)),
Jdagger::Vector=map(dagger, J),
Jdagger::Vector=dagger.(J),
fout::Union{Function,Void}=nothing,
tmp::DenseOperator=deepcopy(rho0),
kwargs...)
Expand Down Expand Up @@ -204,7 +204,7 @@ Further information can be found at [`master`](@ref).
function master_nh(tspan, rho0::DenseOperator, Hnh::Operator, J::Vector;
Gamma::Union{Vector{Float64}, Matrix{Float64}}=ones(Float64, length(J)),
Hnhdagger::Operator=dagger(Hnh),
Jdagger::Vector=map(dagger, J),
Jdagger::Vector=dagger.(J),
fout::Union{Function,Void}=nothing,
tmp::DenseOperator=deepcopy(rho0),
kwargs...)
Expand Down Expand Up @@ -250,7 +250,7 @@ non-hermitian Hamiltonian and then calls master_nh which is slightly faster.
"""
function master(tspan, rho0::DenseOperator, H::Operator, J::Vector;
Gamma::Union{Vector{Float64}, Matrix{Float64}}=ones(Float64, length(J)),
Jdagger::Vector=map(dagger, J),
Jdagger::Vector=dagger.(J),
fout::Union{Function,Void}=nothing,
tmp::DenseOperator=deepcopy(rho0),
kwargs...)
Expand All @@ -262,7 +262,7 @@ end

function master(tspan, rho0::DenseOperator, H::DenseOperator, J::Vector{DenseOperator};
Gamma::Union{Vector{Float64}, Matrix{Float64}}=ones(Float64, length(J)),
Jdagger::Vector{DenseOperator}=map(dagger, J),
Jdagger::Vector{DenseOperator}=dagger.(J),
fout::Union{Function,Void}=nothing,
tmp::DenseOperator=deepcopy(rho0),
kwargs...)
Expand Down
12 changes: 3 additions & 9 deletions src/mcwf.jl
Expand Up @@ -39,9 +39,7 @@ function integrate_mcwf(dmcwf::Function, jumpfun::Function, tspan, psi0::Ket, se
djumpnorm(t, x::Vector{Complex128}) = norm(as_ket(x))^2 - (1-jumpnorm[1])
function dojump(t, x::Vector{Complex128})
jumpfun(rng, t, as_ket(x), tmp)
for i=1:length(x)
x[i] = tmp.data[i]
end
x .= tmp.data
jumpnorm[1] = rand(rng)
return ode_dopri.jump
end
Expand Down Expand Up @@ -81,16 +79,12 @@ Default jump function.
function jump(rng, t::Float64, psi::Ket, J::Vector, psi_new::Ket)
if length(J)==1
operators.gemv!(complex(1.), J[1], psi, complex(0.), psi_new)
N = norm(psi_new)
for i=1:length(psi_new.data)
psi_new.data[i] /= N
end
psi_new.data ./= norm(psi_new)
else
probs = zeros(Float64, length(J))
for i=1:length(J)
operators.gemv!(complex(1.), J[i], psi, complex(0.), psi_new)
#probs[i] = norm(psi_new)^2
probs[i] = dagger(psi_new)*psi_new
probs[i] = dot(psi_new.data, psi_new.data)
end
cumprobs = cumsum(probs./sum(probs))
r = rand(rng)
Expand Down
6 changes: 3 additions & 3 deletions src/operators_lazyproduct.jl
Expand Up @@ -37,8 +37,8 @@ end
LazyProduct(operators::Vector, factor::Number=1) = LazyProduct(convert(Vector{Operator}, operators), factor)
LazyProduct(operators::Operator...) = LazyProduct(Operator[operators...])

Base.full(op::LazyProduct) = op.factor*prod(full(op_i) for op_i in op.operators)
Base.sparse(op::LazyProduct) = op.factor*prod(sparse(op_i) for op_i in op.operators)
Base.full(op::LazyProduct) = op.factor*prod(full.(op.operators))
Base.sparse(op::LazyProduct) = op.factor*prod(sparse.(op.operators))

==(x::LazyProduct, y::LazyProduct) = (x.basis_l == y.basis_l) && (x.basis_r == y.basis_r) && x.operators==y.operators && x.factor == y.factor

Expand All @@ -52,7 +52,7 @@ Base.sparse(op::LazyProduct) = op.factor*prod(sparse(op_i) for op_i in op.operat

identityoperator(::Type{LazyProduct}, b1::Basis, b2::Basis) = LazyProduct(identityoperator(b1, b2))

dagger(op::LazyProduct) = LazyProduct([dagger(op_i) for op_i in reverse(op.operators)], conj(op.factor))
dagger(op::LazyProduct) = LazyProduct(dagger.(reverse(op.operators)), conj(op.factor))

trace(op::LazyProduct) = throw(ArgumentError("Trace of LazyProduct is not defined. Try to convert to another operator type first with e.g. full() or sparse()."))

Expand Down
8 changes: 4 additions & 4 deletions src/operators_lazysum.jl
Expand Up @@ -36,8 +36,8 @@ end
LazySum{T<:Number}(factors::Vector{T}, operators::Vector) = LazySum(complex(factors), Operator[op for op in operators])
LazySum(operators::Operator...) = LazySum(ones(Complex128, length(operators)), Operator[operators...])

Base.full(op::LazySum) = sum(a*full(op_i) for (a, op_i) in zip(op.factors, op.operators))
Base.sparse(op::LazySum) = sum(a*sparse(op_i) for (a, op_i) in zip(op.factors, op.operators))
Base.full(op::LazySum) = sum(op.factors .* full.(op.operators))
Base.sparse(op::LazySum) = sum(op.factors .* sparse.(op.operators))

==(x::LazySum, y::LazySum) = (x.basis_l == y.basis_l) && (x.basis_r == y.basis_r) && x.operators==y.operators && x.factors==y.factors

Expand All @@ -54,9 +54,9 @@ Base.sparse(op::LazySum) = sum(a*sparse(op_i) for (a, op_i) in zip(op.factors, o

identityoperator(::Type{LazySum}, b1::Basis, b2::Basis) = LazySum(identityoperator(b1, b2))

dagger(op::LazySum) = LazySum(conj(op.factors), Operator[dagger(op_i) for op_i in op.operators])
dagger(op::LazySum) = LazySum(conj.(op.factors), dagger.(op.operators))

trace(op::LazySum) = sum(op.factors[i]*trace(op.operators[i]) for i in 1:length(op.operators))
trace(op::LazySum) = sum(op.factors .* trace.(op.operators))

normalize!(op::LazySum) = (op.factors /= trace(op))

Expand Down
2 changes: 1 addition & 1 deletion src/steadystate.jl
Expand Up @@ -37,7 +37,7 @@ function master(H::Operator, J::Vector;
rho0::DenseOperator=tensor(basisstate(H.basis_l, 1), dagger(basisstate(H.basis_r, 1))),
eps::Float64=1e-3, hmin=1e-7,
Gamma::Union{Vector{Float64}, Matrix{Float64}}=ones(Float64, length(J)),
Jdagger::Vector=map(dagger, J),
Jdagger::Vector=dagger.(J),
fout::Union{Function,Void}=nothing,
tmp::DenseOperator=deepcopy(rho0),
kwargs...)
Expand Down
2 changes: 1 addition & 1 deletion src/superoperators.jl
Expand Up @@ -158,7 +158,7 @@ The super-operator ``S`` is defined by
"""
function liouvillian{T<:Operator}(H::T, J::Vector{T};
Gamma::Union{Vector{Float64}, Matrix{Float64}}=ones(Float64, length(J)),
Jdagger::Vector{T}=map(dagger, J))
Jdagger::Vector{T}=dagger.(J))
_check_input(H, J, Jdagger, Gamma)
L = spre(-1im*H) + spost(1im*H)
if typeof(Gamma) == Matrix{Float64}
Expand Down
4 changes: 2 additions & 2 deletions src/timecorrelations.jl
Expand Up @@ -38,7 +38,7 @@ criterion specified in [`steadystate.master`](@ref).
function correlation(tspan::Vector{Float64}, rho0::DenseOperator, H::Operator, J::Vector,
op1::Operator, op2::Operator;
Gamma::Union{Real, Vector, Matrix}=ones(Float64, length(J)),
Jdagger::Vector=map(dagger, J),
Jdagger::Vector=dagger.(J),
tmp::DenseOperator=deepcopy(rho0),
kwargs...)
exp_values = Complex128[]
Expand All @@ -54,7 +54,7 @@ function correlation(rho0::DenseOperator, H::Operator, J::Vector,
op1::Operator, op2::Operator;
eps::Float64=1e-4, h0=10.,
Gamma::Union{Real, Vector, Matrix}=ones(Float64, length(J)),
Jdagger::Vector=map(dagger, J),
Jdagger::Vector=dagger.(J),
tmp::DenseOperator=deepcopy(rho0),
kwargs...)
op2rho0 = op2*rho0
Expand Down
4 changes: 2 additions & 2 deletions src/timeevolution_simple.jl
Expand Up @@ -33,7 +33,7 @@ end
Integrate master equation.
"""
function master(T::Vector, rho0::DenseOperator, H::Operator, J::Vector;
Jdagger=map(dagger,J),
Jdagger=dagger.(J),
gamma::Union{Real, Vector, Matrix}=ones(Int, length(J)),
kwargs...)
check_samebases(rho0, H)
Expand All @@ -59,7 +59,7 @@ function master(T::Vector, rho0::DenseOperator, H::Operator, J::Vector;
return tout, rho_t
end

master(T::Vector, psi0::Ket, H::Operator, J::Vector; kwargs...) = master(T, tensor(psi0, dagger(psi0)), H, J; kwargs...)
master(T::Vector, psi0::Ket, H::Operator, J::Vector; kwargs...) = master(T, dm(psi0), H, J; kwargs...)


"""
Expand Down

0 comments on commit 565ebf2

Please sign in to comment.