Skip to content

Commit ff0a997

Browse files
committed
fix some tests
1 parent c0b2196 commit ff0a997

File tree

9 files changed

+72
-32
lines changed

9 files changed

+72
-32
lines changed

src/qobj/arithmetic_and_attributes.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function LinearAlgebra.:(*)(
6969
B::QuantumObject{DT2,BraQuantumObject},
7070
) where {DT1,DT2}
7171
check_dims(A, B)
72-
return QuantumObject(A.data * B.data, Operator, A.dims)
72+
return QuantumObject(A.data * B.data, Operator, A.dims) # to align with QuTiP, don't use kron(A, B) to do it.
7373
end
7474
function LinearAlgebra.:(*)(
7575
A::QuantumObject{DT1,BraQuantumObject},
@@ -212,8 +212,10 @@ LinearAlgebra.adjoint(
212212
A::AbstractQuantumObject{DT,OpType},
213213
) where {DT,OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
214214
get_typename_wrapper(A)(adjoint(A.data), A.type, transpose(A.dims))
215-
LinearAlgebra.adjoint(A::QuantumObject{DT,KetQuantumObject}) where {DT} = QuantumObject(adjoint(A.data), Bra, transpose(A.dims))
216-
LinearAlgebra.adjoint(A::QuantumObject{DT,BraQuantumObject}) where {DT} = QuantumObject(adjoint(A.data), Ket, transpose(A.dims))
215+
LinearAlgebra.adjoint(A::QuantumObject{DT,KetQuantumObject}) where {DT} =
216+
QuantumObject(adjoint(A.data), Bra, transpose(A.dims))
217+
LinearAlgebra.adjoint(A::QuantumObject{DT,BraQuantumObject}) where {DT} =
218+
QuantumObject(adjoint(A.data), Ket, transpose(A.dims))
217219
LinearAlgebra.adjoint(A::QuantumObject{DT,OperatorKetQuantumObject}) where {DT} =
218220
QuantumObject(adjoint(A.data), OperatorBra, transpose(A.dims))
219221
LinearAlgebra.adjoint(A::QuantumObject{DT,OperatorBraQuantumObject}) where {DT} =

src/qobj/dimensions.jl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export AbstractDimensions, Dimensions
1+
export AbstractDimensions, Dimensions#, CompoundDimensions
22

33
abstract type AbstractDimensions{N} end
44

@@ -15,6 +15,9 @@ function Dimensions(dims::Union{AbstractVector{T},NTuple{N,T}}) where {T<:Intege
1515
return Dimensions(SVector{L,AbstractSpace}(Space.(dims)))
1616
end
1717

18+
# this creates a list of Space(1), it's used to generate `from` for Ket, and `` for Bra)
19+
# oneDimensions(N::Int) = Dimensions(SVector{N,AbstractSpace}(ntuple(i -> Space(1), Val(N))))
20+
1821
_gen_dims(dims::Union{AbstractVector{T},NTuple{N,T}}) where {T<:Integer,N} = Dimensions(dims)
1922
_gen_dims(dims::Int) = Dimensions(SVector{1,AbstractSpace}(Space(dims)))
2023
_gen_dims(dims::AbstractDimensions) = dims
@@ -24,16 +27,36 @@ _gen_dims(dims::Any) = throw(
2427
),
2528
)
2629

27-
Base.prod(dims::Dimensions) = prod(dims.to)
28-
Base.prod(spaces::SVector{1,AbstractSpace}) = spaces[1].size # for `Dimensions.to` has only a single Space
29-
30-
LinearAlgebra.transpose(dims::Dimensions) = dims
31-
32-
struct CompoundDimensions{N} <: AbstractDimensions{N}
30+
#= struct CompoundDimensions{N} <: AbstractDimensions{N}
3331
# note that the number `N` should be the same for both `to` and `from`
3432
to::SVector{N,AbstractSpace} # space acting on the left
3533
from::SVector{N,AbstractSpace} # space acting on the right
3634
end
3735
Base.show(io::IO, D::CompoundDimensions) = print(io, "[", D.to, ", ", D.from, "]")
3836
39-
LinearAlgebra.transpose(dims::CompoundDimensions) = CompoundDimensions(dims.from, dims.to) # switch `to` and `from`
37+
function CompoundDimensions(to::Union{AbstractVector{T},NTuple{N1,T}}, from::Union{AbstractVector{T},NTuple{N2,T}}) where {T<:Integer,N1,N2}
38+
_non_static_array_warning("dims", to)
39+
_non_static_array_warning("dims", from)
40+
41+
L1 = length(to)
42+
L2 = length(from)
43+
((L1 > 0) && (L1 == L2)) || throw(DomainError((to, from), "The arguments `to` and `from` must be in the same length and have at least one element."))
44+
45+
return CompoundDimensions(SVector{L1,AbstractSpace}(Space.(to)), SVector{L1,AbstractSpace}(Space.(from)))
46+
end
47+
CompoundDimensions(to::Int, from::Int) = CompoundDimensions(SVector{1,Int}(to), SVector{1,Int}(from))
48+
CompoundDimensions(::KetQuantumObject, dims::Dimensions) = CompoundDimensions(dims, oneDimensions(length(dims)))
49+
CompoundDimensions(::BraQuantumObject, dims::Dimensions) = CompoundDimensions(oneDimensions(length(dims)), dims)
50+
CompoundDimensions(::OperatorQuantumObject, dims::Dimensions) = CompoundDimensions(dims, dims)
51+
CompoundDimensions(::OperatorQuantumObject, dims::CompoundDimensions) = dims =#
52+
53+
Base.length(dims::AbstractDimensions) = length(dims.to)
54+
55+
Base.prod(dims::Dimensions) = prod(dims.to)
56+
Base.prod(spaces::SVector{1,AbstractSpace}) = spaces[1].size # for `Dimensions.to` has only a single Space
57+
58+
LinearAlgebra.transpose(dims::Dimensions) = dims
59+
# LinearAlgebra.transpose(dims::CompoundDimensions) = CompoundDimensions(dims.from, dims.to) # switch `to` and `from`
60+
61+
LinearAlgebra.kron(Adims::Dimensions, Bdims::Dimensions) = Dimensions(vcat(Adims.to, Bdims.to))
62+
# LinearAlgebra.kron(Adims::CompoundDimensions, Bdims::CompoundDimensions) = CompoundDimensions(vcat(Adims.to, Bdims.to), vcat(Adims.from, Bdims.from))

src/qobj/functions.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,22 @@ julia> a.dims, O.dims
179179
```
180180
"""
181181
function LinearAlgebra.kron(
182-
A::AbstractQuantumObject{DT1,OpType},
183-
B::AbstractQuantumObject{DT2,OpType},
184-
) where {DT1,DT2,OpType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}}
182+
A::AbstractQuantumObject{DT1,AOpType},
183+
B::AbstractQuantumObject{DT2,BOpType},
184+
) where {DT1,DT2,AOpType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject},BOpType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}}
185185
QType = promote_op_type(A, B)
186-
return QType(kron(A.data, B.data), A.type, vcat(A.dims, B.dims))
186+
kron_type = (AOpType == BOpType) ? A.type : Operator
187+
188+
# deal with dims; # TODO: uncomment the following if-else block when CompoundDimensions is supported
189+
# if (A.dims isa Dimensions) && (B.dims isa Dimensions)
190+
_Adims = A.dims
191+
_Bdims = B.dims
192+
# else
193+
# # transfer to CompoundDimensions
194+
# _Adims = CompoundDimensions(A.type, A.dims)
195+
# _Bdims = CompoundDimensions(B.type, B.dims) =#
196+
# end
197+
return QType(kron(A.data, B.data), kron_type, kron(_Adims, _Bdims))
187198
end
188199
LinearAlgebra.kron(A::AbstractQuantumObject) = A
189200
function LinearAlgebra.kron(A::Vector{<:AbstractQuantumObject})

src/qobj/quantum_object.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ It also implements the fundamental functions in Julia standard library:
99
export QuantumObject
1010

1111
@doc raw"""
12-
struct QuantumObject{MT<:AbstractArray,ObjType<:QuantumObjectType,DimType<:AbstractDimensions}
12+
struct QuantumObject{MT<:AbstractArray,ObjType<:QuantumObjectType,N} <: AbstractQuantumObject{MT,ObjType,N}
1313
data::MT
1414
type::ObjType
15-
dims::DimType
15+
dims::AbstractDimensions{N}
1616
end
1717
1818
Julia struct representing any quantum objects.
@@ -33,18 +33,19 @@ julia> a isa QuantumObject
3333
true
3434
```
3535
"""
36-
struct QuantumObject{MT<:AbstractArray,ObjType<:QuantumObjectType,DimType<:AbstractDimensions} <: AbstractQuantumObject{MT,ObjType,DimType}
36+
struct QuantumObject{MT<:AbstractArray,ObjType<:QuantumObjectType,N} <: AbstractQuantumObject{MT,ObjType,N}
3737
data::MT
3838
type::ObjType
39-
dims::DimType
39+
dims::AbstractDimensions{N}
4040

4141
function QuantumObject(data::MT, type::ObjType, dims) where {MT<:AbstractArray,ObjType<:QuantumObjectType}
4242
_dims = _gen_dims(dims)
43+
N = length(_dims)
4344

4445
_size = _get_size(data)
4546
_check_QuantumObject(type, _dims, _size[1], _size[2])
4647

47-
return new{MT,ObjType,typeof(_dims)}(data, type, _dims)
48+
return new{MT,ObjType,N}(data, type, _dims)
4849
end
4950
end
5051

src/qobj/quantum_object_base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ julia> sigmax() isa AbstractQuantumObject
2424
true
2525
```
2626
"""
27-
abstract type AbstractQuantumObject{DataType,ObjType,DimType} end
27+
abstract type AbstractQuantumObject{DataType,ObjType,N} end
2828

2929
abstract type QuantumObjectType end
3030

src/qobj/quantum_object_evo.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ This file defines the QuantumObjectEvolution (QobjEvo) structure.
55
export QuantumObjectEvolution
66

77
@doc raw"""
8-
struct QuantumObjectEvolution{DT<:AbstractSciMLOperator,ObjType<:QuantumObjectType,DimType<:AbstractDimensions} <: AbstractQuantumObject
8+
struct QuantumObjectEvolution{DT<:AbstractSciMLOperator,ObjType<:QuantumObjectType,N} <: AbstractQuantumObject{DT,ObjType,N}
99
data::DT
1010
type::ObjType
11-
dims::DimType
11+
dims::AbstractDimensions{N}
1212
end
1313
1414
Julia struct representing any time-dependent quantum object. The `data` field is a `AbstractSciMLOperator` object that represents the time-dependent quantum object. It can be seen as
@@ -99,11 +99,11 @@ Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=fal
9999
struct QuantumObjectEvolution{
100100
DT<:AbstractSciMLOperator,
101101
ObjType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
102-
DimType<:AbstractDimensions,
103-
} <: AbstractQuantumObject{DT,ObjType,DimType}
102+
N,
103+
} <: AbstractQuantumObject{DT,ObjType,N}
104104
data::DT
105105
type::ObjType
106-
dims::DimType
106+
dims::AbstractDimensions{N}
107107

108108
function QuantumObjectEvolution(
109109
data::DT,
@@ -114,11 +114,12 @@ struct QuantumObjectEvolution{
114114
throw(ArgumentError("The type $type is not supported for QuantumObjectEvolution."))
115115

116116
_dims = _gen_dims(dims)
117+
N = length(_dims)
117118

118119
_size = _get_size(data)
119120
_check_QuantumObject(type, _dims, _size[1], _size[2])
120121

121-
return new{DT,ObjType,typeof(_dims)}(data, type, _dims)
122+
return new{DT,ObjType,N}(data, type, _dims)
122123
end
123124
end
124125

src/qobj/space.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ Base.show(io::IO, s::Space) = print(io, s.size)
1717

1818
# for `prod(::Dimensions)`
1919
Base.:(*)(i::Int, s::Space) = i * s.size
20-
Base.:(*)(s1::Space, s2::Space) = s1.size * s2.size
20+
Base.:(*)(s1::Space, s2::Space) = s1.size * s2.size

test/core-test/quantum_objects.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,15 @@
310310
for T in [ComplexF32, ComplexF64]
311311
N = 4
312312
a = rand(T, N)
313-
@inferred QuantumObject{typeof(a),KetQuantumObject} Qobj(a)
313+
@inferred QuantumObject{typeof(a),KetQuantumObject,1} Qobj(a)
314314
for type in [Ket, OperatorKet]
315315
@inferred Qobj(a, type = type)
316316
end
317317

318-
UnionType =
319-
Union{QuantumObject{Matrix{T},BraQuantumObject,Dimensions{1}},QuantumObject{Matrix{T},OperatorQuantumObject,Dimensions{1}}}
318+
UnionType = Union{
319+
QuantumObject{Matrix{T},BraQuantumObject,1},
320+
QuantumObject{Matrix{T},OperatorQuantumObject,1},
321+
}
320322
a = rand(T, 1, N)
321323
@inferred UnionType Qobj(a)
322324
for type in [Bra, OperatorBra]

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if (GROUP == "All") || (GROUP == "Code-Quality")
3030
using QuantumToolbox
3131
using Aqua, JET
3232

33-
#include(joinpath(testdir, "core-test", "code_quality.jl"))
33+
include(joinpath(testdir, "core-test", "code_quality.jl"))
3434
end
3535

3636
if (GROUP == "All") || (GROUP == "Core")

0 commit comments

Comments
 (0)