User Interface#

The qibo.ui module provides a set of plotting utilities built on top of matplotlib to visualize circuits, states and measurement outcomes. It is imported as from qibo.ui import ....

Circuit drawing#

qibo.ui.plot_circuit(circuit, scale=0.6, cluster_gates=True, fold=-1, style=None)[source]#

Main matplotlib plot function for Qibo circuit

Parameters:
  • circuit (qibo.models.circuit.Circuit) – Circuit to plot.

  • scale (float, optional) – Scaling factor for matplotlib output drawing. Defaults to \(0.6\).

  • cluster_gates (bool, optional) – if True, groups circuit gates on drawing. Defaults to True.

  • fold (int, optional) – Number of gates to display in a row. Defaults to \(-1\) (no folding unless specified).

  • style (dict or str or None, optional) – Style applied to the circuit. It can a built-in style or custom. Built-in options are: garnacha, fardelejo, quantumspain, color-blind and cachirulo. Custom style needs to be a dictionary.

Returns:

Respectively, axes object that encapsulates all the elements of an individual plot, and a matplotlib figure object.

Return type:

(matplotlib.axes.Axes, matplotlib.figure.Figure)

Example

import matplotlib.pyplot as plt

from qibo.models import QFT
# new plot function based on matplotlib
from qibo.ui import plot_circuit

%matplotlib inline

# create a 5-qubit QFT circuit
circuit = QFT(5)
circuit.add(gates.M(qubit) for qubit in range(2))

# print circuit with default options (default black & white style,
# scale factor of 0.6 and clustered gates)
plot_circuit(circuit)

# print the circuit with built-in style "garnacha", clustering gates
# and a custom scale factor
# built-in styles: "garnacha", "fardelejo", "quantumspain", "color-blind",
# "cachirulo" or custom dictionary
plot_circuit(circuit, scale = 0.8, cluster_gates = True, style="garnacha");

# plot the Qibo circuit with a custom style
custom_style = {
    "facecolor" : "#6497bf",
    "edgecolor" : "#01016f",
    "linecolor" : "#01016f",
    "textcolor" : "#01016f",
    "fillcolor" : "#ffb9b9",
    "gatecolor" : "#d8031c",
    "controlcolor" : "#360000"
}

plot_circuit(circuit, scale = 0.8, cluster_gates = True, style=custom_style);

State and result visualization#

qibo.ui.visualize_state(execution_outcome, mode='probabilities', n_most_relevant_components=None)[source]#

Plot circuit execution’s result data according to the chosen mode.

Parameters:
  • execution_outcome (QuantumState | MeasurementOutcomes | CircuitResult) –

    qibo circuit’s result. Depending on the simulation preferences, some of the visualizations can be accessed and some of them not. In particular:

    • if execution_outcome is a QuantumState, only probabilities and amplitudes can be visualized;

    • if execution_outcome is a MeasurementOutcomes, then all the mode options are available.

  • mode (str) – visualization mode can be “amplitudes”, “probabilities” or “frequencies”. Default is “probabilities”.

  • n_most_relevant_components (int) – in case the system is big (more than a few qubits), it can be helpful to reduce the number of ticks in the x-axis. To do so, this argument can be set, reducing the number of plotted ticks to n_most_relevant_components. Default is None.

qibo.ui.plot_density_hist(circuit, title='', alpha=0.5, colors=None, fig_width=16, fig_height=8, n_most_relevant_components=None, backend=None, **kwargs)[source]#

Plot the real and imaginary parts of the density matrix.

Given a qibo.models.circuit.Circuit, plot the real and imaginary parts of the final density matrix as separate 3D cityscape plots, side by side, and with a gray z=0 plane for the imaginary part.

Parameters:
  • circuit (qibo.models.circuit.Circuit) – Circuit to visualize.

  • title (str, optional) – Title of the plot. Defaults to "".

  • alpha (float, optional) – Transparency level for the bars in the plot. Defaults to \(0.5\).

  • colors (list, optional) – A list of two colors for the positive and negative parts of the density matrix. If None, default colors will be used. Defaults to None.

  • backend (qibo.backends.abstract.Backend, optional) – backend to be used in the execution. If None, it uses the current backend. Defaults to None.

  • fig_width (int, optional) – Width of the figure in inches. Defaults to 16.

  • fig_height (int, optional) – Height of the figure in inches. Defaults to 8.

  • n_most_relevant_components (int) – in case the system is big (more than a few qubits), it can be helpful to reduce the number of ticks in the x-axis. To do so, this argument can be set, reducing the number of plotted ticks to n_most_relevant_components. Default is None.

Returns:

Respectively, the figure, and axes for the real and the imaginary parts.

Return type:

tuple

Bloch sphere#

class qibo.ui.bloch.BlochSphere(style_text=<factory>, style=<factory>, _points=<factory>, _vectors=<factory>, _color_points=<factory>, _color_vectors=<factory>, _shown=False, _numpy_backend=None)[source]#

This class creates a Bloch sphere.

Parameters:
  • style_text (dict) –

  • style (dict) –

  • _points (list) –

  • _vectors (list) –

  • _color_points (list) –

  • _color_vectors (list) –

  • _shown (bool) –

  • _numpy_backend (Backend | None) –

add_vector(vector, mode='vector', color='black')[source]#

This function adds a vector to the sphere.

Parameters:
Return type:

None

add_state(state, mode='vector', color='black')[source]#

This function adds a state to the sphere.

Parameters:
Return type:

None

clear()[source]#

This function clears the sphere.

Return type:

None

render()[source]#

This function creates the empty sphere and plots the vectors and points on it.

Return type:

None