
    {	i?                        S r SSKrSSKrSSKJrJrJrJr  SSKr	SSK
Jr  SSKJrJrJrJrJrJrJr  SSKJr  SSKJr  SSKJr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%  \RL                  " \'5      r(S\#S\S\#4S jr)S\!S\!4S jr*S\#S\#4S jr+SSSS.S jr,SSSS.S jr-\" 5       S/4/\" 5       S/4/\" 5       S/4/\" 5       S/4//r.\" 5       S/4/\" 5       S/4/\" 5       S/4/\" 5       S/4//r/\. V Vs/ s H  n \/  H  oU-   PM	     M     snn r0\" 5       S/4/r1\" 5       S/4\" 5       S/4/r2\" 5       S/4/\1\2/r3\" 5       S/4/r4\" 5       S/4\" 5       S/4/r5\" 5       S/4/\4\5/r6\3 V Vs/ s H  n \6  H  oU-   PM	     M     snn r7\" 5       4\" 5       4\" 5       4\" 5       4\" 5       4\" 5       \" 5       4\" 5       \" 5       4\" 5       \" 5       4\" 5       \" 5       4\" 5       \" 5       4\" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       \" 5       \" 5       4\" 5       4\" 5       4\" 5       \" 5       4\" 5       \" 5       4/r8\.SS \0SS S.\3SS \7SS S.S\9" SS5       VVs/ s H  n\8U    Vs/ s H  o3S/4PM	     snPM     snn0S.r:S\\\\!4      S\\\!4   S\\;   4S jr<S  r=S! r>gs  snn f s  snn f s  snf s  snnf )"a  
Noise transformation module

The goal of this module is to transform one 1-qubit noise channel
(given by the QuantumError class) into another, built from specified
"building blocks" (given as Kraus matrices) such that the new channel is
as close as possible to the original one in the Hilber-Schmidt metric.

For a typical use case, consider a simulator for circuits built from the
Clifford group. Computations on such circuits can be simulated at
polynomial time and space, but not all noise channels can be used in such
a simulation. To enable noisy Clifford simulation one can transform the
given noise channel into the closest one, Hilbert-Schmidt wise, that can be
used in a Clifford simulator.
    N)SequenceListUnionCallable)Reset)IGateXGateYGateZGateHGateSGateSdgGate)	transpile)MissingOptionalLibraryError)KrausSuperOpChi)QuantumChannel)TranspilerError   )QuantumError)
NoiseModel)
NoiseErrornoise_modelfuncreturnc                 J   [         R                  " U 5      nUR                  R                  5        H  u  p4U" U5      UR                  U'   M     UR                  R                  5        H6  u  pVUR                  5        H  u  ptU" U5      UR                  U   U'   M     M8     U$ )aR  Return a new noise model by applyign a function to all quantum errors.

This returns a new noise model containing the resulting errors from
applying the supplied function to all QuantumErrors in the noise model.
This function should have singature `func(error: QuantumError) -> QuantumError`
where the returned quantum error is defined on the same number of qubits
as the original error.

Args:
    noise_model: the noise model to be transformed.
    func: function for transforming QuantumErrors.
Returns:
    The transpiled noise model.

Raises:
    NoiseError: if the transformation failed.
)copydeepcopy_default_quantum_errorsitems_local_quantum_errors)r   r   	new_noisekeyerror	inst_name	noise_dicqubitss           _/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_aer/utils/noise_transformation.pytransform_noise_modelr*   :   s    ( k*I77==?
15e	))#. @ !* ? ? E E G	&__.MFAEeI++I6v> / !H     r%   c                      [        U R                  40 UD6n[	        [        X R                  5      5      $ ! [         a  n[        SU  SU 35      UeSnAff = f)a  Return a new quantum error containin transpiled circuits.

This returns a new QuantumError containing the circuits resulting from
transpiling all error circuits using :func:`qiskit.transpile` with the
passed keyword agruments.

Args:
    error: the quantum error to be transformed.
    transpile_kwargs: kwargs for passing to qiskit transpile function.

Returns:
    The transformed quantum error.

Raises:
    NoiseError: if the transformation failed.
z Failed to transpile circuits in z with kwargs N)r   circuitsr   r   r   zipprobabilities)r%   transpile_kwargstranspiled_circserrs       r)   transpile_quantum_errorr3   Z   si    "$U^^H7GH
 ,.A.ABCC	  .ug]CSBTU
	s   6 
A AAc                 &   ^ U4S jn[        X5      $ )a  Return a new noise model with transpiled QuantumErrors.

This returns a new noise model containing the resulting errors from
transpiling all QuantumErrors in the noise model
using :func:`transpile_quantum_error` function with the passed
keyword agruments.

Args:
    noise_model: the noise model to be transformed.
    transpile_kwargs: kwargs for passing to qiskit transpile function.

Returns:
    The transpiled noise model.

Raises:
    NoiseError: if the transformation failed.
c                    > [        U 40 TD6$ N)r3   )r%   r0   s    r)   r   #transpile_noise_model.<locals>.func   s    &uA0@AAr+   r*   )r   r0   r   s    ` r)   transpile_noise_modelr9   t   s    &B !33r+   operator_stringoperator_dictoperator_listc                @   [        U [        [        45      (       d"  [        SU R                  R
                   35      eU R                  S:  a  [        SU R                   S35      eUbP  [        R                  5       nUR                  5       nX;  a  [        U SU 35      e [        U   U R                     nUb  [        UR                  5       6 u  pcUb  [        U[        5      (       d  [        S
U 35      e U Vs/ s H(  n[        U[        5      (       a  UO[        US4/5      PM*     nn[        X5      SS n	[        R                   " S[#        U	5      -
  S5      n
U
S:  d  U
S:  a  [        SSU
-
   35      e[%        5       S/4U
4/n[        X95       H  u  pUR'                  X45        M     [        U5      $ [        S5      e! [         a"  n[        SU SU R                   S	35      UeSnAff = fs  snf ! [         a  n[        SU 35      UeSnAff = f)a  
Return a ``QuantumError`` object that approximates an error
as a mixture of specified operators (channels).

The approximation is done by minimizing the Hilbert-Schmidt distance
between the process matrix of the target error channel (:math:`T`) and
the process matrix of the output channel (:math:`S = \sum_i{p_i S_i}`),
i.e. :math:`Tr[(T-S)^\dagger (T-S)]`,
where
:math:`[p_1, p_2, ..., p_n]` denote probabilities and
:math:`[S_1, S_2, ..., S_n]` denote basis operators (channels).

See `arXiv:1207.0046 <http://arxiv.org/abs/1207.0046>`_ for the details.

Args:
    error (QuantumError or QuantumChannel): the error to be approximated.
        The number of qubits must be 1 or 2.
    operator_string (string): a name for a pre-made set of
        building blocks for the output channel (Default: None).
        Possible values are ``'pauli'``, ``'reset'``, ``'clifford'``.
    operator_dict (dict): a dictionary whose values are the
        building blocks for the output channel (Default: None).
        E.g. {"x": XGate(), "y": YGate()}, keys "x" and "y"
        are not used in transformation.
    operator_list (list): list of building block operators for the
        output channel (Default: None). E.g. [XGate(), YGate()]

Returns:
    QuantumError: the approximate quantum error.

Raises:
    NoiseError: if any invalid argument is specified or approximation failed.
    MissingOptionalLibraryError: if cvxpy is not installed.

Note:
    The operator input precedence is: ``list`` < ``dict`` < ``string``.
    If a ``string`` is given, ``dict`` is overwritten;
    if a ``dict`` is given, ``list`` is overwritten.
    The ``string`` supports only 1- or 2-qubit errors and
    its possible values are ``'pauli'``, ``'reset'``, ``'clifford'``.
    The ``'clifford'`` does not support 2-qubit errors.
zInvalid input error type: r   z2Only 1-qubit and 2-qubit noises can be converted, z-qubit noise found in modelNz3 is not a valid operator_string. It must be one of zPreset 'z<' operators do not support the approximation of errors with z qubitsz!operator_list is not a sequence:    z%Invalid type found in operator list: 	   r   zChannel probabilities sum to zHQuantum error approximation failed - no approximating operators detected)
isinstancer   r   r   	__class____name__
num_qubits_PRESET_OPERATOR_TABLEkeyslowerKeyErrorr.   r!   r   _transform_by_operator_listnproundsumr   append)r%   r;   r<   r=   valid_operator_stringsr2   _opchannel_listr/   identity_prob	noise_opsoperatorprobabilitys                 r)   approximate_quantum_errorrV      so   Z elN;<<5eoo6N6N5OPQQ!@  ;=
 	

 "!7!<!<!>)//18"# $%%;$<> 	2?CEDTDTUM   3 3 56 -22@PQQ	_ ('B !^44,Qy:QQ'   4LHLS%7!7;1 1<Q=N<OPQQwnm45	%(%F!Hh45 &GI&&
_
``7  	?+ ,0050@0@/AJ 	  	_D]OTU[^^	_sB   $G <G?  /G:/G? 
G7G22G7:G? ?
H	HHc                .   ^^^ UUU4S jn[        X5      $ )aD  
Replace all noises in a noise model with ones approximated
by a mixture of operators (channels).

Args:
    model (NoiseModel): the noise model to be approximated.
        All noises in the model must be 1- or 2-qubit noises.
    operator_string (string): a name for a pre-made set of
        building blocks for the output channel (Default: None).
        Possible values are ``'pauli'``, ``'reset'``, ``'clifford'``.
    operator_dict (dict): a dictionary whose values are the
        building blocks for the output channel (Default: None).
        E.g. {"x": XGate(), "y": YGate()}, keys "x" and "y"
        are not used in transformation.
    operator_list (list): list of building block operators for the
        output channel (Default: None). E.g. [XGate(), YGate()]

Returns:
    NoiseModel: the approximate noise model.

Raises:
    NoiseError: if any invalid argument is specified or approximation failed.
    MissingOptionalLibraryError: if cvxpy is not installed.

Note:
    The operator input precedence is: ``list`` < ``dict`` < ``string``.
    If a ``string`` is given, ``dict`` is overwritten;
    if a ``dict`` is given, ``list`` is overwritten.
    The ``string`` supports only 1- or 2-qubit errors and
    its possible values are ``'pauli'``, ``'reset'``, ``'clifford'``.
    The ``'clifford'`` does not support 2-qubit errors.
c                    > [        U TTTS9$ )Nr:   )rV   )noiser<   r=   r;   s    r)   approximate,approximate_noise_model.<locals>.approximate  s    (+''	
 	
r+   r8   )modelr;   r<   r=   rZ   s    ``` r)   approximate_noise_modelr]      s    D
 !44r+   r?   )r?   r      )pauliresetclifford	basis_opstargetc           
      x   SU S   R                   -  n[        [        R                  " U5      5      /U -   n U  Vs/ s H  n[	        U5      R
                  PM     nn[	        U5      R
                  n[        U5      n[        R                  " Xf45      n[        U5       HS  u  p[        U5       H?  u  pX:  a  [        X5      Xx   U
'   M  X:  a  Xz   U   Xx   U
'   M/  [        U	5      Xx   U'   MA     MU     S[        R                  " U V	s/ s H  n	[        XY5      PM     sn	5      -  nS n[        R                  " U  Vs/ s H
  o=" U5      PM     sn5      nU" U5      n SSKnUR#                  U5      nUR%                  UR'                  UR)                  UU5      UR*                  U-  -   5      UR-                  U5      S:*  US:  UR*                  U-  U:*  /S9nUR/                  5         UR0                  nU$ s  snf s  sn	f s  snf ! [         a'  n[        R                  S5        [!        SSS	S
S9UeSnAff = f)a  
Transform (or approximate) the target quantum channel into a mixture of
basis operators (channels) and return the mixing probabilities.

The approximate channel is found by minimizing the Hilbert-Schmidt
distance between the process matrix of the target channel (:math:`T`) and
the process matrix of the output channel (:math:`S = \sum_i{p_i S_i}`),
i.e. :math:`Tr[(T-S)^\dagger (T-S)]`,
where
:math:`[p_1, p_2, ..., p_n]` denote probabilities and
:math:`[S_1, S_2, ..., S_n]` denote basis operators (channels).

Such an optimization can represented as a quadratic program:
Minimize :math:`p^T A p + b^T p`,
subject to :math:`p \ge 0`, `\sum_i{p_i} = 1`, `\sum_i{p_i} = 1`.
The last constraint is for making the approximation conservative by
forcing the output channel have more error than the target channel
in the sense that a "fidelity" against identity channel should be worse.

See `arXiv:1207.0046 <http://arxiv.org/abs/1207.0046>`_ for the details.

Args:
    target: Quantum channel to be transformed.
    basis_ops: a list of channel objects constructing the output channel.

Returns:
    list: A list of amplitudes (probabilities) of basis that define the output channel.

Raises:
    MissingOptionalLibraryError: if cvxpy is not installed.
r   r   c           	      z    [        [        R                  " [        R                  " [	        U 5      5      5      5      $ r6   )floatrJ   abstracer   )channels    r)   fidelity-_transform_by_operator_list.<locals>.fidelity  s$    RVVBHHWW%56788r+   Nz7cvxpy module needs to be installed to use this feature.cvxpyz%Transformation/Approximation of noisezpip install cvxpyzTCVXPY is required to solve an optimization problem of approximating a noise channel.)libnamenamepip_installmsgr?   )constraints)rD   r   rJ   eyer   datalenzeros	enumerate_hs_inner_prod_real_hs_normarrayrm   ImportErrorloggerr%   r   VariableProblemMinimize	quad_formTrL   solvevalue)rb   rc   NrP   basis_ops_matsr   nAiS_ijS_jbrk   source_fids
target_fidrm   r2   xprobr/   s                        r)   rI   rI   Q  s   H 	
Yq\$$$Arvvay!"Y.I .77Yrc"gllYN7FANA
!AN+/FAu-c7Q$q'Q"3-Q 0 , 	RXXnMns*12nMNNA9 ((9=9RHRL9=>K&!J
 	qA==uq!,qssQw67YYq\Q&Q0AZ0OP  D
 	JJLGGMW 8 N >
  NO)8+.	
 	s)   G9	G>
>HH 
H9"H44H9c                     [         R                  " [         R                  " U 5      R                  U -  5      R                  $ r6   rJ   ri   conjr   real)r   s    r)   ry   ry     s(    88BGGAJLL1$%***r+   c                     [         R                  " [         R                  " U R                  5      U-  5      R                  $ r6   r   )r   Bs     r)   rx   rx     s(    88BGGACCL1$%***r+   )?__doc__r   loggingtypingr   r   r   r   numpyrJ   qiskit.circuitr   %qiskit.circuit.library.standard_gatesr   r	   r
   r   r   r   r   qiskit.compilerr   qiskit.exceptionsr   %qiskit.quantum_info.operators.channelr   r   r   5qiskit.quantum_info.operators.channel.quantum_channelr   qiskit.transpiler.exceptionsr   noise.errorsr   noise.noise_modelr   noise.noiseerrorr   	getLoggerrC   r|   r*   r3   r9   rV   r]   
_PAULIS_Q0
_PAULIS_Q1_PAULIS_Q0Q1_RESET_Q0_TO_0_RESET_Q0_TO_1	_RESET_Q0_RESET_Q1_TO_0_RESET_Q1_TO_1	_RESET_Q1_RESET_Q0Q1_CLIFFORD_GATESrangerE   rg   rI   ry   rx   )op_q0op_q1r   gates   0000r)   <module>r      s  "   2 2      & 9 E E P 8 ' * )			8	$z  j @D< D D44z 4* 44 #$d[a| 7;$^b *5\ !~%'A3 0EGaS>2BegPQs^DTU
!~%'A3 0EGaS>2BegPQs^DTU
+5N:%:%::N7QC.!7QC.57QC.1wn~~>	7QC.!7QC.57QC.1wn~~>	*3K)u}})K WJ
WJYL
WJ
WJ
Weg
Weg
Weg
Weg
WgiY
WeguwY!
Weguw
Wegwy!Y#
Wegwy!
WeguwY!
Weguw
WJ
WJ
WegY5> ab>
 QR=qr?
 	
%2,O,Q_Q%78%7TQC[%78,O Tnl:;<T.,./T 
%[Tn+
+e O LR 9Os$   M
<M?MMMM