Skip to content

Commit dc672d5

Browse files
authored
Fixes to various graphs in the docs (#14055)
* Fixes to various graphs in the docs * Fix a bug caused by `tight_layout` crashing on some images * Apply the fix to the timeline plotter * Remove release note and fix bloch sphere visualization * Additional bloch fix * Slighly relax image compairson for bloch sphere due to small discrepancies between the server and local versions.
1 parent a88ed60 commit dc672d5

8 files changed

Lines changed: 28 additions & 7 deletions

File tree

qiskit/circuit/library/graph_state.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class GraphState(QuantumCircuit):
4848
import rustworkx as rx
4949
G = rx.generators.cycle_graph(5)
5050
circuit = GraphState(rx.adjacency_matrix(G))
51+
circuit.name = "Graph state"
5152
_generate_circuit_library_visualization(circuit)
5253
5354
**References:**

qiskit/visualization/counts_visualization.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ def _plotting_core(
408408
if fig:
409409
matplotlib_close_if_inline(fig)
410410
if filename is None:
411+
try:
412+
fig.tight_layout()
413+
except AttributeError:
414+
pass
411415
return fig
412416
else:
413417
return fig.savefig(filename)

qiskit/visualization/library.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ def _generate_circuit_library_visualization(circuit: QuantumCircuit):
2323
import matplotlib.pyplot as plt
2424

2525
circuit = circuit.decompose()
26+
global_phase, circuit.global_phase = circuit.global_phase, 0
2627
ops = circuit.count_ops()
2728
num_nl = circuit.num_nonlocal_gates()
28-
_fig, (ax0, ax1) = plt.subplots(2, 1)
29+
_fig, (ax0, ax1) = plt.subplots(2, 1, figsize=(6.4, 9.6))
2930
circuit.draw("mpl", ax=ax0)
31+
circuit.global_phase = global_phase
3032
ax1.axis("off")
3133
ax1.grid(visible=None)
3234
ax1.table(
3335
[[circuit.name], [circuit.width()], [circuit.depth()], [sum(ops.values())], [num_nl]],
3436
rowLabels=["Circuit Name", "Width", "Depth", "Total Gates", "Non-local Gates"],
37+
loc="top",
3538
)
3639
plt.tight_layout()
3740
plt.show()

qiskit/visualization/state_visualization.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ def plot_bloch_multivector(
281281
reverse_bits (bool): If True, plots qubits following Qiskit's convention [Default:False].
282282
font_size (float): Font size for the Bloch ball figures.
283283
title_font_size (float): Font size for the title.
284-
title_pad (float): Padding for the title (suptitle `y` position is `y=1+title_pad/100`).
284+
title_pad (float): Padding for the title (suptitle `y` position is `0.98`
285+
and the image height will be extended by `1 + title_pad/100`).
285286
286287
Returns:
287288
:class:`matplotlib:matplotlib.figure.Figure` :
@@ -345,6 +346,8 @@ def plot_bloch_multivector(
345346
width *= num
346347
else:
347348
width, height = plt.figaspect(1 / num)
349+
if len(title) > 0:
350+
height += 1 + title_pad / 100 # additional space for the title
348351
default_title_font_size = font_size if font_size is not None else 16
349352
title_font_size = title_font_size if title_font_size is not None else default_title_font_size
350353
fig = plt.figure(figsize=(width, height))
@@ -354,9 +357,13 @@ def plot_bloch_multivector(
354357
plot_bloch_vector(
355358
bloch_data[i], "qubit " + str(pos), ax=ax, figsize=figsize, font_size=font_size
356359
)
357-
fig.suptitle(title, fontsize=title_font_size, y=1.0 + title_pad / 100)
360+
fig.suptitle(title, fontsize=title_font_size, y=0.98)
358361
matplotlib_close_if_inline(fig)
359362
if filename is None:
363+
try:
364+
fig.tight_layout()
365+
except AttributeError:
366+
pass
360367
return fig
361368
else:
362369
return fig.savefig(filename)
@@ -726,6 +733,10 @@ def plot_state_paulivec(state, title="", figsize=None, color=None, ax=None, *, f
726733
if return_fig:
727734
matplotlib_close_if_inline(fig)
728735
if filename is None:
736+
try:
737+
fig.tight_layout()
738+
except AttributeError:
739+
pass
729740
return fig
730741
else:
731742
return fig.savefig(filename)

qiskit/visualization/timeline/plotters/matplotlib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,8 @@ def get_image(self, interactive: bool = False) -> matplotlib.pyplot.Figure:
188188

189189
if self.figure and interactive:
190190
self.figure.show()
191-
191+
try:
192+
self.figure.tight_layout()
193+
except AttributeError:
194+
pass
192195
return self.figure
-3.03 KB
Loading
27.7 KB
Loading

test/visual/mpl/graph/test_graph_matplotlib_drawer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_plot_bloch_multivector(self):
113113
FAILURE_DIFF_DIR,
114114
FAILURE_PREFIX,
115115
)
116-
self.assertGreaterEqual(ratio, 0.99, msg=fname)
116+
self.assertGreaterEqual(ratio, 0.98, msg=fname)
117117

118118
def test_plot_state_hinton(self):
119119
"""test plot_state_hinton"""
@@ -650,15 +650,14 @@ def test_plot_bloch_multivector_figsize_improvements(self):
650650
title_pad=8,
651651
filename=fname,
652652
)
653-
654653
ratio = VisualTestUtilities._save_diff(
655654
self._image_path(fname),
656655
self._reference_path(fname),
657656
fname,
658657
FAILURE_DIFF_DIR,
659658
FAILURE_PREFIX,
660659
)
661-
self.assertGreaterEqual(ratio, 0.99, msg=fname)
660+
self.assertGreaterEqual(ratio, 0.98, msg=fname)
662661

663662

664663
if __name__ == "__main__":

0 commit comments

Comments
 (0)