
    z	i>                    T   S r SSKJr  SSKrSSKrSSKJrJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJr  \R6                  " \5      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 r"S r#S r$S r%g)zE
A collection of useful quantum information functions for operators.
    )annotationsN)QiskitErrorMissingOptionalLibraryError)Gate)BaseOperator)Operator)QuantumChannel)ChoiSuperOp)DensityMatrix)state_fidelity)	optionalsc           
     .   [        U [        SS5      n [        U[        SS5      nU(       a@  U R                  UR                  :w  a&  [	        SU R                   SUR                   S35      eSU 4SU44 H  u  pEUbX  U(       aQ  [        U5      nUS
UR                  -  :  n[        R                  " U5      (       a  [        R                  SUXg   5        Uc  Me  U(       d  Mn  [        U5      n[        R                  " [        R                  " USUR                  UR                  S95      n	[        R                  " U	5      (       d  M  [        R                  SUX   5        M     [        U[        5      (       a!  U R!                  UR#                  5       5      n S	nU R                  u  pUc  [        U [        5      (       a;  [        R$                  " [        R&                  " U R(                  5      U
-  5      S-  nO/[        R&                  " [        U 5      R(                  5      U
S-  -  n[+        [        R,                  " U5      5      $ [/        [1        U 5      R(                  U
-  5      n[/        [1        U5      R(                  U
-  5      n[3        XSS9$ )as  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_fidelitychanneltargetzHInput quantum channel and target unitary must have the same dimensions (z != z).InputTargetNz>%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datafloatrealr   r
   r   )r   r   
require_cp
require_tplabelchancp_condnegtp_condnon_zero	input_dim_fidstate1state2s                  `/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/quantum_info/operators/measures.pyr   r   "   s   l w1CYOGfh0BHMF;;&**$&{{m4

|2?  !'*Xv,>?
#D)GBN*Cvvc{{TL
 

#D)G~~bjj!$))RVR[R[&\]HvvhW% @( &(## //&.."23;;LI~gx((&&',,/);<AC ((77+001Y\BCRWWS\""
 4=--	9:F4<,,y89F&599    c                    [        U [        SS5      n [        U[        SS5      nUb   [        U5      nU R                  u  pV[        XX#S9nXW-  S-   US-   -  $ ! [         a  n[        S5      UeSnAff = f)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   NzTarget 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   r5   f_pros           r9   r<   r<      s    Z w1H)TGfh0GRF	f%F [[FCW
bEK!Oa((  	K 		s   A 
A4#A//A4c                d    [        U [        SS5      n [        U[        SS5      nS[        XX#S9-
  $ )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   r>   r=   )r   r   r   r<   )r   r   r,   r-   s       r9   rB   rB      s?    P wyIGfhhGF$:  r:   c           
     L  ^ SSK Jn  [        S5      m[        [	        U [        SS5      5      n U4S jnU R
                  nU R                  nXV-  nTR                  XU45      nTR                  XU45      n	U" X5      n
TR                  XU45      nTR                  XU45      nU" X5      nTR                  Xw45      nTR                  Xw45      nUR                  U5      nTR                  TR                  UU5      U/UR                  TR                  UU5      //5      nTR                  TR                  UU	5      U/UR                  * TR                  UU5      //5      nU" UU5      n[        R                  " [        R                  " U R                  XVXV45      S5      R                  U R                  R                   5      nUR"                  nUR$                  nU
S-	  XR                  :H  XR                  * :H  TR'                  U5      S:H  US-	  XR                  :H  XR                  * :H  TR'                  U5      S:H  US-	  /	nTR)                  TR'                  UU-  5      TR'                  UU-  5      -   5      nTR+                  UU5      nUR,                  " S
S	U0UD6nU$ )a  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_normchoic                0   > TR                  X* /X//5      $ )z2Block matrix for embedding complex matrix in reals)bmat)mat_rmat_icvxpys     r9   cvx_bmatdiamond_norm.<locals>.cvx_bmat(  s    zzE6?UN;<<r:   )   r   r>   r   r>   solver )scipyrD   _cvxpy_checkr
   r   
_input_dim_output_dimVariableeyerH   kronTr   	transposereshaper)   shaper+   imagr(   MaximizeProblemsolve)rF   rO   kwargsrD   rL   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solrK   s                              @r9   rE   rE      sh   H )*E t^VDED=
 __FGD >>6*+D>>6*+D	$	B>>6*+D>>6*+D	$	B
..$
&C
..$
&C::gD
 **uzz$-s3ceeUZZd=S5TU
VC
**uzz$-s3suufejjt>T5UV
WCcA ll


499v@A<gdiioo  II 	aDQ
aDQ	Q
D ..Y_5IPSO8TT
UC==d#D
**
-F
-f
-CJr:   c                    [         R                  R                  U 5        SSKnUR                  nUS   S:w  a  [        SSSU S3S9eU$ )	z1Check that a supported CVXPY version is installedr   N1zCVXPY >= 1.0rE   zIncompatible CVXPY version z found.)msg)
_optionals	HAS_CVXPYrequire_nowrK   __version__r   )namerK   versions      r9   rR   rR   a  sZ     $$T* GqzS)-gYg>
 	

 Lr:   c           	        U c  U $ [        U [        5      (       a  U $ [        U S5      (       a  U R                  5       $ [        U S5      (       a  U R	                  5       $ [        U [
        [        45      (       a  [        U 5      $ [        U S5      (       a  U R                  5       $ [        SU SU SUR                   S35      e)z(Formatting function for input conversion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__)rt   fallback_class	func_namearg_names       r9   r   r   s  s     {
 #~&&
s'(($$&&sL!!~~ #l+,,}sM""  

#H:T) =%%&i	1 r:   c                N   [        U [        5      (       aI  [        U [        5      (       d  [        U 5      n [        R                  R                  U R                  5      $ [        U 5      R                  n[        R                  " XR                  5       SS/SS/4S9R                  $ )z<Return Choi-matrix eigenvalues for checking if channel is CPr   r>   axes)r$   r	   r
   r   linalgeigvalshr)   r   	tensordotconjr+   )r   unitarys     r9   r   r     sx    '>**'4((7mGyy!!',,//w$$G<<1v1v6FGLLLr:   c                H   [        U [        5      (       a  [        U [        5      (       d  [        U 5      n U R                  n[	        [
        R                  " UR                  5      R                  [        5      5      nX"-   n[
        R                  " [
        R                  " X5      SSS9nO8[        U 5      R                  n[
        R                  " XUR                  5       SS9n[
        R                  R!                  U[
        R"                  " [%        U5      5      -
  5      $ )zGReturn partial tr Choi-matrix eigenvalues for checking if channel is TPr>   rN   )axis1axis2)r   r   r   )r$   r	   r
   r)   tupler   sqrtr[   astypeintr(   rZ   r   r   r   r   r   rV   len)r   rF   dimsr[   tr_choir   s         r9   r!   r!     s    '>**'4((7mG||RWWTZZ(//45((2::d2!1E7#((,,wVD99gs7|(<<==r:   )NTT)
r   zOperator | QuantumChannelr   z Operator | QuantumChannel | Noner,   boolr-   r   returnr*   )NTF)
r   zQuantumChannel | Operatorr   Operator | Noner,   r   r-   r   r   r*   )
r   r	   r   r   r,   r   r-   r   r   r*   )SCS)rF   zChoi | QuantumChannelrO   strr   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   rz   	getLoggerr   r   r   r<   rB   rE   rR   r   r   r!   rP   r:   r9   <module>r      s   #   F $ D ; P ? B > 0			8	$
 04	l:&l:,l: l: 	l:
 l:b #	;)&;);) ;) 	;)
 ;)@ #	,,, , 	,
 ,^`F$2M>r:   