Quantum Circuit Visualization
YaoPlots
is the Quantum circuit visualization component for Yao.
Examples
Example 1: Visualize a QBIR define in Yao
using Yao.EasyBuild, YaoPlots
# show a qft circuit
vizcircuit(qft_circuit(5))
If you are using a Pluto/Jupyter notebook, Atom/VSCode editor, you should see the above image in your plotting panel.
Example 2: Visualize a single qubit
using YaoPlots, Yao
reg = zero_state(1) |> Rx(π/8) |> Rx(π/8)
rho = density_matrix(ghz_state(2), 1)
bloch_sphere("|ψ⟩"=>reg, "ρ"=>rho; show_projection_lines=true)
See more examples.
Adjusting the plot attributes
Various attributes of the visualizations can be altered. The plot can be modified, if we change the following attributes
YaoPlots.CircuitStyles.linecolor[]
for line color, default value being"#000000"
(black color)YaoPlots.CircuitStyles.gate_bgcolor[]
for background color of square blocks, the default value being"#FFFFFF"
(white color)YaoPlots.CircuitStyles.textcolor[]
for text color, default value being"#000000
YaoPlots.CircuitStyles.lw[]
for line width, default value being1
(pt)YaoPlots.CircuitStyles.textsize[]
for text size, default value being16
(pt)YaoPlots.CircuitStyles.paramtextsize[]
for parameter text size, for parameterized gates, default value being10
(pt)
For example,
using YaoPlots, Yao
YaoPlots.CircuitStyles.linecolor[] = "pink"
YaoPlots.CircuitStyles.gate_bgcolor[] = "yellow"
YaoPlots.CircuitStyles.textcolor[] = "#000080" # the navy blue color
YaoPlots.CircuitStyles.fontfamily[] = "JuliaMono"
YaoPlots.CircuitStyles.lw[] = 2.5
YaoPlots.CircuitStyles.textsize[] = 13
YaoPlots.CircuitStyles.paramtextsize[] = 8
vizcircuit(chain(3, put(1=>X), repeat(3, H), put(2=>Y), repeat(3, Rx(π/2))))
Circuit Visualization
YaoPlots.vizcircuit
— Functionvizcircuit(circuit; w_depth=0.85, w_line=0.75, format=:svg, filename=nothing,
show_ending_bar=false, starting_texts=nothing, starting_offset=-0.3,
ending_texts=nothing, ending_offset=0.3)
Visualize a Yao
quantum circuit.
Keyword Arguments
w_depth
is the circuit column width.w_line
is the circuit row width.format
can be:svg
,:png
or:pdf
.filename
can be"*.svg"
,"*.png"
,"*.pdf"
or nothing (not saving to a file).starting_texts
andending_texts
are texts shown before and after the circuit.starting_offset
andend_offset
are offsets (real values) for starting and ending texts.show_ending_bar
is a boolean switch to show ending bar.
Styles
To change the gates styles like colors and lines, please modify the constants in submodule CircuitStyles
. They are defined as:
- CircuitStyles.unit = Ref(60) # number of points in a unit
- CircuitStyles.r = Ref(0.2) # size of nodes
- CircuitStyles.lw = Ref(1.0) # line width
- CircuitStyles.textsize = Ref(16.0) # text size
- CircuitStyles.paramtextsize = Ref(10.0) # text size (longer texts)
- CircuitStyles.fontfamily = Ref("monospace") # font family
- CircuitStyles.linecolor = Ref("#000000") # line color
- CircuitStyles.gate_bgcolor = Ref("transparent") # gate background color
- CircuitStyles.textcolor = Ref("#000000") # text color
YaoPlots.plot
— FunctionAn alias of vizcircuit
Bloch Sphere Visualization
YaoPlots.CircuitStyles
— ModuleCircuitStyles
A module to define the styles of the circuit visualization. To change the styles, please modify the variables in this module, e.g.
julia> using YaoPlots
julia> YaoPlots.CircuitStyles.unit[] = 40
40
Style variables
Sizes
unit
is the number of pixels in a unit.r
is the size of nodes.lw
is the line width.
Texts
textsize
is the text size.paramtextsize
is the text size for longer texts.fontfamily
is the font family.
Colors
linecolor
is the line color.gate_bgcolor
is the gate background color.textcolor
is the text color.
YaoPlots.bloch_sphere
— Functionbloch_sphere(
states...;
textsize,
color,
drawing_size,
offset_x,
offset_y,
filename,
format,
fontfamily,
background_color,
lw,
eye_point,
extra_kwargs...
) -> Luxor.Drawing
Draw a bloch sphere, with the inputs being a list of string => state
pairs, where the string is a label for the state and a state can be a complex vector of size 2, a Yao register or DensityMatrix
. If you want to get a raw drawing, use draw_bloch_sphere
instead.
Keyword Arguments
Note: The default values can be specified in submodule BlochStyles
.
textsize
: the size of the textcolor
: the color of the drawingdrawing_size
: the size of the drawingoffset_x
: the offset of the drawing in x directionoffset_y
: the offset of the drawing in y directionfilename
: the filename of the output file, if not specified, a temporary file will be usedformat
: the format of the output file, if not specified, the format will be inferred from the filenamefontfamily
: the font family of the textbackground_color
: the background color of the drawinglw
: the line width of the drawingeye_point
: the eye point of the drawingextra_kwargs
: extra keyword arguments passed todraw_bloch_sphere
dot_size
: the size of the dotball_size
: the size of the ballshow_projection_lines
: whether to show the projection linesshow_angle_texts
: whether to show the angle textsshow_line
: whether to show the lineshow01
: whether to show the 0 and 1 statescolors
: the colors of the statesaxes_lw
: the line width of the axesaxes_textsize
: the size of the axes textsaxes_colors
: the colors of the axesaxes_texts
: the texts of the axes
Examples
julia> using YaoPlots, YaoArrayRegister
julia> bloch_sphere("|ψ⟩"=>rand_state(1), "ρ"=>density_matrix(rand_state(2), 1));
YaoPlots.BlochStyles
— ModuleBlochStyles
The module to define the default styles for bloch sphere drawing. To change the default styles, you can modify the values in this module, e.g.
using YaoPlots
YaoPlots.BlochStyles.lw[] = 2.0
Style variables
Generic
lw
: the line width of the drawingtextsize
: the size of the textfontfamily
: the font family of the textbackground_color
: the background color of the drawingcolor
: the color of the drawing
Sphere
ball_size
: the size of the balldot_size
: the size of the doteye_point
: the eye point of the drawing
Axis
axes_lw
: the line width of the axesaxes_colors
: the colors of the axesaxes_texts
: the texts of the axes, default to["x", "y", "z"]
State display
show_projection_lines
: whether to show the projection linesshow_angle_texts
: whether to show the angle textsshow_line
: whether to show the lineshow01
: whether to show the 0 and 1 states
Themes
YaoPlots.darktheme!
— Functiondarktheme!()
Change the default theme to dark.
YaoPlots.lighttheme!
— Functionlighttheme!()
Change the default theme to light.