Skip to content

plot_histogram fails to operate correctly when numerous starting and ending values are zero #11949

@sanketsharma

Description

@sanketsharma

Environment

  • Qiskit version: 1.0.1
  • Python version: 3.12.2
  • Operating system: Red Hat Enterprise Linux release 8.7 (Ootpa)

What is happening?

In the code below, I am trying to plot the probabilities of a particle being at positions given by the dictionary indices. plot_histogram isn't working the way it should in case of the first dictionary d while it works correctly in case of the second dictionary d1. These values are generated by a quantum circuit. In the first case the plot is empty while in the second case, the plot shows a histogram. Also, I get the following warning on the first call to plot_histogram.

/nfs/home/ssharm19/.conda/envs/research/lib/python3.12/site-packages/qiskit/visualization/counts_visualization.py:389: UserWarning: Attempting to set identical low and high ylims makes transformation singular; automatically expanding.
  ax.set_ylim([min_ylim, min([1.1 * sum(all_vals), max(1.1 * val for val in all_vals)])])

Figure_1
Figure_2

How can we reproduce the issue?

from qiskit.visualization import plot_histogram
import matplotlib.pyplot as plt
import numpy as np

d = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0.00324, 9: 0.03881, 10: 0.17425, 11: 0.28411, 12: 0.28588, 13: 0.17294, 14: 0.03769, 15: 0.00308, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0}
assert sum(d.values()) == 1, "Probabilities aren't normalized"
d1 = {k -8: v for k,v in d.items() if v !=0 }
assert sum(d1.values()) == 1, "Probabilities aren't normalized"
plot_histogram(d)
plot_histogram(d1)
plt.show()

What should happen?

I should be able to see histograms in both cases.

Any suggestions?

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Assigned

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions