Skip to content

Commit 601d3e1

Browse files
committed
add quantum states, add lines between quantum states on bloch sphere similar to qutip
1 parent 14f8e8f commit 601d3e1

File tree

3 files changed

+108
-15
lines changed

3 files changed

+108
-15
lines changed

docs/src/users_guide/plotting_the_bloch_sphere.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,32 @@ add_vectors!(b, vecs)
4949
fig, _ = render(b)
5050
fig
5151
```
52+
53+
# Add Arc, Line, and Vector
54+
5255
```@example Bloch_sphere_rendering
5356
clear!(b)
5457
vec = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
5558
add_vectors!(b, vec);
56-
add_line!(b, [1,0,0], [0,1,0]);
59+
add_line!(b, [1,0,0], [0,1,0])
5760
add_arc!(b, [1, 0, 0], [0, 1, 0], [0, 0, 1])
5861
fig, _ = render(b)
5962
fig
6063
```
6164

65+
# Add Quantum States
66+
67+
```@example Bloch_sphere_rendering
68+
clear!(b)
69+
x = basis(2, 0) + basis(2, 1)
70+
y = basis(2, 0) - im * basis(2, 1)
71+
z = basis(2, 0)
72+
b = Bloch()
73+
add_states!(b, [x, y, z])
74+
fig, _ = render(b)
75+
fig
76+
```
77+
6278
## Add Multiple Points Around the Equator
6379

6480
```@example Bloch_sphere_rendering

ext/QuantumToolboxMakieExt.jl

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,40 @@ function _render_bloch_makie(bloch_vec::Vector{Float64}; location = nothing, kwa
361361
return fig, ax
362362
end
363363

364-
function add_line!(
365-
b::Bloch,
364+
raw"""
365+
add_line!(
366+
b::QuantumToolbox.Bloch,
367+
start::QuantumToolbox.QuantumObject{<:Union{QuantumToolbox.Ket, QuantumToolbox.Bra, QuantumToolbox.Operator}},
368+
endp::QuantumToolbox.QuantumObject{<:Union{QuantumToolbox.Ket, QuantumToolbox.Bra, QuantumToolbox.Operator}};
369+
fmt = "k",
370+
kwargs...,
371+
)
372+
373+
Add a line between two quantum states or operators on the Bloch sphere visualization.
374+
375+
# Arguments
376+
377+
- `b::Bloch`: The Bloch sphere object to modify.
378+
- `start::QuantumObject`: The starting quantum state or operator. Can be a `Ket`, `Bra`, or `Operator`.
379+
- `endp::QuantumObject`: The ending quantum state or operator. Can be a `Ket`, `Bra`, or `Operator`.
380+
- `fmt::String="k"`: (optional) A format string specifying the line style and color (default is black `"k"`).
381+
- `kwargs...`: Additional keyword arguments forwarded to the underlying line drawing function.
382+
383+
# Description
384+
385+
This function converts the given quantum objects (states or operators) into their Bloch vector representations and adds a line between these two points on the Bloch sphere visualization.
386+
387+
# Example
388+
389+
```julia
390+
b = Bloch()
391+
ψ₁ = normalize(basis(2, 0) + basis(2, 1))
392+
ψ₂ = normalize(basis(2, 0) - im * basis(2, 1))
393+
add_line!(b, ψ₁, ψ₂; fmt = "r--")
394+
```
395+
"""
396+
function QuantumToolbox.add_line!(
397+
b::QuantumToolbox.Bloch,
366398
start::QuantumObject{<:Union{Ket,Bra,Operator}},
367399
endp::QuantumObject{<:Union{Ket,Bra,Operator}};
368400
fmt = "k",
@@ -381,6 +413,29 @@ function add_line!(
381413
return add_line!(b, p1, p2; fmt = fmt, kwargs...)
382414
end
383415

416+
raw"""
417+
QuantumToolbox.add_states!(b::Bloch, states::QuantumObject...)
418+
419+
Add one or more quantum states to the Bloch sphere visualization by converting them into Bloch vectors.
420+
421+
# Arguments
422+
- `b::Bloch`: The Bloch sphere object to modify
423+
- `states::QuantumObject...`: One or more quantum states (Ket, Bra, or Operator)
424+
425+
"""
426+
function QuantumToolbox.add_states!(b::Bloch, states::Vector{<:QuantumObject})
427+
vecs = Vector{Vector{Float64}}()
428+
for state in states
429+
vec = if isket(state) || isbra(state)
430+
_state_to_bloch(state)
431+
else
432+
_dm_to_bloch(state)
433+
end
434+
push!(vecs, vec)
435+
end
436+
return add_vectors!(b, vecs)
437+
end
438+
384439
function QuantumToolbox.render(b::QuantumToolbox.Bloch; location = nothing)
385440
fig, location = _getFigAndLocation(location)
386441
if isnothing(b.fig) || b.fig !== fig
@@ -459,17 +514,23 @@ function QuantumToolbox.render(b::QuantumToolbox.Bloch; location = nothing)
459514
end
460515
for (line, fmt, kwargs) in b.lines
461516
x, y, z = line
462-
color = Base.get(kwargs, :color, fmt == "k" ? Makie.RGBAf(0.1, 0.1, 0.1, 0.8) : fmt)
463-
linewidth = Base.get(kwargs, :linewidth, 1.0)
464-
Makie.lines!(
465-
b.ax,
466-
x,
467-
y,
468-
z;
469-
color = color,
470-
linewidth = linewidth,
471-
linestyle = Base.get(kwargs, :linestyle, :solid),
472-
)
517+
color_map =
518+
Dict("k" => :black, "r" => :red, "g" => :green, "b" => :blue, "c" => :cyan, "m" => :magenta, "y" => :yellow)
519+
c = get(color_map, first(fmt), :black)
520+
ls = nothing
521+
if occursin("--", fmt)
522+
ls = :dash
523+
elseif occursin(":", fmt)
524+
ls = :dot
525+
elseif occursin("-.", fmt)
526+
ls = :dashdot
527+
else
528+
ls = :solid
529+
end
530+
color = get(kwargs, :color, c)
531+
linewidth = get(kwargs, :linewidth, 1.0)
532+
linestyle = get(kwargs, :linestyle, ls)
533+
Makie.lines!(b.ax, x, y, z; color = color, linewidth = linewidth, linestyle = linestyle)
473534
end
474535
for arc_pts in b.arcs
475536
if length(arc_pts) >= 2

src/visualization.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
export plot_wigner,
2-
plot_fock_distribution, plot_bloch, Bloch, add_points!, add_vectors!, add_line!, add_arc!, clear!, render
2+
plot_fock_distribution,
3+
plot_bloch,
4+
Bloch,
5+
add_points!,
6+
add_vectors!,
7+
add_line!,
8+
add_arc!,
9+
clear!,
10+
render,
11+
add_states!
312

413
@doc raw"""
514
plot_wigner(
@@ -311,6 +320,13 @@ function add_arc!(b::Bloch, p1::Vector{<:Real}, p2::Vector{<:Real}, p3::Vector{<
311320
return push!(b.arcs, [convert(Vector{Float64}, p1), convert(Vector{Float64}, p2), convert(Vector{Float64}, p3)])
312321
end
313322

323+
@doc raw"""
324+
QuantumToolbox.add_states!(b::Bloch, states::QuantumObject...)
325+
326+
Add one or more quantum states to the Bloch sphere visualization by converting them into Bloch vectors.
327+
"""
328+
function add_states! end
329+
314330
function clear!(b::Bloch)
315331
empty!(b.points)
316332
empty!(b.vectors)

0 commit comments

Comments
 (0)