§
    º³Ùg6"  ã                  óˆ   — d 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 ddlmZ dd„Zddd„Zd„ Zdd„Zd„ ZdS )z3
Quantum information utility functions for states.
é    )ÚannotationsN)ÚQiskitError)ÚStatevector)ÚDensityMatrix)ÚSuperOp)ÚATOL_DEFAULTÚstateúStatevector | DensityMatrixÚqargsÚlistÚreturnr   c                ó   — t          | d¬¦  «        } | j                             |¬¦  «        }|j        |_        |j        |_        |j        dk    r|                      ¦   «         S t          | t          ¦  «        r½t          | j                             ¦   «         ¦  «        dz
  t          j        |¦  «        z
  }| j                             | j        j        ¦  «        }t          j        ||                     ¦   «         ||f¬¦  «        }t          j        ||j        ¦  «        }t+          ||j        ¬¦  «        S |s|                      ¦   «         S |                      |¦  «        }t1          t          j        |d         ¦  «                             d|d         dz  ¦  «        |d         gdg¬	¦  «        }|                      ||d         g¦  «        }t7          |dd
…         |dd
…         ¦  «        D ]Y\  }	}
t1          t          j        |
¦  «                             d|
dz  ¦  «        |
gdg¬	¦  «        }|                     ||	g¦  «        }ŒZ||_        |S )aÉ  Return reduced density matrix by tracing out part of quantum state.

    If all subsystems are traced over this returns the
    :meth:`~qiskit.quantum_info.DensityMatrix.trace` of the
    input state.

    Args:
        state (Statevector or DensityMatrix): the input state.
        qargs (list): The subsystems to trace over.

    Returns:
        DensityMatrix: The reduced density matrix.

    Raises:
        QiskitError: if input state is invalid.
    F©Úvalidate)r   r   é   )Úaxes©Údimsé   )Ú
input_dimsÚoutput_dimsN)Ú_format_stateÚ	_op_shapeÚremoveÚ_dims_lÚ_dims_rÚ_num_qargs_lÚ_num_qargs_rÚsizeÚtraceÚ
isinstancer   ÚlenÚdims_lÚnpÚarrayÚ_dataÚreshapeÚtensor_shapeÚ	tensordotÚconjÚshaper   Úcopyr   r   ÚeyeÚevolveÚzip)r	   r   Útraced_shapeÚtrace_systemsÚarrÚrhor   Útr_opÚretÚqargÚdims              úV/granary/edev/quantum/lib/python3.11/site-packages/qiskit/quantum_info/states/utils.pyÚpartial_tracer9      s  € õ" ˜%¨%Ð0Ñ0Ô0€Eð ”?×)Ò)°Ð)Ñ6Ô6€Là'Ô/€LÔØ ,Ô 9€LÔð Ô˜AÒÐØ�{Š{‰}Œ}Ðõ �%�Ñ%Ô%ð =Ý˜EœO×2Ò2Ñ4Ô4Ñ5Ô5¸Ñ9½B¼HÀU¹O¼OÑKˆØŒk×!Ò! %¤/Ô">Ñ?Ô?ˆÝŒl˜3 §¢¡
¤
°-ÀÐ1OÐPÑPÔPˆÝŒj˜˜lÔ0Ñ1Ô1ˆÝ˜S |Ô';Ð<Ñ<Ô<Ð<ð ð Ø�zŠz‰|Œ|Ðà�:Š:�eÑÔ€DÝ•B”F˜4 œ7‘O”O×+Ò+¨A¨t°A¬w¸!©|Ñ<Ô<È$ÈqÌ'ÈÐabÐ`cÐdÑdÔd€EØ
�,Š,�u˜u Qœx˜jÑ
)Ô
)€Cå˜˜q˜r˜rœ D¨¨¨¤HÑ-Ô-ð (ð (‰	ˆˆcÝ�œ˜s™œ×+Ò+¨A¨s°A©vÑ6Ô6ÀCÀ5ÐWXÐVYÐZÑZÔZˆØ�jŠj˜  Ñ'Ô'ˆˆà €C„MØ€Jó    r   Úpvecúlist | np.ndarrayÚbaseÚintÚfloatc                óÀ   ‡— |dk    rd„ }n-|t           j        k    rd„ }nt          j        |¦  «        Šˆfd„}d}| D ] }d|cxk     rdk     rn Œ| ||¦  «        z  }Œ!|S )aö  Compute the Shannon entropy of a probability vector.

    The shannon entropy of a probability vector
    :math:`\vec{p} = [p_0, ..., p_{n-1}]` is defined as

    .. math::

        H(\vec{p}) = \sum_{i=0}^{n-1} p_i \log_b(p_i)

    where :math:`b` is the log base and (default 2), and
    :math:`0 \log_b(0) \equiv 0`.

    Args:
        pvec (array_like): a probability vector.
        base (int): the base of the logarithm [Default: 2].

    Returns:
        float: The Shannon entropy H(pvec).
    r   c                ó2   — |  t          j        | ¦  «        z  S ©N)ÚmathÚlog2©Úxs    r8   Úlogfnzshannon_entropy.<locals>.logfni   s   € Ø�2�œ	 !™œÑ$Ð$r:   c                ó2   — |  t          j        | ¦  «        z  S rB   ©rC   ÚlogrE   s    r8   rG   zshannon_entropy.<locals>.logfnn   s   € Ø�2�œ ™œÑ#Ð#r:   c                ó:   •— |  t          j        | ¦  «        z  ‰z  S rB   rI   )rF   Úlog_bases    €r8   rG   zshannon_entropy.<locals>.logfnt   s   ø€ Ø�2�œ ™œÑ# hÑ.Ð.r:   g        r   r   )r$   ÚerC   rJ   )r;   r=   rG   Úh_valrF   rL   s        @r8   Úshannon_entropyrO   S   s²   ø€ ð( ˆq‚y€yð	%ð 	%ð 	%ð 	%ð 
•”Šˆð	$ð 	$ð 	$ð 	$õ ”8˜D‘>”>ˆð	/ð 	/ð 	/ð 	/ð 	/ð €EØð ð ˆØˆqˆ9ˆ9Š9ˆ9�1Š9ˆ9ˆ9ˆ9ˆ9Ø�U�U˜1‘X”XÑˆEøØ€Lr:   c                ó  ‡‡‡‡— t          | d¬¦  «        } t          | t          ¦  «        r|                      ¦   «         } |                      ¦   «         }| j                             |ddd…         ¦  «        }|j        }t          t          |¦  «        ¦  «        Št          |t          t          j        f¦  «        st          d¦  «        ‚t          |¦  «        }|t          ‰¦  «        k    s|                     ‰¦  «        st          d¦  «        ‚t          |¦  «        Šˆfd„‰D ¦   «         }|                      ‰¦  «        Š|                      |¦  «        Št          j        ‰¦  «        }t          j        ‰¦  «        }ˆfd„‰|z   D ¦   «         ddd…         }|                     |¦  «        }|                     ||g¦  «        }	t          j                             |	d¬	¦  «        \  }
}}ˆˆfd
„t'          ||
j        |¦  «        D ¦   «         }|S )as  Return the Schmidt Decomposition of a pure quantum state.

    For an arbitrary bipartite state:

    .. math::
         |\psi\rangle_{AB} = \sum_{i,j} c_{ij}
                             |x_i\rangle_A \otimes |y_j\rangle_B,

    its Schmidt Decomposition is given by the single-index sum over k:

    .. math::
        |\psi\rangle_{AB} = \sum_{k} \lambda_{k}
                            |u_k\rangle_A \otimes |v_k\rangle_B

    where :math:`|u_k\rangle_A` and :math:`|v_k\rangle_B` are an
    orthonormal set of vectors in their respective spaces :math:`A` and :math:`B`,
    and the Schmidt coefficients :math:`\lambda_k` are positive real values.

    Args:
        state (Statevector or DensityMatrix): the input state.
        qargs (list): the list of Input state positions corresponding to subsystem :math:`B`.

    Returns:
        list: list of tuples ``(s, u, v)``, where ``s`` (float) are the Schmidt coefficients
        :math:`\lambda_k`, and ``u`` (Statevector), ``v`` (Statevector) are the Schmidt vectors
        :math:`|u_k\rangle_A`, :math:`|u_k\rangle_B`, respectively.

    Raises:
        QiskitError: if Input qargs is not a list of positions of the Input state.
        QiskitError: if Input qargs is not a proper subset of Input state.

    .. note::
        In Qiskit, qubits are ordered using little-endian notation, with the least significant
        qubits having smaller indices. For example, a four-qubit system is represented as
        :math:`|q_3q_2q_1q_0\rangle`. Using this convention, setting ``qargs=[0]`` will partition the
        state as :math:`|q_3q_2q_1\rangle_A\otimes|q_0\rangle_B`. Furthermore, qubits will be organized
        in this notation regardless of the order they are passed. For instance, passing either
        ``qargs=[1,2]`` or ``qargs=[2,1]`` will result in partitioning the state as
        :math:`|q_3q_0\rangle_A\otimes|q_2q_1\rangle_B`.
    Fr   Néÿÿÿÿz9Input qargs is not a list of positions of the Input statez1Input qargs is not a proper subset of Input statec                ó   •— g | ]}|‰v¯|‘Œ	S © rS   )Ú.0ÚiÚqargs_bs     €r8   ú
<listcomp>z)schmidt_decomposition.<locals>.<listcomp>¼   s#   ø€ Ð5Ð5Ð5�Q A¨WÐ$4Ð$4ˆqÐ$4Ð$4Ð$4r:   c                óL   •— g | ] }‰d d d…                               |¦  «        ‘Œ!S )NrQ   )Úindex)rT   rU   Úquditss     €r8   rW   z)schmidt_decomposition.<locals>.<listcomp>Ã   s1   ø€ ÐCÐCÐC¨A�&˜˜˜2˜”,×$Ò$ QÑ'Ô'ÐCÐCÐCr:   )Úfull_matricesc                ót   •— g | ]4\  }}}|t           k    ¯|t          |‰¬ ¦  «        t          |‰¬ ¦  «        f‘Œ5S )r   )r   r   )rT   ÚsÚuÚvÚdims_aÚdims_bs       €€r8   rW   z)schmidt_decomposition.<locals>.<listcomp>Ê   sU   ø€ ð ð ð áˆAˆq�!Ø�|ÒÐð 
�K˜ Ð'Ñ'Ô'­°Q¸VÐ)DÑ)DÔ)DÐEàÐÐr:   )r   r!   r   Úto_statevectorr   r&   r'   Úndimr   Úranger$   Úndarrayr   ÚsetÚissubsetÚprodÚ	transposeÚlinalgÚsvdr/   ÚT)r	   r   r   Ú
state_tensrc   Úqargs_aÚndim_bÚndim_aÚ
qargs_axesÚ	state_matÚu_matÚs_arrÚvh_matÚschmidt_componentsr`   ra   rV   rZ   s                 @@@@r8   Úschmidt_decompositionrw   ~   s  øøøø€ õR ˜%¨%Ð0Ñ0Ô0€Eõ �%�Ñ'Ô'ð 'Ø×$Ò$Ñ&Ô&ˆð �:Š:‰<Œ<€DØ”×$Ò$ T¨$¨$¨B¨$¤ZÑ0Ô0€JØŒ?€DÝ•%˜‘+”+ÑÔ€Fõ �e�d¥B¤JÐ/Ñ0Ô0ð WÝÐUÑVÔVÐVÝ�‰JŒJ€EØ•�F‘”ÒÐ 5§>¢>°&Ñ#9Ô#9ÐÝÐMÑNÔNÐNõ �5‰kŒk€GØ5Ð5Ð5Ð5˜&Ð5Ñ5Ô5€GØ�ZŠZ˜Ñ Ô €FØ�ZŠZ˜Ñ Ô €FÝŒW�V‰_Œ_€FÝŒW�V‰_Œ_€Fð DÐCÐCÐC°¸7Ñ1BÐCÑCÔCÀDÀDÀbÀDÔI€JØ×%Ò% jÑ1Ô1€Jð ×"Ò" F¨FÐ#3Ñ4Ô4€IÝœ9Ÿ=š=¨À%˜=ÑHÔHÑ€Eˆ5�&ðð ð ð ð å˜5 %¤'¨6Ñ2Ô2ðñ ô Ðð Ðr:   Tc                óê  — t          | t          ¦  «        rt          j        | t          ¬¦  «        } t          | t          j        ¦  «        rX| j        }|dk    rt          | ¦  «        } n;|dk    r5| j        \  }}|dk    rt          | ¦  «        } n||k    rt          | ¦  «        } t          | t          t          f¦  «        st          d¦  «        ‚|r#|                      ¦   «         st          d¦  «        ‚| S )z$Format input state into class object)Údtyper   r   zInput is not a quantum statez"Input quantum state is not a valid)r!   r   r$   r%   Úcomplexre   rc   r   r+   r   r   Úis_valid)r	   r   rc   Údim1Údim2s        r8   r   r   Ó   sò   € å�%�ÑÔð /Ý”˜¥gÐ.Ñ.Ô.ˆÝ�%�œÑ$Ô$ð 	-ØŒzˆØ�1Š9ˆ9Ý Ñ&Ô&ˆEˆEØ�QŠYˆYØœ‰JˆD�$Ø�qŠyˆyÝ# EÑ*Ô*��Ø˜’�Ý% eÑ,Ô,�Ý�e�k­=Ð9Ñ:Ô:ð :ÝÐ8Ñ9Ô9Ð9Øð @˜ŸšÑ(Ô(ð @ÝÐ>Ñ?Ô?Ð?Ø€Lr:   c                óÊ   — ddl m} |                     | ¦  «        \  }}}t          j         ||¦  «        ¦  «        }|                     |¦  «                             |¦  «        S )ab  Apply real scalar function to singular values of a matrix.

    Args:
        matrix (array_like): (N, N) Matrix at which to evaluate the function.
        func (callable): Callable object that evaluates a scalar function f.

    Returns:
        ndarray: funm (N, N) Value of the matrix function specified by func
                 evaluated at `A`.
    r   N)Úscipy.linalgrj   rk   r$   ÚdiagÚdot)ÚmatrixÚfuncÚlaÚunitary1Úsingular_valuesÚunitary2Údiag_func_singulars          r8   Ú	_funm_svdr‰   è   sg   € ð ÐÐÐÐÐà*,¯&ª&°©.¬.Ñ'€Hˆo˜xÝœ   oÑ!6Ô!6Ñ7Ô7ÐØ�<Š<Ð*Ñ+Ô+×/Ò/°Ñ9Ô9Ð9r:   )r	   r
   r   r   r   r   )r   )r;   r<   r=   r>   r   r?   )T)Ú__doc__Ú
__future__r   rC   Únumpyr$   Úqiskit.exceptionsr   Ú&qiskit.quantum_info.states.statevectorr   Ú(qiskit.quantum_info.states.densitymatrixr   Ú%qiskit.quantum_info.operators.channelr   Ú(qiskit.quantum_info.operators.predicatesr   r9   rO   rw   r   r‰   rS   r:   r8   ú<module>r’      s  ððð ð #Ð "Ð "Ð "Ð "Ð "Ø €€€à Ð Ð Ð à )Ð )Ð )Ð )Ð )Ð )Ø >Ð >Ð >Ð >Ð >Ð >Ø BÐ BÐ BÐ BÐ BÐ BØ 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø AÐ AÐ AÐ AÐ AÐ Að3ð 3ð 3ð 3ðl(ð (ð (ð (ð (ðVRð Rð Rðjð ð ð ð*:ð :ð :ð :ð :r:   