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))
Example block output

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)
Example block output

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 being 1 (pt)
  • YaoPlots.CircuitStyles.textsize[] for text size, default value being 16 (pt)
  • YaoPlots.CircuitStyles.paramtextsize[] for parameter text size, for parameterized gates, default value being 10 (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))))
Example block output

Circuit Visualization

YaoPlots.vizcircuitFunction
vizcircuit(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 and ending_texts are texts shown before and after the circuit.
  • starting_offset and end_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
source

Bloch Sphere Visualization

YaoPlots.CircuitStylesModule
CircuitStyles

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.
source
YaoPlots.bloch_sphereFunction
bloch_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 text
  • color: the color of the drawing
  • drawing_size: the size of the drawing
  • offset_x: the offset of the drawing in x direction
  • offset_y: the offset of the drawing in y direction
  • filename: the filename of the output file, if not specified, a temporary file will be used
  • format: the format of the output file, if not specified, the format will be inferred from the filename
  • fontfamily: the font family of the text
  • background_color: the background color of the drawing
  • lw: the line width of the drawing
  • eye_point: the eye point of the drawing
  • extra_kwargs: extra keyword arguments passed to draw_bloch_sphere
    • dot_size: the size of the dot
    • ball_size: the size of the ball
    • show_projection_lines: whether to show the projection lines
    • show_angle_texts: whether to show the angle texts
    • show_line: whether to show the line
    • show01: whether to show the 0 and 1 states
    • colors: the colors of the states
    • axes_lw: the line width of the axes
    • axes_textsize: the size of the axes texts
    • axes_colors: the colors of the axes
    • axes_texts: the texts of the axes

Examples

julia> using YaoPlots, YaoArrayRegister

julia> bloch_sphere("|ψ⟩"=>rand_state(1), "ρ"=>density_matrix(rand_state(2), 1));
source
YaoPlots.BlochStylesModule
BlochStyles

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 drawing
  • textsize: the size of the text
  • fontfamily: the font family of the text
  • background_color: the background color of the drawing
  • color: the color of the drawing

Sphere

  • ball_size: the size of the ball
  • dot_size: the size of the dot
  • eye_point: the eye point of the drawing

Axis

  • axes_lw: the line width of the axes
  • axes_colors: the colors of the axes
  • axes_texts: the texts of the axes, default to ["x", "y", "z"]

State display

  • show_projection_lines: whether to show the projection lines
  • show_angle_texts: whether to show the angle texts
  • show_line: whether to show the line
  • show01: whether to show the 0 and 1 states
source

Themes