From cadc18a904d25a13367c04f0b39fc82ac5069eb5 Mon Sep 17 00:00:00 2001 From: Li-Xun Cai <157601901+TendonFFF@users.noreply.github.com> Date: Thu, 31 Jul 2025 19:46:11 +0800 Subject: [PATCH 1/6] Bloch anime (#519) Co-authored-by: cailixun --- .../users_guide/plotting_the_bloch_sphere.md | 61 ++++++++++++++++++- ext/QuantumToolboxMakieExt.jl | 40 +++++++++--- 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/docs/src/users_guide/plotting_the_bloch_sphere.md b/docs/src/users_guide/plotting_the_bloch_sphere.md index 8bb5facfe..50a6ca5d2 100644 --- a/docs/src/users_guide/plotting_the_bloch_sphere.md +++ b/docs/src/users_guide/plotting_the_bloch_sphere.md @@ -211,4 +211,63 @@ These properties can also be accessed via the `print` command: ```@example Bloch_sphere_rendering b = Bloch() print(b) -``` \ No newline at end of file +``` + +## Animating with the Bloch sphere + +The [`Bloch`](@ref) structure was designed from the outset to generate animations. To animate a set of vectors or data points, the basic idea is: plot the data at time ``t_1``, save the sphere, clear the sphere, plot data at ``t_2``, and so on. The easiest way to animate data on the Bloch sphere is to use the `record` function provided by [`Makie.jl`](https://docs.makie.org/stable/). We will demonstrate this functionality with the following example: the decay of a qubit on the Bloch sphere. + +```@example Bloch_sphere_rendering +# system parameters +ω = 2π +θ = 0.2π +n_th = 0.5 # temperature +γ1 = 0.5 +γ2 = 0.2 + +# operators and the Hamiltonian +sx = sigmax() +sy = sigmay() +sz = sigmaz() +sm = sigmam() +H = ω * (cos(θ) * sz + sin(θ) * sx) + +# collapse operators +c_op_list = ( + √(γ1 * (n_th + 1)) * sm, + √(γ1 * n_th) * sm', + √γ2 * sz +) + +# solving evolution +ψ0 = basis(2, 0) +tlist = LinRange(0, 4, 250) +sol = mesolve(H, ψ0, tlist, c_op_list, e_ops = (sx, sy, sz), progress_bar = Val(false)) +x = real(sol.expect[1,:]) +y = real(sol.expect[2,:]) +z = real(sol.expect[3,:]) +``` + +To animate a set of vectors or data points, we use the `record` function provided by [`Makie.jl`](https://docs.makie.org/stable/): + +```@example Bloch_sphere_rendering +b = Bloch() +b.view = [50,30] + +fig, lscene = render(b) + +record(fig, "qubit_decay.mp4", eachindex(tlist), framerate = 20) do idx + clear!(b) + add_vectors!(b, [sin(θ), 0, cos(θ)]) + add_points!(b, [x[1:idx], y[1:idx], z[1:idx]]) + render(b, location = lscene) +end +nothing # hide +``` + +```@raw html +