Matplotlib viewer#
Matplotlib Bloch spheres: static figures, animations, GIF and MP4 export, an entanglement timeline and the Q-sphere.
Examples
>>> from qiskit import QuantumCircuit
>>> from quantum_mind.viz import circuit_trajectory, animate_trajectory, plot_entanglement
>>> qc = QuantumCircuit(2); qc.h(0); qc.cx(0, 1)
>>> traj = circuit_trajectory(qc, steps=15)
>>> animate_trajectory(traj, save='bell.gif', rotate=0.5)
>>> plot_entanglement(traj).savefig('bell_timeline.png')
Classes
One Bloch sphere on a Matplotlib 3D axis. |
Functions
Static figure with one sphere per qubit. |
|
Animate a trajectory, one sphere per qubit. |
|
Entanglement timeline of a circuit trajectory. |
|
Q-sphere of a multi-qubit state, drawn by Qiskit's |
- class BlochSphere(ax=None, title='', theme='dark', figsize=(5, 5), elev=20, azim=35)[source]#
Bases:
objectOne Bloch sphere on a Matplotlib 3D axis.
- Parameters:
ax (
mpl_toolkits.mplot3d.Axes3D, optional) – Axis to draw on (a new figure by default).title (
str, optional) – Caption under the sphere.theme (
strordict, optional) –'dark'(default),'light'or a custom theme.figsize (
tuple, optional) – Figure size for a new figure.elev (
float, optional) – Camera angles in degrees.azim (
float, optional) – Camera angles in degrees.
Examples
>>> b = BlochSphere(title='q0') >>> b.add_vector([0, 1, 0]) >>> b.save('sphere.png')
- add_state(state, **kw)[source]#
Arrow for a qubit state.
- Parameters:
state (
array_likeorQiskit state) – 2-vector, 2 x 2 density matrix,StatevectororDensityMatrix.**kw – Passed to
add_vector().
- Returns:
- plot_bloch(vectors, names=None, theme='dark', title=None, size=3.6)[source]#
Static figure with one sphere per qubit.
- Parameters:
- Returns:
figure (
matplotlib.figure.Figure)spheres (
listofBlochSphere)
- animate_trajectory(traj, theme='dark', interval=40, trail=True, save=None, fps=25, size=3.6, rotate=0.0, title=None, dpi=90)[source]#
Animate a trajectory, one sphere per qubit.
- Parameters:
traj (
Trajectoryorarray_like) – Trajectory, or Bloch vectors(frames, qubits, 3).interval (
int, optional) – Milliseconds between frames on screen.trail (
bool, optional) – Draw the path followed so far.save (
str, optional) – Path ending in.gif(Pillow) or.mp4(ffmpeg).fps (
int, optional) – Frames per second of the saved file.size (
float, optional) – Size per sphere in inches.rotate (
float, optional) – Camera rotation per frame, in degrees.title (
str, optional)dpi (
int, optional) – Resolution of the saved file.
- Returns:
matplotlib.animation.FuncAnimation
- plot_entanglement(traj, pairs=None, theme='dark', size=(8, 3.2))[source]#
Entanglement timeline of a circuit trajectory.
Plots the Bloch-vector length of every qubit (1 = not entangled with the others, for a pure global state) and the concurrence of qubit pairs (0 = separable, 1 = maximally entangled), with the gate labels on the x axis.
- plot_qsphere(state, **kw)[source]#
Q-sphere of a multi-qubit state, drawn by Qiskit’s
plot_state_qsphere.Basis states are placed by Hamming weight, with amplitude as size and phase as colour.
- Parameters:
state (
array_likeorqiskit.quantum_info.Statevector)**kw – Passed to
plot_state_qsphere.
- Returns:
matplotlib.figure.Figure