Skip to content

Commit

Permalink
Add hinton diagram to plot_state methods
Browse files Browse the repository at this point in the history
The interactive js plot_state visualizations had an additional method
'hinton' for drawing a hinton diagram of the quantum state. However
this method was missing from the non-js plot_state function. This commit
adds the missing method. THe hinton diagram implementation here is based
on the example from the matplotlib documentation, which can be found
here: https://matplotlib.org/gallery/specialty_plots/hinton_demo.html
  • Loading branch information
mtreinish committed Nov 9, 2018
1 parent 2aadd34 commit 079da0d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions qiskit/tools/visualization/_state_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ def draw(self, renderer):
FancyArrowPatch.draw(self, renderer)


def plot_hinton(rho, title='', filename=None):
"""Plot a hinton diagram for the quanum state.
Args:
rho (np.array[[complex]]): array of dimensions 2**n x 2**nn complex
numbers
title (str): a string that represents the plot title
filename (str): the output file to save the plot as. If specified it
will save and exit and not open up the plot in a new window.
"""
fig = plt.figure()
ax = fig.gca()
max_weight = 2 ** np.ceil(np.log(np.abs(rho).max()) / np.log(2))
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())

for (x, y), w in np.ndenumerate(rho):
color = 'white' if w > 0 else 'black'
size = np.sqrt(np.abs(w) / max_weight)
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)

ax.autoscale_view()
ax.invert_yaxis()
plt.title(title)
if filename:
plt.savefig(filename)
else:
plt.show()


def plot_bloch_vector(bloch, title="", filename=None):
"""Plot the Bloch sphere.
Expand Down Expand Up @@ -398,6 +432,8 @@ def plot_state(quantum_state, method='city', filename=None):
plot_bloch_vector(bloch_state, "qubit " + str(i), filename=filename)
elif method == "wigner":
plot_wigner_function(rho, filename=filename)
elif method == "hinton":
plot_hinton(rho, filename=filename)


###############################################################
Expand Down

0 comments on commit 079da0d

Please sign in to comment.