2
$\begingroup$

Qiskit seems to use matplotlib for rendering bloch spheres under the hood. Therefore, it would be nice if we could also make use of matplotlib's subplot technique.

I would like to implement subplots, each containing a bloch sphere (with different vectors), like the plot matrix in this example. I would be very grateful for a short help.

The result should look something like this at the end of the day:

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ Qiskit's visualization methods like plot_bloch_vector return a matplotlib figure instance. For example: fig = plt.figure(plot_bloch_vector([0,1,0])). Did you try to plot multiple figures like this? $\endgroup$ Jun 20 at 10:27
  • $\begingroup$ It sounds to be a good idea. I didn't tired that. $\endgroup$ Jun 20 at 10:37

1 Answer 1

4
$\begingroup$

Function plot_bloch_vector accepts matplotlib.axes.Axes to use for rendering the bloch sphere. So, you can create a figure, add a new Axes for each bloch sphere, then pass it to plot_bloch_vector:

from qiskit.visualization import plot_bloch_vector
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize = [6, 9])

states = [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
    [0.6, 0.8, 0],
    [0.6, 0, 0.8],
    [0, 0.6, 0.8],
]

# Values are in fractions of figure width and height:
positions = [
    [0, 0],
    [0.5, 0],
    [0, 0.333],
    [0.5, 0.333],
    [0, 0.667],
    [0.5, 0.667],
]

for m in range(len(states)):
    ax = fig.add_axes([positions[m][0], positions[m][1], 0.5, 0.333], axes_class = Axes3D)
    plot_bloch_vector(states[m], ax = ax)

The result:

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ This is exactly what I was looking for. It even allows me to use the well-known axes methods from matplotlib. Thank you! $\endgroup$ Jun 20 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.