Skip to content

Commit 309a670

Browse files
authored
Several updates for Dimensions structure (#367)
2 parents 4bda75d + 2ecd7ef commit 309a670

13 files changed

+74
-23
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Change the structure of block diagonalization functions, using `BlockDiagonalForm` struct and changing the function name from `bdf` to `block_diagonal_form`. ([#349])
1111
- Add **GPUArrays** compatibility for `ptrace` function, by using **KernelAbstractions.jl**. ([#350])
12+
- Introduce `Space`, `Dimensions`, `GeneralDimensions` structures to support wider definitions and operations of `Qobj/QobjEvo`, and potential functionalities in the future. ([#271], [#353], [#360])
1213

1314
## [v0.24.0]
1415
Release date: 2024-12-13
@@ -55,6 +56,7 @@ Release date: 2024-11-13
5556
[v0.24.0]: https://github.com/qutip/QuantumToolbox.jl/releases/tag/v0.24.0
5657
[#86]: https://github.com/qutip/QuantumToolbox.jl/issues/86
5758
[#139]: https://github.com/qutip/QuantumToolbox.jl/issues/139
59+
[#271]: https://github.com/qutip/QuantumToolbox.jl/issues/271
5860
[#292]: https://github.com/qutip/QuantumToolbox.jl/issues/292
5961
[#306]: https://github.com/qutip/QuantumToolbox.jl/issues/306
6062
[#309]: https://github.com/qutip/QuantumToolbox.jl/issues/309
@@ -71,3 +73,5 @@ Release date: 2024-11-13
7173
[#347]: https://github.com/qutip/QuantumToolbox.jl/issues/347
7274
[#349]: https://github.com/qutip/QuantumToolbox.jl/issues/349
7375
[#350]: https://github.com/qutip/QuantumToolbox.jl/issues/350
76+
[#353]: https://github.com/qutip/QuantumToolbox.jl/issues/353
77+
[#360]: https://github.com/qutip/QuantumToolbox.jl/issues/360

ext/QuantumToolboxCUDAExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ CuArray(A::QuantumObject{Tq}) where {Tq<:Union{Vector,Matrix}} = QuantumObject(C
1717
1818
If `A.data` is a dense array, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CuArray` with element type `T` for gpu calculations.
1919
"""
20-
CuArray{T}(A::QuantumObject{Tq}) where {T,Tq<:Union{Vector,Matrix}} = QuantumObject(CuArray{T}(A.data), A.type, A.dimensions)
20+
CuArray{T}(A::QuantumObject{Tq}) where {T,Tq<:Union{Vector,Matrix}} =
21+
QuantumObject(CuArray{T}(A.data), A.type, A.dimensions)
2122

2223
@doc raw"""
2324
CuSparseVector(A::QuantumObject)

src/negativity.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ end
8181
function _partial_transpose::QuantumObject{DT,OperatorQuantumObject}, mask::Vector{Bool}) where {DT<:AbstractArray}
8282
isa.dimensions, GeneralDimensions) &&
8383
(get_dimensions_to(ρ) != get_dimensions_from(ρ)) &&
84-
throw(ArgumentError("Invalid partial transpose for dims = $(ρ.dims)"))
84+
throw(ArgumentError("Invalid partial transpose for dims = $(_get_dims_string.dimensions))"))
8585

8686
mask2 = [1 + Int(i) for i in mask]
8787
# mask2 has elements with values equal to 1 or 2
@@ -106,7 +106,7 @@ end
106106
function _partial_transpose::QuantumObject{<:AbstractSparseArray,OperatorQuantumObject}, mask::Vector{Bool})
107107
isa.dimensions, GeneralDimensions) &&
108108
(get_dimensions_to(ρ) != get_dimensions_from(ρ)) &&
109-
throw(ArgumentError("Invalid partial transpose for dims = $(ρ.dims)"))
109+
throw(ArgumentError("Invalid partial transpose for dims = $(_get_dims_string.dimensions))"))
110110

111111
M, N = size(ρ)
112112
dimsTuple = Tuple(dimensions_to_dims(get_dimensions_to(ρ)))

src/qobj/arithmetic_and_attributes.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,10 @@ end
612612
ptrace(QO::QuantumObject{<:AbstractArray,BraQuantumObject}, sel::Union{AbstractVector{Int},Tuple}) = ptrace(QO', sel)
613613

614614
function ptrace(QO::QuantumObject{<:AbstractArray,OperatorQuantumObject}, sel::Union{AbstractVector{Int},Tuple})
615+
# TODO: support for special cases when some of the subsystems have same `to` and `from` space
615616
isa(QO.dimensions, GeneralDimensions) &&
616617
(get_dimensions_to(QO) != get_dimensions_from(QO)) &&
617-
throw(ArgumentError("Invalid partial trace for dims = $(QO.dims)"))
618+
throw(ArgumentError("Invalid partial trace for dims = $(_get_dims_string(QO.dimensions))"))
618619

619620
_non_static_array_warning("sel", sel)
620621

@@ -800,6 +801,11 @@ function permute(
800801
A::QuantumObject{<:AbstractArray{T},ObjType},
801802
order::Union{AbstractVector{Int},Tuple},
802803
) where {T,ObjType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}}
804+
# TODO: fix this (should be able to permute arbitrary GeneralDimensions)
805+
isa(A.dimensions, GeneralDimensions) &&
806+
(get_dimensions_to(A) != get_dimensions_from(A)) &&
807+
throw(ArgumentError("Invalid permute for dims = $(_get_dims_string(A.dimensions))"))
808+
803809
(length(order) != length(A.dimensions)) &&
804810
throw(ArgumentError("The order list must have the same length as the number of subsystems (A.dims)"))
805811

@@ -829,8 +835,9 @@ _dims_and_perm(::OperatorQuantumObject, dims::SVector{N,Int}, order::AbstractVec
829835
reverse(vcat(dims, dims)), reverse((2 * L + 1) .- vcat(order, order .+ L))
830836

831837
# if dims originates from GeneralDimensions
832-
_dims_and_perm(::OperatorQuantumObject, dims::SVector{2,SVector{N,Int}}, order::AbstractVector{Int}, L::Int) where {N} =
833-
reverse(vcat(dims[1], dims[2])), reverse((2 * L + 1) .- vcat(order, order .+ L))
838+
# TODO: fix this
839+
#= _dims_and_perm(::OperatorQuantumObject, dims::SVector{2,SVector{N,Int}}, order::AbstractVector{Int}, L::Int) where {N} =
840+
reverse(vcat(dims[1], dims[2])), reverse((2 * L + 1) .- vcat(order, order .+ L)) =#
834841

835842
_order_dimensions(dimensions::Dimensions{N}, order::AbstractVector{Int}) where {N} = Dimensions{N}(dimensions.to[order])
836843
_order_dimensions(dimensions::GeneralDimensions{N}, order::AbstractVector{Int}) where {N} =

src/qobj/dimensions.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export AbstractDimensions, Dimensions, GeneralDimensions
66

77
abstract type AbstractDimensions{N} end
88

9-
@doc raw""""
9+
@doc raw"""
1010
struct Dimensions{N} <: AbstractDimensions{N}
1111
to::NTuple{N,AbstractSpace}
1212
end
@@ -31,7 +31,7 @@ Dimensions(dims::Any) = throw(
3131
),
3232
)
3333

34-
@doc raw""""
34+
@doc raw"""
3535
struct GeneralDimensions{N} <: AbstractDimensions{N}
3636
to::NTuple{N,AbstractSpace}
3737
from::NTuple{N,AbstractSpace}
@@ -71,7 +71,8 @@ _gen_dimensions(dims::Any) = Dimensions(dims)
7171
# obtain dims in the type of SVector with integers
7272
dimensions_to_dims(dimensions::NTuple{N,AbstractSpace}) where {N} = vcat(map(dimensions_to_dims, dimensions)...)
7373
dimensions_to_dims(dimensions::Dimensions) = dimensions_to_dims(dimensions.to)
74-
dimensions_to_dims(dimensions::GeneralDimensions) = SVector{2}(dimensions_to_dims(dimensions.to), dimensions_to_dims(dimensions.from))
74+
dimensions_to_dims(dimensions::GeneralDimensions) =
75+
SVector{2}(dimensions_to_dims(dimensions.to), dimensions_to_dims(dimensions.from))
7576

7677
Base.length(::AbstractDimensions{N}) where {N} = N
7778

@@ -89,4 +90,4 @@ _get_dims_string(dimensions::Dimensions) = string(dimensions_to_dims(dimensions)
8990
function _get_dims_string(dimensions::GeneralDimensions)
9091
dims = dimensions_to_dims(dimensions)
9192
return "[$(string(dims[1])), $(string(dims[2]))]"
92-
end
93+
end

src/qobj/eigsolve.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ end
351351
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
352352
alg::OrdinaryDiffEqAlgorithm = Tsit5(),
353353
params::NamedTuple = NamedTuple(),
354-
ρ0::AbstractMatrix = rand_dm(prod(H.dims)).data,
354+
ρ0::AbstractMatrix = rand_dm(prod(H.dimensions)).data,
355355
k::Int = 1,
356356
krylovdim::Int = min(10, size(H, 1)),
357357
maxiter::Int = 200,
@@ -390,7 +390,7 @@ function eigsolve_al(
390390
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
391391
alg::OrdinaryDiffEqAlgorithm = Tsit5(),
392392
params::NamedTuple = NamedTuple(),
393-
ρ0::AbstractMatrix = rand_dm(prod(H.dims)).data,
393+
ρ0::AbstractMatrix = rand_dm(prod(H.dimensions)).data,
394394
k::Int = 1,
395395
krylovdim::Int = min(10, size(H, 1)),
396396
maxiter::Int = 200,

src/qobj/quantum_object.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export QuantumObject
1818
!!! note "`dims` property"
1919
For a given `H::QuantumObject`, `H.dims` or `getproperty(H, :dims)` returns its `dimensions` in the type of integer-vector.
2020
21-
Julia struct representing any quantum objects.
21+
Julia structure representing any time-independent quantum objects. For time-dependent cases, see [`QuantumObjectEvolution`](@ref).
2222
2323
# Examples
2424
@@ -150,7 +150,15 @@ function Base.show(
150150
},
151151
}
152152
op_data = QO.data
153-
println(io, "\nQuantum Object: type=", QO.type, " dims=", _get_dims_string(QO.dimensions), " size=", size(op_data))
153+
println(
154+
io,
155+
"\nQuantum Object: type=",
156+
QO.type,
157+
" dims=",
158+
_get_dims_string(QO.dimensions),
159+
" size=",
160+
size(op_data),
161+
)
154162
return show(io, MIME("text/plain"), op_data)
155163
end
156164

src/qobj/quantum_object_base.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ Base.getproperty(A::AbstractQuantumObject, key::Symbol) = getproperty(A, makeVal
219219
Base.getproperty(A::AbstractQuantumObject, ::Val{:dims}) = dimensions_to_dims(getfield(A, :dimensions))
220220
Base.getproperty(A::AbstractQuantumObject, ::Val{K}) where {K} = getfield(A, K)
221221

222+
# this returns `to` in GeneralDimensions representation
222223
get_dimensions_to(A::AbstractQuantumObject{DT,KetQuantumObject,Dimensions{N}}) where {DT,N} = A.dimensions.to
223224
get_dimensions_to(A::AbstractQuantumObject{DT,BraQuantumObject,Dimensions{N}}) where {DT,N} = space_one_list(N)
224225
get_dimensions_to(A::AbstractQuantumObject{DT,OperatorQuantumObject,Dimensions{N}}) where {DT,N} = A.dimensions.to
@@ -229,6 +230,7 @@ get_dimensions_to(
229230
) where {DT,ObjType<:Union{SuperOperatorQuantumObject,OperatorBraQuantumObject,OperatorKetQuantumObject},N} =
230231
A.dimensions.to
231232

233+
# this returns `from` in GeneralDimensions representation
232234
get_dimensions_from(A::AbstractQuantumObject{DT,KetQuantumObject,Dimensions{N}}) where {DT,N} = space_one_list(N)
233235
get_dimensions_from(A::AbstractQuantumObject{DT,BraQuantumObject,Dimensions{N}}) where {DT,N} = A.dimensions.to
234236
get_dimensions_from(A::AbstractQuantumObject{DT,OperatorQuantumObject,Dimensions{N}}) where {DT,N} = A.dimensions.to

src/qobj/quantum_object_evo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Julia struct representing any time-dependent quantum object. The `data` field is
2222
2323
where ``c_i(p, t)`` is a function that depends on the parameters `p` and time `t`, and ``\hat{O}_i`` are the operators that form the quantum object.
2424
25-
For more information about `AbstractSciMLOperator`, see the [SciML](https://docs.sciml.ai/SciMLOperators/stable/) documentation.
25+
For time-independent cases, see [`QuantumObject`](@ref), and for more information about `AbstractSciMLOperator`, see the [SciML](https://docs.sciml.ai/SciMLOperators/stable/) documentation.
2626
2727
# Examples
2828
This operator can be initialized in the same way as the QuTiP `QobjEvo` object. For example

src/qobj/space.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract type AbstractSpace end
1111
size::Int
1212
end
1313
14-
A structure that describes a single Hilbert space.
14+
A structure that describes a single Hilbert space with size = `size`.
1515
"""
1616
struct Space <: AbstractSpace
1717
size::Int

0 commit comments

Comments
 (0)