From a5402f5caa9975421a267184d0caff54882a0bf9 Mon Sep 17 00:00:00 2001 From: Josef Harte Date: Wed, 2 Oct 2024 09:11:50 +0100 Subject: [PATCH 1/6] feat: option to disable legend --- qiskit_addon_obp/utils/visualization.py | 41 +++++++++++++++++-------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/qiskit_addon_obp/utils/visualization.py b/qiskit_addon_obp/utils/visualization.py index 745891f..040b4ee 100644 --- a/qiskit_addon_obp/utils/visualization.py +++ b/qiskit_addon_obp/utils/visualization.py @@ -36,7 +36,7 @@ from .metadata import OBPMetadata -def plot_accumulated_error(metadata: OBPMetadata, axes: Axes) -> None: +def plot_accumulated_error(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: """Plot the accumulated error. This method populates the provided figure axes with a line-plot of the @@ -72,6 +72,7 @@ def plot_accumulated_error(metadata: OBPMetadata, axes: Axes) -> None: Args: metadata: the metadata to be visualized. axes: the matplotlib axes in which to plot. + show_legend: enable/disable showing the legend in the plot. """ if not np.isinf(metadata.truncation_error_budget.max_error_total): axes.axhline( @@ -93,10 +94,12 @@ def plot_accumulated_error(metadata: OBPMetadata, axes: Axes) -> None: ) axes.set_xlabel("backpropagated slice number") axes.set_ylabel("accumulated error") - axes.legend() + _set_legend(axes, show_legend) -def plot_left_over_error_budget(metadata: OBPMetadata, axes: Axes) -> None: +def plot_left_over_error_budget( + metadata: OBPMetadata, axes: Axes, show_legend: bool = True +) -> None: """Plot the left-over error budget. This method populates the provided figure axes with a line-plot of the @@ -127,6 +130,7 @@ def plot_left_over_error_budget(metadata: OBPMetadata, axes: Axes) -> None: Args: metadata: the metadata to be visualized. axes: the matplotlib axes in which to plot. + show_legend: enable/disable showing the legend in the plot. """ for obs_idx in range(len(metadata.backpropagation_history[0].slice_errors)): axes.plot( @@ -139,10 +143,10 @@ def plot_left_over_error_budget(metadata: OBPMetadata, axes: Axes) -> None: ) axes.set_xlabel("backpropagated slice number") axes.set_ylabel("left-over error budget") - axes.legend() + _set_legend(axes, show_legend) -def plot_slice_errors(metadata: OBPMetadata, axes: Axes) -> None: +def plot_slice_errors(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: """Plot the slice errors. This method populates the provided figure axes with a bar-plot of the truncation error incurred @@ -176,6 +180,7 @@ def plot_slice_errors(metadata: OBPMetadata, axes: Axes) -> None: Args: metadata: the metadata to be visualized. axes: the matplotlib axes in which to plot. + show_legend: enable/disable showing the legend in the plot. """ num_observables = len(metadata.backpropagation_history[0].slice_errors) width = 0.8 / num_observables @@ -193,9 +198,10 @@ def plot_slice_errors(metadata: OBPMetadata, axes: Axes) -> None: axes.set_xlabel("backpropagated slice number") axes.set_ylabel("incurred slice error") axes.legend() + _set_legend(axes, show_legend) -def plot_num_paulis(metadata: OBPMetadata, axes: Axes) -> None: +def plot_num_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: """Plot the number of Pauli terms. This method populates the provided figure axes with a line-plot of the number of Pauli terms at @@ -229,6 +235,7 @@ def plot_num_paulis(metadata: OBPMetadata, axes: Axes) -> None: Args: metadata: the metadata to be visualized. axes: the matplotlib axes in which to plot. + show_legend: enable/disable showing the legend in the plot. """ for obs_idx in range(len(metadata.backpropagation_history[0].slice_errors)): axes.plot( @@ -238,10 +245,10 @@ def plot_num_paulis(metadata: OBPMetadata, axes: Axes) -> None: ) axes.set_xlabel("backpropagated slice number") axes.set_ylabel("# Pauli terms") - axes.legend() + _set_legend(axes, show_legend) -def plot_num_truncated_paulis(metadata: OBPMetadata, axes: Axes) -> None: +def plot_num_truncated_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: """Plot the number of truncated Pauli terms. This method populates the provided figure axes with a bar-plot of the number of the truncated @@ -275,6 +282,7 @@ def plot_num_truncated_paulis(metadata: OBPMetadata, axes: Axes) -> None: Args: metadata: the metadata to be visualized. axes: the matplotlib axes in which to plot. + show_legend: enable/disable showing the legend in the plot. """ num_observables = len(metadata.backpropagation_history[0].slice_errors) width = 0.8 / num_observables @@ -291,10 +299,10 @@ def plot_num_truncated_paulis(metadata: OBPMetadata, axes: Axes) -> None: offset += width axes.set_xlabel("backpropagated slice number") axes.set_ylabel("# truncated Pauli terms") - axes.legend() + _set_legend(axes, show_legend) -def plot_sum_paulis(metadata: OBPMetadata, axes: Axes) -> None: +def plot_sum_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: """Plot the total number of all Pauli terms. This method populates the provided figure axes with a line-plot of the total number of all Pauli @@ -329,6 +337,7 @@ def plot_sum_paulis(metadata: OBPMetadata, axes: Axes) -> None: Args: metadata: the metadata to be visualized. axes: the matplotlib axes in which to plot. + show_legend: enable/disable showing the legend in the plot. """ if metadata.operator_budget.max_paulis is not None: axes.axhline( @@ -346,10 +355,10 @@ def plot_sum_paulis(metadata: OBPMetadata, axes: Axes) -> None: ) axes.set_xlabel("backpropagated slice number") axes.set_ylabel("total # of Pauli terms") - axes.legend() + _set_legend(axes, show_legend) -def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes) -> None: +def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: """Plot the number of qubit-wise commuting Pauli groups. This method populates the provided figure axes with a line-plot of the number of qubit-wise @@ -380,6 +389,7 @@ def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes) -> None: Args: metadata: the metadata to be visualized. axes: the matplotlib axes in which to plot. + show_legend: enable/disable showing the legend in the plot. """ if metadata.operator_budget.max_qwc_groups is not None: axes.axhline( @@ -397,4 +407,9 @@ def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes) -> None: ) axes.set_xlabel("backpropagated slice number") axes.set_ylabel("# of qubit-wise commuting Pauli groups") - axes.legend() + _set_legend(axes, show_legend) + + +def _set_legend(axes: Axes, show_legend: bool) -> None: + if show_legend: + axes.legend() From 6a3a158d26253682cea6fc7a0bf445d6b9a6bd3d Mon Sep 17 00:00:00 2001 From: "J. Harte" Date: Mon, 21 Oct 2024 09:46:29 +0100 Subject: [PATCH 2/6] refactor: ensure param is keyword-only --- qiskit_addon_obp/utils/visualization.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/qiskit_addon_obp/utils/visualization.py b/qiskit_addon_obp/utils/visualization.py index 040b4ee..b4e14fc 100644 --- a/qiskit_addon_obp/utils/visualization.py +++ b/qiskit_addon_obp/utils/visualization.py @@ -36,7 +36,7 @@ from .metadata import OBPMetadata -def plot_accumulated_error(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: +def plot_accumulated_error(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = True) -> None: """Plot the accumulated error. This method populates the provided figure axes with a line-plot of the @@ -98,7 +98,7 @@ def plot_accumulated_error(metadata: OBPMetadata, axes: Axes, show_legend: bool def plot_left_over_error_budget( - metadata: OBPMetadata, axes: Axes, show_legend: bool = True + metadata: OBPMetadata, axes: Axes, *, show_legend: bool = True ) -> None: """Plot the left-over error budget. @@ -146,7 +146,7 @@ def plot_left_over_error_budget( _set_legend(axes, show_legend) -def plot_slice_errors(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: +def plot_slice_errors(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = True) -> None: """Plot the slice errors. This method populates the provided figure axes with a bar-plot of the truncation error incurred @@ -201,7 +201,7 @@ def plot_slice_errors(metadata: OBPMetadata, axes: Axes, show_legend: bool = Tru _set_legend(axes, show_legend) -def plot_num_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: +def plot_num_paulis(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = True) -> None: """Plot the number of Pauli terms. This method populates the provided figure axes with a line-plot of the number of Pauli terms at @@ -248,7 +248,9 @@ def plot_num_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) _set_legend(axes, show_legend) -def plot_num_truncated_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: +def plot_num_truncated_paulis( + metadata: OBPMetadata, axes: Axes, *, show_legend: bool = True +) -> None: """Plot the number of truncated Pauli terms. This method populates the provided figure axes with a bar-plot of the number of the truncated @@ -302,7 +304,7 @@ def plot_num_truncated_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bo _set_legend(axes, show_legend) -def plot_sum_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: +def plot_sum_paulis(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = True) -> None: """Plot the total number of all Pauli terms. This method populates the provided figure axes with a line-plot of the total number of all Pauli @@ -358,7 +360,7 @@ def plot_sum_paulis(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) _set_legend(axes, show_legend) -def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes, show_legend: bool = True) -> None: +def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = True) -> None: """Plot the number of qubit-wise commuting Pauli groups. This method populates the provided figure axes with a line-plot of the number of qubit-wise From f4b4e68410ad928cb4649dc74c1e03fd18868ee1 Mon Sep 17 00:00:00 2001 From: "J. Harte" <13206585+boonware@users.noreply.github.com> Date: Mon, 21 Oct 2024 09:49:11 +0100 Subject: [PATCH 3/6] docs: add release note --- .../notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml diff --git a/releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml b/releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml new file mode 100644 index 0000000..a231068 --- /dev/null +++ b/releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + A new parameter `show_legend` has been added to each function in the utils.visualization module that can + show or hide the legend on a plot. The legend is shown by default. This can be useful when + the legend becomes long and obstructs the plot. From fa7f5536409d5dbfe1685968020005ce53bde17f Mon Sep 17 00:00:00 2001 From: Max Rossmannek Date: Tue, 5 Nov 2024 10:05:48 +0100 Subject: [PATCH 4/6] docs: fix RST formatting --- .../notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml b/releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml index a231068..06fda30 100644 --- a/releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml +++ b/releasenotes/notes/add-param-to-hide-legend-d62ff17eef5e80f4.yaml @@ -1,6 +1,7 @@ --- features: - | - A new parameter `show_legend` has been added to each function in the utils.visualization module that can - show or hide the legend on a plot. The legend is shown by default. This can be useful when - the legend becomes long and obstructs the plot. + A new parameter ``show_legend`` has been added to each function in the + :mod:`qiskit_addon_obp.utils.visualization` module that can show or hide the + legend on a plot. The legend is shown by default. This can be useful when + the legend becomes long and obstructs the plot. From a61b255786a45a50c915ef8db2688cc7e9f2f940 Mon Sep 17 00:00:00 2001 From: Max Rossmannek Date: Tue, 5 Nov 2024 10:15:39 +0100 Subject: [PATCH 5/6] docs: close previous figures --- qiskit_addon_obp/utils/visualization.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/qiskit_addon_obp/utils/visualization.py b/qiskit_addon_obp/utils/visualization.py index c698152..72cba43 100644 --- a/qiskit_addon_obp/utils/visualization.py +++ b/qiskit_addon_obp/utils/visualization.py @@ -36,7 +36,7 @@ def plot_accumulated_error(metadata: OBPMetadata, axes: Axes, *, show_legend: bo >>> metadata = OBPMetadata.from_json("docs/_static/dummy_visualization_metadata.json") .. plot:: - :context: + :context: close-figs :include-source: >>> from matplotlib import pyplot as plt @@ -99,7 +99,7 @@ def plot_left_over_error_budget( >>> metadata = OBPMetadata.from_json("docs/_static/dummy_visualization_metadata.json") .. plot:: - :context: + :context: close-figs :include-source: >>> from matplotlib import pyplot as plt @@ -146,7 +146,7 @@ def plot_slice_errors(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = >>> metadata = OBPMetadata.from_json("docs/_static/dummy_visualization_metadata.json") .. plot:: - :context: + :context: close-figs :include-source: >>> from matplotlib import pyplot as plt @@ -201,7 +201,7 @@ def plot_num_paulis(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = Tr >>> metadata = OBPMetadata.from_json("docs/_static/dummy_visualization_metadata.json") .. plot:: - :context: + :context: close-figs :include-source: >>> from matplotlib import pyplot as plt @@ -250,7 +250,7 @@ def plot_num_truncated_paulis( >>> metadata = OBPMetadata.from_json("docs/_static/dummy_visualization_metadata.json") .. plot:: - :context: + :context: close-figs :include-source: >>> from matplotlib import pyplot as plt @@ -304,7 +304,7 @@ def plot_sum_paulis(metadata: OBPMetadata, axes: Axes, *, show_legend: bool = Tr >>> metadata = OBPMetadata.from_json("docs/_static/dummy_visualization_metadata.json") .. plot:: - :context: + :context: close-figs :include-source: >>> from matplotlib import pyplot as plt @@ -360,7 +360,7 @@ def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes, *, show_legend: bool >>> metadata = OBPMetadata.from_json("docs/_static/dummy_visualization_metadata.json") .. plot:: - :context: + :context: close-figs :include-source: >>> from matplotlib import pyplot as plt From 14b7ac03d692ba17a74e49bdb20f6cafe2e2a535 Mon Sep 17 00:00:00 2001 From: Max Rossmannek Date: Tue, 5 Nov 2024 10:16:14 +0100 Subject: [PATCH 6/6] fix: suppress missing coverage --- qiskit_addon_obp/utils/visualization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_addon_obp/utils/visualization.py b/qiskit_addon_obp/utils/visualization.py index 72cba43..06c9a9e 100644 --- a/qiskit_addon_obp/utils/visualization.py +++ b/qiskit_addon_obp/utils/visualization.py @@ -398,5 +398,5 @@ def plot_num_qwc_groups(metadata: OBPMetadata, axes: Axes, *, show_legend: bool def _set_legend(axes: Axes, show_legend: bool) -> None: - if show_legend: + if show_legend: # pragma: no cover axes.legend()