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