Skip to content

Add helper to plot counts from results - #452

Merged
sgrava merged 35 commits into
mainfrom
as/451-add-plot-histogram-function
Sep 10, 2026
Merged

sgrava merged 35 commits into
mainfrom
as/451-add-plot-histogram-function

Conversation

@alessandro-santini

@alessandro-santini alessandro-santini commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

Add helper for plotting measurement counts and distributions

Summary

This PR adds plot_bitstrings helper for visualizing measurement results as either a count histogram

Features

  • Plot measurement outcomes ordered by decreasing frequency.
  • Optionally normalize counts into probabilities normalize=True.
  • Optionally display only the top N results.
  • Support plotting on an already existing Matplotlib axis.
  • Allow some customization of the plot title and axis labels
  • Allow customization of default bar color.
  • Support highlighting a set of bistrings with a different color

Example

from qoolqit.visualization import plot_bitstrings
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)

qubits = 4
counts_1 = {}
counts_2 = {}
for i in range(2**qubits):
    counts_1[np.binary_repr(i, width=qubits)] = np.random.randint(0, 10)
    counts_2[np.binary_repr(i, width=qubits)] = np.random.randint(0, 10)
bitstring_max, _ = max(counts_1.items(), key=lambda item: item[1])

fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(12, 4))
plot_bitstrings(counts_1, top=10, ax=ax[0],normalize=True, highlight={bitstring_max:  "#00C887"}, label="barplot")
ax[0].set_title("Distribution 1 (normalized)")
ax[0].set_xlabel("top 10 bitstrings")
ax[0].legend()

plot_bitstrings(counts_1, ax=ax[1], top=6, color="C2", highlight={bitstring_max:"C1"})
ax[1].set_title("Histogram 1 (top 6)")

plot_bitstrings(counts_2, ax=ax[2], highlight={"0010": "tab:orange", "1000": "tab:cyan", "0101": "tab:red"}, top=6)
ax[2].set_title("Histogram 2")
ax[2].legend()

fig.tight_layout()

Output:

image

@alessandro-santini alessandro-santini self-assigned this Aug 14, 2026
@alessandro-santini alessandro-santini linked an issue Aug 14, 2026 that may be closed by this pull request
4 tasks
@github-actions

github-actions Bot commented Aug 14, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-09-10 15:12 UTC

@alessandro-santini
alessandro-santini marked this pull request as ready for review August 14, 2026 10:48
Comment thread qoolqit/utils/__init__.py Outdated
Comment thread qoolqit/utils/visualization.py Outdated
Comment thread qoolqit/utils/visualization.py Outdated
Comment thread qoolqit/utils/visualization.py Outdated
Comment thread qoolqit/utils/visualization.py Outdated
Comment thread qoolqit/utils/visualization.py Outdated
Comment thread qoolqit/visualization.py Outdated
Comment thread qoolqit/utils/visualization.py Outdated
Comment thread qoolqit/visualization.py Outdated
Comment thread qoolqit/utils/visualization.py Outdated
Comment thread qoolqit/visualization.py Outdated
Comment thread qoolqit/visualization.py Outdated
Comment thread qoolqit/visualization.py Outdated
Comment thread qoolqit/visualization.py Outdated
alessandro-santini and others added 3 commits August 14, 2026 15:59
Co-authored-by: Stefano Grava <42062939+sgrava@users.noreply.github.com>
Comment thread qoolqit/visualization.py Outdated
Comment thread qoolqit/visualization.py
Comment thread qoolqit/visualization.py Outdated
alessandro-santini and others added 6 commits August 14, 2026 18:04
Co-authored-by: Stefano Grava <42062939+sgrava@users.noreply.github.com>
Co-authored-by: Stefano Grava <42062939+sgrava@users.noreply.github.com>
Co-authored-by: Stefano Grava <42062939+sgrava@users.noreply.github.com>
@sgrava sgrava added this to the v1.4.1 milestone Sep 8, 2026

@sgrava sgrava left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few minors! We are nearly there

Comment thread qoolqit/visualization.py
Comment thread qoolqit/visualization.py Outdated

@sgrava sgrava left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@sgrava
sgrava merged commit 91495b1 into main Sep 10, 2026
16 checks passed
@sgrava
sgrava deleted the as/451-add-plot-histogram-function branch September 10, 2026 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add plot_histogram function

2 participants