You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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).
14
+
In [`QuantumToolbox`](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
15
16
16
## Create a Bloch Sphere
17
17
18
-
In [QuantumToolbox.jl](https://qutip.org/QuantumToolbox.jl/), creating a [`Bloch`](@ref) sphere is accomplished by calling either:
18
+
In [`QuantumToolbox`](https://qutip.org/QuantumToolbox.jl/), creating a [`Bloch`](@ref) sphere is accomplished by calling either:
19
19
20
20
```@example Bloch_sphere_rendering
21
-
b = Bloch();
21
+
b = Bloch()
22
22
```
23
23
24
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
25
26
26
```@example Bloch_sphere_rendering
27
-
fig, _ = render(b);
27
+
fig, _ = render(b)
28
28
fig
29
29
```
30
30
31
-
## Add a Single Data Point
31
+
See the [API documentation for Bloch sphere](@ref doc-API:Bloch-Sphere) for a full list of other available functions.
32
+
33
+
## Add a single data point
32
34
33
35
As an example, we can add a single data point via [`add_points!`](@ref):
34
36
35
37
```@example Bloch_sphere_rendering
36
-
pnt = [1 / sqrt(3), 1 / sqrt(3), 1 / sqrt(3)];
37
-
add_points!(b, pnt);
38
-
fig, _ = render(b);
38
+
pnt = [1 / sqrt(3), 1 / sqrt(3), 1 / sqrt(3)]
39
+
add_points!(b, pnt)
40
+
fig, _ = render(b)
39
41
fig
40
42
```
41
43
42
-
## Add a Single Vector
44
+
## Add a single vector
43
45
44
-
and then a single vector via [`add_vectors!`](@ref):
46
+
Add a single vector via [`add_vectors!`](@ref):
45
47
46
48
```@example Bloch_sphere_rendering
47
-
vec = [0, 1, 0];
49
+
vec = [0, 1, 0]
48
50
add_vectors!(b, vec)
49
51
fig, _ = render(b)
50
52
fig
51
53
```
52
54
53
-
and then add another vector corresponding to the ``|0\rangle`` state:
55
+
## Add a single quantum state
56
+
57
+
Add another vector corresponding to the ``|0\rangle`` state:
54
58
55
59
```@example Bloch_sphere_rendering
56
-
x = basis(2, 0)
57
-
add_states!(b, [x])
60
+
z0 = basis(2, 0)
61
+
add_states!(b, z0)
58
62
fig, _ = render(b)
59
63
fig
60
64
```
61
65
62
-
## Add Multiple Vectors
66
+
## Add multiple data
63
67
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:
68
+
We can also plot multiple points, vectors, and states at the same time by passing arrays instead of individual elements via [`add_points!`](@ref),[`add_vectors!`](@ref), and [`add_states!`](@ref), respectively. 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
69
66
70
```@example Bloch_sphere_rendering
67
71
clear!(b)
@@ -73,14 +77,16 @@ Now on the same [`Bloch`](@ref) sphere, we can plot the three states via [`add_s
73
77
74
78
```@example Bloch_sphere_rendering
75
79
x = basis(2, 0) + basis(2, 1)
76
-
y = basis(2, 0) - im * basis(2, 1)
80
+
y = basis(2, 0) + im * basis(2, 1)
77
81
z = basis(2, 0)
78
-
b = Bloch()
79
82
add_states!(b, [x, y, z])
80
83
fig, _ = render(b)
81
84
fig
82
85
```
83
86
87
+
!!! note "State normalization"
88
+
The function [`add_states!`](@ref) will automatically normalize the given quantum state(s), while [`add_vectors!`](@ref) does not normalize the given vectors.
89
+
84
90
A similar method works for adding vectors:
85
91
86
92
```@example Bloch_sphere_rendering
@@ -91,70 +97,107 @@ fig, _ = render(b)
91
97
fig
92
98
```
93
99
94
-
# Add Arc, Line, and Vector
100
+
# Add lines and arcs
95
101
96
102
You can also add lines and arcs via [`add_line!`](@ref) and [`add_arc!`](@ref) respectively:
97
103
98
104
```@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])
105
+
add_line!(b, x, y)
106
+
add_arc!(b, y, z)
104
107
fig, _ = render(b)
105
108
fig
106
109
```
107
110
108
-
## Add Multiple Points
111
+
## Add multiple points
109
112
110
113
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
114
112
115
```@example Bloch_sphere_rendering
113
-
th = range(0, 2π; length=20);
114
116
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);
117
+
118
+
th = LinRange(0, 2π, 20)
119
+
xp = cos.(th)
120
+
yp = sin.(th)
121
+
zp = zeros(20)
122
+
pnts = [xp, yp, zp]
123
+
add_points!(b, pnts)
124
+
fig, ax = render(b)
121
125
fig
122
126
```
123
127
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
128
+
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) function:
127
129
128
130
```@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);
131
+
xz = zeros(20)
132
+
yz = sin.(th)
133
+
zz = cos.(th)
134
+
add_points!(b, [xz, yz, zz])
135
+
fig, ax = render(b)
135
136
fig
136
137
```
137
138
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
+
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. Again, we have had to call [`add_points!`](@ref) twice because adding more than one set of multiple data points is not supported by the [`add_points!`](@ref) function.
139
140
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
+
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`field (see [Configuring the Bloch sphere](@ref doc:Configuring-the-Bloch-sphere) below). Again, after [`clear!`](@ref):
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
156
155
157
```@example Bloch_sphere_rendering
156
-
pnts = [xz, yz, zz] ;
157
-
add_points!(b, pnts);
158
-
fig, ax = render(b);
158
+
pnts = [xz, yz, zz]
159
+
add_points!(b, pnts) # no `meth=:m`
160
+
fig, ax = render(b)
159
161
fig
160
162
```
163
+
164
+
## [Configuring the Bloch sphere](@id doc:Configuring-the-Bloch-sphere)
165
+
166
+
At the end of the last section we saw that the colors and marker shapes of the data plotted on the Bloch sphere are automatically varied according to the number of points and vectors added. But what if you want a different choice of color, or you want your sphere to be purple with different axes labels? Well then you are in luck as the Bloch class has many attributes which one can control. Assuming `b = Bloch()`:
0 commit comments