From 2a71070918e08ba5ae28bd4adbb64cd7c2103c86 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 8 Oct 2024 16:13:55 +0530 Subject: [PATCH 1/4] adding ignore_under feature in plot_histogram --- qiskit/visualization/counts_visualization.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/qiskit/visualization/counts_visualization.py b/qiskit/visualization/counts_visualization.py index 6d93ff6bfac0..85fe719ab072 100644 --- a/qiskit/visualization/counts_visualization.py +++ b/qiskit/visualization/counts_visualization.py @@ -61,6 +61,7 @@ def plot_histogram( figsize=None, color=None, number_to_keep=None, + ignore_under=None, sort="asc", target_string=None, legend=None, @@ -137,6 +138,8 @@ def plot_histogram( # one bitstring to the other) from a target string. hist2 = plot_histogram(counts, sort='hamming', target_string='001') """ + if ignore_under is not None: + data = {key: count for key, count in data.items() if count > (ignore_under-1)} if not isinstance(data, list): data = [data] From 50ec6082fefec1f4ce91430119e577cca8465766 Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 8 Oct 2024 16:31:12 +0530 Subject: [PATCH 2/4] adding release notes --- .../notes/ignore_under_parameter-e06eab711a713571.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml diff --git a/releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml b/releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml new file mode 100644 index 000000000000..433d2eff350e --- /dev/null +++ b/releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml @@ -0,0 +1,7 @@ +--- +features_visualization: + - | + New parameter added to the function plot_histogram. Paramerer is named `ignore_under`. + The introduction of this parameter ignores the values in the data dictionary values + strictly lower than `ignore_under`. This will solve the sparsity problem faced by the + function when the data contains a lot of small values returning an unreadable histogram. From 359413ecccce78a2ba951c316b41e9beac0e479c Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 8 Oct 2024 16:34:43 +0530 Subject: [PATCH 3/4] update release notes --- .../notes/ignore_under_parameter-e06eab711a713571.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml b/releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml index 433d2eff350e..a06ed996eef0 100644 --- a/releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml +++ b/releasenotes/notes/ignore_under_parameter-e06eab711a713571.yaml @@ -5,3 +5,10 @@ features_visualization: The introduction of this parameter ignores the values in the data dictionary values strictly lower than `ignore_under`. This will solve the sparsity problem faced by the function when the data contains a lot of small values returning an unreadable histogram. + + +fixes: + - | + Fixes sparsity problem wit the histograms ``foo()``. Refer to + `#13066 ` for more + details. \ No newline at end of file From 796b7207333e2df1498724fd9b8a76bae1d05bfc Mon Sep 17 00:00:00 2001 From: Shivansh Mittal Date: Tue, 8 Oct 2024 17:35:59 +0530 Subject: [PATCH 4/4] adding docstring for `ignore_under` --- qiskit/visualization/counts_visualization.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/qiskit/visualization/counts_visualization.py b/qiskit/visualization/counts_visualization.py index 85fe719ab072..602c939125ea 100644 --- a/qiskit/visualization/counts_visualization.py +++ b/qiskit/visualization/counts_visualization.py @@ -83,6 +83,8 @@ def plot_histogram( applies to each dataset individually, which may result in more bars than ``number_to_keep + 1``. The ``number_to_keep`` applies to the total values, rather than the x-axis sort. + ignore_under (int): The count of values below which to ignore the bitstrings. It will return + only those bitstrings which have count greater or equal to ``ignore_under``. sort (string): Could be `'asc'`, `'desc'`, `'hamming'`, `'value'`, or `'value_desc'`. If set to `'value'` or `'value_desc'` the x axis will be sorted by the number of counts for each bitstring. @@ -139,7 +141,7 @@ def plot_histogram( hist2 = plot_histogram(counts, sort='hamming', target_string='001') """ if ignore_under is not None: - data = {key: count for key, count in data.items() if count > (ignore_under-1)} + data = {key: count for key, count in data.items() if count > (ignore_under - 1)} if not isinstance(data, list): data = [data]