Skip to content

Commit ef8bf32

Browse files
Other fixes and format
1 parent 6f1bf9c commit ef8bf32

17 files changed

+153
-67
lines changed

src/linear_maps.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where `integrator` is the ODE integrator for the time-evolution. In this way, we
4848
"""
4949
abstract type AbstractLinearMap{T,TS} end
5050

51-
Base.eltype(A::AbstractLinearMap{T}) where T = T
51+
Base.eltype(A::AbstractLinearMap{T}) where {T} = T
5252

5353
Base.size(A::AbstractLinearMap) = A.size
5454
Base.size(A::AbstractLinearMap, i::Int) = A.size[i]

src/metrics.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ function entropy_vn(
5353
base::Int = 0,
5454
tol::Real = 1e-15,
5555
) where {T}
56-
vals = eigvals(ρ)
57-
indexes = abs.(vals) .> tol
58-
1 indexes && return 0
56+
vals = eigenenergies(ρ)
57+
indexes = findall(x -> abs(x) > tol, vals)
58+
length(indexes) == 0 && return zero(real(T))
5959
nzvals = vals[indexes]
6060
logvals = base != 0 ? log.(base, Complex.(nzvals)) : log.(Complex.(nzvals))
61-
return -real(sum(nzvals .* logvals))
61+
return -real(mapreduce(*, +, nzvals, logvals))
6262
end
6363

6464
"""

src/qobj/arithmetic_and_attributes.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ julia> tr(a' * a)
240240
LinearAlgebra.tr(
241241
A::QuantumObject{<:AbstractArray{T},OpType},
242242
) where {T,OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} = tr(A.data)
243-
LinearAlgebra.tr(A::QuantumObject{<:Union{<:Hermitian{TF}, Symmetric{TR}},OpType}) where {TF<:BlasFloat,TR<:Real,OpType<:OperatorQuantumObject} = real(tr(A.data))
243+
LinearAlgebra.tr(
244+
A::QuantumObject{<:Union{<:Hermitian{TF},Symmetric{TR}},OpType},
245+
) where {TF<:BlasFloat,TR<:Real,OpType<:OperatorQuantumObject} = real(tr(A.data))
244246

245247
@doc raw"""
246248
svdvals(A::QuantumObject)
@@ -515,7 +517,7 @@ function ptrace(QO::QuantumObject{<:AbstractArray,KetQuantumObject}, sel::Union{
515517
length(QO.dims) == 1 && return QO
516518

517519
ρtr, dkeep = _ptrace_ket(QO.data, QO.dims, SVector(sel))
518-
return QuantumObject(ρtr, type=Operator, dims = dkeep)
520+
return QuantumObject(ρtr, type = Operator, dims = dkeep)
519521
end
520522

521523
ptrace(QO::QuantumObject{<:AbstractArray,BraQuantumObject}, sel::Union{AbstractVector{Int},Tuple}) = ptrace(QO', sel)
@@ -524,7 +526,7 @@ function ptrace(QO::QuantumObject{<:AbstractArray,OperatorQuantumObject}, sel::U
524526
length(QO.dims) == 1 && return QO
525527

526528
ρtr, dkeep = _ptrace_oper(QO.data, QO.dims, SVector(sel))
527-
return QuantumObject(ρtr, type=Operator, dims = dkeep)
529+
return QuantumObject(ρtr, type = Operator, dims = dkeep)
528530
end
529531
ptrace(QO::QuantumObject, sel::Int) = ptrace(QO, SVector(sel))
530532

src/qobj/eigsolve.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ function eigsolve_al(
389389

390390
# prog = ProgressUnknown(desc="Applications:", showspeed = true, enabled=progress)
391391

392-
Lmap = ArnoldiLindbladIntegratorMap(eltype(MT1),size(L),integrator)
392+
Lmap = ArnoldiLindbladIntegratorMap(eltype(MT1), size(L), integrator)
393393

394394
res = _eigsolve(Lmap, mat2vec(ρ0), L.type, L.dims, k, krylovdim, maxiter = maxiter, tol = eigstol)
395395
# finish!(prog)

src/qobj/operators.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ The `distribution` specifies which of the method used to obtain the unitary matr
3131
!!! warning "Beware of type-stability!"
3232
If you want to keep type stability, it is recommended to use `rand_unitary(dimensions, Val(distribution))` instead of `rand_unitary(dimensions, distribution)`. Also, put `dimensions` as `Tuple` or `SVector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
3333
"""
34-
rand_unitary(dimensions::Int, distribution::Union{Symbol,Val} = Val(:haar)) = rand_unitary(SVector(dimensions), makeVal(distribution))
34+
rand_unitary(dimensions::Int, distribution::Union{Symbol,Val} = Val(:haar)) =
35+
rand_unitary(SVector(dimensions), makeVal(distribution))
3536
rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, distribution::Union{Symbol,Val} = Val(:haar)) =
3637
rand_unitary(dimensions, makeVal(distribution))
3738
function rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, ::Val{:haar})
@@ -484,7 +485,7 @@ end
484485
485486
Generates the projection operator ``\hat{O} = \dyad{i}{j}`` with Hilbert space dimension `N`.
486487
"""
487-
projection(N::Int, i::Int, j::Int) = QuantumObject(sparse([i + 1], [j + 1], [1.0 + 0.0im], N, N), type=Operator)
488+
projection(N::Int, i::Int, j::Int) = QuantumObject(sparse([i + 1], [j + 1], [1.0 + 0.0im], N, N), type = Operator)
488489

489490
@doc raw"""
490491
tunneling(N::Int, m::Int=1; sparse::Union{Bool,Val{<:Bool}}=Val(false))

src/qobj/states.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ It is also possible to specify the list of dimensions `dims` if different subsys
3333
!!! warning "Beware of type-stability!"
3434
If you want to keep type stability, it is recommended to use `fock(N, j, dims=dims, sparse=Val(sparse))` instead of `fock(N, j, dims=dims, sparse=sparse)`. Consider also to use `dims` as a `Tuple` or `SVector` instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
3535
"""
36-
function fock(
37-
N::Int,
38-
j::Int = 0;
39-
dims::Union{Int,AbstractVector{Int},Tuple} = N,
40-
sparse::Union{Bool,Val} = Val(false),
41-
)
36+
function fock(N::Int, j::Int = 0; dims::Union{Int,AbstractVector{Int},Tuple} = N, sparse::Union{Bool,Val} = Val(false))
4237
if getVal(makeVal(sparse))
4338
array = sparsevec([j + 1], [1.0 + 0im], N)
4439
else

src/steadystate.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ end
3131

3232
struct SteadyStateDirectSolver <: SteadyStateSolver end
3333
struct SteadyStateEigenSolver <: SteadyStateSolver end
34-
Base.@kwdef struct SteadyStateLinearSolver{MT<:Union{SciMLLinearSolveAlgorithm,Nothing}} <: SteadyStateSolver
35-
alg::MT = nothing
36-
Pl::Union{Function,Nothing} = nothing
37-
Pr::Union{Function,Nothing} = nothing
34+
Base.@kwdef struct SteadyStateLinearSolver{
35+
MT<:Union{SciMLLinearSolveAlgorithm,Nothing},
36+
PlT<:Union{Function,Nothing},
37+
PrT<:Union{Function,Nothing},
38+
} <: SteadyStateSolver
39+
alg::MT = KrylovJL_GMRES()
40+
Pl::PlT = nothing
41+
Pr::PrT = nothing
3842
end
3943

4044
@doc raw"""
@@ -363,13 +367,13 @@ function _steadystate_floquet(
363367
ρ0 = reshape(ρtot[offset1+1:offset2], Ns, Ns)
364368
ρ0_tr = tr(ρ0)
365369
ρ0 = ρ0 / ρ0_tr
366-
ρ0 = QuantumObject((ρ0 + ρ0') / 2, dims = L_0.dims)
370+
ρ0 = QuantumObject((ρ0 + ρ0') / 2, type = Operator, dims = L_0.dims)
367371
ρtot = ρtot / ρ0_tr
368372

369373
ρ_list = [ρ0]
370374
for i in 0:n_max-1
371375
ρi_m = reshape(ρtot[offset1-(i+1)*N+1:offset1-i*N], Ns, Ns)
372-
ρi_m = QuantumObject(ρi_m, dims = L_0.dims)
376+
ρi_m = QuantumObject(ρi_m, type = Operator, dims = L_0.dims)
373377
push!(ρ_list, ρi_m)
374378
end
375379

src/time_evolution/time_evolution.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ end
197197

198198
function liouvillian_floquet(
199199
H::QuantumObject{<:AbstractArray{T1},OpType1},
200-
c_ops::AbstractVector,
201200
Hₚ::QuantumObject{<:AbstractArray{T2},OpType2},
202201
Hₘ::QuantumObject{<:AbstractArray{T3},OpType3},
203-
ω::Real;
202+
ω::Real,
203+
c_ops::Union{AbstractVector,Nothing} = nothing;
204204
n_max::Int = 3,
205205
tol::Real = 1e-15,
206206
) where {
@@ -226,30 +226,31 @@ function liouvillian_generalized(
226226
H::QuantumObject{MT,OperatorQuantumObject},
227227
fields::Vector,
228228
T_list::Vector{<:Real};
229-
N_trunc::Int = size(H, 1),
229+
N_trunc::Union{Int,Nothing} = nothing,
230230
tol::Real = 1e-12,
231231
σ_filter::Union{Nothing,Real} = nothing,
232232
) where {MT<:AbstractMatrix}
233233
(length(fields) == length(T_list)) || throw(DimensionMismatch("The number of fields, ωs and Ts must be the same."))
234234

235-
dims = N_trunc == size(H, 1) ? H.dims : SVector(N_trunc)
235+
dims = (N_trunc isa Nothing) ? H.dims : SVector(N_trunc)
236+
final_size = prod(dims)
236237
result = eigen(H)
237-
E = real.(result.values[1:N_trunc])
238+
E = real.(result.values[1:final_size])
238239
U = QuantumObject(result.vectors, result.type, result.dims)
239240

240-
H_d = QuantumObject(Diagonal(complex(E)), dims = dims)
241+
H_d = QuantumObject(Diagonal(complex(E)), type = Operator, dims = dims)
241242

242243
Ω = E' .- E
243244
Ωp = triu(dense_to_sparse(Ω, tol), 1)
244245

245246
# Filter in the Hilbert space
246247
σ = isnothing(σ_filter) ? 500 * maximum([norm(field) / length(field) for field in fields]) : σ_filter
247-
F1 = QuantumObject(gaussian.(Ω, 0, σ), dims = dims)
248+
F1 = QuantumObject(gaussian.(Ω, 0, σ), type = Operator, dims = dims)
248249
F1 = dense_to_sparse(F1, tol)
249250

250251
# Filter in the Liouville space
251-
# M1 = ones(N_trunc, N_trunc)
252-
M1 = similar(E, N_trunc, N_trunc)
252+
# M1 = ones(final_size, final_size)
253+
M1 = similar(E, final_size, final_size)
253254
M1 .= 1
254255
Ω1 = kron(Ω, M1)
255256
Ω2 = kron(M1, Ω)
@@ -261,16 +262,16 @@ function liouvillian_generalized(
261262

262263
for i in eachindex(fields)
263264
# The operator that couples the system to the bath in the eigenbasis
264-
X_op = dense_to_sparse((U'*fields[i]*U).data[1:N_trunc, 1:N_trunc], tol)
265+
X_op = dense_to_sparse((U'*fields[i]*U).data[1:final_size, 1:final_size], tol)
265266
if ishermitian(fields[i])
266267
X_op = (X_op + X_op') / 2 # Make sure it's hermitian
267268
end
268269

269270
# Ohmic reservoir
270271
N_th = n_th.(Ωp, T_list[i])
271-
Sp₀ = QuantumObject(triu(X_op, 1), dims = dims)
272-
Sp₁ = QuantumObject(droptol!((@. Ωp * N_th * Sp₀.data), tol), dims = dims)
273-
Sp₂ = QuantumObject(droptol!((@. Ωp * (1 + N_th) * Sp₀.data), tol), dims = dims)
272+
Sp₀ = QuantumObject(triu(X_op, 1), type = Operator, dims = dims)
273+
Sp₁ = QuantumObject(droptol!((@. Ωp * N_th * Sp₀.data), tol), type = Operator, dims = dims)
274+
Sp₂ = QuantumObject(droptol!((@. Ωp * (1 + N_th) * Sp₀.data), tol), type = Operator, dims = dims)
274275
# S0 = QuantumObject( spdiagm(diag(X_op)), dims=dims )
275276

276277
L +=

test/eigenvalues_and_operators.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,25 @@
9393
@test isapprox(vec2mat(vecs[1]).data * exp(-1im * angle(vecs[1][1])), vec2mat(state3[1]).data, atol = 1e-5)
9494

9595
@testset "Type Inference (eigen)" begin
96-
N = 5
97-
a = kron(destroy(N), qeye(N))
98-
a_d = a'
99-
b = kron(qeye(N), destroy(N))
100-
b_d = b'
101-
102-
ωc = 1
103-
ωb = 1
104-
g = 0.01
105-
κ = 0.1
106-
n_thermal = 0.01
107-
108-
H = ωc * a_d * a + ωb * b_d * b + g * (a + a_d) * (b + b_d)
109-
c_ops = [((1 + n_thermal) * κ) * a, κ * b, (n_thermal * κ) * a_d]
110-
L = liouvillian(H, c_ops)
111-
112-
@inferred eigenstates(H, sparse = false)
113-
@inferred eigenstates(H, sparse = true)
114-
@inferred eigenstates(L, sparse = true)
115-
@inferred eigsolve_al(L, 1 \ (40 * κ), k = 10)
96+
N = 5
97+
a = kron(destroy(N), qeye(N))
98+
a_d = a'
99+
b = kron(qeye(N), destroy(N))
100+
b_d = b'
101+
102+
ωc = 1
103+
ωb = 1
104+
g = 0.01
105+
κ = 0.1
106+
n_thermal = 0.01
107+
108+
H = ωc * a_d * a + ωb * b_d * b + g * (a + a_d) * (b + b_d)
109+
c_ops = [((1 + n_thermal) * κ) * a, κ * b, (n_thermal * κ) * a_d]
110+
L = liouvillian(H, c_ops)
111+
112+
@inferred eigenstates(H, sparse = false)
113+
@inferred eigenstates(H, sparse = true)
114+
@inferred eigenstates(L, sparse = true)
115+
@inferred eigsolve_al(L, 1 \ (40 * κ), k = 10)
116116
end
117117
end

test/entanglement.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
rho = state * state'
66
@test entanglement(state, 1) / log(2) 1
77
@test entanglement(rho, 1) / log(2) 1
8+
9+
@testset "Type Stability (entanglement)" begin
10+
@inferred entanglement(state, 1)
11+
@inferred entanglement(rho, 1)
12+
end
813
end

0 commit comments

Comments
 (0)