Skip to content

Commit eb4ca48

Browse files
committed
better show for Settings and Bloch
1 parent 3967eff commit eb4ca48

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

ext/QuantumToolboxMakieExt.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ function _setup_bloch_plot!(b::Bloch, location)
400400
protrusions = (0, 0, 0, 0),
401401
viewmode = :fit,
402402
)
403-
ax.azimuth[] = deg2rad(b.view_angles[1])
404-
ax.elevation[] = deg2rad(b.view_angles[2])
403+
ax.azimuth[] = deg2rad(b.view[1])
404+
ax.elevation[] = deg2rad(b.view[2])
405405
return fig, ax
406406
end
407407

src/settings.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ Base.@kwdef mutable struct Settings
44
end
55

66
function Base.show(io::IO, s::Settings)
7+
# To align the output and make it easier to read
8+
# we use rpad `11`, which is the length of string: `auto_tidyup`
79
println(io, "QuantumToolbox.jl Settings")
810
println(io, "--------------------------")
9-
map(x -> println(io, "$x = ", getfield(s, x)), fieldnames(Settings))
11+
map(n -> println(io, rpad("$n", 11, " "), " = ", getfield(s, n)), fieldnames(Settings))
1012
return nothing
1113
end
1214

src/visualization.jl

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ A structure representing a Bloch sphere visualization for quantum states.
8888
## Point properties
8989
9090
- `point_default_color::Vector{String}}`: Default color cycle for points
91-
- `point_color::Vector{String}}`: Colors for point markers
92-
- `point_marker::Vector{Symbol}}`: Marker shapes (default: [:circle, :rect, :diamond, :utriangle])
93-
- `point_size::Vector{Int}}`: Marker sizes
94-
- `point_style::Vector{Symbol}}`: Marker styles
95-
- `point_alpha::Vector{Float64}}`: Marker transparencies
91+
- `point_color::Vector{String}}`: List of colors for Bloch point markers to cycle through
92+
- `point_marker::Vector{Symbol}}`: List of point marker shapes to cycle through (default: [:circle, :rect, :diamond, :utriangle])
93+
- `point_size::Vector{Int}}`: List of point marker sizes (not all markers look the same size when plotted)
94+
- `point_style::Vector{Symbol}}`: List of marker styles
95+
- `point_alpha::Vector{Float64}}`: List of marker transparencies
9696
9797
## Sphere properties
9898
@@ -107,7 +107,7 @@ A structure representing a Bloch sphere visualization for quantum states.
107107
108108
## Layout properties
109109
110-
- `view_angles::Tuple{Int,Int}}`: Azimuthal and elevation viewing angles in degrees (default: (-60, 30))
110+
- `view::Tuple{Int,Int}}`: Azimuthal and elevation viewing angles in degrees (default: (-60, 30))
111111
112112
## Label properties
113113
- `xlabel::Vector{AbstractString}}`: Labels for x-axis (default: [L"x", ""])
@@ -138,7 +138,7 @@ A structure representing a Bloch sphere visualization for quantum states.
138138
vector_color::Vector{String} = ["green", "#CC6600", "blue", "red"]
139139
vector_width::Float64 = 0.025
140140
vector_arrowsize::NTuple{3,Real} = (0.07, 0.08, 0.08)
141-
view_angles::Tuple{Int,Int} = (-60, 30)
141+
view::Tuple{Int,Int} = (-60, 30)
142142
xlabel::Vector{AbstractString} = [L"x", ""]
143143
xlpos::Vector{Float64} = [1.0, -1.0]
144144
ylabel::Vector{AbstractString} = [L"y", ""]
@@ -147,16 +147,20 @@ A structure representing a Bloch sphere visualization for quantum states.
147147
zlpos::Vector{Float64} = [1.0, -1.0]
148148
end
149149

150+
const BLOCH_DATA_FIELDS = (:points, :vectors, :lines, :arcs)
150151
function Base.show(io::IO, b::Bloch)
151-
data_fields = (:points, :vectors, :lines, :arcs)
152+
# To align the output and make it easier to read
153+
# we use rpad `17` and `19` for Bloch sphere data and properties, respectively
154+
# 17 is the length of string: `Number of vectors`
155+
# 19 is the length of string: `point_default_color`
152156
println(io, "Bloch Sphere\n")
153157
println(io, "data:")
154158
println(io, "-----")
155-
map(n -> println(io, "Number of $n =\t", length(getfield(b, n))), data_fields)
159+
map(n -> println(io, rpad("Number of $n", 17, " "), " = ", length(getfield(b, n))), BLOCH_DATA_FIELDS)
156160
println(io, "")
157161
println(io, "properties:")
158162
println(io, "-----------")
159-
map(n -> (n data_fields) && (println(io, "$n =\t", getfield(b, n))), fieldnames(Bloch))
163+
map(n -> (n BLOCH_DATA_FIELDS) && (println(io, rpad("$n", 19, " "), " = ", getfield(b, n))), fieldnames(Bloch))
160164
return nothing
161165
end
162166

@@ -312,9 +316,9 @@ Add a circular arc through three points on the Bloch sphere.
312316
# Arguments
313317
314318
- `b::Bloch`: The Bloch sphere object to modify
315-
- `p1::Vector{<:Real}`: First 3D point
316-
- `p2::Vector{<:Real}`: Second 3D point (middle point)
317-
- `p3::Vector{<:Real}`: Third 3D point
319+
- `p1::Vector{<:Real}`: Starting 3D point
320+
- `p2::Vector{<:Real}`: [Optional] Middle 3D point
321+
- `p3::Vector{<:Real}`: Ending 3D point
318322
319323
# Examples
320324
@@ -335,6 +339,48 @@ function add_arc!(b::Bloch, p1::Vector{<:Real}, p2::Vector{<:Real}, p3::Vector{<
335339
return push!(b.arcs, [convert(Vector{Float64}, p1), convert(Vector{Float64}, p2), convert(Vector{Float64}, p3)])
336340
end
337341

342+
@doc raw"""
343+
add_arc!(
344+
b::Bloch,
345+
start_point::QuantumObject,
346+
middle_point::QuantumObject,
347+
end_point::QuantumObject
348+
)
349+
350+
Add a circular arc through three points on the Bloch sphere.
351+
352+
# Arguments
353+
354+
- `b::Bloch`: The Bloch sphere object to modify.
355+
- `start_point::QuantumObject`: The starting quantum state. Can be a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
356+
- `middle_point::QuantumObject`: [Optional] The middle quantum state. Can be a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
357+
- `end_point::QuantumObject`: The ending quantum state. Can be a [`Ket`](@ref), [`Bra`](@ref), or [`Operator`](@ref).
358+
359+
# Description
360+
361+
This function converts the given quantum states into their Bloch vector representations and adds a arc between these two (or three) points on the Bloch sphere visualization.
362+
"""
363+
function add_arc!(
364+
b::Bloch,
365+
start_point::QuantumObject{OpType1},
366+
end_point::QuantumObject{OpType2},
367+
) where {OpType1<:Union{Ket,Bra,Operator},OpType2<:Union{Ket,Bra,Operator}}
368+
coords1 = _state_to_bloch(start_point)
369+
coords2 = _state_to_bloch(end_point)
370+
return add_arc!(b, coords1, coords2)
371+
end
372+
function add_arc!(
373+
b::Bloch,
374+
start_point::QuantumObject{OpType1},
375+
middle_point::QuantumObject{OpType2},
376+
end_point::QuantumObject{OpType3},
377+
) where {OpType1<:Union{Ket,Bra,Operator},OpType2<:Union{Ket,Bra,Operator},OpType3<:Union{Ket,Bra,Operator}}
378+
coords1 = _state_to_bloch(start_point)
379+
coords2 = _state_to_bloch(middle_point)
380+
coords3 = _state_to_bloch(end_point)
381+
return add_arc!(b, coords1, coords2, coords3)
382+
end
383+
338384
@doc raw"""
339385
add_states!(b::Bloch, states::Vector{QuantumObject})
340386
@@ -361,7 +407,7 @@ function add_states!(b::Bloch, states::Vector{<:QuantumObject})
361407
end
362408

363409
function add_states!(b::Bloch, state::QuantumObject)
364-
append!(b.vectors, _state_to_bloch(state))
410+
push!(b.vectors, _state_to_bloch(state))
365411
return b.vectors
366412
end
367413

test/ext-test/cpu/makie/makie_ext.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@ end
161161
@info "Render threw unexpected error" exception=e
162162
end
163163
b = Bloch()
164+
add_states!(b, basis(2, 0))
164165
x = normalize!(basis(2, 0) + basis(2, 1)) # normalized
165166
y = basis(2, 0) - im * basis(2, 1) # unnormalized Ket
166167
ρ1 = 0.3 * rand_dm(2) + 0.4 * rand_dm(2) # unnormalized density operator
167168
ρ2 = Qobj(rand(ComplexF64, 2, 2)) # unnormalized and non-Hermitian Operator
168169
@test_logs (:warn,) (:warn,) (:warn,) (:warn,) add_states!(b, [x, y, ρ1, ρ2])
170+
@test length(b.vectors) == 5
169171
th = range(0, 2π; length = 20);
170172
xp = cos.(th);
171173
yp = sin.(th);
@@ -188,7 +190,10 @@ end
188190
b = Bloch()
189191
ψ₁ = normalize(basis(2, 0) + basis(2, 1))
190192
ψ₂ = normalize(basis(2, 0) - im * basis(2, 1))
193+
ψ₃ = basis(2, 0)
191194
add_line!(b, ψ₁, ψ₂; fmt = "r--")
195+
add_arc!(b, ψ₁, ψ₂)
196+
add_arc!(b, ψ₂, ψ₃, ψ₁)
192197
try
193198
fig, ax = render(b)
194199
@test !isnothing(fig)

0 commit comments

Comments
 (0)