Interactive viewer#

Interactive Plotly Bloch spheres: rotate and zoom with the mouse, play animations with a slider, export to a standalone HTML file, and update live in Jupyter (LiveBloch).

Examples

>>> from qiskit import QuantumCircuit
>>> from quantum_mind.viz import circuit_trajectory, animate_bloch, save_html, LiveBloch
>>> qc = QuantumCircuit(2); qc.h(0); qc.cx(0, 1)
>>> save_html(animate_bloch(circuit_trajectory(qc)), 'bell.html')
>>> live = LiveBloch(2).show()
>>> live.play(circuit_trajectory(qc), fps=30)

Classes

LiveBloch

Real-time Bloch spheres.

Functions

bloch_figure

Interactive figure with one sphere per qubit.

animate_bloch

Animated interactive figure with play and pause buttons and a slider labelled with the frame labels.

save_html

Write a standalone HTML file (Plotly JavaScript loaded from its CDN).

class LiveBloch(n_qubits=1, names=None, theme='dark', backend='auto', trail=True, title=None)[source]#

Bases: object

Real-time Bloch spheres.

In Jupyter (with ipywidgets and anywidget) the figure is a Plotly FigureWidget updated in place; elsewhere a Matplotlib window is redrawn. Feed it states, Bloch vectors or whole trajectories.

Parameters:
  • n_qubits (int, optional)

  • names (list of str, optional)

  • theme (str or dict, optional)

  • backend ({'auto', 'plotly', 'matplotlib'}, optional) – 'auto' picks Plotly inside a notebook and Matplotlib elsewhere.

  • trail (bool, optional) – Keep and draw the history of every vector.

  • title (str, optional)

Examples

>>> live = LiveBloch(2).show()
>>> for state in states:
...     live.update(state)
>>> live.play(circuit_trajectory(qc), fps=30)
show()[source]#

Display the spheres (a widget in Jupyter, a window elsewhere).

Returns:

LiveBloch – self.

update(state, label='')[source]#

Move the vectors to a new state.

Parameters:
  • state (array_like or Qiskit state) – Bloch vectors (n, 3), or a state vector, density matrix or Qiskit state.

  • label (str, optional) – Caption.

Returns:

LiveBloch – self.

play(traj, fps=30)[source]#

Replay a trajectory in real time.

Parameters:
  • traj (Trajectory or array_like)

  • fps (int, optional)

Returns:

LiveBloch – self.

reset()[source]#

Clear the trails.

Returns:

LiveBloch – self.

bloch_figure(vectors, names=None, theme='dark', title=None, height=460)[source]#

Interactive figure with one sphere per qubit.

Parameters:
  • vectors (array_like or state) – Bloch vectors (qubits, 3), or a state.

  • names (list of str, optional)

  • theme (str or dict, optional)

  • title (str, optional)

  • height (int, optional) – Height in pixels.

Returns:

plotly.graph_objects.Figure

animate_bloch(traj, theme='dark', fps=30, trail=True, title=None, height=500, max_frames=400)[source]#

Animated interactive figure with play and pause buttons and a slider labelled with the frame labels.

Parameters:
  • traj (Trajectory or array_like)

  • theme (str or dict, optional)

  • fps (int, optional) – Frames per second when playing.

  • trail (bool, optional) – Draw the path followed so far.

  • title (str, optional)

  • height (int, optional)

  • max_frames (int, optional) – Frames are subsampled to at most this many (keeps HTML files small).

Returns:

plotly.graph_objects.Figure

save_html(fig, path, auto_open=False)[source]#

Write a standalone HTML file (Plotly JavaScript loaded from its CDN).

Parameters:
  • fig (plotly.graph_objects.Figure)

  • path (str)

  • auto_open (bool, optional) – Open the file in a browser.

Returns:

str – path.