diff --git a/qiskit/visualization/circuit/matplotlib.py b/qiskit/visualization/circuit/matplotlib.py index f3a1e1ef4084..3b6afcb8f340 100644 --- a/qiskit/visualization/circuit/matplotlib.py +++ b/qiskit/visualization/circuit/matplotlib.py @@ -615,6 +615,10 @@ def lookup_var(var): for width, layer_num, flow_parent in flow_widths.values(): if layer_num != -1 and flow_parent == flow_drawer._flow_parent: raw_gate_width += width + # This is necessary to prevent 1 being added to the width of a + # BoxOp in layer_widths at the end of this method + if isinstance(node.op, BoxOp): + raw_gate_width -= 0.001 # Need extra incr of 1.0 for else and case boxes gate_width += raw_gate_width + (1.0 if circ_num > 0 else 0.0) @@ -747,7 +751,13 @@ def _get_coords( # increment by if/switch width. If more cases increment by width of previous cases. if flow_parent is not None: node_data[node].inside_flow = True - node_data[node].x_index = node_data[flow_parent].x_index + curr_x_index + 1 + # front_space provides a space for 'If', 'While', etc. which is not + # necessary for a BoxOp + front_space = 0 if isinstance(flow_parent.op, BoxOp) else 1 + node_data[node].x_index = ( + node_data[flow_parent].x_index + curr_x_index + front_space + ) + # If an else or case if node_data[node].circ_num > 0: for width in node_data[flow_parent].width[: node_data[node].circ_num]: @@ -1555,7 +1565,9 @@ def _flow_op_gate(self, node, node_data, glob_data): ypos = min(y[1] for y in xy) ypos_max = max(y[1] for y in xy) - if_width = node_data[node].width[0] + WID + # If a BoxOp, bring the right side back tight against the gates to allow for + # better spacing + if_width = node_data[node].width[0] + (WID if not isinstance(node.op, BoxOp) else -0.19) box_width = if_width # Add the else and case widths to the if_width for ewidth in node_data[node].width[1:]: @@ -1605,6 +1617,9 @@ def _flow_op_gate(self, node, node_data, glob_data): expr_spacer = 0.0 empty_default_spacer = 0.3 if len(node.op.blocks[-1]) == 0 else 0.0 elif isinstance(node.op, BoxOp): + # Move the X start position back for a BoxOp, since there is no + # leading text. This tightens the BoxOp with other ops. + xpos -= 0.15 op_spacer = 0.0 expr_spacer = 0.0 empty_default_spacer = 0.0 diff --git a/releasenotes/notes/visualise-less-box-space-00b1d929baf235ed.yaml b/releasenotes/notes/visualise-less-box-space-00b1d929baf235ed.yaml new file mode 100644 index 000000000000..52f65fa3da13 --- /dev/null +++ b/releasenotes/notes/visualise-less-box-space-00b1d929baf235ed.yaml @@ -0,0 +1,5 @@ +--- +other: + - | + The Matplotlib circuit drawer will now insert less extraneous space inside the left edge when + drawing :class:`.BoxOp` instances in circuits. diff --git a/test/visual/mpl/circuit/references/basic_box.png b/test/visual/mpl/circuit/references/basic_box.png index daa907d3c4bb..d3941e6aeb51 100644 Binary files a/test/visual/mpl/circuit/references/basic_box.png and b/test/visual/mpl/circuit/references/basic_box.png differ diff --git a/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py b/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py index 0f222671c8ef..e62489fc17f3 100644 --- a/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py +++ b/test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py @@ -1168,6 +1168,7 @@ def test_basic_box(self): qc = QuantumCircuit(5) with qc.box(): qc.x(0) + qc.x(1) with qc.box(): qc.cx(2, 3) with qc.box():