Repository: Qiskit/qiskit

Issue:
Ket convention statevector drawer ignores max_size
### Environment

- **Qiskit Terra version**: 0.34.0
- **Python version**: 3.9.8
- **Operating system**: OSX


### What is happening?

Currently, `Statevector.draw('latex')` ignores the `max_size` parameter.


### How can we reproduce the issue?

```
from qiskit.quantum_info import Statevector
from qiskit.visualization.state_visualization import state_drawer

sv = Statevector.from_label('++++')
state_drawer(sv, 'latex', max_size=3)
```
<img width="906" alt="Screenshot 2022-01-11 at 12 41 42" src="https://user-images.githubusercontent.com/36071638/148944414-c982f2cc-eb9f-4ab9-a66a-98049e0ed3c6.png">

We get 12 terms, but I expect to get 3.

### What should happen?

The drawer should change its behaviour according to the `max_size` parameter.

### Any suggestions?

I think adding `**args` to this line might do it.
https://github.com/Qiskit/qiskit-terra/blob/5ea6e9557655b144228c29d7099375f5d2c91120/qiskit/visualization/state_visualization.py#L1183
Ket convention statevector drawer ignores max_size
### Environment

- **Qiskit Terra version**: 0.34.0
- **Python version**: 3.9.8
- **Operating system**: OSX


### What is happening?

Currently, `Statevector.draw('latex')` ignores the `max_size` parameter.


### How can we reproduce the issue?

```
from qiskit.quantum_info import Statevector
from qiskit.visualization.state_visualization import state_drawer

sv = Statevector.from_label('++++')
state_drawer(sv, 'latex', max_size=3)
```
<img width="906" alt="Screenshot 2022-01-11 at 12 41 42" src="https://user-images.githubusercontent.com/36071638/148944414-c982f2cc-eb9f-4ab9-a66a-98049e0ed3c6.png">

We get 12 terms, but I expect to get 3.

### What should happen?

The drawer should change its behaviour according to the `max_size` parameter.

### Any suggestions?

I think adding `**args` to this line might do it.
https://github.com/Qiskit/qiskit-terra/blob/5ea6e9557655b144228c29d7099375f5d2c91120/qiskit/visualization/state_visualization.py#L1183


Discussion:
Happy to look at this properly, can you assign me please?
Hi this issue has been causing me a real headache recently. Has there been any progress on it?
I think the solution will be very slightly different to what Frank originally suggested, because the options to `array_to_latex` and `_state_to_latex_ket` are different, so we'll need to only pass the right options to each of those, but in principle that's the only change needed.

I'll just ping @frankharkins to check if you've got time to work on it?  If not, I'll return it to the pool of good first issues for somebody else to take a look at.
Oh and in case it isn't the same bug in the code, the same issue occurs with .draw(output="latex_source")
Sorry I completely forgot about this, I'll have a look this week
Has there been any luck at all?
Yes, sorry- I've fixed and am just writing a test now.
Happy to look at this properly, can you assign me please?
Hi this issue has been causing me a real headache recently. Has there been any progress on it?
I think the solution will be very slightly different to what Frank originally suggested, because the options to `array_to_latex` and `_state_to_latex_ket` are different, so we'll need to only pass the right options to each of those, but in principle that's the only change needed.

I'll just ping @frankharkins to check if you've got time to work on it?  If not, I'll return it to the pool of good first issues for somebody else to take a look at.
Oh and in case it isn't the same bug in the code, the same issue occurs with .draw(output="latex_source")
Sorry I completely forgot about this, I'll have a look this week
Has there been any luck at all?
Yes, sorry- I've fixed and am just writing a test now.

Resolving patch:
diff --git a/qiskit/visualization/state_visualization.py b/qiskit/visualization/state_visualization.py
--- a/qiskit/visualization/state_visualization.py
+++ b/qiskit/visualization/state_visualization.py
@@ -1130,7 +1130,7 @@ def state_to_latex(
     # this means the operator shape should hve no input dimensions and all output dimensions equal to 2
     is_qubit_statevector = len(operator_shape.dims_r()) == 0 and set(operator_shape.dims_l()) == {2}
     if convention == "ket" and is_qubit_statevector:
-        latex_str = _state_to_latex_ket(state._data)
+        latex_str = _state_to_latex_ket(state._data, **args)
     else:
         latex_str = array_to_latex(state._data, source=True, **args)
     return prefix + latex_str + suffix


Test patch:
diff --git a/test/python/visualization/test_state_latex_drawer.py b/test/python/visualization/test_state_latex_drawer.py
new file mode 100644
--- /dev/null
+++ b/test/python/visualization/test_state_latex_drawer.py
@@ -0,0 +1,51 @@
+# This code is part of Qiskit.
+#
+# (C) Copyright IBM 2022.
+#
+# This code is licensed under the Apache License, Version 2.0. You may
+# obtain a copy of this license in the LICENSE.txt file in the root directory
+# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
+#
+# Any modifications or derivative works of this code must retain this
+# copyright notice, and modified files need to carry a notice indicating
+# that they have been altered from the originals.
+
+"""Tests for visualization of latex state and unitary drawers"""
+
+import unittest
+
+from qiskit.quantum_info import Statevector
+from qiskit.visualization.state_visualization import state_drawer
+from .visualization import QiskitVisualizationTestCase
+
+
+class TestLatexStateDrawer(QiskitVisualizationTestCase):
+    """Qiskit state and unitary latex drawer."""
+
+    def test_state(self):
+        """Test latex state vector drawer works with default settings."""
+
+        sv = Statevector.from_label("+-rl")
+        output = state_drawer(sv, "latex_source")
+        expected_output = (
+            r"\frac{1}{4} |0000\rangle- \frac{i}{4} |0001\rangle+\frac{i}{4} |0010\rangle"
+            r"+\frac{1}{4} |0011\rangle- \frac{1}{4} |0100\rangle+\frac{i}{4} |0101\rangle"
+            r" + \ldots +\frac{1}{4} |1011\rangle- \frac{1}{4} |1100\rangle"
+            r"+\frac{i}{4} |1101\rangle- \frac{i}{4} |1110\rangle- \frac{1}{4} |1111\rangle"
+        )
+        self.assertEqual(output, expected_output)
+
+    def test_state_max_size(self):
+        """Test `max_size` parameter for latex ket notation."""
+
+        sv = Statevector.from_label("+-rl")
+        output = state_drawer(sv, "latex_source", max_size=4)
+        expected_output = (
+            r"\frac{1}{4} |0000\rangle- \frac{i}{4} |0001\rangle"
+            r" + \ldots - \frac{1}{4} |1111\rangle"
+        )
+        self.assertEqual(output, expected_output)
+
+
+if __name__ == "__main__":
+    unittest.main(verbosity=2)