Skip to content

Commit fc4cb55

Browse files
committed
minor changes
1 parent eb11b37 commit fc4cb55

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

src/qobj/energy_restricted.jl

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ struct EnrSpace{N} <: AbstractSpace
5656
state2idx::Dict{SVector{N,Int},Int}
5757
idx2state::Dict{Int,SVector{N,Int}}
5858

59-
function EnrSpace(dims::Union{AbstractVector{T},NTuple{N,T}}, excitations::Int) where {T<:Integer,N}
59+
function EnrSpace(dims::Union{AbstractVector{T},NTuple{N,T}}, n_excitations::Int) where {T<:Integer,N}
6060
# all arguments will be checked in `enr_state_dictionaries`
61-
size, state2idx, idx2state = enr_state_dictionaries(dims, excitations)
61+
size, state2idx, idx2state = enr_state_dictionaries(dims, n_excitations)
6262

6363
L = length(dims)
64-
return new{L}(size, SVector{L}(dims), excitations, state2idx, idx2state)
64+
return new{L}(size, SVector{L}(dims), n_excitations, state2idx, idx2state)
6565
end
6666
end
6767

@@ -75,26 +75,26 @@ Base.:(==)(s_enr1::EnrSpace, s_enr2::EnrSpace) = (s_enr1.size == s_enr2.size) &&
7575
dimensions_to_dims(s_enr::EnrSpace) = s_enr.dims
7676

7777
@doc raw"""
78-
enr_state_dictionaries(dims, excitations)
78+
enr_state_dictionaries(dims, n_excitations)
7979
8080
Return the number of states, and lookup-dictionaries for translating a state (`SVector`) to a state index, and vice versa, for a system with a given number of components and maximum number of excitations.
8181
8282
# Arguments
8383
- `dims::Union{AbstractVector,Tuple}`: A list of the number of states in each sub-system
84-
- `excitations::Int`: Maximum number of excitations
84+
- `n_excitations::Int`: Maximum number of excitations
8585
8686
# Returns
8787
- `nstates`: Number of states
8888
- `state2idx`: A dictionary for looking up a state index from a state (`SVector`)
8989
- `idx2state`: A dictionary for looking up state (`SVector`) from a state index
9090
"""
91-
function enr_state_dictionaries(dims::Union{AbstractVector{T},NTuple{N,T}}, excitations::Int) where {T<:Integer,N}
91+
function enr_state_dictionaries(dims::Union{AbstractVector{T},NTuple{N,T}}, n_excitations::Int) where {T<:Integer,N}
9292
# argument checks
9393
_non_static_array_warning("dims", dims)
9494
L = length(dims)
9595
(L > 0) || throw(DomainError(dims, "The argument dims must be of non-zero length"))
9696
all(>=(1), dims) || throw(DomainError(dims, "All the elements of dims must be non-zero integers (≥ 1)"))
97-
(excitations > 0) || throw(DomainError(excitations, "The argument excitations must be a non-zero integer (≥ 1)"))
97+
(n_excitations > 0) || throw(DomainError(n_excitations, "The argument n_excitations must be a non-zero integer (≥ 1)"))
9898

9999
nvec = zeros(Int, L) # Vector
100100
nexc = 0
@@ -106,7 +106,7 @@ function enr_state_dictionaries(dims::Union{AbstractVector{T},NTuple{N,T}}, exci
106106
nvec[end] += 1
107107
nexc += 1
108108
(nvec[idx] < dims[idx]) && push!(result, nvec)
109-
while (nexc == excitations) || (nvec[idx] == dims[idx])
109+
while (nexc == n_excitations) || (nvec[idx] == dims[idx])
110110
idx -= 1
111111

112112
# if idx < 1, break while-loop and return
@@ -124,25 +124,25 @@ function enr_state_dictionaries(dims::Union{AbstractVector{T},NTuple{N,T}}, exci
124124
end
125125

126126
@doc raw"""
127-
enr_fock(dims::Union{AbstractVector,Tuple}, excitations::Int, state::AbstractVector; sparse::Union{Bool,Val}=Val(false))
127+
enr_fock(dims::Union{AbstractVector,Tuple}, n_excitations::Int, state::AbstractVector; sparse::Union{Bool,Val}=Val(false))
128128
enr_fock(s_enr::EnrSpace, state::AbstractVector; sparse::Union{Bool,Val}=Val(false))
129129
130130
Generate the Fock state representation ([`Ket`](@ref)) in an excitation number restricted state space ([`EnrSpace`](@ref)).
131131
132-
The arguments `dims` and `excitations` are used to generate [`EnrSpace`](@ref).
132+
The arguments `dims` and `n_excitations` are used to generate [`EnrSpace`](@ref).
133133
134134
The `state` argument is a list of integers that specifies the state (in the number basis representation) for which to generate the Fock state representation.
135135
136136
!!! warning "Beware of type-stability!"
137-
It is highly recommended to use `enr_fock(dims, excitations, state)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
137+
It is highly recommended to use `enr_fock(dims, n_excitations, state)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
138138
"""
139139
function enr_fock(
140140
dims::Union{AbstractVector{T},NTuple{N,T}},
141-
excitations::Int,
141+
n_excitations::Int,
142142
state::AbstractVector{T};
143143
sparse::Union{Bool,Val} = Val(false),
144144
) where {T<:Integer,N}
145-
s_enr = EnrSpace(dims, excitations)
145+
s_enr = EnrSpace(dims, n_excitations)
146146
return enr_fock(s_enr, state, sparse = sparse)
147147
end
148148
function enr_fock(s_enr::EnrSpace, state::AbstractVector{T}; sparse::Union{Bool,Val} = Val(false)) where {T<:Integer}
@@ -157,25 +157,25 @@ function enr_fock(s_enr::EnrSpace, state::AbstractVector{T}; sparse::Union{Bool,
157157
end
158158

159159
@doc raw"""
160-
enr_thermal_dm(dims::Union{AbstractVector,Tuple}, excitations::Int, n::Union{Real,AbstractVector}; sparse::Union{Bool,Val}=Val(false))
160+
enr_thermal_dm(dims::Union{AbstractVector,Tuple}, n_excitations::Int, n::Union{Real,AbstractVector}; sparse::Union{Bool,Val}=Val(false))
161161
enr_thermal_dm(s_enr::EnrSpace, n::Union{Real,AbstractVector}; sparse::Union{Bool,Val}=Val(false))
162162
163163
Generate the thermal state (density [`Operator`](@ref)) in an excitation number restricted state space ([`EnrSpace`](@ref)).
164164
165-
The arguments `dims` and `excitations` are used to generate [`EnrSpace`](@ref).
165+
The arguments `dims` and `n_excitations` are used to generate [`EnrSpace`](@ref).
166166
167167
The argument `n` is a list that specifies the expectation values for number of particles in each sub-system. If `n` is specified as a real number, it will apply to each sub-system.
168168
169169
!!! warning "Beware of type-stability!"
170-
It is highly recommended to use `enr_thermal_dm(dims, excitations, n)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
170+
It is highly recommended to use `enr_thermal_dm(dims, n_excitations, n)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
171171
"""
172172
function enr_thermal_dm(
173173
dims::Union{AbstractVector{T1},NTuple{N,T1}},
174-
excitations::Int,
174+
n_excitations::Int,
175175
n::Union{T2,AbstractVector{T2}};
176176
sparse::Union{Bool,Val} = Val(false),
177177
) where {T1<:Integer,T2<:Real,N}
178-
s_enr = EnrSpace(dims, excitations)
178+
s_enr = EnrSpace(dims, n_excitations)
179179
return enr_thermal_dm(s_enr, n; sparse = sparse)
180180
end
181181
function enr_thermal_dm(
@@ -204,26 +204,28 @@ function enr_thermal_dm(
204204
end
205205

206206
@doc raw"""
207-
enr_destroy(dims::Union{AbstractVector,Tuple}, excitations::Int)
207+
enr_destroy(dims::Union{AbstractVector,Tuple}, n_excitations::Int)
208208
enr_destroy(s_enr::EnrSpace)
209209
210210
Generate a `Tuple` of annihilation operators for each sub-system in an excitation number restricted state space ([`EnrSpace`](@ref)). Thus, the return `Tuple` will have the same length as `dims`.
211211
212-
The arguments `dims` and `excitations` are used to generate [`EnrSpace`](@ref).
212+
The arguments `dims` and `n_excitations` are used to generate [`EnrSpace`](@ref).
213213
214214
!!! warning "Beware of type-stability!"
215-
It is highly recommended to use `enr_destroy(dims, excitations)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
215+
It is highly recommended to use `enr_destroy(dims, n_excitations)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
216216
"""
217-
function enr_destroy(dims::Union{AbstractVector{T},NTuple{N,T}}, excitations::Int) where {T<:Integer,N}
218-
s_enr = EnrSpace(dims, excitations)
217+
function enr_destroy(dims::Union{AbstractVector{T},NTuple{N,T}}, n_excitations::Int) where {T<:Integer,N}
218+
s_enr = EnrSpace(dims, n_excitations)
219219
return enr_destroy(s_enr)
220220
end
221221
function enr_destroy(s_enr::EnrSpace{N}) where {N}
222222
D = s_enr.size
223223
idx2state = s_enr.idx2state
224224
state2idx = s_enr.state2idx
225225

226-
a_ops = ntuple(i -> QuantumObject(spzeros(ComplexF64, D, D), Operator(), s_enr), N)
226+
I_list = [Int64[] for _ in 1:N]
227+
J_list = [Int64[] for _ in 1:N]
228+
V_list = [ComplexF64[] for _ in 1:N]
227229

228230
for (n1, state1) in idx2state
229231
for (idx, s) in pairs(state1)
@@ -233,27 +235,29 @@ function enr_destroy(s_enr::EnrSpace{N}) where {N}
233235
state2 = Vector(state1)
234236
state2[idx] -= 1
235237
n2 = state2idx[state2]
236-
a_ops[idx][n2, n1] = s
238+
push!(I_list[idx], n2)
239+
push!(J_list[idx], n1)
240+
push!(V_list[idx], s)
237241
end
238242
end
239243
end
240244

241-
return a_ops
245+
return ntuple(i -> QuantumObject(sparse(I_list[i], J_list[i], V_list[i], D, D), Operator(), s_enr), Val(N))
242246
end
243247

244248
@doc raw"""
245-
enr_identity(dims::Union{AbstractVector,Tuple}, excitations::Int)
249+
enr_identity(dims::Union{AbstractVector,Tuple}, n_excitations::Int)
246250
enr_identity(s_enr::EnrSpace)
247251
248252
Generate the identity operator in an excitation number restricted state space ([`EnrSpace`](@ref)).
249253
250-
The arguments `dims` and `excitations` are used to generate [`EnrSpace`](@ref).
254+
The arguments `dims` and `n_excitations` are used to generate [`EnrSpace`](@ref).
251255
252256
!!! warning "Beware of type-stability!"
253-
It is highly recommended to use `enr_identity(dims, excitations)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
257+
It is highly recommended to use `enr_identity(dims, n_excitations)` with `dims` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. 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.
254258
"""
255-
function enr_identity(dims::Union{AbstractVector{T},NTuple{N,T}}, excitations::Int) where {T<:Integer,N}
256-
s_enr = EnrSpace(dims, excitations)
259+
function enr_identity(dims::Union{AbstractVector{T},NTuple{N,T}}, n_excitations::Int) where {T<:Integer,N}
260+
s_enr = EnrSpace(dims, n_excitations)
257261
return enr_identity(s_enr)
258262
end
259263
enr_identity(s_enr::EnrSpace) = QuantumObject(Diagonal(ones(ComplexF64, s_enr.size)), Operator(), s_enr)

0 commit comments

Comments
 (0)