Skip to content

Commit 342768c

Browse files
authored
introduce field dimensions, and property dims (#361)
2 parents 1684fc7 + 51779f2 commit 342768c

28 files changed

+371
-316
lines changed

docs/src/resources/api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ CurrentModule = QuantumToolbox
1111
## [Quantum object (Qobj) and type](@id doc-API:Quantum-object-and-type)
1212

1313
```@docs
14+
Space
15+
Dimensions
16+
GeneralDimensions
1417
AbstractQuantumObject
1518
BraQuantumObject
1619
Bra

docs/src/tutorials/lowrank.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ M = latt.N + 1 # Number of states in the LR basis
4141
Define lr states. Take as initial state all spins up. All other N states are taken as those with miniman Hamming distance to the initial state.
4242

4343
```@example lowrank
44-
ϕ = Vector{QuantumObject{Vector{ComplexF64},KetQuantumObject,M-1}}(undef, M)
44+
ϕ = Vector{QuantumObject{Vector{ComplexF64},KetQuantumObject,Dimensions{M-1}}}(undef, M)
4545
ϕ[1] = kron(fill(basis(2, 1), N_modes)...)
4646
4747
i = 1

ext/QuantumToolboxCUDAExt.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,59 @@ import SparseArrays: SparseVector, SparseMatrixCSC
1010
1111
If `A.data` is a dense array, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CuArray` for gpu calculations.
1212
"""
13-
CuArray(A::QuantumObject{Tq}) where {Tq<:Union{Vector,Matrix}} = QuantumObject(CuArray(A.data), A.type, A.dims)
13+
CuArray(A::QuantumObject{Tq}) where {Tq<:Union{Vector,Matrix}} = QuantumObject(CuArray(A.data), A.type, A._dims)
1414

1515
@doc raw"""
1616
CuArray{T}(A::QuantumObject)
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.dims)
20+
CuArray{T}(A::QuantumObject{Tq}) where {T,Tq<:Union{Vector,Matrix}} = QuantumObject(CuArray{T}(A.data), A.type, A._dims)
2121

2222
@doc raw"""
2323
CuSparseVector(A::QuantumObject)
2424
2525
If `A.data` is a sparse vector, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseVector` for gpu calculations.
2626
"""
27-
CuSparseVector(A::QuantumObject{<:SparseVector}) = QuantumObject(CuSparseVector(A.data), A.type, A.dims)
27+
CuSparseVector(A::QuantumObject{<:SparseVector}) = QuantumObject(CuSparseVector(A.data), A.type, A._dims)
2828

2929
@doc raw"""
3030
CuSparseVector{T}(A::QuantumObject)
3131
3232
If `A.data` is a sparse vector, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseVector` with element type `T` for gpu calculations.
3333
"""
34-
CuSparseVector{T}(A::QuantumObject{<:SparseVector}) where {T} = QuantumObject(CuSparseVector{T}(A.data), A.type, A.dims)
34+
CuSparseVector{T}(A::QuantumObject{<:SparseVector}) where {T} =
35+
QuantumObject(CuSparseVector{T}(A.data), A.type, A._dims)
3536

3637
@doc raw"""
3738
CuSparseMatrixCSC(A::QuantumObject)
3839
3940
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSC` for gpu calculations.
4041
"""
41-
CuSparseMatrixCSC(A::QuantumObject{<:SparseMatrixCSC}) = QuantumObject(CuSparseMatrixCSC(A.data), A.type, A.dims)
42+
CuSparseMatrixCSC(A::QuantumObject{<:SparseMatrixCSC}) = QuantumObject(CuSparseMatrixCSC(A.data), A.type, A._dims)
4243

4344
@doc raw"""
4445
CuSparseMatrixCSC{T}(A::QuantumObject)
4546
4647
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSC` with element type `T` for gpu calculations.
4748
"""
4849
CuSparseMatrixCSC{T}(A::QuantumObject{<:SparseMatrixCSC}) where {T} =
49-
QuantumObject(CuSparseMatrixCSC{T}(A.data), A.type, A.dims)
50+
QuantumObject(CuSparseMatrixCSC{T}(A.data), A.type, A._dims)
5051

5152
@doc raw"""
5253
CuSparseMatrixCSR(A::QuantumObject)
5354
5455
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSR` for gpu calculations.
5556
"""
56-
CuSparseMatrixCSR(A::QuantumObject{<:SparseMatrixCSC}) = QuantumObject(CuSparseMatrixCSR(A.data), A.type, A.dims)
57+
CuSparseMatrixCSR(A::QuantumObject{<:SparseMatrixCSC}) = QuantumObject(CuSparseMatrixCSR(A.data), A.type, A._dims)
5758

5859
@doc raw"""
5960
CuSparseMatrixCSR(A::QuantumObject)
6061
6162
If `A.data` is in the type of `SparseMatrixCSC`, return a new [`QuantumObject`](@ref) where `A.data` is in the type of `CUDA.CUSPARSE.CuSparseMatrixCSR` with element type `T` for gpu calculations.
6263
"""
6364
CuSparseMatrixCSR{T}(A::QuantumObject{<:SparseMatrixCSC}) where {T} =
64-
QuantumObject(CuSparseMatrixCSR{T}(A.data), A.type, A.dims)
65+
QuantumObject(CuSparseMatrixCSR{T}(A.data), A.type, A._dims)
6566

6667
@doc raw"""
6768
cu(A::QuantumObject; word_size::Int=64)

src/correlations.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ function correlation_3op_2t(
5151
ψ0 = steadystate(L)
5252
end
5353

54-
allequal((L.dims, ψ0.dims, A.dims, B.dims, C.dims)) ||
55-
throw(DimensionMismatch("The quantum objects are not of the same Hilbert dimension."))
54+
check_dimensions(L, ψ0, A, B, C)
5655

5756
kwargs2 = merge((saveat = collect(tlist),), (; kwargs...))
5857
ρt_list = mesolve(L, ψ0, tlist; kwargs2...).states
@@ -137,7 +136,7 @@ function correlation_2op_2t(
137136
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
138137
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
139138
}
140-
C = eye(prod(H.dims), dims = H.dims)
139+
C = eye(prod(H.dimensions), dims = H.dimensions)
141140
if reverse
142141
corr = correlation_3op_2t(H, ψ0, tlist, τlist, c_ops, A, B, C; kwargs...)
143142
else

src/negativity.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ julia> round(negativity(ρ, 2), digits=2)
3939
```
4040
"""
4141
function negativity::QuantumObject, subsys::Int; logarithmic::Bool = false)
42-
mask = fill(false, length.dims))
42+
mask = fill(false, length.dimensions))
4343
try
4444
mask[subsys] = true
4545
catch
@@ -68,17 +68,17 @@ Return the partial transpose of a density matrix ``\rho``, where `mask` is an ar
6868
# Returns
6969
- `ρ_pt::QuantumObject`: The density matrix with the selected subsystems transposed.
7070
"""
71-
function partial_transpose::QuantumObject{T,OperatorQuantumObject}, mask::Vector{Bool}) where {T}
72-
if length(mask) != length.dims)
71+
function partial_transpose::QuantumObject{DT,OperatorQuantumObject}, mask::Vector{Bool}) where {DT}
72+
if length(mask) != length.dimensions)
7373
throw(ArgumentError("The length of \`mask\` should be equal to the length of \`ρ.dims\`."))
7474
end
7575
return _partial_transpose(ρ, mask)
7676
end
7777

7878
# for dense matrices
79-
function _partial_transpose::QuantumObject{<:AbstractArray,OperatorQuantumObject}, mask::Vector{Bool})
80-
isa.dims, CompoundDimensions) &&
81-
(ρ.to != ρ.from) &&
79+
function _partial_transpose::QuantumObject{DT,OperatorQuantumObject}, mask::Vector{Bool}) where {DT<:AbstractArray}
80+
isa.dimensions, GeneralDimensions) &&
81+
(get_dimensions_to(ρ) != get_dimensions_from(ρ)) &&
8282
throw(ArgumentError("Invalid partial transpose for dims = $(ρ.dims)"))
8383

8484
mask2 = [1 + Int(i) for i in mask]
@@ -87,27 +87,27 @@ function _partial_transpose(ρ::QuantumObject{<:AbstractArray,OperatorQuantumObj
8787
# 2 - the subsystem need be transposed
8888

8989
nsys = length(mask2)
90-
dimslist = dims_to_list.to)
90+
dims = dimensions_to_dims(get_dimensions_to(ρ))
9191
pt_dims = reshape(Vector(1:(2*nsys)), (nsys, 2))
9292
pt_idx = [
93-
[pt_dims[n, mask2[n]] for n in 1:nsys] # origin value in mask2
94-
[pt_dims[n, 3-mask2[n]] for n in 1:nsys] # opposite value in mask2 (1 -> 2, and 2 -> 1)
93+
[pt_dims[n, mask2[n]] for n in 1:nsys] # origin value in mask2
94+
[pt_dims[n, 3-mask2[n]] for n in 1:nsys] # opposite value in mask2 (1 -> 2, and 2 -> 1)
9595
]
9696
return QuantumObject(
97-
reshape(permutedims(reshape.data, (dimslist..., dimslist...)), pt_idx), size(ρ)),
97+
reshape(permutedims(reshape.data, (dims..., dims...)), pt_idx), size(ρ)),
9898
Operator,
99-
Dimensions.dims.to),
99+
Dimensions.dimensions.to),
100100
)
101101
end
102102

103103
# for sparse matrices
104104
function _partial_transpose::QuantumObject{<:AbstractSparseArray,OperatorQuantumObject}, mask::Vector{Bool})
105-
isa.dims, CompoundDimensions) &&
106-
(ρ.to != ρ.from) &&
105+
isa.dimensions, GeneralDimensions) &&
106+
(get_dimensions_to(ρ) != get_dimensions_from(ρ)) &&
107107
throw(ArgumentError("Invalid partial transpose for dims = $(ρ.dims)"))
108108

109109
M, N = size(ρ)
110-
dimsTuple = Tuple(dims_to_list.to))
110+
dimsTuple = Tuple(dimensions_to_dims(get_dimensions_to(ρ)))
111111
colptr = ρ.data.colptr
112112
rowval = ρ.data.rowval
113113
nzval = ρ.data.nzval
@@ -139,5 +139,5 @@ function _partial_transpose(ρ::QuantumObject{<:AbstractSparseArray,OperatorQuan
139139
end
140140
end
141141

142-
return QuantumObject(sparse(I_pt, J_pt, V_pt, M, N), Operator, ρ.dims)
142+
return QuantumObject(sparse(I_pt, J_pt, V_pt, M, N), Operator, ρ.dimensions)
143143
end

0 commit comments

Comments
 (0)