diff --git a/qiskit/visualization/matplotlib.py b/qiskit/visualization/matplotlib.py index 9cbf51cd9a3c..2eabf9f21805 100644 --- a/qiskit/visualization/matplotlib.py +++ b/qiskit/visualization/matplotlib.py @@ -436,13 +436,16 @@ def _get_text_width(self, text, fontsize, param=False): sum_text *= self._subfont_factor return sum_text - def _param_parse(self, v): - param_parts = [None] * len(v) - for i, e in enumerate(v): + def _param_parse(self, params): + param_parts = [] + for i, param in enumerate(params): + if i > 16: + param_parts.append("...") + break try: - param_parts[i] = pi_check(e, output="mpl", ndigits=3) + param_parts.append(pi_check(param, output="mpl", ndigits=3)) except TypeError: - param_parts[i] = str(e) + param_parts.append(str(param)) param_parts = ", ".join(param_parts).replace("-", "$-$") return param_parts diff --git a/test/ipynb/mpl/references/wide_params.png b/test/ipynb/mpl/references/wide_params.png new file mode 100644 index 000000000000..bdaabade8c40 Binary files /dev/null and b/test/ipynb/mpl/references/wide_params.png differ diff --git a/test/ipynb/mpl/test_circuit_matplotlib_drawer.py b/test/ipynb/mpl/test_circuit_matplotlib_drawer.py index 25a4f80ad89d..8d9abf85c58a 100644 --- a/test/ipynb/mpl/test_circuit_matplotlib_drawer.py +++ b/test/ipynb/mpl/test_circuit_matplotlib_drawer.py @@ -549,6 +549,14 @@ def test_6095(self): filename="6095.png", ) + def test_overwide_gates(self): + """Test gates don't exceed width of default fold""" + circuit = QuantumCircuit(5) + initial_state = np.zeros(2 ** 5) + initial_state[5] = 1 + circuit.initialize(initial_state) + self.circuit_drawer(circuit, filename="wide_params.png") + if __name__ == "__main__": unittest.main(verbosity=1)