22This file defines the Dimensions structures, which can describe composite Hilbert spaces.
33=#
44
5- export AbstractDimensions, Dimensions, GeneralDimensions
5+ export AbstractDimensions, ProductDimensions
66
77abstract type AbstractDimensions{M,N} end
88
@@ -18,22 +18,26 @@ struct ProductDimensions{M,N,T1<:Tuple,T2<:Union{<:Tuple,Nothing}} <: AbstractDi
1818 to:: T1 # space acting on the left
1919 from:: T2 # space acting on the right
2020
21- # make sure the elements in the tuple are all AbstractSpace
22- GeneralDimensions (to:: NTuple{M,AbstractSpace} , from:: Union{NTuple{N,AbstractSpace},Nothing} ) where {M,N} =
23- new {M,N,typeof(to),typeof(from)} (to, from)
21+ function ProductDimensions (to:: NTuple{M} , from:: Union{NTuple,Nothing} ) where {M}
22+ N = isnothing (from) ? M : length (from)
23+
24+ _non_static_array_warning (" to" , to)
25+ isnothing (from) || _non_static_array_warning (" from" , from)
26+
27+ to_space = _dims_tuple_of_space (to)
28+ from_space = _dims_tuple_of_space (from)
29+
30+ new {M,N,typeof(to_space),typeof(from_space)} (to_space, from_space)
31+ end
2432end
25- function ProductDimensions (dims:: Union{AbstractVector{T},NTuple{N,T}} ) where {T <: Union{AbstractVector,NTuple} ,N}
33+ function ProductDimensions (dims:: Union{AbstractVector,Tuple} )
2634 (length (dims) != 2 ) && throw (ArgumentError (" Invalid dims = $dims " ))
2735
28- to = _dims_tuple_of_space (dims[1 ])
29- from = isnothing (dims[2 ]) ? nothing : _dims_tuple_of_space (dims[2 ])
30-
31- return ProductDimensions (to, from)
36+ return ProductDimensions (dims[1 ], dims[2 ])
3237end
3338
3439ProductDimensions (dims:: Union{Int,AbstractSpace} ) = ProductDimensions ((dims,), nothing )
3540ProductDimensions (dims:: Union{AbstractVector{T},NTuple{N,T}} ) where {T<: Integer ,N} = ProductDimensions (dims, nothing )
36- ProductDimensions (to:: NTuple{M,Int} , from:: Union{NTuple{N,Int},Nothing} ) where {M,N} = ProductDimensions ((to, from))
3741ProductDimensions (dims:: ProductDimensions ) = dims
3842
3943# obtain dims in the type of SVector with integers
@@ -56,17 +60,23 @@ liouvillian_dimensions_to_size(::Nothing) = nothing
5660Base. length (:: AbstractDimensions{N} ) where {N} = N
5761
5862Base. transpose (dimensions:: ProductDimensions ) = ProductDimensions (dimensions. from, dimensions. to) # switch `to` and `from`
63+ Base. transpose (dimensions:: ProductDimensions{M,N,T1,Nothing} ) where {M,N,T1<: Tuple } = dimensions
5964Base. adjoint (dimensions:: AbstractDimensions ) = transpose (dimensions)
6065
6166Base.:(== )(dim1:: ProductDimensions , dim2:: ProductDimensions ) = (dim1. to == dim2. to) && (dim1. from == dim2. from)
6267
63- function _dims_tuple_of_space (dims:: NTuple{N,AbstractSpace} ) where {N}
68+ _dims_tuple_of_space (dims:: NTuple{N,AbstractSpace} ) where {N} = dims
69+ function _dims_tuple_of_space (dims:: NTuple{N, Integer} ) where {N}
6470 _non_static_array_warning (" dims" , dims)
6571
6672 N > 0 || throw (DomainError (N, " The length of `dims` must be larger or equal to 1." ))
6773
6874 return ntuple (dim -> HilbertSpace (dims[dim]), Val (N))
6975end
76+ _dims_tuple_of_space (:: Nothing ) = nothing
77+
78+ _gen_dimensions (dims:: AbstractDimensions ) = dims
79+ _gen_dimensions (dims) = ProductDimensions (dims)
7080
7181# this is used to show `dims` for Qobj and QobjEvo
7282function _get_dims_string (dimensions:: ProductDimensions )
0 commit comments