§
    º³Ùg¯>  ã                  ó  — d Z ddlmZ ddlZddl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 dd	lmZmZ dd
lmZ ddlmZ ddlmZ  ej        e¦  «        Z	 	 	 d(d)d„Z	 	 	 d*d+d„Z	 	 	 d*d,d„Z d-d.d#„Z!d$„ Z"d%„ Z#d&„ Z$d'„ Z%dS )/zE
A collection of useful quantum information functions for operators.
é    )ÚannotationsN)ÚQiskitErrorÚMissingOptionalLibraryError)ÚGate)ÚBaseOperator)ÚOperator)ÚQuantumChannel)ÚChoiÚSuperOp)ÚDensityMatrix)Ústate_fidelity)Ú	optionalsTÚchannelúOperator | QuantumChannelÚtargetú Operator | QuantumChannel | NoneÚ
require_cpÚboolÚ
require_tpÚreturnÚfloatc           	     óö  — t          | t          dd¦  «        } t          |t          dd¦  «        }|r0| j        |j        k    r t	          d| j        › d|j        › d�¦  «        ‚d| fd|ffD ]Ù\  }}|�U|rSt          |¦  «        }|d
|j        z  k     }t          j        |¦  «        r"t           
                    d|||         ¦  «         |�{|ryt          |¦  «        }t          j        t          j        |d|j        |j        ¬¦  «        ¦  «        }	t          j        |	¦  «        r"t           
                    d|||	         ¦  «         ŒÚt          |t          ¦  «        r)|                      |                     ¦   «         ¦  «        } d	}| j        \  }
}|€”t          | t          ¦  «        r2t          j        t          j        | j        ¦  «        |
z  ¦  «        dz  }n,t          j        t          | ¦  «        j        ¦  «        |
dz  z  }t+          t          j        |¦  «        ¦  «        S t/          t1          | ¦  «        j        |
z  ¦  «        }t/          t1          |¦  «        j        |
z  ¦  «        }t3          ||d¬¦  «        S )a  Return the process fidelity of a noisy quantum channel.


    The process fidelity :math:`F_{\text{pro}}(\mathcal{E}, \mathcal{F})`
    between two quantum channels :math:`\mathcal{E}, \mathcal{F}` is given by

    .. math::
        F_{\text{pro}}(\mathcal{E}, \mathcal{F})
            = F(\rho_{\mathcal{E}}, \rho_{\mathcal{F}})

    where :math:`F` is the :func:`~qiskit.quantum_info.state_fidelity`,
    :math:`\rho_{\mathcal{E}} = \Lambda_{\mathcal{E}} / d` is the
    normalized :class:`~qiskit.quantum_info.Choi` matrix for the channel
    :math:`\mathcal{E}`, and :math:`d` is the input dimension of
    :math:`\mathcal{E}`.

    When the target channel is unitary this is equivalent to

    .. math::
        F_{\text{pro}}(\mathcal{E}, U)
            = \frac{Tr[S_U^\dagger S_{\mathcal{E}}]}{d^2}

    where :math:`S_{\mathcal{E}}, S_{U}` are the
    :class:`~qiskit.quantum_info.SuperOp` matrices for the *input* quantum
    channel :math:`\mathcal{E}` and *target* unitary :math:`U` respectively,
    and :math:`d` is the input dimension of the channel.

    Args:
        channel (Operator or QuantumChannel): input quantum channel.
        target (Operator or QuantumChannel or None): target quantum channel.
            If `None` target is the identity operator [Default: None].
        require_cp (bool): check if input and target channels are
                           completely-positive and if non-CP log warning
                           containing negative eigenvalues of Choi-matrix
                           [Default: True].
        require_tp (bool): check if input and target channels are
                           trace-preserving and if non-TP log warning
                           containing negative eigenvalues of partial
                           Choi-matrix :math:`Tr_{\text{out}}[\mathcal{E}] - I`
                           [Default: True].

    Returns:
        float: The process fidelity :math:`F_{\text{pro}}`.

    Raises:
        QiskitError: if the channel and target do not have the same dimensions.
    Úprocess_fidelityr   r   zHInput quantum channel and target unitary must have the same dimensions (z != z).ÚInputÚTargetNéÿÿÿÿz>%s channel is not CP. Choi-matrix has negative eigenvalues: %sr   )ÚatolÚrtolzA%s channel is not TP. Tr_2[Choi] - I has non-zero eigenvalues: %sé   F)Úvalidate)Ú_input_formatterr   r   Údimr   Ú_cp_conditionr   ÚnpÚanyÚloggerÚwarningÚ_tp_conditionÚlogical_notÚiscloser   Ú
isinstanceÚcomposeÚadjointÚabsÚtraceÚdatar   Úrealr   r
   r   )r   r   r   r   ÚlabelÚchanÚcp_condÚnegÚtp_condÚnon_zeroÚ	input_dimÚ_ÚfidÚstate1Ústate2s                  ú\/granary/edev/quantum/lib/python3.11/site-packages/qiskit/quantum_info/operators/measures.pyr   r   "   s~  € õl ˜w­Ð1CÀYÑOÔO€GÝ˜f¥hÐ0BÀHÑMÔM€Fàð àŒ;˜&œ*Ò$Ð$Ýð?Ø&œ{ð?ð ?Ø06´
ð?ð ?ð ?ñô ð ð ! 'Ð*¨X°vÐ,>Ð?ð ð ‰ˆˆtØÐ 
ÐÝ# DÑ)Ô)ˆGØ˜B ¤™NÒ*ˆCÝŒv�c‰{Œ{ð Ý—’ØTØØ˜C”Lñô ð ð
 Ð 
ÐÝ# DÑ)Ô)ˆGÝ”~¥b¤j°¸!À$Ä)ÐRVÔR[Ð&\Ñ&\Ô&\Ñ]Ô]ˆHÝŒv�hÑÔð Ý—’ØWØØ˜HÔ%ñô ð øõ �&�(Ñ#Ô#ð ð —/’/ &§.¢.Ñ"2Ô"2Ñ3Ô3ˆØˆà”;�L€IˆqØ€~å�g�xÑ(Ô(ð 	Cå”&�œ '¤,Ñ/Ô/°)Ñ;Ñ<Ô<ÀÑAˆCˆCõ ”(�7 7Ñ+Ô+Ô0Ñ1Ô1°YÀ±\ÑBˆCÝ•R”W˜S‘\”\Ñ"Ô"Ð"õ
 �4 ™=œ=Ô-°	Ñ9Ñ:Ô:€FÝ�4 ™<œ<Ô,¨yÑ8Ñ9Ô9€FÝ˜& &°5Ð9Ñ9Ô9Ð9ó    FúQuantumChannel | OperatorúOperator | Nonec                ó  — t          | t          dd¦  «        } t          |t          dd¦  «        }|�3	 t          |¦  «        }n"# t          $ r}t          d¦  «        |‚d}~ww xY w| j        \  }}t          | |||¬¦  «        }||z  dz   |dz   z  S )a©  Return the average gate fidelity of a noisy quantum channel.

    The average gate fidelity :math:`F_{\text{ave}}` is given by

    .. math::
        \begin{aligned}
        F_{\text{ave}}(\mathcal{E}, U)
            &= \int d\psi \langle\psi|U^\dagger
                \mathcal{E}(|\psi\rangle\!\langle\psi|)U|\psi\rangle \\
            &= \frac{d F_{\text{pro}}(\mathcal{E}, U) + 1}{d + 1}
        \end{aligned}

    where :math:`F_{\text{pro}}(\mathcal{E}, U)` is the
    :meth:`~qiskit.quantum_info.process_fidelity` of the input quantum
    *channel* :math:`\mathcal{E}` with a *target* unitary :math:`U`, and
    :math:`d` is the dimension of the *channel*.

    Args:
        channel (QuantumChannel or Operator): noisy quantum channel.
        target (Operator or None): target unitary operator.
            If `None` target is the identity operator [Default: None].
        require_cp (bool): check if input and target channels are
                           completely-positive and if non-CP log warning
                           containing negative eigenvalues of Choi-matrix
                           [Default: True].
        require_tp (bool): check if input and target channels are
                           trace-preserving and if non-TP log warning
                           containing negative eigenvalues of partial
                           Choi-matrix :math:`Tr_{\text{out}}[\mathcal{E}] - I`
                           [Default: True].

    Returns:
        float: The average gate fidelity :math:`F_{\text{ave}}`.

    Raises:
        QiskitError: if the channel and target do not have the same dimensions,
                     or have different input and output dimensions.
    Úaverage_gate_fidelityr   r   Nz�Target channel is not a unitary channel. To compare two non-unitary channels use the `qiskit.quantum_info.process_fidelity` function instead.©r   r   r   é   )r!   r   r   r   r"   r   )r   r   r   r   Úexr"   r9   Úf_pros           r=   rB   rB   ‘   s¾   € õZ ˜w­Ð1HÈ)ÑTÔT€GÝ˜f¥hÐ0GÈÑRÔR€FàÐð	Ý˜fÑ%Ô%ˆFˆFøÝð 	ð 	ð 	ÝðKñô ð ð	øøøøð	øøøð Œ[�F€CˆÝ˜W¨VÀ
ÐWaÐbÑbÔb€EØ�%‰K˜!‰O  a¡Ñ(Ð(s   ²A Á
A!ÁAÁA!r	   c                óŠ   — t          | t          dd¦  «        } t          |t          dd¦  «        }dt          | |||¬¦  «        z
  S )aŠ  Return the gate error of a noisy quantum channel.

    The gate error :math:`E` is given by the average gate infidelity

    .. math::
        E(\mathcal{E}, U) = 1 - F_{\text{ave}}(\mathcal{E}, U)

    where :math:`F_{\text{ave}}(\mathcal{E}, U)` is the
    :meth:`~qiskit.quantum_info.average_gate_fidelity` of the input
    quantum *channel* :math:`\mathcal{E}` with a *target* unitary
    :math:`U`.

    Args:
        channel (QuantumChannel): noisy quantum channel.
        target (Operator or None): target unitary operator.
            If `None` target is the identity operator [Default: None].
        require_cp (bool): check if input and target channels are
                           completely-positive and if non-CP log warning
                           containing negative eigenvalues of Choi-matrix
                           [Default: True].
        require_tp (bool): check if input and target channels are
                           trace-preserving and if non-TP log warning
                           containing negative eigenvalues of partial
                           Choi-matrix :math:`Tr_{\text{out}}[\mathcal{E}] - I`
                           [Default: True].

    Returns:
        float: The average gate error :math:`E`.

    Raises:
        QiskitError: if the channel and target do not have the same dimensions,
                     or have different input and output dimensions.
    Ú
gate_errorr   r   rD   rC   )r!   r   r   rB   )r   r   r   r   s       r=   rH   rH   Ï   sS   € õP ˜w­°¸yÑIÔI€GÝ˜f¥h°¸hÑGÔG€FØÕ$Ø˜¨:À*ðñ ô ñ ð r>   ÚSCSÚchoiúChoi | QuantumChannelÚsolverÚstrc           
     ó¬  ‡— ddl m} t          d¦  «        Št          t	          | t          dd¦  «        ¦  «        } ˆfd„}| j        }| j        }||z  }‰                     ||f¦  «        }‰                     ||f¦  «        }	 |||	¦  «        }
‰                     ||f¦  «        }‰                     ||f¦  «        } |||¦  «        }‰                     ||f¦  «        }‰                     ||f¦  «        }|                     |¦  «        }‰ 	                    ‰ 
                    ||¦  «        |g|j        ‰ 
                    ||¦  «        gg¦  «        }‰ 	                    ‰ 
                    ||	¦  «        |g|j         ‰ 
                    ||¦  «        gg¦  «        } |||¦  «        }t          j        t          j        | j        ||||f¦  «        d¦  «                             | j        j        ¦  «        }|j        }|j        }|
dz	  ||j        k    |	|	j         k    ‰                     |¦  «        dk    |dz	  ||j        k    ||j         k    ‰                     |¦  «        dk    |dz	  g	}‰                     ‰                     ||z  ¦  «        ‰                     ||z  ¦  «        z   ¦  «        }‰                     ||¦  «        } |j        d
d	|i|¤Ž}|S )aH  Return the diamond norm of the input quantum channel object.

    This function computes the completely-bounded trace-norm (often
    referred to as the diamond-norm) of the input quantum channel object
    using the semidefinite-program from reference [1].

    Args:
        choi(Choi or QuantumChannel): a quantum channel object or
                                      Choi-matrix array.
        solver (str): The solver to use.
        kwargs: optional arguments to pass to CVXPY solver.

    Returns:
        float: The completely-bounded trace norm :math:`\|\mathcal{E}\|_{\diamond}`.

    Raises:
        QiskitError: if CVXPY package cannot be found.

    Additional Information:
        The input to this function is typically *not* a CPTP quantum
        channel, but rather the *difference* between two quantum channels
        :math:`\|\Delta\mathcal{E}\|_\diamond` where
        :math:`\Delta\mathcal{E} = \mathcal{E}_1 - \mathcal{E}_2`.

    Reference:
        J. Watrous. "Simpler semidefinite programs for completely bounded
        norms", arXiv:1207.5726 [quant-ph] (2012).

    .. note::

        This function requires the optional CVXPY package to be installed.
        Any additional kwargs will be passed to the ``cvxpy.solve``
        function. See the CVXPY documentation for information on available
        SDP solvers.
    r   )Úsparsez`diamond_norm`Údiamond_normrJ   c                ó<   •— ‰                      | | g|| gg¦  «        S )z2Block matrix for embedding complex matrix in reals)Úbmat)Úmat_rÚmat_iÚcvxpys     €r=   Úcvx_bmatzdiamond_norm.<locals>.cvx_bmat(  s$   ø€ à�zŠz˜E E 6˜?¨U°E¨NÐ;Ñ<Ô<Ð<r>   )é   r   rD   r   rD   rL   © )ÚscipyrO   Ú_cvxpy_checkr
   r!   Ú
_input_dimÚ_output_dimÚVariableÚeyerR   ÚkronÚTr$   Ú	transposeÚreshaper0   Úshaper1   Úimagr/   ÚMaximizeÚProblemÚsolve)rJ   rL   ÚkwargsrO   rV   Údim_inÚdim_outÚsizeÚr0_rÚr0_iÚr0Úr1_rÚr1_iÚr1Úx_rÚx_iÚidenÚc_rÚc_iÚcÚchoi_rtÚ	choi_rt_rÚ	choi_rt_iÚconsÚobjÚprobÚsolrU   s                              @r=   rP   rP   þ   sì  ø€ ðH ÐÐÐÐÐåÐ)Ñ*Ô*€EåÕ  ¥t¨^¸VÑDÔDÑEÔE€Dð=ð =ð =ð =ð =ð
 Œ_€FØÔ€GØ�GÑ€Dð �>Š>˜6 6Ð*Ñ+Ô+€DØ�>Š>˜6 6Ð*Ñ+Ô+€DØ	ˆ�$˜Ñ	Ô	€Bà�>Š>˜6 6Ð*Ñ+Ô+€DØ�>Š>˜6 6Ð*Ñ+Ô+€DØ	ˆ�$˜Ñ	Ô	€Bà
�.Š.˜$ ˜Ñ
&Ô
&€CØ
�.Š.˜$ ˜Ñ
&Ô
&€CØ�:Š:�gÑÔ€Dð
 �*Š*�u—z’z $¨Ñ-Ô-¨sÐ3°c´e¸U¿ZºZÈÈdÑ=SÔ=SÐ5TÐUÑ
VÔ
V€CØ
�*Š*�u—z’z $¨Ñ-Ô-¨sÐ3°s´u°f¸e¿jºjÈÈtÑ>TÔ>TÐ5UÐVÑ
WÔ
W€CØˆ��cÑÔ€Aõ ŒlÝ
Œ
�4”9˜v w°¸Ð@ÑAÔAÀ<ñô ç‚gˆdŒiŒoÑÔð ð ”€IØ”€Ið 	ˆa‰Ø�”ŠØ�”�ŠØ�Š�DÑÔ˜QÒØ
ˆa‰Ø�”ŠØ�”�ŠØ�Š�DÑÔ˜QÒØ	ˆQ‰ð
€Dð �.Š.˜Ÿš Y°¡_Ñ5Ô5¸¿ºÀIÐPSÁOÑ8TÔ8TÑTÑ
UÔ
U€CØ�=Š=˜˜dÑ#Ô#€DØ
ˆ$Œ*Ð
-Ð
-˜FÐ
- fÐ
-Ð
-€CØ€Jr>   c                óž   — t           j                             | ¦  «         ddl}|j        }|d         dk    rt          ddd|› d�¬¦  «        ‚|S )	z1Check that a supported CVXPY version is installedr   NÚ1zCVXPY >= 1.0rP   zIncompatible CVXPY version z found.)Úmsg)Ú
_optionalsÚ	HAS_CVXPYÚrequire_nowrU   Ú__version__r   )ÚnamerU   Úversions      r=   rZ   rZ   a  so   € õ Ô×$Ò$ TÑ*Ô*Ð*Ø€L€L€Lð Ô€GØˆq„z�SÒÐÝ)ØØØ>¨gÐ>Ð>Ð>ð
ñ 
ô 
ð 	
ð
 €Lr>   c           	     ó¢  — | €| S t          | t          ¦  «        r| S t          | d¦  «        r|                      ¦   «         S t          | d¦  «        r|                      ¦   «         S t          | t
          t          f¦  «        rt          | ¦  «        S t          | d¦  «        r|                      ¦   «         S t          d|› d|› d|j
        › d�¦  «        ‚)	z(Formatting function for input conversionNÚto_quantumchannelÚ
to_channelÚto_operatorzinvalid type supplied to z of z. A z	 is best.)r+   r	   Úhasattrr‰   rŠ   r   r   r   r‹   Ú	TypeErrorÚ__name__)r|   Úfallback_classÚ	func_nameÚarg_names       r=   r!   r!   s  sù   € ð €{Øˆ
õ �#•~Ñ&Ô&ð Øˆ
ÝˆsÐ'Ñ(Ô(ð 'Ø×$Ò$Ñ&Ô&Ð&Ýˆs�LÑ!Ô!ð  Ø�~Š~ÑÔÐõ �#��lÐ+Ñ,Ô,ð Ý˜‰}Œ}ÐÝˆs�MÑ"Ô"ð !Ø�ŠÑ Ô Ð Ý
ð	1 Hð 	1ð 	1°)ð 	1ð 	1ØÔ%ð	1ð 	1ð 	1ñô ð r>   c                óL  — t          | t          ¦  «        rHt          | t          ¦  «        st          | ¦  «        } t          j                             | j        ¦  «        S t          | ¦  «        j        }t          j        || 	                    ¦   «         ddgddgf¬¦  «        j
        S )z<Return Choi-matrix eigenvalues for checking if channel is CPr   rD   ©Úaxes)r+   r	   r
   r$   ÚlinalgÚeigvalshr0   r   Ú	tensordotÚconjr1   )r   Úunitarys     r=   r#   r#   Œ  sˆ   € å�'�>Ñ*Ô*ð 0Ý˜'¥4Ñ(Ô(ð 	$Ý˜7‘m”mˆGÝŒy×!Ò! '¤,Ñ/Ô/Ð/Ý�wÑÔÔ$€GÝŒ<˜ §¢¡¤¸¸1°vÀÀ1¸vÐ6FÐGÑGÔGÔLÐLr>   c                óZ  — t          | t          ¦  «        r™t          | t          ¦  «        st          | ¦  «        } | j        }t	          t          j        |j        ¦  «                             t          ¦  «        ¦  «        }||z   }t          j
        t          j        ||¦  «        dd¬¦  «        }n=t          | ¦  «        j        }t          j        ||                     ¦   «         d¬¦  «        }t
          j                             |t          j        t%          |¦  «        ¦  «        z
  ¦  «        S )zGReturn partial tr Choi-matrix eigenvalues for checking if channel is TPrD   rW   )Úaxis1Úaxis2)r   r   r“   )r+   r	   r
   r0   Útupler$   Úsqrtrc   ÚastypeÚintr/   rb   r   r—   r˜   r•   r–   r^   Úlen)r   rJ   Údimsrc   Útr_choir™   s         r=   r(   r(   –  sæ   € å�'�>Ñ*Ô*ð 	EÝ˜'¥4Ñ(Ô(ð 	$Ý˜7‘m”mˆGØŒ|ˆÝ•R”W˜TœZÑ(Ô(×/Ò/µÑ4Ô4Ñ5Ô5ˆØ�t‘ˆÝ”(�2œ: d¨EÑ2Ô2¸!À1ÐEÑEÔEˆˆå˜7Ñ#Ô#Ô(ˆÝ”,˜w¨¯ª©¬¸VÐDÑDÔDˆÝŒ9×Ò˜g­¬­s°7©|¬|Ñ(<Ô(<Ñ<Ñ=Ô=Ð=r>   )NTT)
r   r   r   r   r   r   r   r   r   r   )NTF)
r   r?   r   r@   r   r   r   r   r   r   )
r   r	   r   r@   r   r   r   r   r   r   )rI   )rJ   rK   rL   rM   r   r   )&Ú__doc__Ú
__future__r   ÚloggingÚnumpyr$   Úqiskit.exceptionsr   r   Úqiskit.circuit.gater   Ú+qiskit.quantum_info.operators.base_operatorr   Ú&qiskit.quantum_info.operators.operatorr   Ú5qiskit.quantum_info.operators.channel.quantum_channelr	   Ú%qiskit.quantum_info.operators.channelr
   r   Ú(qiskit.quantum_info.states.densitymatrixr   Ú#qiskit.quantum_info.states.measuresr   Úqiskit.utilsr   r‚   Ú	getLoggerrŽ   r&   r   rB   rH   rP   rZ   r!   r#   r(   rX   r>   r=   ú<module>r²      sÔ  ððð ð #Ð "Ð "Ð "Ð "Ð "Ø €€€Ø Ð Ð Ð à FÐ FÐ FÐ FÐ FÐ FÐ FÐ FØ $Ð $Ð $Ð $Ð $Ð $Ø DÐ DÐ DÐ DÐ DÐ DØ ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø PÐ PÐ PÐ PÐ PÐ PØ ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø BÐ BÐ BÐ BÐ BÐ BØ >Ð >Ð >Ð >Ð >Ð >Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0à	ˆÔ	˜8Ñ	$Ô	$€ð
 04ØØð	l:ð l:ð l:ð l:ð l:ðb #ØØð	;)ð ;)ð ;)ð ;)ð ;)ð@ #ØØð	,ð ,ð ,ð ,ð ,ð^`ð `ð `ð `ð `ðFð ð ð$ð ð ð2Mð Mð Mð>ð >ð >ð >ð >r>   