Skip to content

Commit 201e066

Browse files
Unify structure of QuantumObjectType (#456)
1 parent fe4eaf0 commit 201e066

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+588
-802
lines changed

.github/ISSUE_TEMPLATE/bug_report.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ body:
2222
attributes:
2323
label: Code Output
2424
description: Please paste the relevant output here (automatically formatted)
25-
placeholder: "Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true\n2×2 Diagonal{ComplexF64, Vector{ComplexF64}}:\n 1.0+0.0im ⋅ \n ⋅ 1.0+0.0im"
25+
placeholder: "Quantum Object: type=Operator() dims=[2] size=(2, 2) ishermitian=true\n2×2 Diagonal{ComplexF64, Vector{ComplexF64}}:\n 1.0+0.0im ⋅ \n ⋅ 1.0+0.0im"
2626
render: shell
2727
- type: textarea
2828
id: expected-behaviour

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

1010
- Return `sesolve` when `mesolve` allows it. ([#455])
11+
- Simplify structure of `QuantumObjectType`s. ([#456])
1112

1213
## [v0.30.1]
1314
Release date: 2025-04-24
@@ -215,3 +216,4 @@ Release date: 2024-11-13
215216
[#450]: https://github.com/qutip/QuantumToolbox.jl/issues/450
216217
[#453]: https://github.com/qutip/QuantumToolbox.jl/issues/453
217218
[#455]: https://github.com/qutip/QuantumToolbox.jl/issues/455
219+
[#456]: https://github.com/qutip/QuantumToolbox.jl/issues/456

docs/src/getting_started/type_stability.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ and its type is
158158
obj_type = typeof(σx_2)
159159
```
160160

161-
This is exactly what the Julia compiler sees: it is a [`QuantumObject`](@ref), composed by a field of type `SparseMatrixCSC{ComplexF64, Int64}` (i.e., the 8x8 matrix containing the Pauli matrix, tensored with the identity matrices of the other two qubits). Then, we can also see that it is a [`OperatorQuantumObject`](@ref), with `3` subsystems in total. Hence, just looking at the type of the object, the compiler has all the information it needs to generate a specialized version of the functions.
161+
This is exactly what the Julia compiler sees: it is a [`QuantumObject`](@ref), composed by a field of type `SparseMatrixCSC{ComplexF64, Int64}` (i.e., the 8x8 matrix containing the Pauli matrix, tensored with the identity matrices of the other two qubits). Then, we can also see that it is a [`Operator`](@ref), with `3` subsystems in total. Hence, just looking at the type of the object, the compiler has all the information it needs to generate a specialized version of the functions.
162162

163163
Let's see more in the details all the internal fields of the [`QuantumObject`](@ref) type:
164164

@@ -174,7 +174,6 @@ fieldnames(obj_type)
174174
σx_2.type
175175
```
176176

177-
[`Operator`](@ref) is a synonym for [`OperatorQuantumObject`](@ref).
178177

179178
```@example type-stability
180179
σx_2.dims
@@ -184,7 +183,7 @@ The `dims` field contains the dimensions of the subsystems (in this case, three
184183

185184
```@example type-stability
186185
function reshape_operator_data(dims)
187-
op = Qobj(randn(prod(dims), prod(dims)), type=Operator, dims=dims)
186+
op = Qobj(randn(prod(dims), prod(dims)), type=Operator(), dims=dims)
188187
op_dims = op.dims
189188
op_data = op.data
190189
return reshape(op_data, vcat(op_dims, op_dims)...)
@@ -235,7 +234,7 @@ function my_fock(N::Int, j::Int = 0; sparse::Bool = false)
235234
array = zeros(ComplexF64, N)
236235
array[j+1] = 1
237236
end
238-
return QuantumObject(array; type = Ket)
237+
return QuantumObject(array; type = Ket())
239238
end
240239
@show my_fock(2, 1)
241240
@show my_fock(2, 1; sparse = true)
@@ -263,7 +262,7 @@ function my_fock_good(N::Int, j::Int = 0; sparse::Val = Val(false))
263262
else
264263
array = sparsevec([j + 1], [1.0 + 0im], N)
265264
end
266-
return QuantumObject(array; type = Ket)
265+
return QuantumObject(array; type = Ket())
267266
end
268267
@show my_fock_good(2, 1)
269268
@show my_fock_good(2, 1; sparse = Val(true))

docs/src/resources/api.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@ Space
2121
Dimensions
2222
GeneralDimensions
2323
AbstractQuantumObject
24-
BraQuantumObject
2524
Bra
26-
KetQuantumObject
2725
Ket
28-
OperatorQuantumObject
2926
Operator
30-
OperatorBraQuantumObject
3127
OperatorBra
32-
OperatorKetQuantumObject
3328
OperatorKet
34-
SuperOperatorQuantumObject
3529
SuperOperator
3630
QuantumObject
3731
QuantumObjectEvolution

docs/src/users_guide/QuantumObject/QuantumObject.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Qobj(M, dims = SVector(2, 2)) # dims as StaticArrays.SVector (recommended)
5454
Please note that here we put the `dims` as a tuple `(2, 2)`. Although it supports also `Vector` type (`dims = [2, 2]`), it is recommended to use `Tuple` or `SVector` from [`StaticArrays.jl`](https://github.com/JuliaArrays/StaticArrays.jl) to improve performance. For a brief explanation on the impact of the type of `dims`, see the Section [The Importance of Type-Stability](@ref doc:Type-Stability).
5555

5656
```@example Qobj
57-
Qobj(rand(4, 4), type = SuperOperator)
57+
Qobj(rand(4, 4), type = SuperOperator())
5858
```
5959

6060
!!! note "Difference between `dims` and `size`"

docs/src/users_guide/extensions/cuda.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ V = fock(2, 0) # CPU dense vector
3636
```
3737

3838
```
39-
Quantum Object: type=Ket dims=[2] size=(2,)
39+
Quantum Object: type=Ket() dims=[2] size=(2,)
4040
2-element Vector{ComplexF64}:
4141
1.0 + 0.0im
4242
0.0 + 0.0im
@@ -47,7 +47,7 @@ cu(V)
4747
```
4848

4949
```
50-
Quantum Object: type=Ket dims=[2] size=(2,)
50+
Quantum Object: type=Ket() dims=[2] size=(2,)
5151
2-element CuArray{ComplexF64, 1, CUDA.DeviceMemory}:
5252
1.0 + 0.0im
5353
0.0 + 0.0im
@@ -58,7 +58,7 @@ cu(V; word_size = 32)
5858
```
5959

6060
```
61-
Quantum Object: type=Ket dims=[2] size=(2,)
61+
Quantum Object: type=Ket() dims=[2] size=(2,)
6262
2-element CuArray{ComplexF32, 1, CUDA.DeviceMemory}:
6363
1.0 + 0.0im
6464
0.0 + 0.0im
@@ -69,7 +69,7 @@ M = Qobj([1 2; 3 4]) # CPU dense matrix
6969
```
7070

7171
```
72-
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=false
72+
Quantum Object: type=Operator() dims=[2] size=(2, 2) ishermitian=false
7373
2×2 Matrix{Int64}:
7474
1 2
7575
3 4
@@ -80,7 +80,7 @@ cu(M)
8080
```
8181

8282
```
83-
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=false
83+
Quantum Object: type=Operator() dims=[2] size=(2, 2) ishermitian=false
8484
2×2 CuArray{Int64, 2, CUDA.DeviceMemory}:
8585
1 2
8686
3 4
@@ -91,7 +91,7 @@ cu(M; word_size = 32)
9191
```
9292

9393
```
94-
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=false
94+
Quantum Object: type=Operator() dims=[2] size=(2, 2) ishermitian=false
9595
2×2 CuArray{Int32, 2, CUDA.DeviceMemory}:
9696
1 2
9797
3 4
@@ -104,7 +104,7 @@ V = fock(2, 0; sparse=true) # CPU sparse vector
104104
```
105105

106106
```
107-
Quantum Object: type=Ket dims=[2] size=(2,)
107+
Quantum Object: type=Ket() dims=[2] size=(2,)
108108
2-element SparseVector{ComplexF64, Int64} with 1 stored entry:
109109
[1] = 1.0+0.0im
110110
```
@@ -114,7 +114,7 @@ cu(V)
114114
```
115115

116116
```
117-
Quantum Object: type=Ket dims=[2] size=(2,)
117+
Quantum Object: type=Ket() dims=[2] size=(2,)
118118
2-element CuSparseVector{ComplexF64, Int32} with 1 stored entry:
119119
[1] = 1.0+0.0im
120120
```
@@ -124,7 +124,7 @@ cu(V; word_size = 32)
124124
```
125125

126126
```
127-
Quantum Object: type=Ket dims=[2] size=(2,)
127+
Quantum Object: type=Ket() dims=[2] size=(2,)
128128
2-element CuSparseVector{ComplexF32, Int32} with 1 stored entry:
129129
[1] = 1.0+0.0im
130130
```
@@ -134,7 +134,7 @@ M = sigmax() # CPU sparse matrix
134134
```
135135

136136
```
137-
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
137+
Quantum Object: type=Operator() dims=[2] size=(2, 2) ishermitian=true
138138
2×2 SparseMatrixCSC{ComplexF64, Int64} with 2 stored entries:
139139
⋅ 1.0+0.0im
140140
1.0+0.0im ⋅
@@ -145,7 +145,7 @@ cu(M)
145145
```
146146

147147
```
148-
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
148+
Quantum Object: type=Operator() dims=[2] size=(2, 2) ishermitian=true
149149
2×2 CuSparseMatrixCSC{ComplexF64, Int32} with 2 stored entries:
150150
⋅ 1.0+0.0im
151151
1.0+0.0im ⋅
@@ -156,7 +156,7 @@ cu(M; word_size = 32)
156156
```
157157

158158
```
159-
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
159+
Quantum Object: type=Operator() dims=[2] size=(2, 2) ishermitian=true
160160
2×2 CuSparseMatrixCSC{ComplexF32, Int32} with 2 stored entries:
161161
⋅ 1.0+0.0im
162162
1.0+0.0im ⋅

docs/src/users_guide/states_and_operators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ B = Qobj(rand(ComplexF64, N, N))
389389
mat2vec(A * ρ * B) ≈ spre(A) * spost(B) * mat2vec(ρ) ≈ sprepost(A, B) * mat2vec(ρ)
390390
```
391391

392-
In addition, dynamical generators on this extended space, often called Liouvillian superoperators, can be created using the [`liouvillian`](@ref) function. Each of these takes a Hamiltonian along with a list of collapse operators, and returns a [`type=SuperOperator`](@ref SuperOperator) object that can be exponentiated to find the superoperator for that evolution.
392+
In addition, dynamical generators on this extended space, often called Liouvillian superoperators, can be created using the [`liouvillian`](@ref) function. Each of these takes a Hamiltonian along with a list of collapse operators, and returns a [`QuantumObject`](@ref) of type [`SuperOperator`](@ref) that can be exponentiated to find the superoperator for that evolution.
393393

394394
```@example states_and_operators
395395
H = 10 * sigmaz()

ext/QuantumToolboxMakieExt.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function QuantumToolbox.plot_wigner(
5454
location::Union{GridPosition,Nothing} = nothing,
5555
colorbar::Bool = false,
5656
kwargs...,
57-
) where {OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
57+
) where {OpType<:Union{Bra,Ket,Operator}}
5858
QuantumToolbox.getVal(projection) == :two_dim ||
5959
QuantumToolbox.getVal(projection) == :three_dim ||
6060
throw(ArgumentError("Unsupported projection: $projection"))
@@ -84,7 +84,7 @@ function _plot_wigner(
8484
location::Union{GridPosition,Nothing},
8585
colorbar::Bool;
8686
kwargs...,
87-
) where {OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
87+
) where {OpType<:Union{Bra,Ket,Operator}}
8888
fig, location = _getFigAndLocation(location)
8989

9090
lyt = GridLayout(location)
@@ -117,7 +117,7 @@ function _plot_wigner(
117117
location::Union{GridPosition,Nothing},
118118
colorbar::Bool;
119119
kwargs...,
120-
) where {OpType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
120+
) where {OpType<:Union{Bra,Ket,Operator}}
121121
fig, location = _getFigAndLocation(location)
122122

123123
lyt = GridLayout(location)
@@ -148,7 +148,7 @@ end
148148
unit_y_range::Bool = true,
149149
location::Union{GridPosition,Nothing} = nothing,
150150
kwargs...
151-
) where {SType<:Union{KetQuantumObject,OperatorQuantumObject}}
151+
) where {SType<:Union{Ket,Operator}}
152152
153153
Plot the [Fock state](https://en.wikipedia.org/wiki/Fock_state) distribution of `ρ`.
154154
@@ -178,7 +178,7 @@ function QuantumToolbox.plot_fock_distribution(
178178
unit_y_range::Bool = true,
179179
location::Union{GridPosition,Nothing} = nothing,
180180
kwargs...,
181-
) where {SType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
181+
) where {SType<:Union{Bra,Ket,Operator}}
182182
return _plot_fock_distribution(
183183
library,
184184
ρ;
@@ -196,7 +196,7 @@ function _plot_fock_distribution(
196196
unit_y_range::Bool = true,
197197
location::Union{GridPosition,Nothing} = nothing,
198198
kwargs...,
199-
) where {SType<:Union{BraQuantumObject,KetQuantumObject,OperatorQuantumObject}}
199+
) where {SType<:Union{Bra,Ket,Operator}}
200200
ρ = ket2dm(ρ)
201201
D = prod.dims)
202202
isapprox(tr(ρ), 1, atol = 1e-4) || (@warn "The input ρ should be normalized.")

src/correlations.jl

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ function correlation_3op_2t(
2929
tlist::AbstractVector,
3030
τlist::AbstractVector,
3131
c_ops::Union{Nothing,AbstractVector,Tuple},
32-
A::QuantumObject{OperatorQuantumObject},
33-
B::QuantumObject{OperatorQuantumObject},
34-
C::QuantumObject{OperatorQuantumObject};
32+
A::QuantumObject{Operator},
33+
B::QuantumObject{Operator},
34+
C::QuantumObject{Operator};
3535
kwargs...,
36-
) where {
37-
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
38-
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
39-
}
36+
) where {HOpType<:Union{Operator,SuperOperator},StateOpType<:Union{Ket,Operator}}
4037
# check tlist and τlist
4138
_check_correlation_time_list(tlist)
4239
_check_correlation_time_list(τlist)
@@ -78,14 +75,11 @@ function correlation_3op_1t(
7875
ψ0::Union{Nothing,QuantumObject{StateOpType}},
7976
τlist::AbstractVector,
8077
c_ops::Union{Nothing,AbstractVector,Tuple},
81-
A::QuantumObject{OperatorQuantumObject},
82-
B::QuantumObject{OperatorQuantumObject},
83-
C::QuantumObject{OperatorQuantumObject};
78+
A::QuantumObject{Operator},
79+
B::QuantumObject{Operator},
80+
C::QuantumObject{Operator};
8481
kwargs...,
85-
) where {
86-
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
87-
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
88-
}
82+
) where {HOpType<:Union{Operator,SuperOperator},StateOpType<:Union{Ket,Operator}}
8983
corr = correlation_3op_2t(H, ψ0, [0], τlist, c_ops, A, B, C; kwargs...)
9084

9185
return corr[1, :] # 1 means tlist[1] = 0
@@ -114,14 +108,11 @@ function correlation_2op_2t(
114108
tlist::AbstractVector,
115109
τlist::AbstractVector,
116110
c_ops::Union{Nothing,AbstractVector,Tuple},
117-
A::QuantumObject{OperatorQuantumObject},
118-
B::QuantumObject{OperatorQuantumObject};
111+
A::QuantumObject{Operator},
112+
B::QuantumObject{Operator};
119113
reverse::Bool = false,
120114
kwargs...,
121-
) where {
122-
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
123-
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
124-
}
115+
) where {HOpType<:Union{Operator,SuperOperator},StateOpType<:Union{Ket,Operator}}
125116
C = eye(prod(H.dimensions), dims = H.dimensions)
126117
if reverse
127118
corr = correlation_3op_2t(H, ψ0, tlist, τlist, c_ops, A, B, C; kwargs...)
@@ -153,14 +144,11 @@ function correlation_2op_1t(
153144
ψ0::Union{Nothing,QuantumObject{StateOpType}},
154145
τlist::AbstractVector,
155146
c_ops::Union{Nothing,AbstractVector,Tuple},
156-
A::QuantumObject{OperatorQuantumObject},
157-
B::QuantumObject{OperatorQuantumObject};
147+
A::QuantumObject{Operator},
148+
B::QuantumObject{Operator};
158149
reverse::Bool = false,
159150
kwargs...,
160-
) where {
161-
HOpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject},
162-
StateOpType<:Union{KetQuantumObject,OperatorQuantumObject},
163-
}
151+
) where {HOpType<:Union{Operator,SuperOperator},StateOpType<:Union{Ket,Operator}}
164152
corr = correlation_2op_2t(H, ψ0, [0], τlist, c_ops, A, B; reverse = reverse, kwargs...)
165153

166154
return corr[1, :] # 1 means tlist[1] = 0

0 commit comments

Comments
 (0)