|
| 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