|
| 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 |
| 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`](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`](https://qutip.org/QuantumToolbox.jl/), creating a [`Bloch`](@ref) sphere is accomplished by calling either: |
| 19 | + |
| 20 | +!!! note "Import plotting libraries" |
| 21 | + Remember to import plotting libraries first. Here, we demonstrate the functionalities with [`CairoMakie.jl`](https://docs.makie.org/stable/explanations/backends/cairomakie.html). |
| 22 | + |
| 23 | +```@example Bloch_sphere_rendering |
| 24 | +b = Bloch() |
| 25 | +``` |
| 26 | + |
| 27 | +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: |
| 28 | + |
| 29 | +```@example Bloch_sphere_rendering |
| 30 | +fig, _ = render(b) |
| 31 | +fig |
| 32 | +``` |
| 33 | + |
| 34 | +See the [API documentation for Bloch sphere](@ref doc-API:Bloch-Sphere) for a full list of other available functions. |
| 35 | + |
| 36 | +## Add a single data point |
| 37 | + |
| 38 | +As an example, we can add a single data point via [`add_points!`](@ref): |
| 39 | + |
| 40 | +```@example Bloch_sphere_rendering |
| 41 | +pnt = [1 / sqrt(3), 1 / sqrt(3), 1 / sqrt(3)] |
| 42 | +add_points!(b, pnt) |
| 43 | +fig, _ = render(b) |
| 44 | +fig |
| 45 | +``` |
| 46 | + |
| 47 | +## Add a single vector |
| 48 | + |
| 49 | +Add a single vector via [`add_vectors!`](@ref): |
| 50 | + |
| 51 | +```@example Bloch_sphere_rendering |
| 52 | +vec = [0, 1, 0] |
| 53 | +add_vectors!(b, vec) |
| 54 | +fig, _ = render(b) |
| 55 | +fig |
| 56 | +``` |
| 57 | + |
| 58 | +## Add a single quantum state |
| 59 | + |
| 60 | +Add another vector corresponding to the ``|0\rangle`` state: |
| 61 | + |
| 62 | +```@example Bloch_sphere_rendering |
| 63 | +z0 = basis(2, 0) |
| 64 | +add_states!(b, z0) |
| 65 | +fig, _ = render(b) |
| 66 | +fig |
| 67 | +``` |
| 68 | + |
| 69 | +## Add multiple data |
| 70 | + |
| 71 | +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: |
| 72 | + |
| 73 | +```@example Bloch_sphere_rendering |
| 74 | +clear!(b) |
| 75 | +fig, _ = render(b) |
| 76 | +fig |
| 77 | +``` |
| 78 | + |
| 79 | +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: |
| 80 | + |
| 81 | +```@example Bloch_sphere_rendering |
| 82 | +x = basis(2, 0) + basis(2, 1) |
| 83 | +y = basis(2, 0) + im * basis(2, 1) |
| 84 | +z = basis(2, 0) |
| 85 | +add_states!(b, [x, y, z]) |
| 86 | +fig, _ = render(b) |
| 87 | +fig |
| 88 | +``` |
| 89 | + |
| 90 | +!!! note "State normalization" |
| 91 | + The function [`add_states!`](@ref) will automatically normalize the given quantum state(s), while [`add_vectors!`](@ref) does not normalize the given vectors. |
| 92 | + |
| 93 | +A similar method works for adding vectors: |
| 94 | + |
| 95 | +```@example Bloch_sphere_rendering |
| 96 | +clear!(b) |
| 97 | +vecs = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] |
| 98 | +add_vectors!(b, vecs) |
| 99 | +fig, _ = render(b) |
| 100 | +fig |
| 101 | +``` |
| 102 | + |
| 103 | +# Add lines and arcs |
| 104 | + |
| 105 | +You can also add lines and arcs via [`add_line!`](@ref) and [`add_arc!`](@ref) respectively: |
| 106 | + |
| 107 | +```@example Bloch_sphere_rendering |
| 108 | +add_line!(b, x, y) |
| 109 | +add_arc!(b, y, z) |
| 110 | +fig, _ = render(b) |
| 111 | +fig |
| 112 | +``` |
| 113 | + |
| 114 | +## Add multiple points |
| 115 | + |
| 116 | +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)): |
| 117 | + |
| 118 | +```@example Bloch_sphere_rendering |
| 119 | +clear!(b) |
| 120 | + |
| 121 | +th = LinRange(0, 2π, 20) |
| 122 | +xp = cos.(th) |
| 123 | +yp = sin.(th) |
| 124 | +zp = zeros(20) |
| 125 | +pnts = [xp, yp, zp] |
| 126 | +add_points!(b, pnts) |
| 127 | +fig, lscene = render(b) |
| 128 | +fig |
| 129 | +``` |
| 130 | + |
| 131 | +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: |
| 132 | + |
| 133 | +```@example Bloch_sphere_rendering |
| 134 | +xz = zeros(20) |
| 135 | +yz = sin.(th) |
| 136 | +zz = cos.(th) |
| 137 | +add_points!(b, [xz, yz, zz]) |
| 138 | +fig, lscene = render(b) |
| 139 | +fig |
| 140 | +``` |
| 141 | + |
| 142 | +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. |
| 143 | + |
| 144 | +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): |
| 145 | + |
| 146 | +```@example Bloch_sphere_rendering |
| 147 | +clear!(b) |
| 148 | + |
| 149 | +xp = cos.(th) |
| 150 | +yp = sin.(th) |
| 151 | +zp = zeros(20) |
| 152 | +pnts = [xp, yp, zp] |
| 153 | +add_points!(b, pnts, meth=:m) # add `meth=:m` to signify 'multi' colored points |
| 154 | +fig, lscene = render(b) |
| 155 | +fig |
| 156 | +``` |
| 157 | + |
| 158 | +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: |
| 159 | + |
| 160 | +```@example Bloch_sphere_rendering |
| 161 | +pnts = [xz, yz, zz] |
| 162 | +add_points!(b, pnts) # no `meth=:m` |
| 163 | +fig, lscene = render(b) |
| 164 | +fig |
| 165 | +``` |
| 166 | + |
| 167 | +## [Configuring the Bloch sphere](@id doc:Configuring-the-Bloch-sphere) |
| 168 | + |
| 169 | +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`](@ref) structure has many fields which one can control. Assuming `b = Bloch()`: |
| 170 | + |
| 171 | +### Data storage |
| 172 | + |
| 173 | +| **Field** | **Description** | **Default setting** | |
| 174 | +|:----------|:----------------|:--------------------| |
| 175 | +| `b.points` | Points to plot on the Bloch sphere (3D coordinates) | `Vector{Matrix{Float64}}()` (empty) | |
| 176 | +| `b.vectors` | Vectors to plot on the Bloch sphere | `Vector{Vector{Float64}}()` (empty) | |
| 177 | +| `b.lines` | Lines to draw on the sphere with each line given as `([start_pt, end_pt], line_format)` | `Vector{Tuple{Vector{Vector{Float64}},String}}()` (empty) | |
| 178 | +| `b.arcs` | Arcs to draw on the sphere | `Vector{Vector{Vector{Float64}}}()` (empty) | |
| 179 | + |
| 180 | +### Properties |
| 181 | + |
| 182 | +| **Field** | **Description** | **Default setting** | |
| 183 | +|:----------|:----------------|:--------------------| |
| 184 | +| `b.font_color` | Color of axis labels and text | `"black"` | |
| 185 | +| `b.font_size` | Font size for labels | `20` | |
| 186 | +| `b.frame_alpha` | Transparency of the wire frame | `0.2` | |
| 187 | +| `b.frame_color` | Color of the wire frame | `"gray"` | |
| 188 | +| `b.frame_width` | Width of wire frame | `1.0` | |
| 189 | +| `b.point_default_color` | Default color cycle for points | `["blue", "red", "green", "#CC6600"]` | |
| 190 | +| `b.point_color` | List of colors for Bloch point markers to cycle through | `Union{Nothing,String}[]` | |
| 191 | +| `b.point_marker` | List of point marker shapes to cycle through | `[:circle, :rect, :diamond, :utriangle]` | |
| 192 | +| `b.point_size` | List of point marker sizes (not all markers look the same size when plotted) | `[5.5, 6.2, 6.5, 7.5]` | |
| 193 | +| `b.point_style` | List of marker styles | `Symbol[]` | |
| 194 | +| `b.point_alpha` | List of marker transparencies | `Float64[]` | |
| 195 | +| `b.sphere_color` | Color of Bloch sphere surface | `0.2` | |
| 196 | +| `b.sphere_alpha` | Transparency of sphere surface | `"#FFDDDD"` | |
| 197 | +| `b.vector_color` | Colors for vectors | `["green", "#CC6600", "blue", "red"]` | |
| 198 | +| `b.vector_width` | Width of vectors | `0.025` | |
| 199 | +| `b.vector_arrowsize` | Scales the size of the arrow head. The first two elements scale the radius (in `x/y` direction) and the last one is the length of the cone. | `[0.07, 0.08, 0.08]` | |
| 200 | +| `b.view` | Azimuthal and elevation viewing angles in degrees | `[30, 30]` | |
| 201 | +| `b.xlabel` | Labels for x-axis | `[L"x", ""]` (``+x`` and ``-x``) | |
| 202 | +| `b.xlpos` | Positions of x-axis labels | `[1.2, -1.2]` | |
| 203 | +| `b.ylabel` | Labels for y-axis | `[L"y", ""]` (``+y`` and ``-y``) | |
| 204 | +| `b.ylpos` | Positions of y-axis labels | `[1.2, -1.2]` | |
| 205 | +| `b.zlabel` | Labels for z-axis | `[L"\|0\rangle", L"\|1\rangle]"` (``+z`` and ``-z``) | |
| 206 | +| `b.zlpos` | Positions of z-axis labels | `[1.2, -1.2]` | |
| 207 | + |
| 208 | +These properties can also be accessed via the `print` command: |
| 209 | + |
| 210 | +```@example Bloch_sphere_rendering |
| 211 | +b = Bloch() |
| 212 | +print(b) |
| 213 | +``` |
0 commit comments