§
    º³ÙgñÍ  ã                   ó0  — d Z ddlZddlmZmZ ddlmZ ddlZddlZ	ddl
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ ddlm Z  ej!        j"        d7ddœd„¦   «         Z#ej!        j"        	 d8d„¦   «         Z$ej!        j"        	 	 d9ddddddœd„¦   «         Z%ej!        j"        	 	 	 	 	 	 d:ddœd„¦   «         Z&ej!        j"        d7ddœd„¦   «         Z'd„ Z(d„ Z)d„ Z*d„ Z+ej!        j"        ej,        j"        	 	 	 	 	 d;ddœd„¦   «         ¦   «         Z-ej!        j"        d „ ¦   «         Z.d!„ Z/d<d"„Z0	 d=d$eeef         d%e1d&e2d'e2fd(„Z3d>d*ee4         d+e5d'ee2         fd,„Z6	 d?d.ee4         d/e5d0e2d+e5d'e2f
d1„Z7 G d2„ d3¦  «        Z8d<d4„Z9d5„ Z:d6„ Z;dS )@z-
Visualization functions for quantum states.
é    N)ÚListÚUnion)Úreduce)Úuser_config)ÚStatevector)ÚOperator)Ú	PauliListÚSparsePauliOp)ÚDensityMatrix)Ú	optionals)Úpi_checké   )Ú_num_to_latexÚarray_to_latex)Úmatplotlib_close_if_inline)ÚVisualizationErrorÚ )Úfilenamec                ó"  ‡— ddl m} t          | ¦  «        }|j        Š‰€t	          d¦  «        ‚dt          j        t          j        t          j	        |j
        ¦  «                             ¦   «         ¦  «        ¦  «        z  }t          j        |j
        ¦  «        }	t          j        |j
        ¦  «        }
|€d}|s!|s|                     dd|¬¦  «        \  }\  }}n/|r|                     ¦   «         }n|                     ¦   «         }|}|}ˆfd	„t!          d‰z  ¦  «        D ¦   «         }ˆfd
„t!          d‰z  ¦  «        D ¦   «         ddd…         }|	j        \  }}|�rí|j                             d¦  «         |                     dd¦  «         |j                             |                     ¦   «         ¦  «         |j                             |                     ¦   «         ¦  «         t          j        |	¦  «        D ]ˆ\  \  }}}|||z
  dz
  }}|dk    rdnd}t          j        t          j	        |¦  «        |z  ¦  «        }|                     d|z   |dz  z
  d|z   |dz  z
  g||||¬¦  «        }|                     |¦  «         Œ‰|                     dt          j        |¦  «        z   ¦  «         |                     dt          j        |¦  «        z   ¦  «         |                      d|g¦  «         | !                    d|g¦  «         | "                    |d¬¦  «         | #                    |dd¬¦  «         | $                    dd¬¦  «         |�rí|j                             d¦  «         |                     dd¦  «         |j                             |                     ¦   «         ¦  «         |j                             |                     ¦   «         ¦  «         t          j        |
¦  «        D ]ˆ\  \  }}}|||z
  dz
  }}|dk    rdnd}t          j        t          j	        |¦  «        |z  ¦  «        }|                     d|z   |dz  z
  d|z   |dz  z
  g||||¬¦  «        }|                     |¦  «         Œ‰|                     dt          j        |¦  «        z   ¦  «         |                     dt          j        |¦  «        z   ¦  «         |                      d|g¦  «         | !                    d|g¦  «         | "                    |d¬¦  «         | #                    |dd¬¦  «         | $                    dd¬¦  «         | %                    ¦   «          |r| &                    |d¬¦  «         |€|€tO          |¦  «         |€|S | (                    |¦  «        S )aÌ  Plot a hinton diagram for the density matrix of a quantum state.

    The hinton diagram represents the values of a matrix using
    squares, whose size indicate the magnitude of their corresponding value
    and their color, its sign. A white square means the value is positive and
    a black one means negative.

    Args:
        state (Statevector or DensityMatrix or ndarray): An N-qubit quantum state.
        title (str): a string that represents the plot title
        figsize (tuple): Figure size in inches.
        filename (str): file path to save image to.
        ax_real (matplotlib.axes.Axes): An optional Axes object to be used for
            the visualization output. If none is specified a new matplotlib
            Figure will be created and used. If this is specified without an
            ax_imag only the real component plot will be generated.
            Additionally, if specified there will be no returned Figure since
            it is redundant.
        ax_imag (matplotlib.axes.Axes): An optional Axes object to be used for
            the visualization output. If none is specified a new matplotlib
            Figure will be created and used. If this is specified without an
            ax_imag only the real component plot will be generated.
            Additionally, if specified there will be no returned Figure since
            it is redundant.

    Returns:
        :class:`matplotlib:matplotlib.figure.Figure` :
            The matplotlib.Figure of the visualization if
            neither ax_real or ax_imag is set.

    Raises:
        MissingOptionalLibraryError: Requires matplotlib.
        VisualizationError: Input is not a valid N-qubit state.

    Examples:
        .. plot::
           :alt: Output from the previous code.
           :include-source:

            import numpy as np
            from qiskit import QuantumCircuit
            from qiskit.quantum_info import DensityMatrix
            from qiskit.visualization import plot_state_hinton

            qc = QuantumCircuit(2)
            qc.h([0, 1])
            qc.cz(0,1)
            qc.ry(np.pi/3 , 0)
            qc.rx(np.pi/5, 1)

            state = DensityMatrix(qc)
            plot_state_hinton(state, title="New Hinton Plot")

    r   ©ÚpyplotNú)Input is not a multi-qubit quantum state.é   )é   é   r   ©Úfigsizec                 ód   •— g | ],}t          |¦  «        d d…                              ‰¦  «        ‘Œ-S ©r   N©ÚbinÚzfill©Ú.0ÚiÚnums     €ú^/granary/edev/quantum/lib/python3.11/site-packages/qiskit/visualization/state_visualization.pyú
<listcomp>z%plot_state_hinton.<locals>.<listcomp>v   ó5   ø€ ÐAÐAÐA¨a•C˜‘F”F˜1˜2˜2”J×$Ò$ SÑ)Ô)ÐAÐAÐAó    c                 ód   •— g | ],}t          |¦  «        d d…                              ‰¦  «        ‘Œ-S r   r    r#   s     €r'   r(   z%plot_state_hinton.<locals>.<listcomp>w   ó5   ø€ Ð>Ð>Ð>¨1•�Q‘”˜˜˜”×!Ò! #Ñ&Ô&Ð>Ð>Ð>r*   éÿÿÿÿÚgrayÚequalÚboxÚwhiteÚblackç      à?)Ú	facecolorÚ	edgecoloré   ©ÚfontsizeéZ   ©r8   Úrotationz
Re[$\rho$]z
Im[$\rho$]é   ))Ú
matplotlibr   r   Ú
num_qubitsr   ÚmathÚceilÚlog2ÚnpÚabsÚdataÚmaxÚrealÚimagÚsubplotsÚ
get_figureÚrangeÚshapeÚpatchÚset_facecolorÚ
set_aspectÚxaxisÚset_major_locatorÚNullLocatorÚyaxisÚndenumerateÚsqrtÚ	RectangleÚ	add_patchÚ
set_xticksÚarangeÚ
set_yticksÚset_xlimÚset_ylimÚset_yticklabelsÚset_xticklabelsÚ	set_titleÚtight_layoutÚsuptitler   Úsavefig)ÚstateÚtitler   Úax_realÚax_imagr   ÚpltÚrhoÚ
max_weightÚdatarealÚdataimagÚfigÚax1Úax2Úcolumn_namesÚ	row_namesÚlyÚlxÚxÚyÚwÚplot_xÚplot_yÚcolorÚsizeÚrectr&   s                             @r'   Úplot_state_hintonrz   '   sŽ  ø€ ðp )Ð(Ð(Ð(Ð(Ð(õ ˜Ñ
Ô
€CØ
Œ.€CØ
€{Ý Ð!LÑMÔMÐMØ•d”i¥¤	­"¬&°´Ñ*:Ô*:×*>Ò*>Ñ*@Ô*@Ñ AÔ AÑBÔBÑB€JÝŒw�s”xÑ Ô €HÝŒw�s”xÑ Ô €Hà€ØˆØð ˜7ð ØŸ,š, q¨!°W˜,Ñ=Ô=‰ˆ‰Zˆc�3�3àð 	'Ø×$Ò$Ñ&Ô&ˆCˆCà×$Ò$Ñ&Ô&ˆCØˆØˆàAÐAÐAÐAµ5¸¸C¹±=´=ÐAÑAÔA€LØ>Ð>Ð>Ð>µ°a¸±f±´Ð>Ñ>Ô>¸t¸tÀ¸tÔD€IØŒ^�F€Bˆà
ñ 2ØŒ	×Ò Ñ'Ô'Ð'Ø�Š�w Ñ&Ô&Ð&ØŒ	×#Ò# C§O¢OÑ$5Ô$5Ñ6Ô6Ð6ØŒ	×#Ò# C§O¢OÑ$5Ô$5Ñ6Ô6Ð6åœ¨Ñ1Ô1ð 	 ð 	 ‰I‰FˆQ��Aà  Q¡¨¡
�FˆFØ  1šu˜u�G�G¨'ˆEÝ”7�2œ6 !™9œ9 zÑ1Ñ2Ô2ˆDØ—=’=Ø�v‘  q¡Ñ(¨#°©,¸À¹Ñ*AÐBØØØØð !ñ ô ˆDð �MŠM˜$ÑÔÐÐà�Š�s�RœY r™]œ]Ñ*Ñ+Ô+Ð+Ø�Š�s�RœY r™]œ]Ñ*Ñ+Ô+Ð+Ø�Š�a˜�WÑÔÐØ�Š�a˜�WÑÔÐØ×Ò˜I°ÐÑ3Ô3Ð3Ø×Ò˜L°2ÀÐÑCÔCÐCØ�Š�m¨bˆÑ1Ô1Ð1à
ñ 2ØŒ	×Ò Ñ'Ô'Ð'Ø�Š�w Ñ&Ô&Ð&ØŒ	×#Ò# C§O¢OÑ$5Ô$5Ñ6Ô6Ð6ØŒ	×#Ò# C§O¢OÑ$5Ô$5Ñ6Ô6Ð6åœ¨Ñ1Ô1ð 	 ð 	 ‰I‰FˆQ��Aà  Q¡¨¡
�FˆFØ  1šu˜u�G�G¨'ˆEÝ”7�2œ6 !™9œ9 zÑ1Ñ2Ô2ˆDØ—=’=Ø�v‘  q¡Ñ(¨#°©,¸À¹Ñ*AÐBØØØØð !ñ ô ˆDð �MŠM˜$ÑÔÐÐà�Š�s�RœY r™]œ]Ñ*Ñ+Ô+Ð+Ø�Š�s�RœY r™]œ]Ñ*Ñ+Ô+Ð+Ø�Š�a˜�WÑÔÐØ�Š�a˜�WÑÔÐØ×Ò˜I°ÐÑ3Ô3Ð3Ø×Ò˜L°2ÀÐÑCÔCÐCØ�Š�m¨bˆÑ1Ô1Ð1Ø×ÒÑÔÐØð )Ø�Š�U RˆÑ(Ô(Ð(Ø€˜7˜?Ý" 3Ñ'Ô'Ð'ØÐØˆ
à�{Š{˜8Ñ$Ô$Ð$r*   Ú	cartesianc                 ó.  — ddl m} |€d} |||¬¦  «        }|dk    r�| d         | d         | d         }
}	}|t          j        |	¦  «        z  t          j        |
¦  «        z  | d<   |t          j        |	¦  «        z  t          j        |
¦  «        z  | d<   |t          j        |	¦  «        z  | d<   |                     | ¦  «         |                     |¬	¦  «         |€:|j        }|                     |d         |d         ¦  «         t          |¦  «         |S dS )
a,  Plot the Bloch sphere.

    Plot a Bloch sphere with the specified coordinates, that can be given in both
    cartesian and spherical systems.

    Args:
        bloch (list[double]): array of three elements where [<x>, <y>, <z>] (Cartesian)
            or [<r>, <theta>, <phi>] (spherical in radians)
            <theta> is inclination angle from +z direction
            <phi> is azimuth from +x direction
        title (str): a string that represents the plot title
        ax (matplotlib.axes.Axes): An Axes to use for rendering the bloch
            sphere
        figsize (tuple): Figure size in inches. Has no effect is passing ``ax``.
        coord_type (str): a string that specifies coordinate type for bloch
            (Cartesian or spherical), default is Cartesian
        font_size (float): Font size.

    Returns:
        :class:`matplotlib:matplotlib.figure.Figure` : A matplotlib figure instance if ``ax = None``.

    Raises:
        MissingOptionalLibraryError: Requires matplotlib.

    Examples:
        .. plot::
           :alt: Output from the previous code.
           :include-source:

           from qiskit.visualization import plot_bloch_vector

           plot_bloch_vector([0,1,0], title="New Bloch Sphere")

        .. plot::
           :alt: Output from the previous code.
           :include-source:

           import numpy as np
           from qiskit.visualization import plot_bloch_vector

           # You can use spherical coordinates instead of cartesian.

           plot_bloch_vector([1, np.pi/2, np.pi/3], coord_type='spherical')

    r   )ÚBlochN)r   r   )ÚaxesÚ	font_sizeÚ	sphericalr   r   )rc   )
Úblochr}   rB   ÚsinÚcosÚadd_vectorsÚrenderrk   Úset_size_inchesr   )r�   rc   Úaxr   Ú
coord_typer   r}   ÚBÚrÚthetaÚphirk   s               r'   Úplot_bloch_vectorr�   ¼   s  € ðb ÐÐÐÐÐà€ØˆØˆ�2 Ð+Ñ+Ô+€AØ�[Ò Ð Ø˜aœ %¨¤(¨E°!¬H�#ˆ5ˆØ•r”v˜e‘}”}Ñ$¥r¤v¨c¡{¤{Ñ2ˆˆa‰Ø•r”v˜e‘}”}Ñ$¥r¤v¨c¡{¤{Ñ2ˆˆa‰Ø•r”v˜e‘}”}Ñ$ˆˆa‰Ø‡M‚M�%ÑÔÐØ‡H‚H�5€HÑÔÐØ	€zØŒeˆØ×Ò˜G AœJ¨°¬
Ñ3Ô3Ð3Ý" 3Ñ'Ô'Ð'Øˆ
Øˆ4r*   F)Úreverse_bitsr   r   Útitle_font_sizeÚ	title_padc                ój  — ddl m} |rt          | ¦  «        ddd…         nt          | ¦  «        }	t          |	¦  «        }
|�|\  }}||
z  }n|                     d|
z  ¦  «        \  }}|�|nd}|�|n|}|                     ||f¬¦  «        }t          |
¦  «        D ]T}|r|
dz
  |z
  n|}|                     d|
|dz   d¬	¦  «        }t          |	|         d
t          |¦  «        z   |||¬¦  «         ŒU| 
                    ||d|dz  z   ¬¦  «         t          |¦  «         |€|S |                     |¦  «        S )aW	  Plot a Bloch sphere for each qubit.

    Each component :math:`(x,y,z)` of the Bloch sphere labeled as 'qubit i' represents the expected
    value of the corresponding Pauli operator acting only on that qubit, that is, the expected value
    of :math:`I_{N-1} \otimes\dotsb\otimes I_{i+1}\otimes P_i \otimes I_{i-1}\otimes\dotsb\otimes
    I_0`, where :math:`N` is the number of qubits, :math:`P\in \{X,Y,Z\}` and :math:`I` is the
    identity operator.

    Args:
        state (Statevector or DensityMatrix or ndarray): an N-qubit quantum state.
        title (str): a string that represents the plot title
        figsize (tuple): size of each individual Bloch sphere figure, in inches.
        reverse_bits (bool): If True, plots qubits following Qiskit's convention [Default:False].
        font_size (float): Font size for the Bloch ball figures.
        title_font_size (float): Font size for the title.
        title_pad (float): Padding for the title (suptitle `y` position is `y=1+title_pad/100`).

    Returns:
        :class:`matplotlib:matplotlib.figure.Figure` :
            A matplotlib figure instance.

    Raises:
        MissingOptionalLibraryError: Requires matplotlib.
        VisualizationError: if input is not a valid N-qubit state.

    Examples:
        .. plot::
           :alt: Output from the previous code.
           :include-source:

            from qiskit import QuantumCircuit
            from qiskit.quantum_info import Statevector
            from qiskit.visualization import plot_bloch_multivector

            qc = QuantumCircuit(2)
            qc.h(0)
            qc.x(1)

            state = Statevector(qc)
            plot_bloch_multivector(state)

        .. plot::
           :alt: Output from the previous code.
           :include-source:

           from qiskit import QuantumCircuit
           from qiskit.quantum_info import Statevector
           from qiskit.visualization import plot_bloch_multivector

           qc = QuantumCircuit(2)
           qc.h(0)
           qc.x(1)

           # You can reverse the order of the qubits.

           from qiskit.quantum_info import DensityMatrix

           qc = QuantumCircuit(2)
           qc.h([0, 1])
           qc.t(1)
           qc.s(0)
           qc.cx(0,1)

           matrix = DensityMatrix(qc)
           plot_bloch_multivector(matrix, title='My Bloch Spheres', reverse_bits=True)

    r   r   Nr-   r   r<   r   Ú3d©Ú
projectionzqubit )r‡   r   r   ç      ð?éd   )r8   rs   )r=   r   Ú_bloch_multivector_dataÚlenÚ	figaspectÚfigurerJ   Úadd_subplotr�   Ústrr`   r   ra   )rb   rc   r   rŽ   r   r   r�   r�   rf   Ú
bloch_datar&   ÚwidthÚheightÚdefault_title_font_sizerk   r%   Úposr‡   s                     r'   Úplot_bloch_multivectorr¢     s‹  € ð^ )Ð(Ð(Ð(Ð(Ð(ð 1=Ð`Õ Ñ&Ô& t t¨ tÔ,Ð,ÕBYÐZ_ÑB`ÔB`ð õ ˆj‰/Œ/€CØÐØ‰ˆˆvØ�‰ˆˆàŸš a¨#¡gÑ.Ô.‰ˆˆvØ+4Ð+@˜i˜iÀbÐØ)8Ð)D�o�oÐJa€OØ
�*Š*˜e V˜_ˆ*Ñ
-Ô
-€CÝ�3‰ZŒZð 
ð 
ˆØ)Ð0ˆc�A‰g˜‰kˆk¨qˆØ�_Š_˜Q  Q¨¡U°tˆ_Ñ<Ô<ˆÝØ�qŒM˜8¥c¨#¡h¤hÑ.°2¸wÐR[ð	
ñ 	
ô 	
ð 	
ð 	
ð ‡L‚L� °C¸)Àc¹/Ñ4I€LÑJÔJÐJÝ˜sÑ#Ô#Ð#ØÐØˆ
à�{Š{˜8Ñ$Ô$Ð$r*   c                óh  ‡5— ddl m} ddlm}	 ddlm}
 t          | ¦  «        }|j        Š5‰5€t          d¦  «        ‚t          j
        |j        ¦  «        }t          j        |j        ¦  «        }ˆ5fd„t          d‰5z  ¦  «        D ¦   «         }ˆ5fd„t          d‰5z  ¦  «        D ¦   «         }|j        dd…         \  }}t          j        d|d	¦  «        }t          j        d|d	¦  «        }t          j        |d
z   |d
z   ¦  «        \  }}|                     ¦   «         }|                     ¦   «         }t          j        ||z  ¦  «        }dt          j        |¦  «        z  }|                     ¦   «         }|                     ¦   «         }|                     ¦   «         }|€d\  }}nFt+          |¦  «        dk    rt-          d¦  «        ‚|d         €dn|d         }|d	         €dn|d	         }|€R|€P|€d}|	                     |d¬¦  «        }|                     d	dd	dd¬¦  «        }|                     d	dddd¬¦  «        }n3|�|                     ¦   «         }|}|}n|                     ¦   «         }d}|}|                     ¦   «          t          j        |¦  «        }t          j        |¦  «        }|                     ¦   «         \  } }!t;          | dz  |!¦  «        }"t=          d|"z  ¦  «        }#ddt          j        |"¦  «        z   z  }$|||df|||dffD �]G\  }%}&}'}(|%€Œt          j        |&¦  «        })t          j        |&¦  «        }*tA          |'tB          ¦  «        r*|' "                    d¦  «        r| #                    |'¦  «        }'|&dk     }+t          j$        |+¦  «        r�tK          ||+         ||+         ||+         ||+         ||+         |&|+         |'¦  «        },|% &                    ||+         ||+         ||+         ||+         ||+         |&|+         |d¬¦  «        }-|- '                    |,¦  «         |*dcxk     r|)k     r†n nƒd|gd|g}/}.tQ          tS          |.|.ddd…         z   t          j*        |/d¦  «        dgdz  ¦  «        ¦  «        g}0 |
|0d
dd	¬ ¦  «        }1|1 +                    d!¦  «         |% ,                    |1¦  «         |&dk    }2t          j$        |2¦  «        r�tK          ||2         ||2         ||2         ||2         ||2         |&|2         |'¦  «        },|% &                    ||2         ||2         ||2         ||2         ||2         |&|2         |d"¬¦  «        }3|3 '                    |,¦  «         |% -                    |(› d#�|#¬$¦  «         |% .                    t          j        d|dz   d	¦  «        ¦  «         |% /                    t          j        d|dz   d	¦  «        ¦  «         |)|*k    r-|%j0         1                    |*t7          |d%z   |¦  «        ¦  «         nN|*dk    r-|%j0         1                    |*t7          |d%z   |¦  «        ¦  «         n|%j0         1                    d&¬'¦  «         |% 2                    ¦   «          |%j3         4                    ||#d(d)d*¬+¦  «         |%j5         4                    ||#d,d-d.¬+¦  «         |%j6         7                    ¦   «         D ]P}4|4j8         9                    |#¦  «         |4j8         :                    d-¦  «         |4j8         ;                    d/¦  «         ŒQ|% <                    d0|$¬1¦  «         |% =                    d¦  «         |% >                    d¦  «         �ŒI| ?                    ||#d2z  ¬$¦  «         | @                    d3ddd	dd¬4¦  «         |€|€tƒ          |¦  «         |€|S | B                    |¦  «        S )5aÆ  Plot the cityscape of quantum state.

    Plot two 3d bar graphs (two dimensional) of the real and imaginary
    part of the density matrix rho.

    Args:
        state (Statevector or DensityMatrix or ndarray): an N-qubit quantum state.
        title (str): a string that represents the plot title
        figsize (tuple): Figure size in inches.
        color (list): A list of len=2 giving colors for real and
            imaginary components of matrix elements.
        alpha (float): Transparency value for bars
        ax_real (matplotlib.axes.Axes): An optional Axes object to be used for
            the visualization output. If none is specified a new matplotlib
            Figure will be created and used. If this is specified without an
            ax_imag only the real component plot will be generated.
            Additionally, if specified there will be no returned Figure since
            it is redundant.
        ax_imag (matplotlib.axes.Axes): An optional Axes object to be used for
            the visualization output. If none is specified a new matplotlib
            Figure will be created and used. If this is specified without an
            ax_real only the imaginary component plot will be generated.
            Additionally, if specified there will be no returned Figure since
            it is redundant.

    Returns:
        :class:`matplotlib:matplotlib.figure.Figure` :
            The matplotlib.Figure of the visualization if the
            ``ax_real`` and ``ax_imag`` kwargs are not set

    Raises:
        MissingOptionalLibraryError: Requires matplotlib.
        ValueError: When 'color' is not a list of len=2.
        VisualizationError: if input is not a valid N-qubit state.

    Examples:
        .. plot::
           :alt: Output from the previous code.
           :include-source:

           # You can choose different colors for the real and imaginary parts of the density matrix.

           from qiskit import QuantumCircuit
           from qiskit.quantum_info import DensityMatrix
           from qiskit.visualization import plot_state_city

           qc = QuantumCircuit(2)
           qc.h(0)
           qc.cx(0, 1)

           state = DensityMatrix(qc)
           plot_state_city(state, color=['midnightblue', 'crimson'], title="New State City")

        .. plot::
           :alt: Output from the previous code.
           :include-source:

           # You can make the bars more transparent to better see the ones that are behind
           # if they overlap.

           import numpy as np
           from qiskit.quantum_info import Statevector
           from qiskit.visualization import plot_state_city
           from qiskit import QuantumCircuit

           qc = QuantumCircuit(2)
           qc.h(0)
           qc.cx(0, 1)


           qc = QuantumCircuit(2)
           qc.h([0, 1])
           qc.cz(0,1)
           qc.ry(np.pi/3, 0)
           qc.rx(np.pi/5, 1)

           state = Statevector(qc)
           plot_state_city(state, alpha=0.6)

    r   Nr   )ÚPoly3DCollectionr   c                 ód   •— g | ],}t          |¦  «        d d…                              ‰¦  «        ‘Œ-S r   r    r#   s     €r'   r(   z#plot_state_city.<locals>.<listcomp>×  r)   r*   r   c                 ód   •— g | ],}t          |¦  «        d d…                              ‰¦  «        ‘Œ-S r   r    r#   s     €r'   r(   z#plot_state_city.<locals>.<listcomp>Ø  r,   r*   r   g      Ð?r3   )ú#648fffr§   z 'color' must be a list of len=2.r§   )r<   r   rt   )r   r4   r’   F)r”   Úcomputed_zorderg      @é   é
   ÚRealÚ	Imaginaryú#g      ä?)ÚalphaÚzorderr-   é   Úk)r®   r4   Ú
linewidthsç      è?g      ì?u    Amplitude (Ï�)r7   g•Ö&è.>T)Úautoé-   ÚrightÚtop)r8   r;   ÚhaÚvag     €6ÀÚleftÚcenterÚbottom)r°   r°   r°   )ÚaspectÚzoomg      ô?gÍÌÌÌÌÌì?)r·   r¼   rº   r¶   ÚhspaceÚwspace)CÚmatplotlib.colorsÚcolorsr=   r   Úmpl_toolkits.mplot3d.art3dr¤   r   r>   r   rB   rF   rD   rG   rJ   rK   rX   ÚmeshgridÚflattenÚzerosÚ	ones_likeÚcopyr˜   Ú
ValueErrorrš   r›   rI   r_   rE   Úget_size_inchesÚminÚintrT   Ú
isinstancerœ   Ú
startswithÚto_rgba_arrayÚanyÚgenerate_facecolorsÚbar3drM   ÚlistÚzipÚrepeatÚ
set_zorderÚadd_collection3dr^   rW   rY   r~   Ú
set_zlim3dÚget_autoscalez_onrO   Úset_ticklabelsrR   ÚzaxisÚget_major_ticksÚlabel1Úset_fontsizeÚset_horizontalalignmentÚset_verticalalignmentÚset_box_aspectÚset_xmarginÚset_ymarginr`   Úsubplots_adjustr   ra   )6rb   rc   r   rw   r®   rd   re   r   Úmcolorsrf   r¤   rg   ri   rj   rn   ro   rp   rq   ÚxposÚyposÚzposÚdxÚdyÚdzrÚdziÚ
real_colorÚ
imag_colorrk   rl   rm   Úmax_dzrÚmax_dziÚ	fig_widthÚ
fig_heightÚmax_plot_sizeÚmax_font_sizeÚmax_zoomr‡   ÚdzÚcolÚzlabelÚmax_dzÚmin_dzÚdznÚfcÚnegative_barsÚxlimÚylimÚvertsÚplaneÚdzpÚpositive_barsÚtickr&   s6                                                        @r'   Úplot_state_cityr  m  s  ø€ ðx (Ð'Ð'Ð'Ð'Ð'Ø(Ð(Ð(Ð(Ð(Ð(Ø;Ð;Ð;Ð;Ð;Ð;å
˜Ñ
Ô
€CØ
Œ.€CØ
€{Ý Ð!LÑMÔMÐMõ Œw�s”xÑ Ô €HÝŒw�s”xÑ Ô €Hð BÐAÐAÐAµ5¸¸C¹±=´=ÐAÑAÔA€LØ>Ð>Ð>Ð>µ°a¸±f±´Ð>Ñ>Ô>€IàŒ^˜B˜Q˜BÔ�F€BˆÝŒ9�Q˜˜AÑÔ€DÝŒ9�Q˜˜AÑÔ€DÝ”˜T D™[¨$°©+Ñ6Ô6�J€Dˆ$à�<Š<‰>Œ>€DØ�<Š<‰>Œ>€DÝŒ8�B˜‘GÑÔ€Dà	�rŒ|˜DÑ!Ô!Ñ	!€BØ	�Š‰Œ€BØ
×
Ò
Ñ
Ô
€CØ
×
Ò
Ñ
Ô
€Cà€}Ø!5Ñˆ
�J�Jåˆu‰:Œ:˜Š?ˆ?ÝÐ?Ñ@Ô@Ð@Ø"'¨¤(Ð"2�Y�Y¸¸a¼ˆ
Ø"'¨¤(Ð"2�Y�Y¸¸a¼ˆ
Ø€˜7˜?àˆ?ØˆGà�jŠj °CˆjÑ8Ô8ˆØ�oŠo˜a  A°$ÈˆoÑNÔNˆØ�oŠo˜a  A°$ÈˆoÑNÔNˆˆà	Ð	Ø× Ò Ñ"Ô"ˆØˆØˆˆà× Ò Ñ"Ô"ˆØˆØˆà×ÒÑÔÐåŒf�S‰kŒk€GÝŒf�S‰kŒk€Gð  ×/Ò/Ñ1Ô1Ñ€IˆzÝ˜	 DÑ(¨*Ñ5Ô5€MÝ˜˜MÑ)Ñ*Ô*€MØ�R�"œ' -Ñ0Ô0Ñ0Ñ1€Hð 
ˆc�:˜vÐ&Ø	ˆc�:˜{Ð+ð ð Rñ RÑˆˆB��Vð
 ˆ:Øå”˜‘”ˆÝ”˜‘”ˆå�c�3ÑÔð 	- C§N¢N°3Ñ$7Ô$7ð 	-Ø×'Ò'¨Ñ,Ô,ˆCà�1ŠfˆÝŒ6�#‰;Œ;ð 	,Ý$Ø�S”	˜4 œ9 d¨3¤i°°C´¸"¸S¼'À2ÀcÄ7ÈCñô ˆBð ŸHšHØ�S”	Ø�S”	Ø�S”	Ø�3”Ø�3”Ø�3”ØØð %ñ 	ô 	ˆMð ×'Ò'¨Ñ+Ô+Ð+à�AÐÐÒÐ˜ÒÐÐÐÐØ˜R˜ 1 b '�$ˆDÝ�#˜d T¨$¨$¨B¨$¤ZÑ/µ´¸4ÀÑ1CÔ1CÀaÀSÈ1ÁWÑMÔMÑNÔNÐOˆEØ$Ð$ U°$À#ÐRSÐTÑTÔTˆEØ×Ò˜TÑ"Ô"Ð"Ø×Ò Ñ&Ô&Ð&à�AŠgˆÝŒ6�#‰;Œ;ð 	,Ý$Ø�S”	˜4 œ9 d¨3¤i°°C´¸"¸S¼'À2ÀcÄ7ÈCñô ˆBð ŸHšHØ�S”	Ø�S”	Ø�S”	Ø�3”Ø�3”Ø�3”ØØð %ñ 	ô 	ˆMð ×'Ò'¨Ñ+Ô+Ð+à
�Š˜Ð/Ð/Ð/¸-ˆÑHÔHÐHà
�Š•b”i  R¨#¡X¨qÑ1Ô1Ñ2Ô2Ð2Ø
�Š•b”i  R¨#¡X¨qÑ1Ô1Ñ2Ô2Ð2Ø�VÒÐØŒG×Ò˜v¥s¨7°T©>¸7Ñ'CÔ'CÑDÔDÐDÐDà˜Š{ˆ{Ø”×"Ò" 6­3¨w¸©~¸wÑ+GÔ+GÑHÔHÐHÐHà”×"Ò"¨Ð"Ñ-Ô-Ð-Ø
×ÒÑÔÐà
Œ×ÒØ ¸¸wÈ5ð 	 ñ 	
ô 	
ð 	
ð 	Œ×ÒØ =¸5ÀVÐPXð 	 ñ 	
ô 	
ð 	
ð ”H×,Ò,Ñ.Ô.ð 	8ð 	8ˆDØŒK×$Ò$ ]Ñ3Ô3Ð3ØŒK×/Ò/°Ñ7Ô7Ð7ØŒK×-Ò-¨hÑ7Ô7Ð7Ð7à
×Ò °ÐÑ:Ô:Ð:Ø
�Š�qÑÔÐØ
�Š�qÑÔÐÑà‡L‚L� °Ñ!5€LÑ6Ô6Ð6Ø×Ò˜C¨°¸À1ÈQÐÑOÔOÐOØ€˜7˜?Ý" 3Ñ'Ô'Ð'ØÐØˆ
à�{Š{˜8Ñ$Ô$Ð$r*   c                óä  — ddl m} t          | ¦  «        \  }}t          |¦  «        }	|€d}|€d}t	          j        |	¦  «        }
d}|€d}|                     |¬¦  «        \  }}nd	}|                     ¦   «         }|                     dd
d¬¦  «         | 	                    |
|||d¬¦  «         | 
                    d
d¬¦  «         |                     dd¬¦  «         |                     |
¦  «         |                     g d¢¦  «         |                     |dd¬¦  «         |                     dd¬¦  «         |                     dd
g¦  «         |                     d¦  «         |j                             ¦   «         |j                             ¦   «         z   D ]}|j                             d¦  «         Œ|                     |d¬¦  «         |rt1          |¦  «         |€|S |                     |¦  «        S )ak
  Plot the Pauli-vector representation of a quantum state as bar graph.

    The Pauli-vector of a density matrix :math:`\rho` is defined by the expectation of each
    possible tensor product of single-qubit Pauli operators (including the identity), that is

    .. math ::

        \rho = \frac{1}{2^n} \sum_{\sigma \in \{I, X, Y, Z\}^{\otimes n}}
               \mathrm{Tr}(\sigma \rho) \sigma.

    This function plots the coefficients :math:`\mathrm{Tr}(\sigma\rho)` as bar graph.

    Args:
        state (Statevector or DensityMatrix or ndarray): an N-qubit quantum state.
        title (str): a string that represents the plot title
        figsize (tuple): Figure size in inches.
        color (list or str): Color of the coefficient value bars.
        ax (matplotlib.axes.Axes): An optional Axes object to be used for
            the visualization output. If none is specified a new matplotlib
            Figure will be created and used. Additionally, if specified there
            will be no returned Figure since it is redundant.

    Returns:
         :class:`matplotlib:matplotlib.figure.Figure` :
            The matplotlib.Figure of the visualization if the
            ``ax`` kwarg is not set

    Raises:
        MissingOptionalLibraryError: Requires matplotlib.
        VisualizationError: if input is not a valid N-qubit state.

    Examples:
        .. plot::
           :alt: Output from the previous code.
           :include-source:

           # You can set a color for all the bars.

           from qiskit import QuantumCircuit
           from qiskit.quantum_info import Statevector
           from qiskit.visualization import plot_state_paulivec

           qc = QuantumCircuit(2)
           qc.h(0)
           qc.cx(0, 1)

           state = Statevector(qc)
           plot_state_paulivec(state, color='midnightblue', title="New PauliVec plot")

        .. plot::
           :alt: Output from the previous code.
           :include-source:

           # If you introduce a list with less colors than bars, the color of the bars will
           # alternate following the sequence from the list.

           import numpy as np
           from qiskit.quantum_info import DensityMatrix
           from qiskit import QuantumCircuit
           from qiskit.visualization import plot_state_paulivec

           qc = QuantumCircuit(2)
           qc.h(0)
           qc.cx(0, 1)

           qc = QuantumCircuit(2)
           qc.h([0, 1])
           qc.cz(0, 1)
           qc.ry(np.pi/3, 0)
           qc.rx(np.pi/5, 1)

           matrix = DensityMatrix(qc)
           plot_state_paulivec(matrix, color=['crimson', 'midnightblue', 'seagreen'])
    r   r   N)é   r   r§   r3   Tr   Fr   z--)r¯   Ú	linewidthÚ	linestyler   ©rw   r¯   r±   )r  rw   ÚCoefficientsr6   r7   )r-   g      à¿r   r3   r   éF   r:   ÚPaulir-   z#eeeeeer<   )r=   r   Ú_paulivec_datar˜   rB   rX   rH   rI   ÚgridÚbarÚaxhlineÚ
set_ylabelrW   rY   r]   Ú
set_xlabelr[   rM   rO   rÜ   rR   rÝ   rÞ   r^   r   ra   )rb   rc   r   rw   r‡   r   rf   ÚlabelsÚvaluesÚnumelemÚindrž   Ú
return_figrk   r  s                  r'   Úplot_state_paulivecr  j  s  € ðX )Ð(Ð(Ð(Ð(Ð(å# EÑ*Ô*�N€FˆFÝ�&‰kŒk€Gà€ØˆØ€}Øˆå
Œ)�GÑ
Ô
€CØ€EØ	€zØˆ
Ø—,’, w�,Ñ/Ô/‰ˆˆRˆRàˆ
Ø�mŠm‰oŒoˆØ‡G‚G�1 ¨T€GÑ2Ô2Ð2Ø‡F‚Fˆ3�˜ U°1€FÑ5Ô5Ð5Ø‡J‚J˜ #€JÑ&Ô&Ð&à‡M‚M�.¨2€MÑ.Ô.Ð.Ø‡M‚M�#ÑÔÐØ‡M‚MÐ'Ð'Ð'Ñ(Ô(Ð(Ø×Ò�v¨°RÐÑ8Ô8Ð8Ø‡M‚M�' B€MÑ'Ô'Ð'Ø‡K‚K��Q�ÑÔÐØ×Ò�YÑÔÐØ”×(Ò(Ñ*Ô*¨R¬X×-EÒ-EÑ-GÔ-GÑGð %ð %ˆØŒ× Ò  Ñ$Ô$Ð$Ð$Ø‡L‚L� €LÑ$Ô$Ð$Øð (Ý" 3Ñ'Ô'Ð'ØÐØˆ
à�{Š{˜8Ñ$Ô$Ð$r*   c                 ó¢   — | dk    rdS t          d„ t          t          | |z
  dz   | dz   ¦  «        t          d|dz   ¦  «        ¦  «        d¦  «        S )zÕReturn the number of combinations for n choose k.

    Args:
        n (int): the total number of options .
        k (int): The number of elements.

    Returns:
        int: returns the binomial coefficient
    r   c                 ó*   — | |d         z  |d         z  S )Nr   r   © )rr   rs   s     r'   ú<lambda>zn_choose_k.<locals>.<lambda>ê  s   € ˜q 1 Q¤4™x¨!¨A¬$™€ r*   r   )r   rÔ   rJ   )Únr±   s     r'   Ú
n_choose_kr  Þ  sY   € ð 	ˆA‚v€vØˆqÝÐ.Ð.µµE¸!¸a¹%À!¹)ÀQÈÁUÑ4KÔ4KÍUÐSTÐVWÐZ[ÑV[É_Ì_Ñ0]Ô0]Ð_`ÑaÔaÐar*   c                 óØ   ‡ ‡‡— t          |¦  «        ‰k    rt          d¦  «        ‚ˆ fd„|D ¦   «         Št          ˆˆfd„t          ‰¦  «        D ¦   «         ¦  «        }t	          |¦  «        S )a1  Return  the lex index of a combination..

    Args:
        n (int): the total number of options .
        k (int): The number of elements.
        lst (list): list

    Returns:
        int: returns int index for lex order

    Raises:
        VisualizationError: if length of list is not equal to k
    zlist should have length kc                 ó    •— g | ]
}‰d z
  |z
  ‘ŒS )r   r  )r$   rr   r  s     €r'   r(   zlex_index.<locals>.<listcomp>ý  s!   ø€ Ð#Ð#Ð#˜!ˆA�‰E�A‰IÐ#Ð#Ð#r*   c              3   óV   •K  — | ]#}t          ‰‰d z
  |z
           |d z   ¦  «        V — Œ$dS )r   N)r  )r$   r%   Úcombr±   s     €€r'   ú	<genexpr>zlex_index.<locals>.<genexpr>þ  s=   øè è € ÐEÐE°q•
˜4  A¡¨¡	œ?¨A°©EÑ2Ô2ÐEÐEÐEÐEÐEÐEr*   )r˜   r   ÚsumrJ   rÌ   )r  r±   ÚlstÚdualmr#  s   ``  @r'   Ú	lex_indexr(  í  su   øøø€ õ ˆ3�x„x�1‚}€}Ý Ð!<Ñ=Ô=Ð=Ø#Ð#Ð#Ð#˜sÐ#Ñ#Ô#€DÝÐEÐEÐEÐEÐE½EÀ!¹H¼HÐEÑEÔEÑEÔE€EÝˆu‰:Œ:Ðr*   c                 óô   — t          | ¦  «        }|                      d¦  «        }|                      d¦  «        ||z
  k    rt          d¦  «        ‚d„ t          | ¦  «        D ¦   «         }t	          |||¦  «        S )z*Return the index of a string of 0s and 1s.Ú1Ú0zs must be a string of 0 and 1c                 ó$   — g | ]\  }}|d k    ¯|‘ŒS )r*  r  )r$   r¡   Úchars      r'   r(   z$bit_string_index.<locals>.<listcomp>  s!   € Ð=Ð=Ð=‘I�C˜°¸²°ˆC°°°r*   )r˜   Úcountr   Ú	enumerater(  )Úsr  r±   Úoness       r'   Úbit_string_indexr2    so   € åˆA‰Œ€AØ	�Š�‰Œ€AØ‡w‚wˆs�|„|�q˜1‘uÒÐÝ Ð!@ÑAÔAÐAØ=Ð=¥¨1¡¤Ð=Ñ=Ô=€DÝ�Q˜˜4Ñ Ô Ð r*   c                 óÀ   — t          j        | ¦  «        t           j        dz  dz  z   t           j        dz  z  }t          j        |t           j        dz  z  dd¦  «        }|S )zªMap a phase of a complexnumber to a color in (r,g,b).

    complex_number is phase is first mapped to angle in the range
    [0, 2pi] and then to the HSL color wheel
    r   r°   r   r3   )rB   ÚangleÚpiÚcolorsysÚ
hls_to_rgb)Úcomplex_numberÚanglesÚrgbs      r'   Úphase_to_rgbr;    sQ   € õ Œh�~Ñ&Ô&­"¬%°!©)°a©-Ñ8½R¼UÀQ¹YÑG€FÝ
Ô
˜f­¬°©	Ñ2°C¸Ñ
=Ô
=€CØ€Jr*   Tc                óv  — ddl m} ddl m} ddlm}	 ddl}
ddlm} ddlm	} t          | ¦  «        }|j        }|€t          d	¦  «        ‚|                     |j        ¦  «        \  }}|€d
}|€d}|                     |¬¦  «        }nd}|                     ¦   «         }|                     dd¬¦  «        }|                     |dd…dd…f         d¬¦  «        }|j                             dd¦  «         |j                             dd¦  «         |j                             dd¦  «         |j                             d¦  «         |                     dd¬¦  «         t3          |j        d¦  «        r|j                             d¦  «         t7          j        ddt6          j        z  d¦  «        }t7          j        dt6          j        d¦  «        }t7          j        t7          j        |¦  «        t7          j         |¦  «        ¦  «        }t7          j        t7          j         |¦  «        t7          j         |¦  «        ¦  «        }t7          j        t7          j!        t7          j"        |¦  «        ¦  «        t7          j        |¦  «        ¦  «        }| #                    |||dd|j$        d         dd¬¦  «         |j%         &                    d¦  «         |j'         &                    d¦  «         |j(         &                    d¦  «         |j%        j)         *                    d¦  «         |j'        j)         *                    d¦  «         |j(        j)         *                    d¦  «         | +                    g ¦  «         | ,                    g ¦  «         | -                    g ¦  «         t]          |j/        d         dz
  dd¦  «        D �]þ}||         d k    �rï|dd…|f         } t7          j0        | ¦  «         1                    ¦   «         }t7          j2        | |         ¦  «        dt6          j        z  z   dt6          j        z  z  }t7          j3        d!|z  ¦  «        }|| z  } |}t]          d|z  ¦  «        D �]{}ti          |¦  «        dd…          5                    |¦  «        }| 6                    d"¦  «        } d#| z  |z  dz   }!to          || ¦  «        }"tq          |¦  «        }#ts          | ¦  «        |z  t6          j        dz  z  |#dz  t6          j        |"z  z  z   }$| |dz  k    s| |dz  k    r+|#|"dz  k    r"t6          j        |$z
  dt6          j        z  |"z  z
  }$t7          j:        d|!dz  z
  ¦  «        t7          j        |$¦  «        z  }%t7          j:        d|!dz  z
  ¦  «        t7          j         |$¦  «        z  }&t7          j;        t7          j<        | |         | |          =                    ¦   «         ¦  «        ¦  «        }'t}          |'d¦  «        }'t          | |         ¦  «        }(d})|&d$k    rd|&z
  })t7          j@        |'d¦  «        �sL|�rId%}*t7          jA        t7          j:        d|!dz  z
  ¦  «        |!¦  «        }+|*t7          j         |+¦  «        z  t7          j        |$¦  «        z  },|*t7          j         |+¦  «        z  t7          j         |$¦  «        z  }-|*t7          j        |+¦  «        z  }.d&|z   d'z   }/|r…t7          j2        | |         ¦  «        t6          j        d(z  z   t6          j        dz  z  }0|r|/d)|0d*z  t6          j        z  d+›d,�z  }/n.t…          |0d¬-¦  «         C                    d.d/¦  «        }0|/d)|0› d0�z  }/| D                    |,|-|.|/d1d1d2¬3¦  «         | E                    |%g|&g|!g|(|(d4t7          j:        |'¦  «        d5z  |)¬6¦  «          |d|%gd|&gd|!gd7|'d8|(d¬9¦  «        }1| F                    |1¦  «         �Œ}t]          |dz   ¦  «        D ]¡} t7          j        d#t6          j        z  dt6          j        z  d:¦  «        }2d#| z  |z  dz   }t7          j:        d|dz  z
  ¦  «        }3|3t7          j        |2¦  «        z  }|3t7          j         |2¦  «        z  }| E                    |||d;dd<d=¬>¦  «         Œ¢| E                    dgdgdgd;d;d4dd¬6¦  «         �Œÿ d?}4t7          j!        |4¦  «        }2|
 G                    |4¦  «        }5|                     |dd…dd…f         ¦  «        }6|6 H                    |2|5d|4z  d@z  d…         |5dd|4z  d@z  …         z   dA¬B¦  «         |6 F                     |	dCd=dDd¬E¦  «        ¦  «         dF}7|rg dG¢}8ng dH¢}8|6 D                    dd|8d         d1d1dI¬J¦  «         |6 D                    |7d|8d         d1d1dI¬J¦  «         |6 D                    d|7|8d         d1d1dI¬J¦  «         |6 D                    |7 d|8d         d1d1dI¬J¦  «         |6 D                    d|7 |8d(         d1d1dI¬J¦  «         |rt“          |¦  «         |€|S | J                    |¦  «        S )Ka³	  Plot the qsphere representation of a quantum state.
    Here, the size of the points is proportional to the probability
    of the corresponding term in the state and the color represents
    the phase.

    Args:
        state (Statevector or DensityMatrix or ndarray): an N-qubit quantum state.
        figsize (tuple): Figure size in inches.
        ax (matplotlib.axes.Axes): An optional Axes object to be used for
            the visualization output. If none is specified a new matplotlib
            Figure will be created and used. Additionally, if specified there
            will be no returned Figure since it is redundant.
        show_state_labels (bool): An optional boolean indicating whether to
            show labels for each basis state.
        show_state_phases (bool): An optional boolean indicating whether to
            show the phase for each basis state.
        use_degrees (bool): An optional boolean indicating whether to use
            radians or degrees for the phase values in the plot.

    Returns:
        :class:`matplotlib:matplotlib.figure.Figure` :
            A matplotlib figure instance if the ``ax`` kwarg is not set

    Raises:
        MissingOptionalLibraryError: Requires matplotlib.
        VisualizationError: Input is not a valid N-qubit state.

        QiskitError: Input statevector does not have valid dimensions.

    Examples:
        .. plot::
           :alt: Output from the previous code.
           :include-source:

           from qiskit import QuantumCircuit
           from qiskit.quantum_info import Statevector
           from qiskit.visualization import plot_state_qsphere

           qc = QuantumCircuit(2)
           qc.h(0)
           qc.cx(0, 1)

           state = Statevector(qc)
           plot_state_qsphere(state)

        .. plot::
           :alt: Output from the previous code.
           :include-source:

           # You can show the phase of each state and use
           # degrees instead of radians

           from qiskit.quantum_info import DensityMatrix
           import numpy as np
           from qiskit import QuantumCircuit
           from qiskit.visualization import plot_state_qsphere

           qc = QuantumCircuit(2)
           qc.h([0, 1])
           qc.cz(0,1)
           qc.ry(np.pi/3, 0)
           qc.rx(np.pi/5, 1)
           qc.z(1)

           matrix = DensityMatrix(qc)
           plot_state_qsphere(matrix,
                show_state_phases = True, use_degrees = True)
    r   )Úgridspecr   )ÚCircleN)Úlinalgr   )ÚArrow3Dr   )r  r  Tr   Fr©   )ÚnrowsÚncolsr’   r“   g      ð¿r•   r   i  )ÚelevÚazimrá   ©r   r   r   r   é   z
grid.colorgš™™™™™É?)ÚrstrideÚcstriderw   r®   r  )r•   r•   r•   g        r-   gü©ñÒMbP?y       €      ð¿r*  éþÿÿÿgš™™™™™¹?gÍÌÌÌÌÌô?z$\vertz\rangle$r°   z
$é´   z.1fz^\circ$)Úndigitsr5  z\piú$r»   é   )r¸   r¹   rx   Úoé   )ÚmarkerfacecolorÚmarkeredgecolorÚmarkerÚ
markersizer®   é   ú-)Úmutation_scaler®   Ú
arrowstylerw   Úlwr–   )r3   r3   r3   ú:r3   )rw   rX  Úlsr®   é@   r   r³   )rÂ   Úradius)r   r   r1   r
  gffffffî?)zPhase
(Deg)r+  Ú90z180   Ú270)ÚPhasez$0$z$\pi/2$z$\pi$z$3\pi/2$r6   )ÚhorizontalalignmentÚverticalalignmentr8   )Kr=   r=  r   Úmatplotlib.patchesr>  ÚseabornÚscipyr?  r�   r@  r   r>   r   ÚeighrD   rš   rI   ÚGridSpecr›   r~   Ú
set_xlim3dÚ
set_ylim3drØ   r  Ú	view_initÚhasattrrá   rB   Úlinspacer5  Úouterrƒ   r‚   r1  rx   Úplot_surfaceÚrcParamsrO   Úset_pane_colorrR   rÛ   ÚlineÚ	set_colorrW   rY   Ú
set_zticksrJ   rK   ÚabsoluteÚargmaxr4  Úexpr!   r"   r.  r  r2  ÚfloatrT   rF   ÚdotÚconjrË   r;  ÚiscloseÚarctan2r   ÚreplaceÚtextÚplotÚ
add_artistÚhls_paletteÚpier   ra   )9rb   r   r‡   Úshow_state_labelsÚshow_state_phasesÚuse_degreesr   r=  rf   r>  Úsnsr?  r@  rg   r&   ÚeigvalsÚeigvecsr  rk   ÚgsÚuÚvrr   rs   ÚzÚidxÚlocr9  ÚanglesetÚdr%   ÚelementÚweightÚzvalueÚnumber_of_divisionsÚweight_orderr4  ÚxvalueÚyvalueÚprobÚ
colorstateÚalfaÚrprimeÚangle_thetaÚxvalue_textÚyvalue_textÚzvalue_textÚelement_textÚelement_angleÚar‹   rŠ   r  rÂ   rm   Úoffsetr  s9                                                            r'   Úplot_state_qspherer¢    sš
  € ð` $Ð#Ð#Ð#Ð#Ð#Ø(Ð(Ð(Ð(Ð(Ð(Ø)Ð)Ð)Ð)Ð)Ð)ØÐÐÐØÐÐÐÐÐØÐÐÐÐÐå
˜Ñ
Ô
€CØ
Œ.€CØ
€{Ý Ð!LÑMÔMÐMà—{’{ 3¤8Ñ,Ô,Ñ€GˆWà€Øˆà	€zØˆ
Ø�jŠj ˆjÑ)Ô)ˆˆàˆ
Ø�mŠm‰oŒoˆà	×	Ò	 ¨!Ð	Ñ	,Ô	,€Bà	�Š˜˜A˜a˜C  1 ˜Hœ°$ˆÑ	7Ô	7€BØ„G×Ò�t˜SÑ!Ô!Ð!Ø„G×Ò�t˜SÑ!Ô!Ð!Ø„G×Ò�t˜SÑ!Ô!Ð!Ø„G‡L‚L�ÑÔÐØ‡L‚L�a˜c€LÑ"Ô"Ð"õ ˆrŒwÐ(Ñ)Ô)ð *Ø
Œ×Ò˜yÑ)Ô)Ð)õ 	Œ�A�q�2œ5‘y "Ñ%Ô%€AÝ
Œ�A•r”u˜bÑ!Ô!€AÝ
Œ•”˜‘”�BœF 1™IœIÑ&Ô&€AÝ
Œ•”˜‘”�BœF 1™IœIÑ&Ô&€AÝ
Œ•”�œ ™œÑ$Ô$¥b¤f¨Q¡i¤iÑ0Ô0€AØ‡O‚OØ	ˆ1ˆa˜ A¨S¬\¸,Ô-GÈsÐ^_ð ñ ô ð ð
 „H×ÒÐ0Ñ1Ô1Ð1Ø„H×ÒÐ0Ñ1Ô1Ð1Ø„H×ÒÐ0Ñ1Ô1Ð1ð „H„M×ÒÐ0Ñ1Ô1Ð1Ø„H„M×ÒÐ0Ñ1Ô1Ð1Ø„H„M×ÒÐ0Ñ1Ô1Ð1ð ‡M‚M�"ÑÔÐØ‡M‚M�"ÑÔÐØ‡M‚M�"ÑÔÐõ �W”] 1Ô%¨Ñ)¨2¨rÑ2Ô2ð kñ kˆØ�3Œ<˜%ÒÑà˜A˜A˜A˜s˜F”OˆEÝ”+˜eÑ$Ô$×+Ò+Ñ-Ô-ˆCå”h˜u SœzÑ*Ô*¨Qµ´©YÑ6¸1½r¼u¹9ÑEˆFÝ”v˜c F™lÑ+Ô+ˆHØ˜uÑ$ˆEàˆAÝ˜1˜c™6‘]”]ð I!ñ I!�å˜a™&œ&   œ*×*Ò*¨3Ñ/Ô/�Ø Ÿš sÑ+Ô+�Ø˜f™ q™¨1Ñ,�Ý&0°°FÑ&;Ô&;Ð#Ý/°Ñ8Ô8�Ý˜v™œ¨Ñ*­r¬u°q©yÑ9Ø  1Ñ$­¬Ð0CÑ(CÑDñ�ð ˜Q ™U’N�NØ˜q 1™u’_�_¨<Ð;NÐQRÑ;RÒ+RÐ+RåœE E™M¨Qµ´©YÐ9LÑ-LÑM�Eåœ  V¨Q¡Y¡Ñ/Ô/µ"´&¸±-´-Ñ?�Ýœ  V¨Q¡Y¡Ñ/Ô/µ"´&¸±-´-Ñ?�õ ”w�rœv e¨A¤h°°a´·²±´Ñ@Ô@ÑAÔA�Ý˜4 ‘|”|�Ý)¨%°¬(Ñ3Ô3�
à�Ø˜S’=�=Ø ™<�Då”z $¨Ñ*Ô*ñ Ð/@ñ Ø �FÝ"$¤*­R¬W°Q¸À¹±]Ñ-CÔ-CÀVÑ"LÔ"L�KØ"(­2¬6°+Ñ+>Ô+>Ñ">ÅÄÈÁÄÑ"N�KØ"(­2¬6°+Ñ+>Ô+>Ñ">ÅÄÈÁÄÑ"N�KØ"(­2¬6°+Ñ+>Ô+>Ñ">�KØ#,¨wÑ#6¸Ñ#D�LØ(ð CÝ)+¬°%¸´(Ñ);Ô);½r¼uÀq¹yÑ)IÍbÌeÐVWÉiÑ(X˜Ø&ð CØ(Ð,[°-À#Ñ2EÍÌÑ2MÐ,[Ð,[Ð,[Ð,[Ñ[˜L˜Lå,4°]ÈAÐ,NÑ,NÔ,N×,VÒ,VÐW[Ð]cÑ,dÔ,d˜MØ(Ð,B°-Ð,BÐ,BÐ,BÑB˜LØ—G’GØ#Ø#Ø#Ø$Ø#Ø#Øð ñ ô ð ð —’Ø�HØ�HØ�HØ$.Ø$.ØÝ!œw t™}œ}¨rÑ1Øð ñ 	ô 	ð 	ð �GØ˜�KØ˜�KØ˜�KØ#%ØØ"Ø$Øð	ñ 	ô 	�ð —’˜aÑ Ô Ð Ñ õ    A¡™,œ,ð Qð Q�Ýœ B­¬¡J°µB´E±	¸3Ñ?Ô?�Ø˜‘K !‘O aÑ'�Ý”G˜A  1¡™HÑ%Ô%�Ø�œ˜u™œÑ%�Ø�œ˜u™œÑ%�Ø—’˜˜1˜a ¸1ÀÈC�ÑPÔPÐPÐPð �GŠGØ�Ø�Ø�Ø /Ø /ØØØð ñ 	ô 	ð 	ñ 	ð à
€AÝŒG�A‰JŒJ€EØ�_Š_˜QÑÔ€Fà
�/Š/˜"˜Q˜R˜R   ˜Vœ*Ñ
%Ô
%€CØ‡G‚GˆE˜&  Q¡¨!¡  Ô.°¸¸!¸a¹%À1¹*¸Ô1EÑEÈd€GÑSÔSÐSØ‡N‚N�6�6˜& #¨W¸QÐ?Ñ?Ô?Ñ@Ô@Ð@Ø€Fàð EØ=Ð=Ð=ˆˆàDÐDÐDˆà‡H‚HˆQ��6˜!”9°(ÈhÐac€HÑdÔdÐdØ‡H‚HØ��6˜!”9°(ÈhÐacð ñ ô ð ð ‡H‚HØ	ˆ6�6˜!”9°(ÈhÐacð ñ ô ð ð ‡H‚HØ	ˆ��F˜1”I°8ÈxÐbdð ñ ô ð ð ‡H‚HØ	ˆFˆ7�F˜1”I°8ÈxÐbdð ñ ô ð ð ð (Ý" 3Ñ'Ô'Ð'ØÐØˆ
à�{Š{˜8Ñ$Ô$Ð$r*   c                 ó  — ddl m} t          j        g d¢¦  «        }t          j        | j        |j        z   ¦  «        }	d| |fd||fd||ffD ]Y\  }
}}|dt          j        t          j        f         }|dt          j        t          j        f         }|||d|
f         z  z   |	d|
f<   ŒZ|	                     d|	j        dd…         z   ¦  «        }	g }t          |¦  «        t          | ¦  «        k    r|D ]}| 	                    |gdz  ¦  «         ŒnWt          |                     |¦  «        ¦  «        }t          |¦  «        t          | ¦  «        k     r|dt          | ¦  «        z  z  }t          |	¦  «        }t          ||¦  «        S )	aÃ  Generates shaded facecolors for shaded bars.

    This is here to work around a Matplotlib bug
    where alpha does not work in Bar3D.

    Args:
        x (array_like): The x- coordinates of the anchor point of the bars.
        y (array_like): The y- coordinates of the anchor point of the bars.
        z (array_like): The z- coordinates of the anchor point of the bars.
        dx (array_like): Width of bars.
        dy (array_like): Depth of bars.
        dz (array_like): Height of bars.
        color (array_like): sequence of valid color specifications, optional
    Returns:
        list: Shaded colors for bars.
    Raises:
        MissingOptionalLibraryError: If matplotlib is not installed
    r   N))©r   r   r   ©r   r   r   ©r   r   r   ©r   r   r   )©r   r   r   ©r   r   r   rE  ©r   r   r   )r¤  r§  r©  r¨  )r¥  rª  rE  r¦  )r¤  r¨  rª  r¥  )r§  r¦  rE  r©  r   r   .)r-   é   )rÁ   rÂ   rB   ÚarrayÚemptyrK   ÚnewaxisÚreshaper˜   ÚextendrÓ   rÏ   Ú_generate_normalsÚ_shade_colors)rr   rs   rŠ  ré   rê   rö   rw   rå   ÚcuboidÚpolysr%   ÚpÚdpÚ
facecolorsÚcÚnormalss                   r'   rÑ   rÑ   9  sž  € ð( (Ð'Ð'Ð'Ð'Ð'åŒXð+	
ð +	
ð +	
ñ-ô -€Fõ` ŒH�Q”W˜vœ|Ñ+Ñ,Ô,€Eà˜˜B�Z ! Q¨ ¨a°°B¨ZÐ8ð 0ð 0‰ˆˆ1ˆbØˆc•2”:�rœzÐ)Ô*ˆØ�•R”Z¥¤Ð+Ô,ˆØ˜B ¨¨Q¨¤Ñ/Ñ/ˆˆc�1ˆf‰ˆð �MŠM˜% %¤+¨a¨b¨b¤/Ñ1Ñ2Ô2€Eà€JÝ
ˆ5�z„z•S˜‘V”VÒÐàð 	'ð 	'ˆAØ×Ò˜q˜c A™gÑ&Ô&Ð&Ð&ð	'õ ˜'×/Ò/°Ñ6Ô6Ñ7Ô7ˆ
Ýˆz‰?Œ?�S ™VœVÒ#Ð#Ø˜!�c !™fœf™*Ñ$ˆJå Ñ&Ô&€GÝ˜ WÑ-Ô-Ð-r*   c                 óx  — t          | t          j        ¦  «        rS| j        d         }d|dz  d|z  dz  }}}| d|dd…f         | d|dd…f         z
  }| d|dd…f         | d|dd…f         z
  }n¹t          j        t          | ¦  «        df¦  «        }t          j        t          | ¦  «        df¦  «        }t          | ¦  «        D ]c\  }}t          |¦  «        }d|dz  d|z  dz  }}}||dd…f         ||dd…f         z
  ||dd…f<   ||dd…f         ||dd…f         z
  ||dd…f<   Œdt          j        ||¦  «        S )a`  Takes a list of polygons and return an array of their normals.

    Normals point towards the viewer for a face with its vertices in
    counterclockwise order, following the right hand rule.
    Uses three points equally spaced around the polygon.
    This normal of course might not make sense for polygons with more than
    three points not lying in a plane, but it's a plausible and fast
    approximation.

    Args:
        polygons (list): list of (M_i, 3) array_like, or (..., M, 3) array_like
            A sequence of polygons to compute normals for, which can have
            varying numbers of vertices. If the polygons all have the same
            number of vertices and array is passed, then the operation will
            be vectorized.
    Returns:
        normals: (..., 3) array_like
            A normal vector estimated for the polygon.
    rI  r   r©   r   .N)rÍ   rB   ÚndarrayrK   r­  r˜   r/  Úcross)	Úpolygonsr  Úi1Úi2Úi3Úv1Úv2Úpoly_iÚpss	            r'   r±  r±  ˜  sv  € õ( �(�BœJÑ'Ô'ð 2ð ŒN˜2ÔˆØ˜˜Q™  A¡¨¡
�ˆBˆØ�c˜2˜q˜q˜q�jÔ! H¨S°"°a°a°a¨ZÔ$8Ñ8ˆØ�c˜2˜q˜q˜q�jÔ! H¨S°"°a°a°a¨ZÔ$8Ñ8ˆˆõ ŒX•s˜8‘}”} aÐ(Ñ)Ô)ˆÝŒX•s˜8‘}”} aÐ(Ñ)Ô)ˆÝ# HÑ-Ô-ð 	2ð 	2‰JˆF�BÝ�B‘”ˆAØ˜A ™F A¨¡E¨Q¡J�B�ˆBØ˜r 1 1 1˜uœI¨¨2¨q¨q¨q¨5¬	Ñ1ˆBˆv�q�q�qˆy‰MØ˜r 1 1 1˜uœI¨¨2¨q¨q¨q¨5¬	Ñ1ˆBˆv�q�q�qˆy‰MˆMåŒ8�B˜ÑÔÐr*   c                 óf  ‡‡— ddl m}m} ddl m} ‰€ |dd¬¦  «        Šd„ Št	          j        ˆˆfd„|D ¦   «         ¦  «        }t	          j        |¦  «         }|                     ¦   «         rž |t          ||         ¦  «        t          ||         ¦  «        ¦  «        }t          ||         ¦  «        || <   | 
                    | ¦  «        } | dd…d	f         }	d
 ||¦  «        dd…t          j        f         d
z  z   | z  }
|	|
dd…d	f<   n&t	          j        | ¦  «                             ¦   «         }
|
S )z‚
    Shade *color* using normal vectors given by *normals*.
    *color* can also be an array of the same length as *normals*.
    r   )Ú	NormalizeÚLightSourceNéá   gà-� x3@)ÚazdegÚaltdegc                 ól   — t          j        | d         dz  | d         dz  z   | d         dz  z   ¦  «        S )Nr   r   r   )rB   rT   )r‰  s    r'   Úmodz_shade_colors.<locals>.modÌ  s4   € ÝŒw�q˜”t˜q‘y 1 Q¤4¨1¡9Ñ,¨q°¬t°q©yÑ8Ñ9Ô9Ð9r*   c                 óŠ   •— g | ]?} ‰|¦  «        r&t          j        | ‰|¦  «        z  ‰j        ¦  «        nt           j        ‘Œ@S r  )rB   rw  Ú	directionÚnan)r$   r  ÚlightsourcerÌ  s     €€r'   r(   z!_shade_colors.<locals>.<listcomp>Ð  sH   ø€ ÐZÐZÐZÈQ°c°c¸!±f´fÐ	H�Œ��C�C˜‘F”F‘
˜KÔ1Ñ	2Ô	2Ð	2Å"Ä&ÐZÐZÐZr*   r©   r3   )rÁ   rÆ  rÇ  rÂ   rB   r¬  ÚisnanrÐ   rË   rE   rÏ   r®  Ú
asanyarrayrÈ   )rw   r¹  rÐ  rÆ  rÇ  rå   ÚshadeÚmaskÚnormr®   rÂ   rÌ  s     `        @r'   r²  r²  À  sm  øø€ ð
 9Ð8Ð8Ð8Ð8Ð8Ð8Ð8Ø'Ð'Ð'Ð'Ð'Ð'àÐà!�k¨°GÐ<Ñ<Ô<ˆð:ð :ð :õ ŒHØZÐZÐZÐZÐZÐRYÐZÑZÔZñô €Eõ ŒH�U‰OŒOÐ€Dà‡x‚x�z„zð -Øˆy�˜U 4œ[Ñ)Ô)­3¨u°T¬{Ñ+;Ô+;Ñ<Ô<ˆÝ˜5 œ;Ñ'Ô'ˆˆtˆe‰Ø×%Ò% eÑ,Ô,ˆð �a�a�a˜�d”ˆØ˜˜˜U™œ A A A¥r¤z MÔ2°SÑ8Ñ8¸EÑAˆØˆˆqˆqˆq�!ˆt‰ˆå”˜uÑ%Ô%×*Ò*Ñ,Ô,ˆà€Mr*   Úketrb   ÚdimsÚ
conventionÚreturnc                 ó¼  — |€+t          |                      ¦   «         ¦  «        dhk    rd}nd}d}d}|r!d}| j                             ¦   «         }d|› d�}| j        }t	          |                     ¦   «         ¦  «        d	k    o%t          |                     ¦   «         ¦  «        dhk    }|d
k    r|rt          | j        fi |¤Ž}	nt          | j        fddi|¤Ž}	||	z   |z   S )aS  Return a Latex representation of a state. Wrapper function
    for `qiskit.visualization.array_to_latex` for convention 'vector'.
    Adds dims if necessary.
    Intended for use within `state_drawer`.

    Args:
        state: State to be drawn
        dims (bool): Whether to display the state's `dims`
        convention (str): Either 'vector' or 'ket'. For 'ket' plot the state in the ket-notation.
                Otherwise plot as a vector
        **args: Arguments to be passed directly to `array_to_latex` for convention 'ket'

    Returns:
        Latex representation of the state
    Nr   FTr   z\begin{align}
z\\
\text{dims=z}
\end{align}r   rÖ  Úsource)	Úsetr×  Ú	_op_shapeÚdims_lr˜   Údims_rÚ_state_to_latex_ketÚ_datar   )
rb   r×  rØ  ÚargsÚprefixÚsuffixÚdims_strÚoperator_shapeÚis_qubit_statevectorÚ	latex_strs
             r'   Ústate_to_latexré  ä  s  € ð$ €|Ýˆu�zŠz‰|Œ|ÑÔ  Ò#Ð#ØˆDˆDàˆDà€FØ€FØð DØ#ˆØ”?×)Ò)Ñ+Ô+ˆØC xÐCÐCÐCˆà”_€Nõ ˜~×4Ò4Ñ6Ô6Ñ7Ô7¸1Ò<ÐdÅÀ^×EZÒEZÑE\ÔE\ÑA]ÔA]ÐbcÐadÒAdÐØ�UÒÐÐ3ÐÝ'¨¬Ð<Ð<°tÐ<Ð<ˆ	ˆ	å" 5¤;ÐDÐD°tÐD¸tÐDÐDˆ	Ø�IÑ Ñ&Ð&r*   rª   ÚnumbersÚdecimalsc                 ól   — d}g }| D ],}t          |||d¬¦  «        }|                     |¦  «         d}Œ-|S )a>  Convert a list of numbers to latex formatted terms

    The first non-zero term is treated differently. For this term a leading + is suppressed.

    Args:
        numbers: List of numbers to format
        decimals: Number of decimal places to round to (default: 10).
    Returns:
        List of formatted terms
    T)rë  Ú
first_termÚcoefficientF)r   Úappend)rê  rë  rí  ÚtermsÚnumberÚterms         r'   Ú_numbers_to_latex_termsró    sR   € ð €JØ€EØð ð ˆÝ˜V¨hÀ:Ð[_Ð`Ñ`Ô`ˆØ�Š�TÑÔÐØˆ
ˆ
Ø€Lr*   rM  rD   Úmax_sizerã  c                 óN  ‡— t          t          j        t          | ¦  «        ¦  «        ¦  «        Šˆfd„}t	          j        | |¦  «        } t	          j        | dk    ¦  «        d                              ¦   «         }t          |¦  «        |k    rB|d|dz  …         dgz   || dz  dz   d…         z   }t          | |         |¦  «        }d||dz  <   nt          | |         |¦  «        }d}t          |¦  «        D ]+\  }}	|	€|dz  }Œ||         }
 ||	¦  «        }||
› d|› d	�z  }Œ,||z   S )
aò  Convert state vector to latex representation

    Args:
        data: State vector
        max_size: Maximum number of non-zero terms in the expression. If the number of
                 non-zero terms is larger than the max_size, then the representation is truncated.
        prefix: Latex string to be prepended to the latex, intended for labels.
        decimals: Number of decimal places to round to (default: 10).

    Returns:
        String with LaTeX representation of the state vector
    c                 óX   •— t          | ¦  «        dd …                              ‰¦  «        S )Nr   r    )r%   r&   s    €r'   Úket_namez%_state_to_latex_ket.<locals>.ket_name3  s%   ø€ Ý�1‰vŒv�a�b�bŒz×Ò Ñ$Ô$Ð$r*   r   Nr   r   r   z
 + \ldots z |z\rangle)
rÌ   r?   rA   r˜   rB   ÚaroundÚwhereÚtolistró  r/  )rD   rô  rã  rë  r÷  Únonzero_indicesÚlatex_termsrè  r‹  Úket_idxrò  rÖ  r&   s               @r'   rà  rà  "  sk  ø€ õ �dŒi�˜D™	œ	Ñ"Ô"Ñ
#Ô
#€Cð%ð %ð %ð %ð %õ Œ9�T˜8Ñ$Ô$€DÝ”h˜t qšyÑ)Ô)¨!Ô,×3Ò3Ñ5Ô5€OÝ
ˆ?ÑÔ˜hÒ&Ð&à˜O˜h¨!™m˜OÔ,°¨sÑ2°_ÀhÀYÐRSÁ^ÐVWÑEWÐEYÐEYÔ5ZÑZð 	õ .¨d°?Ô.CÀXÑNÔNˆØ)-ˆ˜ A™Ñ&Ð&å-¨d°?Ô.CÀXÑNÔNˆà€IÝ! /Ñ2Ô2ð 2ð 2‰ˆˆWØˆ?Ø˜Ñ&ˆIˆIà˜sÔ#ˆDØ�(˜7Ñ#Ô#ˆCØ˜DÐ1Ð1 CÐ1Ð1Ð1Ñ1ˆIˆIØ�IÑÐr*   c                   ó&   — e Zd ZdZdd„Zd„ Zd„ ZdS )	Ú
TextMatrixzeText representation of an array, with `__str__` method so it
    displays nicely in Jupyter notebooksr   Nr   c                 óà  — || _         || _        |€ït          |t          t          f¦  «        r&t          |                     ¦   «         ¦  «        dhk    s¨t          |t          ¦  «        r–t          | 	                    ¦   «         ¦  «        t          | 
                    ¦   «         ¦  «        k    rRt          | 	                    ¦   «         ¦  «        t          | 
                    ¦   «         ¦  «        cxk    rdhk    rn nd}nd}|| _        || _        || _        t          |t          ¦  «        r	|| _        d S t          |t          ¦  «        rt          |¦  «        dz  | _        d S |d         | _        d S )Nr   FTr   )rb   rô  rÍ   r   r   rÜ  r×  r   r˜   Ú
input_dimsÚoutput_dimsrã  rä  rÌ   rË   )Úselfrb   rô  r×  rã  rä  s         r'   Ú__init__zTextMatrix.__init__P  sT  € ØˆŒ
Ø ˆŒØˆ<Ý˜5¥;µÐ">Ñ?Ô?ð ÅCÈÏ
Ê
ÉÌÑDUÔDUÐZ[ÐY\ÒD\ÐD\Ý˜5¥(Ñ+Ô+ð E]å˜×(Ò(Ñ*Ô*Ñ+Ô+­s°5×3DÒ3DÑ3FÔ3FÑ/GÔ/GÒGÐGÝ˜×(Ò(Ñ*Ô*Ñ+Ô+­s°5×3DÒ3DÑ3FÔ3FÑ/GÔ/GÐNÐNÒNÐNÈAÈ3ÒNÐNÐNÐNÐNà��à�ØˆŒ	ØˆŒØˆŒÝ�h¥Ñ$Ô$ð 	(Ø$ˆDŒMˆMˆMÝ˜�}Ñ-Ô-ð 	(õ   ™MœM¨QÑ.ˆDŒMˆMˆMà$ QœKˆDŒMˆMˆMr*   c                 óâ  — | j         }t          j        | j        j        | j        |d¬¦  «        }d}| j        r¤|dz  }|dt          | j        ¦  «        z  z  }t          | j        t          t          f¦  «        r%|d| j        j                             ¦   «         › �z  }n?|d| j                             ¦   «         › d�z  }|d	| j                             ¦   «         › �z  }| j        |z   |z   | j        z   S )
Nú,)rã  Ú	thresholdÚ	separatorr   z,
ú zdims=zinput_dims=z, zoutput_dims=)rô  rB   Úarray2stringrb   rá  rã  r×  r˜   rÍ   r   r   rÝ  rÞ  r  r  rä  )r  r  rD   Údimstrs       r'   Ú__str__zTextMatrix.__str__h  s  € Ø”Mˆ	ÝŒØŒJÔ T¤[¸IÐQTð
ñ 
ô 
ˆð ˆØŒ9ð 	DØ�E‰MˆDØ�c�C ¤Ñ,Ô,Ñ,Ñ,ˆFÝ˜$œ*¥{µMÐ&BÑCÔCð DØÐA $¤*Ô"6×"=Ò"=Ñ"?Ô"?ÐAÐAÑA��àÐC¨¬
×(=Ò(=Ñ(?Ô(?ÐCÐCÐCÑC�ØÐC¨¬×)?Ò)?Ñ)AÔ)AÐCÐCÑC�àŒ{˜TÑ! FÑ*¨T¬[Ñ8Ð8r*   c                 ó*   — |                       ¦   «         S ©N)r  )r  s    r'   Ú__repr__zTextMatrix.__repr__y  s   € Ø�|Š|‰~Œ~Ðr*   )r   Nr   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r  r  r  r  r*   r'   rÿ  rÿ  L  sP   € € € € € ð,ð ,ð(ð (ð (ð (ð09ð 9ð 9ð"ð ð ð ð r*   rÿ  c           	      óF  — t          j        ¦   «         }d}|€|r|                     dd¦  «        }|}|                     ¦   «         }t          t
          t          t          t          t          t          dœ}|dk    rCt          j                             d¦  «         ddlm} |d         } |d	 || fi |¤Ž› d	�¦  «        S |dk    r|                      ¦   «         S 	 ||         } || fi |¤ŽS # t"          $ r.}t%          d
|› dt'          | ¦  «        j        › d�¦  «        |‚d}~ww xY w)aç  Returns a visualization of the state.

    **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
            circuit. Valid choices are ``text``, ``latex``, ``latex_source``,
            ``qsphere``, ``hinton``, ``bloch``, ``city`` or ``paulivec``.
            Default is `'text`'.
        drawer_args: Arguments to be passed to the relevant drawer. For
            'latex' and 'latex_source' see ``array_to_latex``

    Returns:
        :class:`matplotlib.figure` or :class:`str` or
        :class:`TextMatrix` or :class:`IPython.display.Latex`:
        Drawing of the state.

    Raises:
        MissingOptionalLibraryError: when `output` is `latex` and IPython is not installed.
        ValueError: when `output` is not a valid selection.
    ÚreprNÚstate_drawer)r|  Úlatex_sourceÚqsphereÚhintonr�   ÚcityÚpaulivecÚlatexr   )ÚLatexr  z$$ú'z$' is not a valid option for drawing z—
             objects. Please choose from:
            'text', 'latex', 'latex_source', 'qsphere', 'hinton',
            'bloch', 'city' or 'paulivec'.)r   Ú
get_configÚgetÚlowerrÿ  ré  r¢  rz   r¢   r  r  Ú
_optionalsÚHAS_IPYTHONÚrequire_nowÚIPython.displayr  r  ÚKeyErrorrÉ   Útyper  )	rb   ÚoutputÚdrawer_argsÚconfigÚdefault_outputÚdrawersr  Ú	draw_funcÚerrs	            r'   r  r  }  s~  € õL Ô#Ñ%Ô%€Fà€NØ€~Øð 	@Ø#ŸZšZ¨¸Ñ?Ô?ˆNØˆØ�\Š\‰^Œ^€Fõ Ý&Ý%Ý#Ý'ÝÝ'ðð €Gð �ÒÐÝÔ×*Ò*¨>Ñ:Ô:Ð:Ø)Ð)Ð)Ð)Ð)Ð)à˜NÔ+ˆ	ØˆuÐ=˜)˜) EÐ9Ð9¨[Ð9Ð9Ð=Ð=Ð=Ñ>Ô>Ð>à�ÒÐØ�~Š~ÑÔÐð	Ø˜F”Oˆ	Øˆy˜Ð.Ð. +Ð.Ð.Ð.øÝð ð ð Ýð.�&ð .ð .½dÀ5¹k¼kÔ>Rð .ð .ð .ñ
ô 
ð
 ð	øøøøðøøøs   ÃC( Ã(
D Ã2)DÄD c           	      óê  ‡— t          | ¦  «        Š‰j        }|€t          d¦  «        ‚t          g d¢¦  «        }g }t	          |¦  «        D ]§}|dk    rht          j        t          j        d|dz
  ft          ¬¦  «        t          j        d|dz
  ft          ¬¦  «        ¦  «         	                    ||d¬¦  «        }n|}ˆfd	„| 
                    ¦   «         D ¦   «         }|                     |¦  «         Œ¨|S )
a  Return list of Bloch vectors for each qubit

    Args:
        state (DensityMatrix or Statevector): an N-qubit state.

    Returns:
        list: list of Bloch vectors (x, y, z) for each qubit.

    Raises:
        VisualizationError: if input is not an N-qubit state.
    Nr   )ÚXÚYÚZr   r©   )ÚdtypeT)Úqubitc           
      óŒ   •— g | ]@}t          j        t          j        t          j        |‰j        ¦  «        ¦  «        ¦  «        ‘ŒAS r  )rB   rF   Útracerw  rD   )r$   Úmatrg   s     €r'   r(   z+_bloch_multivector_data.<locals>.<listcomp>å  s;   ø€ Ð`Ð`Ð`ÀC•r”w�rœx­¬¨s°C´HÑ(=Ô(=Ñ>Ô>Ñ?Ô?Ð`Ð`Ð`r*   )r   r>   r   r	   rJ   Úfrom_symplecticrB   rÆ   ÚboolÚinsertÚmatrix_iterrï  )rb   r&   Úpauli_singlesr�   r%   ÚpaulisÚbloch_staterg   s          @r'   r—   r—   Ì  s  ø€ õ ˜Ñ
Ô
€CØ
Œ.€CØ
€{Ý Ð!LÑMÔMÐMÝ˜o˜o˜oÑ.Ô.€MØ€JÝ�3‰ZŒZð 'ð 'ˆØ�Š7ˆ7ÝÔ.Ý”˜!˜c A™g˜­tÐ4Ñ4Ô4µb´hÀÀCÈ!ÁG¸~ÕUYÐ6ZÑ6ZÔ6Zñô çŠf�Q˜¨TˆfÑ2Ô2ð ˆFð #ˆFØ`Ð`Ð`Ð`È6×K]ÒK]ÑK_ÔK_Ð`Ñ`Ô`ˆØ×Ò˜+Ñ&Ô&Ð&Ð&ØÐr*   c                 óê   — t          j        t          | ¦  «        ¦  «        }|j        €t	          d¦  «        ‚|j                             ¦   «         t          j        |j	        d|j        z  z  ¦  «        fS )zýReturn paulivec data for plotting.

    Args:
        state (DensityMatrix or Statevector): an N-qubit state.

    Returns:
        tuple: (labels, values) for Pauli vector.

    Raises:
        VisualizationError: if input is not an N-qubit state.
    Nr   r   )
r
   Úfrom_operatorr   r>   r   r=  Ú	to_labelsrB   rF   Úcoeffs)rb   rg   s     r'   r  r  ê  sd   € õ Ô
%¥m°EÑ&:Ô&:Ñ
;Ô
;€CØ
„~ÐÝ Ð!LÑMÔMÐMØŒ:×ÒÑ!Ô!¥2¤7¨3¬:¸¸3¼>Ñ8IÑ+IÑ#JÔ#JÐJÐJr*   )r   NNN)r   NNr{   N)r   N)r   NNr   NN)NNTFFr  )NrÖ  )rª   )rM  r   rª   )<r  r?   Útypingr   r   Ú	functoolsr   r6  ÚnumpyrB   Úqiskitr   Ú&qiskit.quantum_info.states.statevectorr   Ú&qiskit.quantum_info.operators.operatorr   Ú(qiskit.quantum_info.operators.symplecticr	   r
   Ú(qiskit.quantum_info.states.densitymatrixr   Úqiskit.utilsr   r"  Úqiskit.circuit.tools.pi_checkr   r¬  r   r   Úutilsr   Ú
exceptionsr   ÚHAS_MATPLOTLIBÚrequire_in_callrz   r�   r¢   r  r  r  r(  r2  r;  ÚHAS_SEABORNr¢  rÑ   r±  r²  r9  rœ   ré  ÚcomplexrÌ   ró  rà  rÿ  r  r—   r  r  r*   r'   ú<module>rS     s‡  ðð ð ð €€€Ø Ð Ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø €€€à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø >Ð >Ð >Ð >Ð >Ð >Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø MÐ MÐ MÐ MÐ MÐ MÐ MÐ MØ BÐ BÐ BÐ BÐ BÐ BØ 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2à 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø -Ð -Ð -Ð -Ð -Ð -Ø *Ð *Ð *Ð *Ð *Ð *ð ÔÔ*ðQ%Ð]að Q%ð Q%ð Q%ð Q%ñ +Ô*ðQ%ðh ÔÔ*àNRðAð Að Añ +Ô*ðAðH ÔÔ*ð Øðh%ð
 ØØØØðh%ð h%ð h%ð h%ñ +Ô*ðh%ðV ÔÔ*ð ØØ
Ø
ØØðy%ð ðy%ð y%ð y%ð y%ñ +Ô*ðy%ðx ÔÔ*ðp%ÐX\ð p%ð p%ð p%ð p%ñ +Ô*ðp%ðfbð bð bðð ð ð*!ð !ð !ðð ð ð ÔÔ*ØÔÔ'ð ØØØØð]%ð ð]%ð ]%ð ]%ð ]%ñ (Ô'ñ +Ô*ð]%ð@	 ÔÔ*ð[.ð [.ñ +Ô*ð[.ð|%ð %ð %ðP!ð !ð !ð !ðJ TYð''ð ''Ø�˜mÐ+Ô,ð''Ø48ð''ØMPð''àð''ð ''ð ''ð ''ðTð  T¨'¤]ð ¸cð È4ÐPSÌ9ð ð ð ð ð* PRð'ð 'Ø
ˆwŒ-ð'Ø#&ð'Ø58ð'ØILð'àð'ð 'ð 'ð 'ðT.ð .ð .ð .ð .ñ .ô .ð .ðbLð Lð Lð Lð^ð ð ð<Kð Kð Kð Kð Kr*   