Plotting on the Bloch Sphere
Introduction
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.
In QuantumToolbox
, this can be done using the Bloch
or plot_bloch
methods that provide same syntax as QuTiP.
Create a Bloch Sphere
In QuantumToolbox
, creating a Bloch
sphere is accomplished by calling either:
Import plotting libraries
Remember to import plotting libraries first. Here, we demonstrate the functionalities with CairoMakie.jl
.
b = Bloch()
Bloch Sphere
data:
-----
Number of points = 0
Number of vectors = 0
Number of lines = 0
Number of arcs = 0
properties:
-----------
font_color = black
font_size = 20
frame_alpha = 0.2
frame_color = gray
frame_width = 1.0
point_default_color = ["blue", "red", "green", "#CC6600"]
point_color = Union{Nothing, String}[]
point_marker = [:circle, :rect, :diamond, :utriangle]
point_size = [5.5, 6.2, 6.5, 7.5]
point_style = Symbol[]
point_alpha = Float64[]
sphere_alpha = 0.2
sphere_color = #FFDDDD
vector_color = ["green", "#CC6600", "blue", "red"]
vector_width = 0.025
vector_arrowsize = [0.07, 0.08, 0.08]
view = [30, 30]
xlabel = AbstractString[L"$x$", ""]
xlpos = [1.2, -1.2]
ylabel = AbstractString[L"$y$", ""]
ylpos = [1.2, -1.2]
zlabel = AbstractString[L"$|0\rangle$", L"$|1\rangle$"]
zlpos = [1.2, -1.2]
which will load an instance of Bloch
. Before getting into the details of these objects, we can simply plot the blank Bloch
sphere associated with these instances via:
fig, _ = render(b)
fig
See the API documentation for Bloch sphere for a full list of other available functions.
Add a single data point
As an example, we can add a single data point via add_points!
:
pnt = [1 / sqrt(3), 1 / sqrt(3), 1 / sqrt(3)]
add_points!(b, pnt)
fig, _ = render(b)
fig
Add a single vector
Add a single vector via add_vectors!
:
vec = [0, 1, 0]
add_vectors!(b, vec)
fig, _ = render(b)
fig
Add a single quantum state
Add another vector corresponding to the
z0 = basis(2, 0)
add_states!(b, z0)
fig, _ = render(b)
fig
Add multiple data
We can also plot multiple points, vectors, and states at the same time by passing arrays instead of individual elements via add_points!
, add_vectors!
, and add_states!
, respectively. Before giving an example, we can use clear!
to remove the current data from our Bloch
sphere instead of creating a new instance:
clear!(b)
fig, _ = render(b)
fig
Now on the same Bloch
sphere, we can plot the three states via add_states!
associated with the x
, y
, and z
directions:
x = basis(2, 0) + basis(2, 1)
y = basis(2, 0) + im * basis(2, 1)
z = basis(2, 0)
add_states!(b, [x, y, z])
fig, _ = render(b)
fig
State normalization
The function add_states!
will automatically normalize the given quantum state(s), while add_vectors!
does not normalize the given vectors.
A similar method works for adding vectors:
clear!(b)
vecs = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
add_vectors!(b, vecs)
fig, _ = render(b)
fig
Add lines and arcs
You can also add lines and arcs via add_line!
and add_arc!
respectively:
add_line!(b, x, y)
add_arc!(b, y, z)
fig, _ = render(b)
fig
Add multiple points
Adding multiple points to the Bloch
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!
):
clear!(b)
th = LinRange(0, 2π, 20)
xp = cos.(th)
yp = sin.(th)
zp = zeros(20)
pnts = [xp, yp, zp]
add_points!(b, pnts)
fig, lscene = render(b)
fig
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!
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!
function:
xz = zeros(20)
yz = sin.(th)
zz = cos.(th)
add_points!(b, [xz, yz, zz])
fig, lscene = render(b)
fig
The color and shape of the data points is varied automatically by Bloch
. Notice how the color and point markers change for each set of data. Again, we have had to call add_points!
twice because adding more than one set of multiple data points is not supported by the add_points!
function.
What if we want to vary the color of our points. We can tell Bloch
to vary the color of each point according to the colors listed in the point_color
field (see Configuring the Bloch sphere below). Again, after clear!
:
clear!(b)
xp = cos.(th)
yp = sin.(th)
zp = zeros(20)
pnts = [xp, yp, zp]
add_points!(b, pnts, meth=:m) # add `meth=:m` to signify 'multi' colored points
fig, lscene = render(b)
fig
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 y-z
plane:
pnts = [xz, yz, zz]
add_points!(b, pnts) # no `meth=:m`
fig, lscene = render(b)
fig
Configuring the Bloch sphere
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
structure has many fields which one can control. Assuming b = Bloch()
:
Data storage
Field | Description | Default setting |
---|---|---|
b.points | Points to plot on the Bloch sphere (3D coordinates) | Vector{Matrix{Float64}}() (empty) |
b.vectors | Vectors to plot on the Bloch sphere | Vector{Vector{Float64}}() (empty) |
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) |
b.arcs | Arcs to draw on the sphere | Vector{Vector{Vector{Float64}}}() (empty) |
Properties
Field | Description | Default setting |
---|---|---|
b.font_color | Color of axis labels and text | "black" |
b.font_size | Font size for labels | 20 |
b.frame_alpha | Transparency of the wire frame | 0.2 |
b.frame_color | Color of the wire frame | "gray" |
b.frame_width | Width of wire frame | 1.0 |
b.point_default_color | Default color cycle for points | ["blue", "red", "green", "#CC6600"] |
b.point_color | List of colors for Bloch point markers to cycle through | Union{Nothing,String}[] |
b.point_marker | List of point marker shapes to cycle through | [:circle, :rect, :diamond, :utriangle] |
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] |
b.point_style | List of marker styles | Symbol[] |
b.point_alpha | List of marker transparencies | Float64[] |
b.sphere_color | Color of Bloch sphere surface | 0.2 |
b.sphere_alpha | Transparency of sphere surface | "#FFDDDD" |
b.vector_color | Colors for vectors | ["green", "#CC6600", "blue", "red"] |
b.vector_width | Width of vectors | 0.025 |
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] |
b.view | Azimuthal and elevation viewing angles in degrees | [30, 30] |
b.xlabel | Labels for x-axis | [L"x", ""] ( |
b.xlpos | Positions of x-axis labels | [1.2, -1.2] |
b.ylabel | Labels for y-axis | [L"y", ""] ( |
b.ylpos | Positions of y-axis labels | [1.2, -1.2] |
b.zlabel | Labels for z-axis | [L"|0\rangle", L"|1\rangle]" ( |
b.zlpos | Positions of z-axis labels | [1.2, -1.2] |
These properties can also be accessed via the print
command:
b = Bloch()
print(b)
Bloch Sphere
data:
-----
Number of points = 0
Number of vectors = 0
Number of lines = 0
Number of arcs = 0
properties:
-----------
font_color = black
font_size = 20
frame_alpha = 0.2
frame_color = gray
frame_width = 1.0
point_default_color = ["blue", "red", "green", "#CC6600"]
point_color = Union{Nothing, String}[]
point_marker = [:circle, :rect, :diamond, :utriangle]
point_size = [5.5, 6.2, 6.5, 7.5]
point_style = Symbol[]
point_alpha = Float64[]
sphere_alpha = 0.2
sphere_color = #FFDDDD
vector_color = ["green", "#CC6600", "blue", "red"]
vector_width = 0.025
vector_arrowsize = [0.07, 0.08, 0.08]
view = [30, 30]
xlabel = AbstractString[L"$x$", ""]
xlpos = [1.2, -1.2]
ylabel = AbstractString[L"$y$", ""]
ylpos = [1.2, -1.2]
zlabel = AbstractString[L"$|0\rangle$", L"$|1\rangle$"]
zlpos = [1.2, -1.2]