|
| 1 | +#= |
| 2 | +This file defines the energy restricted space structure. |
| 3 | +=# |
| 4 | + |
| 5 | +export EnrSpace, enr_state_dictionaries |
| 6 | +export enr_identity, enr_fock, enr_destroy, enr_thermal_dm |
| 7 | + |
| 8 | +struct EnrSpace{N} <: AbstractSpace |
| 9 | + size::Int |
| 10 | + dims::NTuple{N,Int} |
| 11 | + n_excitations::Int |
| 12 | + state2idx::Dict{SVector{N,Int},Int} |
| 13 | + idx2state::Dict{Int,SVector{N,Int}} |
| 14 | + |
| 15 | + function EnrSpace(dims::Union{Tuple,AbstractVector}, excitations::Int) |
| 16 | + _non_static_array_warning("dims", dims) |
| 17 | + dim_len = length(dims) |
| 18 | + dims_T = NTuple{dim_len}(dims) |
| 19 | + |
| 20 | + size, state2idx, idx2state = enr_state_dictionaries(dims, excitations) |
| 21 | + |
| 22 | + return new{dim_len}(size, dims_T, excitations, state2idx, idx2state) |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | +function Base.show(io::IO, s::EnrSpace) |
| 27 | + print(io, "EnrSpace($(s.dims), $(s.n_excitations))") |
| 28 | + return nothing |
| 29 | +end |
| 30 | + |
| 31 | +Base.:(==)(s_enr1::EnrSpace, s_enr2::EnrSpace) = (all([s_enr1.size, s_enr1.dims] .== [s_enr2.size, s_enr2.dims])) |
| 32 | + |
| 33 | +dimensions_to_dims(s_enr::EnrSpace) = s_enr.dims |
| 34 | + |
| 35 | +function enr_state_dictionaries(dims::Union{Tuple,AbstractVector}, excitations::Int) |
| 36 | + len = length(dims) |
| 37 | + nvec = zeros(Int, len) |
| 38 | + result = SVector{len,Int}[nvec] # in the following, all nvec will first be converted (copied) to SVector and then push to result |
| 39 | + nexc = 0 |
| 40 | + |
| 41 | + while true |
| 42 | + idx = len |
| 43 | + nvec[end] += 1 |
| 44 | + nexc += 1 |
| 45 | + if nvec[idx] < dims[idx] |
| 46 | + push!(result, nvec) |
| 47 | + end |
| 48 | + while (nexc == excitations) || (nvec[idx] == dims[idx]) |
| 49 | + #nvec[idx] = 0 |
| 50 | + idx -= 1 |
| 51 | + if idx < 1 |
| 52 | + enr_size = length(result) |
| 53 | + return (enr_size, Dict(zip(result, 1:enr_size)), Dict(zip(1:enr_size, result))) |
| 54 | + end |
| 55 | + |
| 56 | + nexc -= nvec[idx+1] - 1 |
| 57 | + nvec[idx+1] = 0 |
| 58 | + nvec[idx] += 1 |
| 59 | + if nvec[idx] < dims[idx] |
| 60 | + push!(result, nvec) |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +function enr_identity(dims::Union{Tuple,AbstractVector}, excitations::Int) |
| 67 | + s_enr = EnrSpace(dims, excitations) |
| 68 | + return QuantumObject(Diagonal(ones(ComplexF64, s_enr.size)), Operator(), Dimensions(s_enr)) |
| 69 | +end |
| 70 | + |
| 71 | +function enr_fock( |
| 72 | + dims::Union{Tuple,AbstractVector}, |
| 73 | + excitations::Int, |
| 74 | + state::AbstractVector; |
| 75 | + sparse::Union{Bool,Val} = Val(false), |
| 76 | +) |
| 77 | + s_enr = EnrSpace(dims, excitations) |
| 78 | + if getVal(sparse) |
| 79 | + array = sparsevec([s_enr.state2idx[[state...]]], [1.0 + 0im], s_enr.size) |
| 80 | + else |
| 81 | + j = s_enr.state2idx[state] |
| 82 | + array = [i == j ? 1.0 + 0im : 0.0 + 0im for i in 1:(s_enr.size)] |
| 83 | + |
| 84 | + # s = zeros(ComplexF64, s_enr.size) |
| 85 | + # s[s_enr.state2idx[state]] += 1 |
| 86 | + # s |
| 87 | + end |
| 88 | + |
| 89 | + return QuantumObject(array, Ket(), s_enr) |
| 90 | +end |
| 91 | + |
| 92 | +function enr_destroy(dims::Union{Tuple,AbstractVector}, excitations::Int) |
| 93 | + s_enr = EnrSpace(dims, excitations) |
| 94 | + N = s_enr.size |
| 95 | + idx2state = s_enr.idx2state |
| 96 | + state2idx = s_enr.state2idx |
| 97 | + |
| 98 | + a_ops = ntuple(i -> QuantumObject(spzeros(ComplexF64, N, N), Operator(), s_enr), length(dims)) |
| 99 | + |
| 100 | + for (n1, state1) in idx2state |
| 101 | + for (idx, s) in pairs(state1) |
| 102 | + # if s > 0, the annihilation operator of mode idx has a non-zero |
| 103 | + # entry with one less excitation in mode idx in the final state |
| 104 | + if s > 0 |
| 105 | + state2 = Vector(state1) |
| 106 | + state2[idx] -= 1 |
| 107 | + n2 = state2idx[state2] |
| 108 | + a_ops[idx][n2, n1] = √s |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + return a_ops |
| 114 | +end |
| 115 | + |
| 116 | +function enr_thermal_dm(dims::Union{Tuple,AbstractVector}, excitations::Int, n::Union{Int,AbstractVector}) |
| 117 | + if n isa Number |
| 118 | + nvec = Vector{typeof(n)}(n, length(dims)) |
| 119 | + else |
| 120 | + length(n) == length(dims) || throw(ArgumentError("The Vector `n` has different length to `dims`.")) |
| 121 | + nvec = n |
| 122 | + end |
| 123 | + |
| 124 | + s_enr = EnrSpace(dims, excitations) |
| 125 | + N = s_enr.size |
| 126 | + idx2state = s_enr.idx2state |
| 127 | + |
| 128 | + diags = [prod((nvec ./ (1 .+ nvec)) .^ idx2state[idx]) for idx in 1:N] |
| 129 | + |
| 130 | + diags /= sum(diags) |
| 131 | + |
| 132 | + return QuantumObject(Diagonal(diags), Operator(), s_enr) |
| 133 | +end |
0 commit comments