From fa85ed714a3605bb284628df5f6ae75e96efa495 Mon Sep 17 00:00:00 2001 From: Yi-Te Huang Date: Sun, 7 Apr 2024 17:43:21 +0800 Subject: [PATCH] support and for --- src/general_functions.jl | 15 ++++++++++++++- test/quantum_objects.jl | 8 ++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/general_functions.jl b/src/general_functions.jl index 1f418bfa..6a02ed2c 100644 --- a/src/general_functions.jl +++ b/src/general_functions.jl @@ -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) @@ -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 \ No newline at end of file +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) \ No newline at end of file diff --git a/test/quantum_objects.jl b/test/quantum_objects.jl index 0f01baac..2c9ce581 100644 --- a/test/quantum_objects.jl +++ b/test/quantum_objects.jl @@ -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