diff --git a/qiskit/visualization/state_visualization.py b/qiskit/visualization/state_visualization.py index a63160eafec9..e0a7c824d344 100644 --- a/qiskit/visualization/state_visualization.py +++ b/qiskit/visualization/state_visualization.py @@ -24,6 +24,7 @@ import numpy as np from scipy import linalg from qiskit.quantum_info.operators.pauli import pauli_group, Pauli +from qiskit.circuit.tools.pi_check import pi_check from .matplotlib import HAS_MATPLOTLIB if HAS_MATPLOTLIB: @@ -630,7 +631,8 @@ def phase_to_rgb(complex_number): return rgb -def plot_state_qsphere(rho, figsize=None, ax=None): +def plot_state_qsphere(rho, figsize=None, ax=None, show_state_labels=True, + show_state_phases=False, use_degrees=False): """Plot the qsphere representation of a quantum state. Here, the size of the points is proportional to the probability of the corresponding term in the state and the color represents @@ -644,6 +646,12 @@ def plot_state_qsphere(rho, figsize=None, ax=None): the visualization output. If none is specified a new matplotlib Figure will be created and used. Additionally, if specified there will be no returned Figure since it is redundant. + show_state_labels (bool): An optional boolean indicating whether to + show labels for each basis state. + show_state_phases (bool): An optional boolean indicating whether to + show the phase for each basis state. + use_degrees (bool): An optional boolean indicating whether to use + radians or degrees for the phase values in the plot. Returns: Figure: A matplotlib figure instance if the ``ax`` kwag is not set @@ -774,6 +782,23 @@ def plot_state_qsphere(rho, figsize=None, ax=None): if yvalue >= 0.1: alfa = 1.0 - yvalue + if prob > 0 and show_state_labels: + rprime = 1.3 + angle_theta = np.arctan2(np.sqrt(1 - zvalue ** 2), zvalue) + xvalue_text = rprime * np.sin(angle_theta) * np.cos(angle) + yvalue_text = rprime * np.sin(angle_theta) * np.sin(angle) + zvalue_text = rprime * np.cos(angle_theta) + element_text = '$\\vert' + element + '\\rangle$' + if show_state_phases: + element_angle = (np.angle(state[i]) + (np.pi * 4)) % (np.pi * 2) + if use_degrees: + element_text += '\n$%.1f^\\circ$' % (element_angle * 180/np.pi) + else: + element_angle = pi_check(element_angle, ndigits=3).replace('pi', '\\pi') + element_text += '\n$%s$' % (element_angle) + ax.text(xvalue_text, yvalue_text, zvalue_text, element_text, + ha='center', va='center', size=12) + ax.plot([xvalue], [yvalue], [zvalue], markerfacecolor=colorstate, markeredgecolor=colorstate, @@ -807,20 +832,22 @@ def plot_state_qsphere(rho, figsize=None, ax=None): ax2 = fig.add_subplot(gs[2:, 2:]) ax2.pie(theta, colors=sns.color_palette("hls", n), radius=0.75) ax2.add_artist(Circle((0, 0), 0.5, color='white', zorder=1)) - ax2.text(0, 0, 'Phase', horizontalalignment='center', - verticalalignment='center', fontsize=14) - offset = 0.95 # since radius of sphere is one. - ax2.text(offset, 0, r'$0$', horizontalalignment='center', + if use_degrees: + labels = ['Phase\n(Deg)', '0', '90', '180 ', '270'] + else: + labels = ['Phase', '$0$', '$\\pi/2$', '$\\pi$', '$3\\pi/2$'] + + ax2.text(0, 0, labels[0], horizontalalignment='center', verticalalignment='center', fontsize=14) - ax2.text(0, offset, r'$\pi/2$', horizontalalignment='center', + ax2.text(offset, 0, labels[1], horizontalalignment='center', verticalalignment='center', fontsize=14) - - ax2.text(-offset, 0, r'$\pi$', horizontalalignment='center', + ax2.text(0, offset, labels[2], horizontalalignment='center', verticalalignment='center', fontsize=14) - - ax2.text(0, -offset, r'$3\pi/2$', horizontalalignment='center', + ax2.text(-offset, 0, labels[3], horizontalalignment='center', + verticalalignment='center', fontsize=14) + ax2.text(0, -offset, labels[4], horizontalalignment='center', verticalalignment='center', fontsize=14) if return_fig: diff --git a/releasenotes/notes/qsphere-state-display-819cc1e3e61314a2.yaml b/releasenotes/notes/qsphere-state-display-819cc1e3e61314a2.yaml new file mode 100644 index 000000000000..98d3d9ab4f69 --- /dev/null +++ b/releasenotes/notes/qsphere-state-display-819cc1e3e61314a2.yaml @@ -0,0 +1,13 @@ +--- +features: + - | + Introduced an update to ``plot_state_qsphere`` that shows the basis states next + to each blob by default. This feature can be disabled using the ``show_state_labels`` + argument. + - | + Introduced an argument to ``plot_state_qsphere`` called ``show_state_phases`` which is + set to ``False`` by default. When enabled, it displays the phase of each basis state. + - | + Introduced an argument to ``plot_state_qsphere`` called ``use_degrees`` which is + set to ``False`` by default. When enabled, it displays the phase of each basis state + in degrees, along with the phase circle at the bottom right.