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: Circuit, scale: float = 0.6, cluster_gates: bool = True, fold: int = -1, style: dict | str | None = None) → tuple[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: QuantumState | MeasurementOutcomes | CircuitResult, mode: str = 'probabilities', n_most_relevant_components: int | None = None)[source]#

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

Parameters:
  • execution_outcome –

    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 – 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: Circuit, title: str = '', alpha: float = 0.5, colors: list[str] | None = None, fig_width: int = 16, fig_height: int = 8, n_most_relevant_components: int | None = None, backend: Backend | None = 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: dict = <factory>, style: dict = <factory>, _points: list = <factory>, _vectors: list = <factory>, _color_points: list = <factory>, _color_vectors: list = <factory>, _shown: bool = False, _numpy_backend: ~qibo.backends.abstract.Backend | None = None)[source]#

This class creates a Bloch sphere.

add_vector(vector: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], mode: str | list[str] = 'vector', color: str | list[str] = 'black') → None[source]#

This function adds a vector to the sphere.

add_state(state: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], mode: str | list[str] = 'vector', color: str | list[str] = 'black') → None[source]#

This function adds a state to the sphere.

clear() → None[source]#

This function clears the sphere.

render() → None[source]#

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