
    z	i                     p    S r SSKrSSKJr  SSKJr  \R                  " S5      S	S j5       rS
S jr	SS jr
g)z
Tools to create LaTeX arrays.
    N)MissingOptionalLibraryError)	HAS_SYMPYz1Create a latex representation of a ket expressionc                 j   SSK n[        R                  " XS9R                  5       n UR	                  U SS9n[        XTR                  R                  R                  5      (       a   UR                  S:  a  UR                  5       n[        XTR                  R                  R                  5      (       a  [        XQ5      nUR                  USS9nU(       d  U$ [        XTR                  R                  5      (       a  SU S	3nUS
:X  a  SnUS:X  a  SnU(       d  UR                  S5      (       d  SU 3nU$ )a  Convert a complex number to latex code suitable for a ket expression

Args:
    raw_value (complex): Value to convert.
    decimals (int): Number of decimal places to round to (default 15).
    coefficient (bool): Whether the number is to be used as a coefficient
                        of a ket.
    first_term (bool): If a coefficient, whether this number is the first
                       coefficient in the expression.
Returns:
    str: latex code
r   NdecimalsF)rational2   )	full_prec()1 z-1-+)sympynparounditem	nsimplify
isinstancecorenumbersRationaldenominatorevalfFloatroundlatexAdd
startswith)	raw_valuer   
first_termcoefficientr   valueelements          T/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/visualization/array.py_num_to_latexr'      s     		)7<<>IOOIO6E%++4455%:K:Kb:P%++1122e&kk%5k1G%((gYa.#~$g0055gY-N    c                 
  ^^
 [        U5      S:  a  [        S5      eSU S3nUS-  nU4S jm
U
4S jnUu  pgU R                  S:X  a  XE" U /U5      -  nO[        U 5      U:  a  XE" U SUS	-   U5      -  nX`R                  S   :  a  US
U R                  S   -  -  nO:US	-  nUS	-  [
        R                  " US	5      -   S-
  n	US
U-  -  nUS-  nUS
U	-  -  nUSS S-   nXE" X* S	-  S-   S U5      -  nO
XE" X5      -  nUS-  nU$ )a  Latex representation of a complex numpy array (with maximum dimension 2)

Args:
    matrix (ndarray): The matrix to be converted to latex, must have dimension 2.
    decimals (int): For numbers not close to integers, the number of decimal places
                     to round to.
    prefix (str): Latex string to be prepended to the latex, intended for labels.
    max_size (list(```int```)): Indexable containing two integers: Maximum width and maximum
                      height of output Latex matrix (including dots characters). If the
                      width and/or height of matrix exceeds the maximum, the centre values
                      will be replaced with dots. Maximum width or height must be greater
                      than 3.

Returns:
    str: Latex representation of the matrix

Raises:
    ValueError: If minimum value in max_size < 3
    MissingOptionalLibraryError: If sympy is not installed
   z=Smallest value in max_size must be greater than or equal to 3
z\begin{bmatrix}
c                 J   > SnU  H  n[        UTS9nXS-   -  nM     US S nU$ )Nr   r   z & )r'   )elements	el_stringel
num_stringr   s       r&   _elements_to_latex,_matrix_to_latex.<locals>._elements_to_latex`   s@     	B&rH=Je++I  crN	r(   c                    > SnU  HM  n[        U5      U::  a  UT" U5      -  nO*UT" US US-   5      -  nUS-  nUT" X1* S-  S-   S  5      -  nUS-  nMO     U$ )Nr      z& \cdots &    z \\
 )len)rows	max_width
row_stringrr2   s       r&   _rows_to_latex(_matrix_to_latex.<locals>._rows_to_latexj   s     
A1v"033
03CY!^1DEE
n,
0:?Q3F3H1IJJ
*$J  r(   r6   Nr5   z	\vdots & z	\ddots & r-   z\\
 z\end{bmatrix}
)min
ValueErrorndimr7   shaper   mod)matrixr   prefixmax_size
out_stringr<   r9   
max_height	pre_vdots
post_vdotsr2   s    `        @r&   _matrix_to_latexrJ   E   s^   * 8}q\]]fXRJ&&J %I{{anfXy99
	Vz	! 	nV,=jAo%>	JJ
Q',a88J "QI"a"&&A*>>BJ,22J,&J,33J_y0
nVK1,<q,@,B%CYOO
 	nV77
$$Jr(   c                 f    [         R                  " U 5      n U S   S-   nU R                  S:  a  [	        S5      e[        U[        5      (       a  XD4n[        XX$S9nUSL a  U$  SS	KJ	n  U" SU S35      $ ! [         a  n[        S5      UeSnAff = f! [         a  n[        S
SSS9UeSnAff = f)a  Latex representation of a complex numpy array (with dimension 1 or 2)

Args:
    array (ndarray): The array to be converted to latex, must have dimension 1 or 2 and
                     contain only numerical data.
    precision (int): For numbers not close to integers or common terms, the number of
                     decimal places to round to.
    prefix (str): Latex string to be prepended to the latex, intended for labels.
    source (bool): If ``False``, will return IPython.display.Latex object. If display is
                   ``True``, will instead return the LaTeX source string.
    max_size (list(int) or int): The maximum size of the output Latex array.

        * If list(``int``), then the 0th element of the list specifies the maximum
          width (including dots characters) and the 1st specifies the maximum height
          (also inc. dots characters).
        * If a single ``int`` then this value sets the maximum width _and_ maximum
          height.

Returns:
    str or IPython.display.Latex: If ``source`` is ``True``, a ``str`` of the LaTeX
        representation of the array, else an ``IPython.display.Latex`` representation of
        the array.

Raises:
    TypeError: If array can not be interpreted as a numerical numpy array.
    ValueError: If the dimension of array is not 1 or 2.
    MissingOptionalLibraryError: If ``source`` is ``False`` and ``IPython.display.Latex`` cannot be
                 imported. Or if sympy is not installed.
r   r6   z}array_to_latex can only convert numpy arrays containing numerical data,
        or types that can be converted to such arraysNr5   zBarray_to_latex can only convert numpy ndarrays of dimension 1 or 2)r   rD   rE   T)LatexIPythonarray_to_latexzpip install ipython)libnamenamepip_installz$$)r   asarray	TypeErrorr@   r?   r   intrJ   IPython.displayrL   ImportErrorr   )	array	precisionrD   sourcerE   _erroutstrrL   s	            r&   rN   rN      s    <

5!!HqL zzA~]^^(C  'eZF~) 2fXR!!3  9
 	&  )!-
 		s/   A7 %B 7
BBB
B0B++B0)   TF)
   r   )   r_   )r^   r   Fr_   )__doc__numpyr   qiskit.exceptionsr   qiskit.utils.optionalsr   require_in_callr'   rJ   rN    r(   r&   <module>rf      sC     9 , NO+ P+\M`:"r(   