Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Empty outputs layer removed from flow's partial order.
- Flow well-formedness check does not trigger false negative for flows on open graphs without outputs.

- #554, #560: Pauli-measurement entry is removed from the legend when visualizing an open graph without Pauli measurements.

### Changed

- #452: Use `uv` for dependency management
Expand Down
42 changes: 32 additions & 10 deletions graphix/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,16 +587,20 @@ def _draw_legend(self) -> None:
marker=DEFAULT_NODE_MARKER,
)
if self.options.pauli_measurements:
plt.scatter(
[],
[],
edgecolor=DEFAULT_NODE_EC,
facecolor=PAULI_NODE_FC,
s=150,
zorder=2,
label="Pauli-measured nodes",
marker=DEFAULT_NODE_MARKER,
has_pauli_measurements = any(
isinstance(measurement, PauliMeasurement) for measurement in self.og.measurements.values()
)
if has_pauli_measurements:
plt.scatter(
[],
[],
edgecolor=DEFAULT_NODE_EC,
facecolor=PAULI_NODE_FC,
s=150,
zorder=2,
label="Pauli-measured nodes",
marker=DEFAULT_NODE_MARKER,
)
plt.plot([], [], "--", c=EDGE_C, label="Graph edge")

assert self._source is not None
Expand All @@ -612,7 +616,25 @@ def _draw_legend(self) -> None:
case _:
assert_never(self._source)

plt.legend(loc="center left", fontsize=10, bbox_to_anchor=(1, 0.5))
# Set scatterpoints=1 to prevent node icons from appearing
# three times in the legend with the Agg backend used by
# pytest-mpl.
legend = plt.legend(loc="center left", fontsize=10, bbox_to_anchor=(1, 0.5), scatterpoints=1)

# Resize the figure to accommodate the legend.
fig = plt.gcf()
fig.canvas.draw() # Force layout

bbox = legend.get_window_extent()

# Width of the legend in inches
padding = 0.5
legend_width = bbox.width / fig.dpi + padding
legend_height = bbox.height / fig.dpi + padding

width, height = fig.get_size_inches()
fig.set_size_inches(width + legend_width, max(height, legend_height), forward=True)
fig.subplots_adjust(right=width / (width + legend_width))


@singledispatch
Expand Down
Binary file modified tests/baseline/test_causal_flow_draw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_draw_graph_reference_False.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_draw_graph_reference_True.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_gflow_draw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_og_draw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_pauli_flow_draw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_xzcorr_draw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/test_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,17 @@ def test_draw_graph_reference(flow_and_not_pauli_presimulate: bool) -> Figure:
flow_from_pattern=flow_and_not_pauli_presimulate, node_distance=(1, 1), measurement_labels=True, legend=False
)
return plt.gcf()


@pytest.mark.usefixtures("mock_plot")
@pytest.mark.parametrize("infer_pauli_measurements", [False, True])
@pytest.mark.mpl_image_compare
def test_legend_pauli_measurements(infer_pauli_measurements: bool) -> Figure:
# See https://github.com/TeamGraphix/graphix/issues/554
circuit = Circuit(1)
circuit.h(0)
pattern = circuit.transpile().pattern
if infer_pauli_measurements:
pattern = pattern.infer_pauli_measurements()
pattern.draw()
return plt.gcf()
Loading