Exemple #1
0
    def draw(self, output=None, **drawer_args):
        """Return a visualization of the Statevector.

        **repr**: ASCII TextMatrix of the state's ``__repr__``.

        **text**: ASCII TextMatrix that can be printed in the console.

        **latex**: An IPython Latex object for displaying in Jupyter Notebooks.

        **latex_source**: Raw, uncompiled ASCII source to generate array using LaTeX.

        **qsphere**: Matplotlib figure, rendering of statevector using `plot_state_qsphere()`.

        **hinton**: Matplotlib figure, rendering of statevector using `plot_state_hinton()`.

        **bloch**: Matplotlib figure, rendering of statevector using `plot_bloch_multivector()`.

        **city**: Matplotlib figure, rendering of statevector using `plot_state_city()`.

        **paulivec**: Matplotlib figure, rendering of statevector using `plot_state_paulivec()`.

        Args:
            output (str): Select the output method to use for drawing the
                state. Valid choices are `repr`, `text`, `latex`, `latex_source`,
                `qsphere`, `hinton`, `bloch`, `city`, or `paulivec`. Default is `repr`.
                Default can be changed by adding the line ``state_drawer = <default>`` to
                ``~/.qiskit/settings.conf`` under ``[default]``.
            drawer_args: Arguments to be passed directly to the relevant drawing
                function or constructor (`TextMatrix()`, `array_to_latex()`,
                `plot_state_qsphere()`, `plot_state_hinton()` or `plot_bloch_multivector()`).
                See the relevant function under `qiskit.visualization` for that function's
                documentation.

        Returns:
            :class:`matplotlib.Figure` or :class:`str` or
            :class:`TextMatrix` or :class:`IPython.display.Latex`:
            Drawing of the Statevector.

        Raises:
            ValueError: when an invalid output method is selected.

        Examples:

            Plot one of the Bell states

            .. jupyter-execute::

                from numpy import sqrt
                from qiskit.quantum_info import Statevector
                sv=Statevector([1/sqrt(2), 0, 0, -1/sqrt(2)])
                sv.draw(output='latex')

        """
        # pylint: disable=cyclic-import
        from qiskit.visualization.state_visualization import state_drawer

        return state_drawer(self, output=output, **drawer_args)
Exemple #2
0
    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)
    def draw(self,
             output=None,
             max_size=16,
             dims=None,
             prefix='',
             **drawer_args):
        """Returns a visualization of the Statevector.

        **text**: ASCII TextMatrix that can be printed in the console.

        **latex**: An IPython Latex object for displaying in Jupyter Notebooks.

        **latex_source**: Raw, uncompiled ASCII source to generate array using LaTeX.

        **qsphere**: Matplotlib figure, rendering of statevector using `plot_state_qsphere()`.

        **hinton**: Matplotlib figure, rendering of statevector using `plot_state_hinton()`.

        **bloch**: Matplotlib figure, rendering of statevector using `plot_bloch_multivector()`.

        Args:
            output (str): Select the output method to use for drawing the
                circuit. Valid choices are ``text``, ``latex``, ``latex_source``,
                ``qsphere``, ``hinton``, or ``bloch``. Default is `'latex`'.
            max_size (int): Maximum number of elements before array is
                summarized instead of fully represented. For ``latex``
                and ``latex_source`` drawers, this is also the maximum number
                of elements that will be drawn in the output array, including
                elipses elements. For ``text`` drawer, this is the ``threshold``
                parameter in ``numpy.array2string()``.
            dims (bool): For `text` and `latex`. Whether to display the
                dimensions.
            prefix (str): For `text` and `latex`. Text to be displayed before
                the state.
            drawer_args: Arguments to be passed directly to the relevant drawer
                function (`plot_state_qsphere()`, `plot_state_hinton()` or
                `plot_bloch_multivector()`). See the relevant function under
                `qiskit.visualization` for that function's documentation.

        Returns:
            :class:`matplotlib.figure` or :class:`str` or
            :class:`TextMatrix`: or :class:`IPython.display.Latex`

        Raises:
            ValueError: when an invalid output method is selected.
        """
        # pylint: disable=cyclic-import
        from qiskit.visualization.state_visualization import state_drawer
        return state_drawer(self,
                            output=output,
                            max_size=max_size,
                            dims=dims,
                            prefix=prefix,
                            **drawer_args)
Exemple #4
0
    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)