From fcc7af329af653c6f262470936ea800146d52045 Mon Sep 17 00:00:00 2001 From: Alberto Mercurio Date: Wed, 26 Jun 2024 13:55:03 +0200 Subject: [PATCH 1/5] Resolve type instabilities when generating QuantumObjects --- src/qobj/quantum_object.jl | 125 +++++++++++++++++++++++-------------- test/quantum_objects.jl | 51 ++++++++++----- test/runtests.jl | 38 +++++------ 3 files changed, 131 insertions(+), 83 deletions(-) diff --git a/src/qobj/quantum_object.jl b/src/qobj/quantum_object.jl index 9e20d19d..34288a78 100644 --- a/src/qobj/quantum_object.jl +++ b/src/qobj/quantum_object.jl @@ -141,6 +141,82 @@ struct QuantumObject{MT<:AbstractArray,ObjType<:QuantumObjectType} <: AbstractQu dims::Vector{Int} end +function QuantumObject( + A::AbstractMatrix{T}; + type::ObjType = nothing, + dims = nothing, +) where {T,ObjType<:Union{Nothing,QuantumObjectType}} + + if type isa Nothing + type = Operator # default type + end + + if type != Operator && type != SuperOperator + throw(ArgumentError("The argument type must be OperatorQuantumObject or SuperOperatorQuantumObject if the input array is a matrix.")) + end + + _size = size(A) + + if dims isa Nothing + if type isa OperatorQuantumObject + dims = [_size[1]] + elseif type isa SuperOperatorQuantumObject + dims = [isqrt(_size[1])] + end + else + _check_dims(dims) + end + + _check_QuantumObject(type, dims, _size[1], _size[2]) + return QuantumObject(A, type, dims) +end + +function QuantumObject( + A::AbstractVector{T}; + type::ObjType = nothing, + dims = nothing, +) where {T,ObjType<:Union{Nothing,QuantumObjectType}} + + if type isa Nothing + type = Ket # default type + end + + if type != Ket && type != Bra && type != OperatorKet && type != OperatorBra + throw(ArgumentError("The argument type must be KetQuantumObject, BraQuantumObject, OperatorKetQuantumObject or OperatorBraQuantumObject if the input array is a vector.")) + end + + _size = (length(A), 1) + + if dims isa Nothing + if (type isa KetQuantumObject) || (type isa BraQuantumObject) + dims = [_size[1]] + elseif (type isa OperatorKetQuantumObject) || (type isa OperatorBraQuantumObject) + dims = [isqrt(_size[1])] + end + else + _check_dims(dims) + end + + if (type isa BraQuantumObject) || (type isa OperatorBraQuantumObject) + data = A' + _size = (1, length(A)) + else + data = A + end + + _check_QuantumObject(type, dims, _size[1], _size[2]) + return QuantumObject(data, type, dims) +end + +function QuantumObject( + A::AbstractArray{T,N}; + type::ObjType = nothing, + dims = nothing, +) where {T,N,ObjType<:Union{Nothing,QuantumObjectType}} + + throw(ArgumentError("The input array must be a vector or a matrix.")) +end + function Base.show( io::IO, QO::QuantumObject{<:AbstractArray{T},OpType}, @@ -175,51 +251,6 @@ function Base.show(io::IO, QO::QuantumObject{<:AbstractArray{T},OpType}) where { return show(io, MIME("text/plain"), op_data) end -function QuantumObject( - A::AbstractArray{T,N}; - type::ObjType = nothing, - dims = nothing, -) where {T,N,ObjType<:Union{Nothing,QuantumObjectType}} - - # only accept 1D- and 2D-array - if N == 1 - Size = (length(A), 1) - else - Size = size(A) - (N > 2) && throw(DomainError(Size, "The dimension of the array is not compatible with Quantum Object")) - end - - # decide QuantumObjectType from the size of A - if type isa Nothing - if Size[1] == Size[2] - type = Operator - elseif Size[2] == 1 - type = Ket - elseif Size[1] == 1 - type = Bra - else - throw(DomainError(Size, "The dimension of the array is not compatible with Quantum Object")) - end - end - - # decide dims from the size of A and the given type - if dims isa Nothing - if (type isa KetQuantumObject) || (type isa OperatorQuantumObject) - dims = [Size[1]] - elseif (type isa SuperOperatorQuantumObject) || (type isa OperatorKetQuantumObject) - dims = [isqrt(Size[1])] - elseif type isa BraQuantumObject - dims = [Size[2]] - elseif type isa OperatorBraQuantumObject - dims = [isqrt(Size[2])] - end - else - _check_dims(dims) - end - _check_QuantumObject(type, dims, Size[1], Size[2]) - return QuantumObject(A, type, dims) -end - _check_dims(dims::Vector{Int}) = all(>(0), dims) || throw(DomainError(dims, "The argument dims must be a vector with positive integers.")) _check_dims(dims::Any) = throw(ArgumentError("The argument dims must be a vector with positive integers.")) @@ -269,8 +300,8 @@ function QuantumObject( type::ObjType = A.type, dims = A.dims, ) where {T,N,ObjType<:QuantumObjectType} - Size = N == 1 ? (length(A), 1) : size(A) - _check_QuantumObject(type, dims, Size[1], Size[2]) + _size = N == 1 ? (length(A), 1) : size(A) + _check_QuantumObject(type, dims, _size[1], _size[2]) return QuantumObject(copy(A.data), type, dims) end diff --git a/test/quantum_objects.jl b/test/quantum_objects.jl index fca5e418..cf8aed64 100644 --- a/test/quantum_objects.jl +++ b/test/quantum_objects.jl @@ -1,9 +1,15 @@ @testset "Quantum Objects" begin # unsupported size of array - for a in [rand(ComplexF64, 3, 2), rand(ComplexF64, 2, 2, 2)] - for t in [nothing, Ket, Bra, Operator, SuperOperator, OperatorBra, OperatorKet] - @test_throws DomainError Qobj(a, type = t) - end + a = rand(ComplexF64, 3, 2) + for t in [nothing, Operator, SuperOperator] + @test_throws DomainError Qobj(a, type = t) + end + for t in [Ket, Bra, OperatorBra, OperatorKet] + @test_throws ArgumentError Qobj(a, type = t) + end + a = rand(ComplexF64, 2, 2, 2) + for t in [nothing, Ket, Bra, Operator, SuperOperator, OperatorBra, OperatorKet] + @test_throws ArgumentError Qobj(a, type = t) end # unsupported dims @@ -16,19 +22,17 @@ N = 10 a = rand(ComplexF64, 10) # @test_logs (:warn, "The norm of the input data is not one.") QuantumObject(a) - @test_throws DomainError Qobj(a, type = Bra) - @test_throws DomainError Qobj(a, type = Operator) - @test_throws DomainError Qobj(a, type = SuperOperator) - @test_throws DomainError Qobj(a, type = OperatorBra) - @test_throws DomainError Qobj(a', type = Ket) + @test_throws ArgumentError Qobj(a, type = Operator) + @test_throws ArgumentError Qobj(a, type = SuperOperator) + @test_throws ArgumentError Qobj(a', type = Ket) @test_throws DomainError Qobj(a', type = Operator) @test_throws DomainError Qobj(a', type = SuperOperator) - @test_throws DomainError Qobj(a', type = OperatorKet) + @test_throws ArgumentError Qobj(a', type = OperatorKet) @test_throws DimensionMismatch Qobj(a, dims = [2]) - @test_throws DimensionMismatch Qobj(a', dims = [2]) - a2 = Qobj(a') + @test_throws DomainError Qobj(a', dims = [2]) + a2 = Qobj(a, type = Bra) a3 = Qobj(a) - @test dag(a3) == a2 + @test a3' == a2 @test isket(a2) == false @test isbra(a2) == true @test isoper(a2) == false @@ -43,7 +47,6 @@ @test isoperbra(a3) == false @test Qobj(a3) == a3 @test !(Qobj(a3) === a3) - @test isket(Qobj(Matrix([2 3])')) == true a = sprand(ComplexF64, 100, 100, 0.1) a2 = Qobj(a) @@ -62,7 +65,6 @@ @test isoperket(a3) == false @test isoperbra(a3) == false @test_throws DimensionMismatch Qobj(a, dims = [2]) - @test_throws DimensionMismatch Qobj(a, dims = [2]) # Operator-Ket, Operator-Bra tests H = 0.3 * sigmax() + 0.7 * sigmaz() @@ -70,7 +72,7 @@ ρ = Qobj(rand(ComplexF64, 2, 2)) ρ_ket = mat2vec(ρ) ρ_bra = ρ_ket' - @test ρ_bra == Qobj(mat2vec(ρ.data)', type = OperatorBra) + @test ρ_bra == Qobj(mat2vec(ρ.data), type = OperatorBra) @test ρ == vec2mat(ρ_ket) @test isket(ρ_ket) == false @test isbra(ρ_ket) == false @@ -94,7 +96,7 @@ @test (ρ_bra * L')' == L * ρ_ket @test sum((conj(ρ) .* ρ).data) ≈ dot(ρ_ket, ρ_ket) ≈ ρ_bra * ρ_ket @test_throws DimensionMismatch Qobj(ρ_ket.data, type = OperatorKet, dims = [4]) - @test_throws DimensionMismatch Qobj(ρ_bra.data, type = OperatorBra, dims = [4]) + @test_throws ArgumentError Qobj(ρ_bra.data, type = OperatorBra, dims = [4]) # matrix element s0 = Qobj(basis(4, 0).data; type = OperatorKet) @@ -383,4 +385,19 @@ @test_throws ArgumentError permute(bra_bdca, wrong_order2) @test_throws ArgumentError permute(op_bdca, wrong_order1) @test_throws ArgumentError permute(op_bdca, wrong_order2) + + @testset "Type Inference" begin + for T in [Float32, Float64, ComplexF32, ComplexF64] + N = 25 + a = rand(T, N) + for type in [Ket, Bra, OperatorKet, OperatorBra] + @inferred Qobj(a, type = type) + end + + a = rand(T, N, N) + for type in [Operator, SuperOperator] + @inferred Qobj(a, type = type) + end + end + end end diff --git a/test/runtests.jl b/test/runtests.jl index 2044736b..425f2c9d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,28 +9,28 @@ const testdir = dirname(@__FILE__) # Put core tests in alphabetical order core_tests = [ - "correlations_and_spectrum.jl", - "dynamical_fock_dimension_mesolve.jl", - "dynamical-shifted-fock.jl", - "eigenvalues_and_operators.jl", - "entanglement.jl", - "generalized_master_equation.jl", - "low_rank_dynamics.jl", - "negativity_and_partial_transpose.jl", - "permutation.jl", - "progress_bar.jl", + # "correlations_and_spectrum.jl", + # "dynamical_fock_dimension_mesolve.jl", + # "dynamical-shifted-fock.jl", + # "eigenvalues_and_operators.jl", + # "entanglement.jl", + # "generalized_master_equation.jl", + # "low_rank_dynamics.jl", + # "negativity_and_partial_transpose.jl", + # "permutation.jl", + # "progress_bar.jl", "quantum_objects.jl", - "states_and_operators.jl", - "steady_state.jl", - "time_evolution_and_partial_trace.jl", - "wigner.jl", + # "states_and_operators.jl", + # "steady_state.jl", + # "time_evolution_and_partial_trace.jl", + # "wigner.jl", ] -if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") - Pkg.add(["Aqua", "JET"]) - include(joinpath(testdir, "aqua.jl")) - include(joinpath(testdir, "jet.jl")) -end +# if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") +# Pkg.add(["Aqua", "JET"]) +# include(joinpath(testdir, "aqua.jl")) +# include(joinpath(testdir, "jet.jl")) +# end if (GROUP == "All") || (GROUP == "Core") QuantumToolbox.about() From d23aff59268e17680ced0fa9e8328be3f8c27468 Mon Sep 17 00:00:00 2001 From: Alberto Mercurio Date: Wed, 26 Jun 2024 14:08:19 +0200 Subject: [PATCH 2/5] Fix format check and uncomment runtests --- src/qobj/quantum_object.jl | 17 +++++++++++------ test/runtests.jl | 38 +++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/qobj/quantum_object.jl b/src/qobj/quantum_object.jl index 34288a78..b9a9454e 100644 --- a/src/qobj/quantum_object.jl +++ b/src/qobj/quantum_object.jl @@ -146,13 +146,16 @@ function QuantumObject( type::ObjType = nothing, dims = nothing, ) where {T,ObjType<:Union{Nothing,QuantumObjectType}} - if type isa Nothing type = Operator # default type end if type != Operator && type != SuperOperator - throw(ArgumentError("The argument type must be OperatorQuantumObject or SuperOperatorQuantumObject if the input array is a matrix.")) + throw( + ArgumentError( + "The argument type must be OperatorQuantumObject or SuperOperatorQuantumObject if the input array is a matrix.", + ), + ) end _size = size(A) @@ -176,13 +179,16 @@ function QuantumObject( type::ObjType = nothing, dims = nothing, ) where {T,ObjType<:Union{Nothing,QuantumObjectType}} - if type isa Nothing type = Ket # default type end if type != Ket && type != Bra && type != OperatorKet && type != OperatorBra - throw(ArgumentError("The argument type must be KetQuantumObject, BraQuantumObject, OperatorKetQuantumObject or OperatorBraQuantumObject if the input array is a vector.")) + throw( + ArgumentError( + "The argument type must be KetQuantumObject, BraQuantumObject, OperatorKetQuantumObject or OperatorBraQuantumObject if the input array is a vector.", + ), + ) end _size = (length(A), 1) @@ -190,7 +196,7 @@ function QuantumObject( if dims isa Nothing if (type isa KetQuantumObject) || (type isa BraQuantumObject) dims = [_size[1]] - elseif (type isa OperatorKetQuantumObject) || (type isa OperatorBraQuantumObject) + elseif (type isa OperatorKetQuantumObject) || (type isa OperatorBraQuantumObject) dims = [isqrt(_size[1])] end else @@ -213,7 +219,6 @@ function QuantumObject( type::ObjType = nothing, dims = nothing, ) where {T,N,ObjType<:Union{Nothing,QuantumObjectType}} - throw(ArgumentError("The input array must be a vector or a matrix.")) end diff --git a/test/runtests.jl b/test/runtests.jl index 425f2c9d..2044736b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,28 +9,28 @@ const testdir = dirname(@__FILE__) # Put core tests in alphabetical order core_tests = [ - # "correlations_and_spectrum.jl", - # "dynamical_fock_dimension_mesolve.jl", - # "dynamical-shifted-fock.jl", - # "eigenvalues_and_operators.jl", - # "entanglement.jl", - # "generalized_master_equation.jl", - # "low_rank_dynamics.jl", - # "negativity_and_partial_transpose.jl", - # "permutation.jl", - # "progress_bar.jl", + "correlations_and_spectrum.jl", + "dynamical_fock_dimension_mesolve.jl", + "dynamical-shifted-fock.jl", + "eigenvalues_and_operators.jl", + "entanglement.jl", + "generalized_master_equation.jl", + "low_rank_dynamics.jl", + "negativity_and_partial_transpose.jl", + "permutation.jl", + "progress_bar.jl", "quantum_objects.jl", - # "states_and_operators.jl", - # "steady_state.jl", - # "time_evolution_and_partial_trace.jl", - # "wigner.jl", + "states_and_operators.jl", + "steady_state.jl", + "time_evolution_and_partial_trace.jl", + "wigner.jl", ] -# if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") -# Pkg.add(["Aqua", "JET"]) -# include(joinpath(testdir, "aqua.jl")) -# include(joinpath(testdir, "jet.jl")) -# end +if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") + Pkg.add(["Aqua", "JET"]) + include(joinpath(testdir, "aqua.jl")) + include(joinpath(testdir, "jet.jl")) +end if (GROUP == "All") || (GROUP == "Core") QuantumToolbox.about() From 0a1961d4f41540d09a60f8b70641819551db6816 Mon Sep 17 00:00:00 2001 From: Alberto Mercurio Date: Wed, 26 Jun 2024 18:11:17 +0200 Subject: [PATCH 3/5] Go back to the previous implementation for Bras --- src/qobj/quantum_object.jl | 29 +++++++++++------------------ test/quantum_objects.jl | 15 +++++++++------ test/runtests.jl | 10 +++++----- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/qobj/quantum_object.jl b/src/qobj/quantum_object.jl index b9a9454e..f231de70 100644 --- a/src/qobj/quantum_object.jl +++ b/src/qobj/quantum_object.jl @@ -150,10 +150,10 @@ function QuantumObject( type = Operator # default type end - if type != Operator && type != SuperOperator + if type != Operator && type != SuperOperator && type != Bra && type != OperatorBra throw( ArgumentError( - "The argument type must be OperatorQuantumObject or SuperOperatorQuantumObject if the input array is a matrix.", + "The argument type must be Operator, SuperOperator, Bra or OperatorBra if the input array is a matrix.", ), ) end @@ -165,6 +165,10 @@ function QuantumObject( dims = [_size[1]] elseif type isa SuperOperatorQuantumObject dims = [isqrt(_size[1])] + elseif type isa BraQuantumObject + dims = [_size[2]] + elseif type isa OperatorBraQuantumObject + dims = [isqrt(_size[2])] end else _check_dims(dims) @@ -183,35 +187,24 @@ function QuantumObject( type = Ket # default type end - if type != Ket && type != Bra && type != OperatorKet && type != OperatorBra - throw( - ArgumentError( - "The argument type must be KetQuantumObject, BraQuantumObject, OperatorKetQuantumObject or OperatorBraQuantumObject if the input array is a vector.", - ), - ) + if type != Ket && type != OperatorKet + throw(ArgumentError("The argument type must be Ket or OperatorKet if the input array is a vector.")) end _size = (length(A), 1) if dims isa Nothing - if (type isa KetQuantumObject) || (type isa BraQuantumObject) + if type isa KetQuantumObject dims = [_size[1]] - elseif (type isa OperatorKetQuantumObject) || (type isa OperatorBraQuantumObject) + elseif type isa OperatorKetQuantumObject dims = [isqrt(_size[1])] end else _check_dims(dims) end - if (type isa BraQuantumObject) || (type isa OperatorBraQuantumObject) - data = A' - _size = (1, length(A)) - else - data = A - end - _check_QuantumObject(type, dims, _size[1], _size[2]) - return QuantumObject(data, type, dims) + return QuantumObject(A, type, dims) end function QuantumObject( diff --git a/test/quantum_objects.jl b/test/quantum_objects.jl index cf8aed64..9855897f 100644 --- a/test/quantum_objects.jl +++ b/test/quantum_objects.jl @@ -1,10 +1,10 @@ @testset "Quantum Objects" begin # unsupported size of array a = rand(ComplexF64, 3, 2) - for t in [nothing, Operator, SuperOperator] + for t in [nothing, Operator, SuperOperator, Bra, OperatorBra] @test_throws DomainError Qobj(a, type = t) end - for t in [Ket, Bra, OperatorBra, OperatorKet] + for t in [Ket, OperatorKet] @test_throws ArgumentError Qobj(a, type = t) end a = rand(ComplexF64, 2, 2, 2) @@ -30,7 +30,7 @@ @test_throws ArgumentError Qobj(a', type = OperatorKet) @test_throws DimensionMismatch Qobj(a, dims = [2]) @test_throws DomainError Qobj(a', dims = [2]) - a2 = Qobj(a, type = Bra) + a2 = Qobj(a', type = Bra) a3 = Qobj(a) @test a3' == a2 @test isket(a2) == false @@ -72,7 +72,7 @@ ρ = Qobj(rand(ComplexF64, 2, 2)) ρ_ket = mat2vec(ρ) ρ_bra = ρ_ket' - @test ρ_bra == Qobj(mat2vec(ρ.data), type = OperatorBra) + @test ρ_bra == Qobj(mat2vec(ρ.data)', type = OperatorBra) @test ρ == vec2mat(ρ_ket) @test isket(ρ_ket) == false @test isbra(ρ_ket) == false @@ -96,7 +96,7 @@ @test (ρ_bra * L')' == L * ρ_ket @test sum((conj(ρ) .* ρ).data) ≈ dot(ρ_ket, ρ_ket) ≈ ρ_bra * ρ_ket @test_throws DimensionMismatch Qobj(ρ_ket.data, type = OperatorKet, dims = [4]) - @test_throws ArgumentError Qobj(ρ_bra.data, type = OperatorBra, dims = [4]) + @test_throws DimensionMismatch Qobj(ρ_bra.data, type = OperatorBra, dims = [4]) # matrix element s0 = Qobj(basis(4, 0).data; type = OperatorKet) @@ -390,9 +390,12 @@ for T in [Float32, Float64, ComplexF32, ComplexF64] N = 25 a = rand(T, N) - for type in [Ket, Bra, OperatorKet, OperatorBra] + for type in [Ket, OperatorKet] @inferred Qobj(a, type = type) end + for type in [Bra, OperatorBra] + @inferred Qobj(a', type = type) + end a = rand(T, N, N) for type in [Operator, SuperOperator] diff --git a/test/runtests.jl b/test/runtests.jl index 2044736b..049cf46c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,11 +26,11 @@ core_tests = [ "wigner.jl", ] -if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") - Pkg.add(["Aqua", "JET"]) - include(joinpath(testdir, "aqua.jl")) - include(joinpath(testdir, "jet.jl")) -end +# if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") +# Pkg.add(["Aqua", "JET"]) +# include(joinpath(testdir, "aqua.jl")) +# include(joinpath(testdir, "jet.jl")) +# end if (GROUP == "All") || (GROUP == "Core") QuantumToolbox.about() From e228fe9c2669cde300e7827b626a22151b94c660 Mon Sep 17 00:00:00 2001 From: Alberto Mercurio Date: Wed, 26 Jun 2024 18:21:41 +0200 Subject: [PATCH 4/5] Minor changes --- src/qobj/quantum_object.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qobj/quantum_object.jl b/src/qobj/quantum_object.jl index f231de70..dcabf0d2 100644 --- a/src/qobj/quantum_object.jl +++ b/src/qobj/quantum_object.jl @@ -150,7 +150,7 @@ function QuantumObject( type = Operator # default type end - if type != Operator && type != SuperOperator && type != Bra && type != OperatorBra + if !isa(type, Operator) && !isa(type, SuperOperator) && !isa(type, Bra) && !isa(type, OperatorBra) throw( ArgumentError( "The argument type must be Operator, SuperOperator, Bra or OperatorBra if the input array is a matrix.", @@ -187,7 +187,7 @@ function QuantumObject( type = Ket # default type end - if type != Ket && type != OperatorKet + if !isa(type, Ket) && !isa(type, OperatorKet) throw(ArgumentError("The argument type must be Ket or OperatorKet if the input array is a vector.")) end From bb99b86fb3a05e0aac88e6e9d38f401dd121013a Mon Sep 17 00:00:00 2001 From: Alberto Mercurio Date: Wed, 26 Jun 2024 19:30:48 +0200 Subject: [PATCH 5/5] Fix JET and minor things --- src/qobj/quantum_object.jl | 4 ++-- test/quantum_objects.jl | 3 ++- test/runtests.jl | 10 +++++----- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/qobj/quantum_object.jl b/src/qobj/quantum_object.jl index dcabf0d2..f231de70 100644 --- a/src/qobj/quantum_object.jl +++ b/src/qobj/quantum_object.jl @@ -150,7 +150,7 @@ function QuantumObject( type = Operator # default type end - if !isa(type, Operator) && !isa(type, SuperOperator) && !isa(type, Bra) && !isa(type, OperatorBra) + if type != Operator && type != SuperOperator && type != Bra && type != OperatorBra throw( ArgumentError( "The argument type must be Operator, SuperOperator, Bra or OperatorBra if the input array is a matrix.", @@ -187,7 +187,7 @@ function QuantumObject( type = Ket # default type end - if !isa(type, Ket) && !isa(type, OperatorKet) + if type != Ket && type != OperatorKet throw(ArgumentError("The argument type must be Ket or OperatorKet if the input array is a vector.")) end diff --git a/test/quantum_objects.jl b/test/quantum_objects.jl index 9855897f..440817fa 100644 --- a/test/quantum_objects.jl +++ b/test/quantum_objects.jl @@ -24,6 +24,7 @@ # @test_logs (:warn, "The norm of the input data is not one.") QuantumObject(a) @test_throws ArgumentError Qobj(a, type = Operator) @test_throws ArgumentError Qobj(a, type = SuperOperator) + @test_throws ArgumentError Qobj(a, type = OperatorBra) @test_throws ArgumentError Qobj(a', type = Ket) @test_throws DomainError Qobj(a', type = Operator) @test_throws DomainError Qobj(a', type = SuperOperator) @@ -32,7 +33,7 @@ @test_throws DomainError Qobj(a', dims = [2]) a2 = Qobj(a', type = Bra) a3 = Qobj(a) - @test a3' == a2 + @test dag(a3) == a2 # Here we are also testing the dag function @test isket(a2) == false @test isbra(a2) == true @test isoper(a2) == false diff --git a/test/runtests.jl b/test/runtests.jl index 049cf46c..2044736b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,11 +26,11 @@ core_tests = [ "wigner.jl", ] -# if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") -# Pkg.add(["Aqua", "JET"]) -# include(joinpath(testdir, "aqua.jl")) -# include(joinpath(testdir, "jet.jl")) -# end +if ((GROUP == "All") || (GROUP == "Code-Quality")) && (VERSION >= v"1.9") + Pkg.add(["Aqua", "JET"]) + include(joinpath(testdir, "aqua.jl")) + include(joinpath(testdir, "jet.jl")) +end if (GROUP == "All") || (GROUP == "Core") QuantumToolbox.about()