Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new argument to plot_state_qsphere (update to #5607) #6596

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
28 changes: 28 additions & 0 deletions qiskit/visualization/state_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ def plot_state_qsphere(
figsize=None,
ax=None,
show_state_labels=True,
show_all_state_labels=False,
show_state_phases=False,
use_degrees=False,
*,
Expand All @@ -824,6 +825,10 @@ def plot_state_qsphere(
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_all_state_labels (bool): An optional boolean indicating whether
to show labels for all possible basis states. This defaults false, as with
larger qubit counts (eg. >3) the number of state labels can clutter
the output.
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
Expand Down Expand Up @@ -1003,6 +1008,29 @@ def plot_state_qsphere(
va="center",
size=12,
)
elif show_all_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],
Expand Down