Skip to content

Commit

Permalink
Support mat2vec and vec2mat for QuantumObject
Browse files Browse the repository at this point in the history
  • Loading branch information
ytdHuang authored Apr 7, 2024
2 parents 0060fff + fa85ed7 commit 0096192
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/general_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ function vec2mat(A::AbstractVector)
reshape(A, newsize, newsize)
end

@doc raw"""
vec2mat(A::QuantumObject)
Convert a quantum object from vector (`OperatorKetQuantumObject`-type) to matrix (`OperatorQuantumObject`-type)
"""
vec2mat(A::QuantumObject{<:AbstractArray{T},OperatorKetQuantumObject}) where {T} = QuantumObject(vec2mat(A.data), OperatorQuantumObject, A.dims)

@doc raw"""
gaussian(x::Number, μ::Number, σ::Number)
Expand Down Expand Up @@ -387,4 +393,11 @@ function mat2vec(::Type{M}) where M <: Union{Adjoint{<:BlasFloat,<:SparseMatrixC
npar = length(par)
(2 == npar) || error("Type $M is not supported.")
return SparseVector{par[1], par[2]}
end
end

@doc raw"""
mat2vec(A::QuantumObject)
Convert a quantum object from matrix (`OperatorQuantumObject`-type) to vector (`OperatorKetQuantumObject`-type)
"""
mat2vec(A::QuantumObject{<:AbstractArray{T},OperatorQuantumObject}) where {T} = QuantumObject(mat2vec(A.data), OperatorKetQuantumObject, A.dims)
8 changes: 4 additions & 4 deletions test/quantum_objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
# Operator-Ket, Operator-Bra tests
H = 0.3 * sigmax() + 0.7 * sigmaz()
L = liouvillian(H)
a4 = rand(ComplexF64, 2, 2)
ρ = Qobj(a4)
ρ_ket = Qobj(mat2vec(a4), type=OperatorKetQuantumObject)
ρ = Qobj(rand(ComplexF64, 2, 2))
ρ_ket = mat2vec(ρ)
ρ_bra = ρ_ket'
@test ρ_bra == Qobj(mat2vec(a4)', type=OperatorBraQuantumObject)
@test ρ_bra == Qobj(mat2vec.data)', type=OperatorBraQuantumObject)
@test ρ == vec2mat(ρ_ket)
@test isket(ρ_ket) == false
@test isbra(ρ_ket) == false
@test isoper(ρ_ket) == false
Expand Down

0 comments on commit 0096192

Please sign in to comment.