Skip to content

Commit 47deb2c

Browse files
Fe-r-ozalbertomercurioytdHuang
authored
Implement Bloch Sphere rendering (#472)
Co-authored-by: Alberto Mercurio <[email protected]> Co-authored-by: Yi-Te Huang <[email protected]>
1 parent fbe80da commit 47deb2c

File tree

8 files changed

+1259
-3
lines changed

8 files changed

+1259
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +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. ([#472])
1213

1314
## [v0.31.1]
1415
Release date: 2025-05-16
@@ -231,5 +232,6 @@ Release date: 2024-11-13
231232
[#455]: https://github.com/qutip/QuantumToolbox.jl/issues/455
232233
[#456]: https://github.com/qutip/QuantumToolbox.jl/issues/456
233234
[#460]: https://github.com/qutip/QuantumToolbox.jl/issues/460
235+
[#472]: https://github.com/qutip/QuantumToolbox.jl/issues/472
234236
[#473]: https://github.com/qutip/QuantumToolbox.jl/issues/473
235237
[#476]: https://github.com/qutip/QuantumToolbox.jl/issues/476

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const PAGES = [
5151
],
5252
"Manipulating States and Operators" => "users_guide/states_and_operators.md",
5353
"Tensor Products and Partial Traces" => "users_guide/tensor.md",
54+
"Plotting on the Bloch Sphere" => "users_guide/plotting_the_bloch_sphere.md",
5455
"Time Evolution and Dynamics" => [
5556
"Introduction" => "users_guide/time_evolution/intro.md",
5657
"Time Evolution Solutions" => "users_guide/time_evolution/solution.md",

docs/src/resources/api.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,13 @@ meshgrid
319319
```@docs
320320
plot_wigner
321321
plot_fock_distribution
322+
plot_bloch
323+
Bloch
324+
render
325+
add_points!
326+
add_vectors!
327+
add_line!
328+
add_arc!
329+
clear!
330+
add_states!
322331
```

docs/src/users_guide/extensions/cairomakie.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ The supported plotting functions are listed as follows:
1919
| **Plotting Function** | **Description** |
2020
|:----------------------|:----------------|
2121
| [`plot_wigner`](@ref) | [Wigner quasipropability distribution](https://en.wikipedia.org/wiki/Wigner_quasiprobability_distribution) |
22-
| [`plot_fock_distribution`](@ref) | [Fock state](https://en.wikipedia.org/wiki/Fock_state) distribution |
22+
| [`plot_fock_distribution`](@ref) | [Fock state](https://en.wikipedia.org/wiki/Fock_state) distribution |
23+
| [`plot_bloch`](@ref) | [Plotting on the Bloch Sphere](@ref doc:Plotting-on-the-Bloch-Sphere) |
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# [Plotting on the Bloch Sphere](@id doc:Plotting-on-the-Bloch-Sphere)
2+
3+
```@setup Bloch_sphere_rendering
4+
using QuantumToolbox
5+
6+
using CairoMakie
7+
CairoMakie.enable_only_mime!(MIME"image/svg+xml"())
8+
```
9+
10+
## [Introduction](@id doc:Bloch_sphere_rendering)
11+
12+
When studying the dynamics of a two-level system, it's often convenient to visualize the state of the system by plotting the state vector or density matrix on the Bloch sphere.
13+
14+
In [QuantumToolbox.jl](https://qutip.org/QuantumToolbox.jl/), this can be done using the [`Bloch`](@ref) or [`plot_bloch`](@ref) methods that provide same syntax as [QuTiP](https://qutip.readthedocs.io/en/stable/guide/guide-bloch.html).
15+
16+
## Create a Bloch Sphere
17+
18+
In [QuantumToolbox.jl](https://qutip.org/QuantumToolbox.jl/), creating a [`Bloch`](@ref) sphere is accomplished by calling either:
19+
20+
```@example Bloch_sphere_rendering
21+
b = Bloch();
22+
```
23+
24+
which will load an instance of [`Bloch`](@ref). Before getting into the details of these objects, we can simply plot the blank [`Bloch`](@ref) sphere associated with these instances via:
25+
26+
```@example Bloch_sphere_rendering
27+
fig, _ = render(b);
28+
fig
29+
```
30+
31+
## Add a Single Data Point
32+
33+
As an example, we can add a single data point via [`add_points!`](@ref):
34+
35+
```@example Bloch_sphere_rendering
36+
pnt = [1 / sqrt(3), 1 / sqrt(3), 1 / sqrt(3)];
37+
add_points!(b, pnt);
38+
fig, _ = render(b);
39+
fig
40+
```
41+
42+
## Add a Single Vector
43+
44+
and then a single vector via [`add_vectors!`](@ref):
45+
46+
```@example Bloch_sphere_rendering
47+
vec = [0, 1, 0];
48+
add_vectors!(b, vec)
49+
fig, _ = render(b)
50+
fig
51+
```
52+
53+
and then add another vector corresponding to the ``|0\rangle`` state:
54+
55+
```@example Bloch_sphere_rendering
56+
x = basis(2, 0)
57+
add_states!(b, [x])
58+
fig, _ = render(b)
59+
fig
60+
```
61+
62+
## Add Multiple Vectors
63+
64+
We can also plot multiple points, vectors, and states at the same time by passing arrays instead of individual elements via [`add_vectors!](@ref). Before giving an example, we can use [`clear!`](@ref) to remove the current data from our [`Bloch`](@ref) sphere instead of creating a new instance:
65+
66+
```@example Bloch_sphere_rendering
67+
clear!(b)
68+
fig, _ = render(b)
69+
fig
70+
```
71+
72+
Now on the same [`Bloch`](@ref) sphere, we can plot the three states via [`add_states!`](@ref) associated with the `x`, `y`, and `z` directions:
73+
74+
```@example Bloch_sphere_rendering
75+
x = basis(2, 0) + basis(2, 1)
76+
y = basis(2, 0) - im * basis(2, 1)
77+
z = basis(2, 0)
78+
b = Bloch()
79+
add_states!(b, [x, y, z])
80+
fig, _ = render(b)
81+
fig
82+
```
83+
84+
A similar method works for adding vectors:
85+
86+
```@example Bloch_sphere_rendering
87+
clear!(b)
88+
vecs = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
89+
add_vectors!(b, vecs)
90+
fig, _ = render(b)
91+
fig
92+
```
93+
94+
# Add Arc, Line, and Vector
95+
96+
You can also add lines and arcs via [`add_line!`](@ref) and [`add_arc!`](@ref) respectively:
97+
98+
```@example Bloch_sphere_rendering
99+
clear!(b)
100+
vec = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
101+
add_vectors!(b, vec);
102+
add_line!(b, [1,0,0], [0,1,0])
103+
add_arc!(b, [1, 0, 0], [0, 1, 0], [0, 0, 1])
104+
fig, _ = render(b)
105+
fig
106+
```
107+
108+
## Add Multiple Points
109+
110+
Adding multiple points to the [`Bloch`](@ref) sphere works slightly differently than adding multiple states or vectors. For example, lets add a set of `20` points around the equator (after calling [`clear!`](@ref)):
111+
112+
```@example Bloch_sphere_rendering
113+
th = range(0, 2π; length=20);
114+
clear!(b)
115+
xp = cos.(th);
116+
yp = sin.(th);
117+
zp = zeros(20);
118+
pnts = [xp, yp, zp];
119+
add_points!(b, pnts);
120+
fig, ax = render(b);
121+
fig
122+
```
123+
124+
Notice that, in contrast to states or vectors, each point remains the same color as the initial point. This is because adding multiple data points using [`add_points!`](@ref) is interpreted, by default, to correspond to a single data point (single qubit state) plotted at different times. This is very useful when visualizing the dynamics of a qubit. If we want to plot additional qubit states we can call additional [`add_points!`](@ref):
125+
126+
## Add Another Set of Points
127+
128+
```@example Bloch_sphere_rendering
129+
xz = zeros(20);
130+
yz = sin.(th);
131+
zz = cos.(th);
132+
pnts = [xz, yz, zz];
133+
add_points!(b, pnts);
134+
fig, ax = render(b);
135+
fig
136+
```
137+
138+
The color and shape of the data points is varied automatically by [`Bloch`](@ref). Notice how the color and point markers change for each set of data.
139+
140+
What if we want to vary the color of our points. We can tell [`Bloch`](@ref) to vary the color of each point according to the colors listed in the `point_color` attribute.
141+
142+
```@example Bloch_sphere_rendering
143+
clear!(b)
144+
xp = cos.(th);
145+
yp = sin.(th);
146+
zp = zeros(20);
147+
pnts = [xp, yp, zp];
148+
add_points!(b, pnts, meth=:m);
149+
fig, ax = render(b);
150+
fig
151+
```
152+
153+
Now, the data points cycle through a variety of predefined colors. Now lets add another set of points, but this time we want the set to be a single color, representing say a qubit going from the ``|0\rangle`` state to the ``|1\rangle`` state in the `y-z` plane:
154+
155+
```@example Bloch_sphere_rendering
156+
pnts = [xz, yz, zz] ;
157+
add_points!(b, pnts);
158+
fig, ax = render(b);
159+
fig
160+
```

0 commit comments

Comments
 (0)