Skip to content

Commit 5b08c04

Browse files
authored
Improve show for Qobj/QobjEvo lists (#365)
* improve `show` for `Qobj` lists * fix doctests
1 parent c30a321 commit 5b08c04

File tree

12 files changed

+53
-13
lines changed

12 files changed

+53
-13
lines changed

src/metrics.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ matrix ``\hat{\rho}``.
2121
Pure state:
2222
```jldoctest
2323
julia> ψ = fock(2,0)
24+
2425
Quantum Object: type=Ket dims=[2] size=(2,)
2526
2-element Vector{ComplexF64}:
2627
1.0 + 0.0im
2728
0.0 + 0.0im
2829
2930
julia> ρ = ket2dm(ψ)
31+
3032
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
3133
2×2 Matrix{ComplexF64}:
3234
1.0+0.0im 0.0+0.0im
@@ -39,6 +41,7 @@ julia> entropy_vn(ρ, base=2)
3941
Mixed state:
4042
```jldoctest
4143
julia> ρ = maximally_mixed_dm(2)
44+
4245
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
4346
2×2 Diagonal{ComplexF64, Vector{ComplexF64}}:
4447
0.5-0.0im ⋅

src/negativity.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and ``\Vert \hat{X} \Vert_1=\textrm{Tr}\sqrt{\hat{X}^\dagger \hat{X}}`` is the t
1919
2020
```jldoctest
2121
julia> Ψ = bell_state(0, 0)
22+
2223
Quantum Object: type=Ket dims=[2, 2] size=(4,)
2324
4-element Vector{ComplexF64}:
2425
0.7071067811865475 + 0.0im
@@ -27,6 +28,7 @@ Quantum Object: type=Ket dims=[2, 2] size=(4,)
2728
0.7071067811865475 + 0.0im
2829
2930
julia> ρ = ket2dm(Ψ)
31+
3032
Quantum Object: type=Operator dims=[2, 2] size=(4, 4) ishermitian=true
3133
4×4 Matrix{ComplexF64}:
3234
0.5+0.0im 0.0+0.0im 0.0+0.0im 0.5+0.0im

src/qobj/arithmetic_and_attributes.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Note that this function only supports for [`Operator`](@ref) and [`SuperOperator
246246
247247
```jldoctest
248248
julia> a = destroy(20)
249+
249250
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
250251
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
251252
⎡⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⎤
@@ -286,6 +287,7 @@ Return the standard vector `p`-norm or [Schatten](https://en.wikipedia.org/wiki/
286287
287288
```jldoctest
288289
julia> ψ = fock(10, 2)
290+
289291
Quantum Object: type=Ket dims=[10] size=(10,)
290292
10-element Vector{ComplexF64}:
291293
0.0 + 0.0im
@@ -510,6 +512,7 @@ Note that this function will always return [`Operator`](@ref). No matter the inp
510512
Two qubits in the state ``\ket{\psi} = \ket{e,g}``:
511513
```jldoctest
512514
julia> ψ = kron(fock(2,0), fock(2,1))
515+
513516
Quantum Object: type=Ket dims=[2, 2] size=(4,)
514517
4-element Vector{ComplexF64}:
515518
0.0 + 0.0im
@@ -518,6 +521,7 @@ Quantum Object: type=Ket dims=[2, 2] size=(4,)
518521
0.0 + 0.0im
519522
520523
julia> ptrace(ψ, 2)
524+
521525
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
522526
2×2 Matrix{ComplexF64}:
523527
0.0+0.0im 0.0+0.0im
@@ -527,6 +531,7 @@ Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
527531
or in an entangled state ``\ket{\psi} = \frac{1}{\sqrt{2}} \left( \ket{e,e} + \ket{g,g} \right)``:
528532
```jldoctest
529533
julia> ψ = 1 / √2 * (kron(fock(2,0), fock(2,0)) + kron(fock(2,1), fock(2,1)))
534+
530535
Quantum Object: type=Ket dims=[2, 2] size=(4,)
531536
4-element Vector{ComplexF64}:
532537
0.7071067811865475 + 0.0im
@@ -535,6 +540,7 @@ Quantum Object: type=Ket dims=[2, 2] size=(4,)
535540
0.7071067811865475 + 0.0im
536541
537542
julia> ptrace(ψ, 1)
543+
538544
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
539545
2×2 Matrix{ComplexF64}:
540546
0.5+0.0im 0.0+0.0im

src/qobj/eigsolve.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ julia> λ
5151
5252
julia> ψ
5353
2-element Vector{QuantumObject{Vector{ComplexF64}, KetQuantumObject, 1}}:
54-
Quantum Object: type=Ket dims=[2] size=(2,)
54+
55+
Quantum Object: type=Ket dims=[2] size=(2,)
5556
2-element Vector{ComplexF64}:
5657
-0.7071067811865475 + 0.0im
5758
0.7071067811865475 + 0.0im
58-
Quantum Object: type=Ket dims=[2] size=(2,)
59+
60+
Quantum Object: type=Ket dims=[2] size=(2,)
5961
2-element Vector{ComplexF64}:
6062
0.7071067811865475 + 0.0im
6163
0.7071067811865475 + 0.0im

src/qobj/functions.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Returns the [Kronecker product](https://en.wikipedia.org/wiki/Kronecker_product)
161161
162162
```jldoctest
163163
julia> a = destroy(20)
164+
164165
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
165166
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
166167
⎡⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⎤

src/qobj/operators.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This operator acts on a fock state as ``\hat{a} \ket{n} = \sqrt{n} \ket{n-1}``.
9090
9191
```jldoctest
9292
julia> a = destroy(20)
93+
9394
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
9495
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
9596
⎡⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⎤
@@ -115,6 +116,7 @@ This operator acts on a fock state as ``\hat{a}^\dagger \ket{n} = \sqrt{n+1} \ke
115116
116117
```jldoctest
117118
julia> a_d = create(20)
119+
118120
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
119121
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
120122
⎡⠢⡀⠀⠀⠀⠀⠀⠀⠀⠀⎤
@@ -244,18 +246,21 @@ Note that if the parameter `which` is not specified, returns a set of Spin-`j` o
244246
# Examples
245247
```jldoctest
246248
julia> jmat(0.5, :x)
249+
247250
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=true
248251
2×2 SparseMatrixCSC{ComplexF64, Int64} with 2 stored entries:
249252
⋅ 0.5+0.0im
250253
0.5+0.0im ⋅
251254
252255
julia> jmat(0.5, Val(:-))
256+
253257
Quantum Object: type=Operator dims=[2] size=(2, 2) ishermitian=false
254258
2×2 SparseMatrixCSC{ComplexF64, Int64} with 1 stored entry:
255259
⋅ ⋅
256260
1.0+0.0im ⋅
257261
258262
julia> jmat(1.5, Val(:z))
263+
259264
Quantum Object: type=Operator dims=[4] size=(4, 4) ishermitian=true
260265
4×4 SparseMatrixCSC{ComplexF64, Int64} with 4 stored entries:
261266
1.5+0.0im ⋅ ⋅ ⋅

src/qobj/quantum_object.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Julia struct representing any quantum objects.
2121
2222
```jldoctest
2323
julia> a = destroy(20)
24+
2425
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
2526
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
2627
⎡⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⎤
@@ -145,15 +146,15 @@ function Base.show(
145146
},
146147
}
147148
op_data = QO.data
148-
println(io, "Quantum Object: type=", QO.type, " dims=", QO.dims, " size=", size(op_data))
149+
println(io, "\nQuantum Object: type=", QO.type, " dims=", QO.dims, " size=", size(op_data))
149150
return show(io, MIME("text/plain"), op_data)
150151
end
151152

152153
function Base.show(io::IO, QO::QuantumObject)
153154
op_data = QO.data
154155
println(
155156
io,
156-
"Quantum Object: type=",
157+
"\nQuantum Object: type=",
157158
QO.type,
158159
" dims=",
159160
QO.dims,

src/qobj/quantum_object_evo.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ where ``c_i(p, t)`` is a function that depends on the parameters `p` and time `t
2323
This operator can be initialized in the same way as the QuTiP `QobjEvo` object. For example
2424
```jldoctest qobjevo
2525
julia> a = tensor(destroy(10), qeye(2))
26+
2627
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
2728
20×20 SparseMatrixCSC{ComplexF64, Int64} with 18 stored entries:
2829
⎡⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⎤
@@ -35,6 +36,7 @@ julia> coef1(p, t) = exp(-1im * t)
3536
coef1 (generic function with 1 method)
3637
3738
julia> op = QobjEvo(a, coef1)
39+
3840
Quantum Object Evo.: type=Operator dims=[10, 2] size=(20, 20) ishermitian=true isconstant=false
3941
ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20)
4042
```
@@ -43,6 +45,7 @@ If there are more than 2 operators, we need to put each set of operator and coef
4345
4446
```jldoctest qobjevo
4547
julia> σm = tensor(qeye(10), sigmam())
48+
4649
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
4750
20×20 SparseMatrixCSC{ComplexF64, Int64} with 10 stored entries:
4851
⎡⠂⡀⠀⠀⠀⠀⠀⠀⠀⠀⎤
@@ -55,13 +58,15 @@ julia> coef2(p, t) = sin(t)
5558
coef2 (generic function with 1 method)
5659
5760
julia> op1 = QobjEvo(((a, coef1), (σm, coef2)))
61+
5862
Quantum Object Evo.: type=Operator dims=[10, 2] size=(20, 20) ishermitian=true isconstant=false
5963
(ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20) + ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20))
6064
```
6165
6266
We can also concretize the operator at a specific time `t`
6367
```jldoctest qobjevo
6468
julia> op1(0.1)
69+
6570
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
6671
20×20 SparseMatrixCSC{ComplexF64, Int64} with 28 stored entries:
6772
⎡⠂⡑⢄⠀⠀⠀⠀⠀⠀⠀⎤
@@ -80,13 +85,15 @@ julia> coef2(p, t) = sin(p.ω2 * t)
8085
coef2 (generic function with 1 method)
8186
8287
julia> op1 = QobjEvo(((a, coef1), (σm, coef2)))
88+
8389
Quantum Object Evo.: type=Operator dims=[10, 2] size=(20, 20) ishermitian=true isconstant=false
8490
(ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20) + ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20))
8591
8692
julia> p = (ω1 = 1.0, ω2 = 0.5)
8793
(ω1 = 1.0, ω2 = 0.5)
8894
8995
julia> op1(p, 0.1)
96+
9097
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
9198
20×20 SparseMatrixCSC{ComplexF64, Int64} with 28 stored entries:
9299
⎡⠂⡑⢄⠀⠀⠀⠀⠀⠀⠀⎤
@@ -128,7 +135,7 @@ function Base.show(io::IO, QO::QuantumObjectEvolution)
128135
op_data = QO.data
129136
println(
130137
io,
131-
"Quantum Object Evo.: type=",
138+
"\nQuantum Object Evo.: type=",
132139
QO.type,
133140
" dims=",
134141
QO.dims,
@@ -190,6 +197,7 @@ Note that if `α` is provided, all the operators in `op_func_list` will be pre-m
190197
This operator can be initialized in the same way as the QuTiP `QobjEvo` object. For example
191198
```jldoctest qobjevo
192199
julia> a = tensor(destroy(10), qeye(2))
200+
193201
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
194202
20×20 SparseMatrixCSC{ComplexF64, Int64} with 18 stored entries:
195203
⎡⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⎤
@@ -199,6 +207,7 @@ Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=fal
199207
⎣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⎦
200208
201209
julia> σm = tensor(qeye(10), sigmam())
210+
202211
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
203212
20×20 SparseMatrixCSC{ComplexF64, Int64} with 10 stored entries:
204213
⎡⠂⡀⠀⠀⠀⠀⠀⠀⠀⠀⎤
@@ -214,13 +223,15 @@ julia> coef2(p, t) = sin(t)
214223
coef2 (generic function with 1 method)
215224
216225
julia> op1 = QobjEvo(((a, coef1), (σm, coef2)))
226+
217227
Quantum Object Evo.: type=Operator dims=[10, 2] size=(20, 20) ishermitian=true isconstant=false
218228
(ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20) + ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20))
219229
```
220230
221231
We can also concretize the operator at a specific time `t`
222232
```jldoctest qobjevo
223233
julia> op1(0.1)
234+
224235
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
225236
20×20 SparseMatrixCSC{ComplexF64, Int64} with 28 stored entries:
226237
⎡⠂⡑⢄⠀⠀⠀⠀⠀⠀⠀⎤
@@ -239,13 +250,15 @@ julia> coef2(p, t) = sin(p.ω2 * t)
239250
coef2 (generic function with 1 method)
240251
241252
julia> op1 = QobjEvo(((a, coef1), (σm, coef2)))
253+
242254
Quantum Object Evo.: type=Operator dims=[10, 2] size=(20, 20) ishermitian=true isconstant=false
243255
(ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20) + ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20))
244256
245257
julia> p = (ω1 = 1.0, ω2 = 0.5)
246258
(ω1 = 1.0, ω2 = 0.5)
247259
248260
julia> op1(p, 0.1)
261+
249262
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
250263
20×20 SparseMatrixCSC{ComplexF64, Int64} with 28 stored entries:
251264
⎡⠂⡑⢄⠀⠀⠀⠀⠀⠀⠀⎤
@@ -293,6 +306,7 @@ Generate [`QuantumObjectEvolution`](@ref).
293306
# Examples
294307
```jldoctest
295308
julia> a = tensor(destroy(10), qeye(2))
309+
296310
Quantum Object: type=Operator dims=[10, 2] size=(20, 20) ishermitian=false
297311
20×20 SparseMatrixCSC{ComplexF64, Int64} with 18 stored entries:
298312
⎡⠀⠑⢄⠀⠀⠀⠀⠀⠀⠀⎤
@@ -305,6 +319,7 @@ julia> coef(p, t) = exp(-1im * t)
305319
coef (generic function with 1 method)
306320
307321
julia> op = QobjEvo(a, coef)
322+
308323
Quantum Object Evo.: type=Operator dims=[10, 2] size=(20, 20) ishermitian=true isconstant=false
309324
ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20)
310325
```
@@ -458,6 +473,7 @@ Apply the time-dependent [`QuantumObjectEvolution`](@ref) object `A` to the inpu
458473
# Examples
459474
```jldoctest
460475
julia> a = destroy(20)
476+
461477
Quantum Object: type=Operator dims=[20] size=(20, 20) ishermitian=false
462478
20×20 SparseMatrixCSC{ComplexF64, Int64} with 19 stored entries:
463479
⎡⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⎤
@@ -473,6 +489,7 @@ julia> coef2(p, t) = cos(t)
473489
coef2 (generic function with 1 method)
474490
475491
julia> A = QobjEvo(((a, coef1), (a', coef2)))
492+
476493
Quantum Object Evo.: type=Operator dims=[20] size=(20, 20) ishermitian=true isconstant=false
477494
(ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20) + ScalarOperator(0.0 + 0.0im) * MatrixOperator(20 × 20))
478495

src/qobj/states.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Here, `x = 1` (`z = 1`) means applying Pauli-``X`` ( Pauli-``Z``) unitary transf
254254
255255
```jldoctest
256256
julia> bell_state(0, 0)
257+
257258
Quantum Object: type=Ket dims=[2, 2] size=(4,)
258259
4-element Vector{ComplexF64}:
259260
0.7071067811865475 + 0.0im
@@ -262,6 +263,7 @@ Quantum Object: type=Ket dims=[2, 2] size=(4,)
262263
0.7071067811865475 + 0.0im
263264
264265
julia> bell_state(Val(1), Val(0))
266+
265267
Quantum Object: type=Ket dims=[2, 2] size=(4,)
266268
4-element Vector{ComplexF64}:
267269
0.0 + 0.0im

src/wigner.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The `method` parameter can be either `WignerLaguerre()` or `WignerClenshaw()`. T
3737
# Example
3838
```jldoctest wigner
3939
julia> ψ = fock(10, 0) + fock(10, 1) |> normalize
40+
4041
Quantum Object: type=Ket dims=[10] size=(10,)
4142
10-element Vector{ComplexF64}:
4243
0.7071067811865475 + 0.0im

0 commit comments

Comments
 (0)