
    z	i&                        S r SSKJr  SSKrSSKJr  SSKJr  SSK	J
r
  SSKJrJrJrJr   S       SS jjrSSS	 jjrSSS
 jjrSSS jjrSS jrSS jrS rg)zJ
Quantum information measures, metrics, and related functions for states.
    )annotationsN)QiskitError)Statevector)DensityMatrix)partial_traceshannon_entropy_format_state	_funm_svdc                   [        XS9n [        XS9nU R                  nUR                  n[        U [        5      (       a{  [        U[        5      (       a7  [        R
                  " UR                  5       R                  U5      5      S-  nOUR                  5       R                  U5      R                  U5      nO[        U[        5      (       a/  UR                  5       R                  U5      R                  U5      nOd[        U[        R                  5      n[        U[        R                  5      n[        R                  R                  UR                  U5      SS9S-  n[        [        R                  " U5      5      $ )a-  Return the state fidelity between two quantum states.

The state fidelity :math:`F` for density matrix input states
:math:`\rho_1, \rho_2` is given by

.. math::
    F(\rho_1, \rho_2) = Tr[\sqrt{\sqrt{\rho_1}\rho_2\sqrt{\rho_1}}]^2.

If one of the states is a pure state this simplifies to
:math:`F(\rho_1, \rho_2) = \langle\psi_1|\rho_2|\psi_1\rangle`, where
:math:`\rho_1 = |\psi_1\rangle\!\langle\psi_1|`.

Args:
    state1 (Statevector or DensityMatrix): the first quantum state.
    state2 (Statevector or DensityMatrix): the second quantum state.
    validate (bool): check if the inputs are valid quantum states
                     [Default: True]

Returns:
    float: The state fidelity :math:`F(\rho_1, \rho_2)`.

Raises:
    QiskitError: if ``validate=True`` and the inputs are invalid quantum states.
validate   nuc)ord)r	   data
isinstancer   npabsconjdotr
   sqrtlinalgnormfloatreal)state1state2r   arr1arr2fids1sqs2sqs           ]/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/quantum_info/states/measures.pystate_fidelityr$      s   8 65F65F ;;D;;D&+&&fk**&&./14C ))+//$'++D1C	FK	(	(iikood#''- rww'rww'iinnTXXd^n71<    c                4    [        XS9n U R                  5       $ )a  Calculate the purity of a quantum state.

The purity of a density matrix :math:`\rho` is

.. math::

    \text{Purity}(\rho) = Tr[\rho^2]

Args:
    state (Statevector or DensityMatrix): a quantum state.
    validate (bool): check if input state is valid [Default: True]

Returns:
    float: the purity :math:`Tr[\rho^2]`.

Raises:
    QiskitError: if the input isn't a valid quantum state.
r   )r	   purity)stater   s     r#   r'   r'   R   s    & %3E<<>r%   c                    SSK Jn  [        U SS9n [        U [        5      (       a  g[
        R                  " [
        R                  " UR                  U R                  5      5      S5      n[        X1S9$ )a  Calculate the von-Neumann entropy of a quantum state.

The entropy :math:`S` is given by

.. math::

    S(\rho) = - Tr[\rho \log(\rho)]

Args:
    state (Statevector or DensityMatrix): a quantum state.
    base (int): the base of the logarithm [Default: 2].

Returns:
    float: The von-Neumann entropy S(rho).

Raises:
    QiskitError: if the input state is not a valid QuantumState.
r   NTr           base)scipy.linalgr   r	   r   r   r   maximumr   eigvalsr   r   )r(   r,   laevalss       r#   entropyr2   i   sT    & %$/E%%%JJrwwrzz%**56<E5,,r%   c                    [        U SS9n [        U R                  5       5      S:w  a  [        S5      e[	        U S/5      n[	        U S/5      n[        X!S9[        X1S9-   [        XS9-
  $ )aw  Calculate the mutual information of a bipartite state.

The mutual information :math:`I` is given by:

.. math::

    I(\rho_{AB}) = S(\rho_A) + S(\rho_B) - S(\rho_{AB})

where :math:`\rho_A=Tr_B[\rho_{AB}], \rho_B=Tr_A[\rho_{AB}]`, are the
reduced density matrices of the bipartite state :math:`\rho_{AB}`.

Args:
    state (Statevector or DensityMatrix): a bipartite state.
    base (int): the base of the logarithm [Default: 2].

Returns:
    float: The mutual information :math:`I(\rho_{AB})`.

Raises:
    QiskitError: if the input state is not a valid QuantumState.
    QiskitError: if input is not a bipartite QuantumState.
Tr   r   z(Input must be a bipartite quantum state.   r   r+   )r	   lendimsr   r   r2   )r(   r,   rho_arho_bs       r#   mutual_informationr9      sj    . %$/E
5::<ADEE%!%E%!%E5$wu'@@75C\\\r%   c                   SSK Jn  [        U SS9n [        U [        5      (       a  U R                  5       n[        U5      S:w  a  [        S5      eUS   US   :  a  S/OS/n[        X5      n[        [        R                  " SS[        R                  " [        U5      5      -
  -  5      5      $ U R                  S:w  a  [        S	5      e[        U 5      R                   n[        R"                  " [        R$                  " / S
Q5      5      nUR'                  U5      R'                  UR)                  5       5      R'                  U5      n[        R*                  " [        R                  " UR-                  U5      5      5      n[        R                  " [        R.                  " US5      5      n[1        SUS   [        R2                  " USS 5      -
  5      $ )a  Calculate the concurrence of a quantum state.

The concurrence of a bipartite
:class:`~qiskit.quantum_info.Statevector` :math:`|\psi\rangle` is
given by

.. math::

    C(|\psi\rangle) = \sqrt{2(1 - Tr[\rho_0^2])}

where :math:`\rho_0 = Tr_1[|\psi\rangle\!\langle\psi|]` is the
reduced state from by taking the
:func:`~qiskit.quantum_info.partial_trace` of the input state.

For density matrices the concurrence is only defined for
2-qubit states, it is given by:

.. math::

    C(\rho) = \max(0, \lambda_1 - \lambda_2 - \lambda_3 - \lambda_4)

where  :math:`\lambda _1 \ge \lambda _2 \ge \lambda _3 \ge \lambda _4`
are the ordered eigenvalues of the matrix
:math:`R=\sqrt{\sqrt{\rho }(Y\otimes Y)\overline{\rho}(Y\otimes Y)\sqrt{\rho}}`.

Args:
    state (Statevector or DensityMatrix): a 2-qubit quantum state.

Returns:
    float: The concurrence.

Raises:
    QiskitError: if the input state is not a valid QuantumState.
    QiskitError: if input is not a bipartite QuantumState.
    QiskitError: if density matrix input is not a 2-qubit state.
r   NTr   r   'Input is not a bipartite quantum state.r4      -Input density matrix must be a 2-qubit state.)r4   r4   r>   r*   r>   )r-   r   r	   r   r   r6   r5   r   r   r   r   r   r   r'   dimr   r   fliplrdiagr   r   sortr/   r.   maxsum)r(   r0   r6   qargsrhoyy_matsigmaws           r#   concurrencerJ      sX   J  %$/E%%%zz|t9>GHHAwa(qcE)RWWQ!bggfSk&:":;<==yyA~IJJ


#
#CYYrww~./FGGFO
+//7E


5)*+A


1c"#AsAbEBFF1Qr7O+,,r%   c                   [        U SS9n [        U [        5      (       aO  U R                  5       n[	        U5      S:w  a  [        S5      eUS   US   :  a  S/OS/n[        [        X5      SS9$ U R                  S:w  a  [        S	5      e[        U 5      nS[        R                  " SUS-  -
  5      -   S-  n[        USU-
  /5      $ )
a  Calculate the entanglement of formation of quantum state.

The input quantum state must be either a bipartite state vector, or a
2-qubit density matrix.

Args:
    state (Statevector or DensityMatrix): a 2-qubit quantum state.

Returns:
    float: The entanglement of formation.

Raises:
    QiskitError: if the input state is not a valid QuantumState.
    QiskitError: if input is not a bipartite QuantumState.
    QiskitError: if density matrix input is not a 2-qubit state.
Tr   r   r;   r   r4   r+   r<   r=   )r	   r   r   r6   r5   r   r2   r   r?   rJ   r   r   r   )r(   r6   rE   concvals        r#   entanglement_of_formationrN      s    " %$/E%%% zz|t9>GHHAwa(qc}U2;; yyA~IJJuDrwwqD!G}%%
*CCS>**r%   c                    [        U [        5      (       a  [        U 5      n U R                  U5      n [        R
                  R                  U R                  SS9n[        R                  " U5      nUS-
  S-  nU$ )a  Calculates the negativity

The mathematical expression for negativity is given by:
.. math::
    {\cal{N}}(\rho) = \frac{|| \rho^{T_A}|| - 1 }{2}

Args:
    state (Statevector or DensityMatrix): a quantum state.
    qargs (list): The subsystems to be transposed.

Returns:
    negv (float): Negativity value of the quantum state

Raises:
    QiskitError: if the input state is not a valid QuantumState.
F)
compute_uvr4   r   )	r   r   r   partial_transposer   r   svdr   rD   )r(   rE   singular_valuesr/   negvs        r#   
negativityrU     sf    $ %%%e$##E*EiimmEJJ5mAOff_%GaK1DKr%   )T)r   Statevector | DensityMatrixr   rV   r   boolreturnr   )r(   rV   r   rW   rX   r   )r   )r(   rV   r,   intrX   r   )r(   rV   rX   r   )__doc__
__future__r   numpyr   qiskit.exceptionsr   &qiskit.quantum_info.states.statevectorr   (qiskit.quantum_info.states.densitymatrixr    qiskit.quantum_info.states.utilsr   r   r	   r
   r$   r'   r2   r9   rJ   rN   rU    r%   r#   <module>rb      ss    #  ) > B  `d2'21L2X\2
2j.-:]>9-x +Fr%   