Skip to content

Commit a4620a9

Browse files
authored
Improve Bloch alignment with qutip (#489)
1 parent dc4ecde commit a4620a9

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

CHANGELOG.md

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

1010
- Introduce `Lanczos` solver for `spectrum`. ([#476])
1111
- Add Bloch-Redfield master equation solver. ([#473])
12-
- Implement Bloch Sphere rendering and align style with qutip. ([#472], [#480], [#485], [#487])
12+
- Implement Bloch Sphere rendering and align style with qutip. ([#472], [#480], [#485], [#487], [#489])
1313
- Add `Base.copy` method for `AbstractQuantumObject`. ([#486])
1414

1515
## [v0.31.1]
@@ -240,3 +240,4 @@ Release date: 2024-11-13
240240
[#485]: https://github.com/qutip/QuantumToolbox.jl/issues/485
241241
[#486]: https://github.com/qutip/QuantumToolbox.jl/issues/486
242242
[#487]: https://github.com/qutip/QuantumToolbox.jl/issues/487
243+
[#489]: https://github.com/qutip/QuantumToolbox.jl/issues/489

docs/src/users_guide/plotting_the_bloch_sphere.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ At the end of the last section we saw that the colors and marker shapes of the d
183183
|:----------|:----------------|:--------------------|
184184
| `b.font_color` | Color of axis labels and text | `"black"` |
185185
| `b.font_size` | Font size for labels | `20` |
186-
| `b.frame_alpha` | Transparency of the wire frame | `0.1` |
186+
| `b.frame_alpha` | Transparency of the wire frame | `0.2` |
187187
| `b.frame_color` | Color of the wire frame | `"gray"` |
188+
| `b.frame_width` | Width of wire frame | `1.0` |
188189
| `b.point_default_color` | Default color cycle for points | `["blue", "red", "green", "#CC6600"]` |
189190
| `b.point_color` | List of colors for Bloch point markers to cycle through | `Union{Nothing,String}[]` |
190191
| `b.point_marker` | List of point marker shapes to cycle through | `[:circle, :rect, :diamond, :utriangle]` |

ext/QuantumToolboxMakieExt.jl

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ function _draw_bloch_sphere!(b::Bloch, lscene)
414414

415415
# highlight circles for XY and XZ planes
416416
φ = range(0, 2π, length = 100)
417-
lines!(lscene, [Point3f(cos(φi), sin(φi), 0) for φi in φ]; color = b.frame_color, linewidth = 1.0) # XY
418-
lines!(lscene, [Point3f(cos(φi), 0, sin(φi)) for φi in φ]; color = b.frame_color, linewidth = 1.0) # XZ
417+
lines!(lscene, [Point3f(cos(φi), sin(φi), 0) for φi in φ]; color = b.frame_color, linewidth = b.frame_width) # XY
418+
lines!(lscene, [Point3f(cos(φi), 0, sin(φi)) for φi in φ]; color = b.frame_color, linewidth = b.frame_width) # XZ
419419

420420
# other curves of longitude (with polar angle φ and azimuthal angle θ)
421421
φ_curve = range(0, 2π, 600)
@@ -424,7 +424,7 @@ function _draw_bloch_sphere!(b::Bloch, lscene)
424424
x_line = radius * sin.(φ_curve) .* cos(θi)
425425
y_line = radius * sin.(φ_curve) .* sin(θi)
426426
z_line = radius * cos.(φ_curve)
427-
lines!(lscene, x_line, y_line, z_line; color = b.frame_color, alpha = b.frame_alpha)
427+
lines!(lscene, x_line, y_line, z_line; color = b.frame_color, alpha = b.frame_alpha, linewidth = b.frame_width)
428428
end
429429

430430
# other curves of latitude (with polar angle φ and azimuthal angle θ)
@@ -434,7 +434,7 @@ function _draw_bloch_sphere!(b::Bloch, lscene)
434434
x_ring = radius * sin(ϕ) .* cos.(θ_curve)
435435
y_ring = radius * sin(ϕ) .* sin.(θ_curve)
436436
z_ring = fill(radius * cos(ϕ), length(θ_curve))
437-
lines!(lscene, x_ring, y_ring, z_ring; color = b.frame_color, alpha = b.frame_alpha)
437+
lines!(lscene, x_ring, y_ring, z_ring; color = b.frame_color, alpha = b.frame_alpha, linewidth = b.frame_width)
438438
end
439439
return nothing
440440
end
@@ -558,14 +558,11 @@ function _plot_points!(b::Bloch, lscene)
558558
end
559559
end
560560
if style in (:s, :m)
561-
xs = raw_x[indperm]
562-
ys = raw_y[indperm]
563-
zs = raw_z[indperm]
564561
scatter!(
565562
lscene,
566-
xs,
567-
ys,
568-
zs;
563+
raw_x[indperm],
564+
raw_y[indperm],
565+
raw_z[indperm];
569566
color = colors,
570567
markersize = b.point_size[mod1(k, length(b.point_size))],
571568
marker = marker,
@@ -575,11 +572,8 @@ function _plot_points!(b::Bloch, lscene)
575572
)
576573

577574
elseif style == :l
578-
xs = raw_x
579-
ys = raw_y
580-
zs = raw_z
581575
c = isa(colors, Vector) ? colors[1] : colors
582-
lines!(lscene, xs, ys, zs; color = c, linewidth = 2.0, transparency = alpha < 1.0, alpha = alpha)
576+
lines!(lscene, raw_x, raw_y, raw_z; color = c, transparency = alpha < 1.0, alpha = alpha)
583577
end
584578
end
585579
return nothing
@@ -613,7 +607,7 @@ function _plot_lines!(b::Bloch, lscene)
613607
else
614608
:solid
615609
end
616-
lines!(lscene, x, y, z; color = color, linewidth = 1.0, linestyle = linestyle)
610+
lines!(lscene, x, y, z; color = color, linestyle = linestyle)
617611
end
618612
return nothing
619613
end
@@ -643,7 +637,7 @@ function _plot_arcs!(b::Bloch, lscene)
643637
end
644638
t_range = range(0, θ, length = 100)
645639
arc_points = [Point3f((v1 * cos(t) + cross(n, v1) * sin(t))) for t in t_range]
646-
lines!(lscene, arc_points; color = RGBAf(0.8, 0.4, 0.1, 0.9), linewidth = 2.0, linestyle = :solid)
640+
lines!(lscene, arc_points; color = "blue", linestyle = :solid)
647641
end
648642
return nothing
649643
end

src/visualization.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,31 @@ A structure representing a Bloch sphere visualization for quantum states. Availa
7979
8080
## Style properties
8181
82-
- `font_color::String`: Color of axis labels and text
82+
- `font_color::String`: Color of axis labels and text. Default: `"black"`
8383
- `font_size::Int`: Font size for labels. Default: `20`
84-
- `frame_alpha::Float64`: Transparency of the wire frame
85-
- `frame_color::String`: Color of the wire frame
84+
- `frame_alpha::Float64`: Transparency of the wire frame. Default: `0.2`
85+
- `frame_color::String`: Color of the wire frame. Default: `"gray"`
86+
- `frame_width::Float64` : Width of wire frame. Default: `1.0`
8687
8788
## Point properties
8889
89-
- `point_default_color::Vector{String}}`: Default color cycle for points
90-
- `point_color::Vector{String}}`: List of colors for Bloch point markers to cycle through
90+
- `point_default_color::Vector{String}}`: Default color cycle for points. Default: `["blue", "red", "green", "#CC6600"]`
91+
- `point_color::Vector{String}}`: List of colors for Bloch point markers to cycle through. Default: `Union{Nothing,String}[]`
9192
- `point_marker::Vector{Symbol}}`: List of point marker shapes to cycle through. Default: `[:circle, :rect, :diamond, :utriangle]`
92-
- `point_size::Vector{Int}}`: List of point marker sizes (not all markers look the same size when plotted)
93-
- `point_style::Vector{Symbol}}`: List of marker styles
94-
- `point_alpha::Vector{Float64}}`: List of marker transparencies
93+
- `point_size::Vector{Int}}`: List of point marker sizes (not all markers look the same size when plotted). Default: `[5.5, 6.2, 6.5, 7.5]`
94+
- `point_style::Vector{Symbol}}`: List of marker styles. Default: `Symbol[]`
95+
- `point_alpha::Vector{Float64}}`: List of marker transparencies. Default: `Float64[]`
9596
9697
## Sphere properties
9798
98-
- `sphere_color::String`: Color of Bloch sphere surface
99+
- `sphere_color::String`: Color of Bloch sphere surface. Default: `"#FFDDDD"`
99100
- `sphere_alpha::Float64`: Transparency of sphere surface. Default: `0.2`
100101
101102
## Vector properties
102103
103-
- `vector_color::Vector{String}`: Colors for vectors
104-
- `vector_width::Float64`: Width of vectors
105-
- `vector_arrowsize::Vector{Float64}`: Scales the size of the arrow head. The first two elements scale the radius (in `x/y` direction) and the last one is the length of the cone.
104+
- `vector_color::Vector{String}`: Colors for vectors. Default: `["green", "#CC6600", "blue", "red"]`
105+
- `vector_width::Float64`: Width of vectors. Default: `0.025`
106+
- `vector_arrowsize::Vector{Float64}`: Scales the size of the arrow head. The first two elements scale the radius (in `x/y` direction) and the last one is the length of the cone. Default: `[0.07, 0.08, 0.08]`
106107
107108
## Layout properties
108109
@@ -124,8 +125,9 @@ A structure representing a Bloch sphere visualization for quantum states. Availa
124125
arcs::Vector{Vector{Vector{Float64}}} = Vector{Vector{Vector{Float64}}}()
125126
font_color::String = "black"
126127
font_size::Int = 20
127-
frame_alpha::Float64 = 0.1
128+
frame_alpha::Float64 = 0.2
128129
frame_color::String = "gray"
130+
frame_width::Float64 = 1.0
129131
point_default_color::Vector{String} = ["blue", "red", "green", "#CC6600"]
130132
point_color::Vector{Union{Nothing,String}} = Union{Nothing,String}[]
131133
point_marker::Vector{Symbol} = [:circle, :rect, :diamond, :utriangle]
@@ -405,7 +407,7 @@ function add_states!(b::Bloch, states::Vector{<:QuantumObject}; kind::Symbol = :
405407
if kind == :vector
406408
add_vectors!(b, vecs)
407409
elseif kind == :point
408-
add_points!(b, hcat(vecs...), kwargs...)
410+
add_points!(b, hcat(vecs...); kwargs...)
409411
else
410412
throw(ArgumentError("Invalid kind = :$kind"))
411413
end

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,15 @@ end
205205
@test false
206206
@info "Render threw unexpected error" exception=e
207207
end
208+
208209
b = Bloch()
209210
ψ₁ = normalize(basis(2, 0) + basis(2, 1))
210211
ψ₂ = normalize(basis(2, 0) - im * basis(2, 1))
211212
ψ₃ = basis(2, 0)
212213
add_line!(b, ψ₁, ψ₂; fmt = "r--")
213214
add_arc!(b, ψ₁, ψ₂)
214215
add_arc!(b, ψ₂, ψ₃, ψ₁)
216+
add_states!(b, [ψ₂, ψ₃], kind = :point, meth = :l)
215217
try
216218
fig, lscene = render(b)
217219
@test !isnothing(fig)

0 commit comments

Comments
 (0)