
    {	in                     8   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr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  S	SKJr  S	SKJrJr  S	SKJr  SSK J!r!J"r"J#r#  SSK$J%r%J&r&  SSK'J(r(  SSK)J*r*  \RV                  " \,5      r- " S S\\5      r.g)z
Aer qasm simulator backend.
    N)ABCabstractmethod)QuantumCircuitParameterExpressionDelay)	BackendV2)Result)CouplingMap)Target   )AerError)AerJob)
NoiseModelQuantumErrorLocation)QuantumChannelInstruction   )compile_circuitassemble_circuitsgenerate_aer_config)format_save_typecircuit_optypes)NAME_MAPPING)	AerConfigc                      ^  \ rS rSrSr SU 4S jjrS rSS jrSS jrS r	S r
S	 r\S
 5       r\S 5       rS rS r SS jr\S 5       rS rS rS r\S 5       rS rS rS rS rS rSrU =r$ )
AerBackend,   zAer Backend class.c                   > [         TU ]  UUR                  UR                  UR                  S9  X l        Xl        0 U l        0 U l        X@l	        [        U l        Ub  SU l        OSU l        Ub  U R                  " S0 UD6  U R                  5       R                  b)  [!        U R                  5       R                  5      U l        gg)a;  Aer class for backends.

This method should initialize the module and its configuration, and
raise an exception if a component of the module is
not available.

Args:
    configuration (AerBackendConfiguration): backend configuration.
    properties (AerBackendProperties or None): Optional, backend properties.
    provider (Provider): Optional, provider responsible for this backend.
    target (Target):  initial target for backend
    backend_options (dict or None): Optional set custom backend options.

Raises:
    AerError: if there is no name in the configuration
)providernamedescriptionbackend_versionNTF )super__init__backend_namer    r!   _properties_configuration_options_configuration_options_properties_targetr   _mapping_from_backendset_optionsconfigurationcoupling_mapr
   _coupling_map)selfr.   
propertiesr   targetbackend_options	__class__s         X/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_aer/backends/aerbackend.pyr$   AerBackend.__init__/   s    ( 	++%11)99	 	 	
 &+ ')##% $!%D!&D &// ,,8!,T-?-?-A-N-N!OD 9    c                 V  ^^	 / m	UU	4S jnU" [         R                  SUR                  5        [        UR                  5       Hb  u  pVUR
                  R                  5       (       d  M&  [        UR
                  R                  5       H  u  pxU" U(       a  X5   OUXx5        M     Md     T	$ )Nc           
      H  > UT	;   a  T
R                  X4T	U   /5        g [        U[        5      (       a  UR                  (       d  g T	(       d  [	        S5      e[        [        [        T	R                  5       5      5      5      n[        U5       VVs/ s H8  nUR                  T	R                  5       -   Vs0 s H  nUT	U   U   _M     snPM:     nnnU Vs/ s H  n[        UR                  U5      5      PM     nnT
R                  X4U/5        g g s  snf s  snnf s  snf )Nz(The element of parameter_binds is empty.)append
isinstancer   
parametersr   lennextitervaluesrangekeysfloatbind)indexbind_posparamlen_valsi	parameter	bind_listxbound_valuesbindsparameterizationss            r6   append_param_values>AerBackend._convert_circuit_binds.<locals>.append_param_valuesc   s   ~!((5*;U5\)JKE#677 ''"#MNNtD$89: #8_
 - */)9)9EJJL)H)HI "5#3A#66)H -   ?HHiejjm 4iH!((5*;\)JK! 8  Is   %D7D
D$DD)r   GLOBAL_PHASE_POSglobal_phase	enumeratedata	operationis_parameterizedparams)
r1   circuitrO   idx_maprQ   rF   instructionrG   rH   rP   s
     `      @r6   _convert_circuit_binds!AerBackend._convert_circuit_binds`   s    	L* 	I66G<P<PQ"+GLL"9E$$5577'01F1F1M1M'NOH''uh^ (O #: ! r8   c           
      J   [        U[        5      (       a0  [        U5      S:  a  [        S5      eU R	                  XS   S 5      /$ [        U5      [        U5      :w  a  [        S5      e[        U5       VVs/ s H  u  pEU R	                  XRU   X4   5      PM     nnnU$ s  snnf )Nr   z9More than 1 parameter table provided for a single circuitr   zSNumber of input circuits does not match number of input parameter bind dictionaries)r<   r   r>   r   r^   rV   )r1   circuitsparameter_bindsidx_mapsidxr[   rP   s          r6   _convert_bindsAerBackend._convert_binds   s    h//?#a'Z[[//!:LdSTT!S]2.  !*( 3
 3 ''1Ex}U 3 	 
 ! 	
s   7#Bc                 X    [        U[        5      (       a  U/nU R                  " X40 UD6$ )a  Run circuits on the backend.

Args:
    circuits (QuantumCircuit or list): The QuantumCircuit (or list
        of QuantumCircuit objects) to run
    parameter_binds (list): A list of parameter binding dictionaries.
                            See additional information (default: None).
    run_options (kwargs): additional run time backend options.

Returns:
    AerJob: The simulation job.

Raises:
    TypeError: If ``parameter_binds`` is specified with an input or
        has a length mismatch with the number of circuits.

Additional Information:
    * Each parameter binding dictionary is of the form::

        {
            param_a: [val_1, val_2],
            param_b: [val_3, val_1],
        }

      for all parameters in that circuit. The length of the value
      list must be the same for all parameters, and the number of
      parameter dictionaries in the list must match the length of
      ``circuits`` (if ``circuits`` is a single ``QuantumCircuit``
      object it should a list of length 1).
    * kwarg options specified in ``run_options`` will temporarily override
      any set options of the same name for the current run.

Raises:
    ValueError: if run is not implemented
)r<   r   _run_circuits)r1   ra   rb   run_optionss       r6   runAerBackend.run   s/    H h// zH!!(K{KKr8   c           	          [        [        R                  " 5       5      n[        U UU R                  UUUS9nUR                  5         U$ )z+Run circuits by generating native circuits.)rb   ra   ri   )struuiduuid4r   _execute_circuits_jobsubmit)r1   ra   rb   ri   job_idaer_jobs         r6   rh   AerBackend._run_circuits   sG     TZZ\"&&+#
 	r8   c                    [         R                   " U R                  5      nU R                  R                  5        H  u  p#[	        XU5        M     [        US5      (       a  UR                  UR                  -   Ul        U$ )zsReturn the simulator backend configuration.

Returns:
    BackendConfiguration: the configuration for the backend.
custom_instructions)copyr'   r(   itemssetattrhasattrbasis_gatesrv   )r1   configkeyvals       r6   r.   AerBackend.configuration   sk     4../3399;HCF% < 6011!'!3!3f6P6P!PFr8   c                     [         R                   " U R                  5      nU R                  R                  5        H  u  p#[	        XU5        M     U$ )zReturn the simulator backend properties if set.

Returns:
    BackendProperties: The backend properties or ``None`` if the
                       backend does not have properties set.
)rw   r&   r)   rx   ry   )r1   r2   r}   r~   s       r6   r2   AerBackend.properties   sC     YYt//0
00668HCJS) 9r8   c                 v    [        U R                  5       S5      (       a  U R                  5       R                  $ g )Nmax_experiments)rz   r.   r   r1   s    r6   max_circuitsAerBackend.max_circuits   s1    4%%'):;;%%'777r8   c                    U R                   (       a  U R                  $ SSKJn  SSKJnJnJnJn  SSK	J
n  SSKJn  SSKJn  SSKJn	  SSKJn
  S	S
/nU R%                  5       nU R'                  5       nU" 5       nUR)                  [*        5        UUUUS.nSUR,                  0n[/        US5      (       a  UR0                  US'   [/        US5      (       a  UR)                  UR2                  5        [5        [7        US/ 5      5      n[5        [7        US/ 5      5      nUR8                   Vs0 s H  nUR:                  U_M     nn[4        R=                  U[5        U5      UR?                  U	5      5      n0 n[5        5       n[5        5       n/ nU H  nUU;   a  M  UU;   a
  UU   UU'   M  UU;   aT  UU   n[A        [C        U[7        US/ 5      5      5      n[7        US/ 5      nU" UU(       a  [E        US   5      OSUS9UU'   Mu  [F        RH                  " SU S3[J        5        URM                  U5        M     U H  nURO                  U5        M     [P        RS                  U5      nU HJ  nUU;   d  M  [7        UU   SS 5      =n(       d  M$  [P        RS                  [C        [T        U5      5      UU'   ML     U(       Ga  S n[W        UR,                  5       V s1 s H  n URY                  U 5      (       a  M  U iM     nn / n![W        SUR,                  5       Hc  n" UR[                  U"S9n#U!RM                  U
" U#R_                  SS5      S   U#R_                  SS5      S   U#R_                  SS5      S   S95        Me     U!US'   U H  nURa                  U5      Rc                  5        Hz  u  n$n[4        R?                  UU$5      (       d  URe                  UU$5      (       d   UU   U$	 URg                  UU$45        MT  UU   c  0 UU'   U" U" US5      U" US5      S 9UU   U$'   M|     [i        UU   [P        5      (       d  M  [k        S! UU   Rm                  5        5       5      (       d  M  [n        Rq                  S"U5        M     0 US	'   [W        UR,                  5       H8  n%U%U;   a  M  UR[                  U%5      n&U" U" U&S#5      U" U&S$5      S 9US	   U%4'   M:     U H>  n'UU'   b  M  [W        UR,                  5       V s0 s H  n U U;  d  M  U 4S _M     sn UU''   M@     [s        S'0 UD6n(U HB  n)U)U;   a  U(Ru                  UU)   U)S%9  M  U(Ru                  UU)   UR_                  U)S 5      U)S&9  MD     U Rv                  b)  U Rv                  Rx                  R{                  5       U(l>        U($ s  snf s  sn f ! [\         a     GM  f = f! [\         a     GNf = fs  sn f )(Nr   )InstructionProperties)	ForLoopOpIfElseOpSwitchCaseOpWhileLoopOp)get_standard_gate_name_mapping)	Parameter)Gate)CONTROL_FLOW_OP_NAMES)QubitPropertiesmeasuredelay)if_else
while_loopfor_loopswitch_case
num_qubitsdttiming_constraintsr{   supported_instructionsr=   r/   )r   r   rZ   zNo gate definition for z> can be found and is being excluded from the generated target.c                 @    U R                  US 5      =n(       a  US   $ g )Nr   )get)	prop_dict	prop_namendvals      r6   
_get_value%AerBackend.target.<locals>._get_valueN  s$    %MM)T::5: 8Or8   )qubitT1)NNT2	frequency)t1t2r   qubit_properties
gate_errorgate_length)errordurationc              3   (   #    U  H  oS L v   M
     g 7fNr"   ).0vs     r6   	<genexpr>$AerBackend.target.<locals>.<genexpr>|  s      A'C!I'Cs   zGate properties of instruction %s are not provided for every qubits. This gate is ideal for some qubits and the rest is with finite error. Created backend target may confuse error-aware circuit optimization.readout_errorreadout_length)r]   r   )r]   r2   r   r"   )?r,   r*   qiskit.transpiler.targetr   qiskit.circuit.controlflowr   r   r   r   %qiskit.circuit.library.standard_gatesr   qiskit.circuit.parameterr   qiskit.circuit.gater   r   qiskit.providers.backendr   r.   r2   updater   r   rz   r   r   setgetattrgatesr   unionintersectionlistmapr>   warningswarnRuntimeWarningr;   removedictfromkeystuplerB   is_qubit_operationalqubit_propertyKeyErrorr   gate_propertyrx   is_gate_operationaladdr<   anyrA   loggerinfor   add_instructionr0   graphrw   _coupling_graph)*r1   r   r   r   r   r   r   r   r   r   r   requiredr.   r2   qiskit_inst_mappingqiskit_control_flow_mappingin_datar{   r   gategate_configsall_instructionsinst_name_map
faulty_opsfaulty_qubitsunsupported_instructionsr   this_configrZ   r/   prop_name_mapr   qr   qir   qubits	qubit_idx
qubit_propopr3   	inst_names*                                             r6   r3   AerBackend.target   s   << 	C]]X6,D<w'**,__&
 =>""<0  %!'	'
#  !9!9: =$''),,GDM="677NN=;;< '-CD!$W]<TVX%Y!Z4A4G4GH4GD		44GH99X(>(K(KLa(b
 U
#%  %D22**&9$&?d#%*40c)W[,PR-STU&{NBG&*7Cs<?3!'d# -dV 41 1"
 )//5) %, -D##D) - &67$D|##*<+=~t#TT<T
 +/--E<8P*QM$' %  !!9!9::a*BaBabcBd:    "A}778 * 9 9 9 CI !''#$==|<Q?$==|<Q?"+--\"J1"M 9 +;G&'(&0&>&>t&D&J&J&LNFF''%v ';;D&II! !.d 3F ; #f~6 $T*2.0d+2G(>!+FM!B3M$'/ 'M$ mD1488S A'4T':'A'A'CA > >
 KK_ 	1 )@ (*M)$"=#;#;<	-'66yA
9N$ZA'
4DE:i()6	 = BR ((-m.F.F(G%(G11TaKaJQD$J(G%b!  "'")I77&& ;I F" ' 
 && -i 8,00DA" '  * )%)%7%7%=%=%B%B%DF"k Ix   (  ( ! !J%sB   )V$V)3V)V.W 9
WW.
V=<V= 
WWc                 l    U R                   (       d#  XR                  l        U R                  SU5        gg)z9Set maximun number of qubits to be used for this backend.n_qubitsN)r,   r'   r   _set_configuration_option)r1   
max_qubitss     r6   set_max_qubitsAerBackend.set_max_qubits  s,    !!+5(**:zB "r8   c                 J    U R                  5       U l        0 U l        0 U l        g)z.Reset the simulator options to default values.N)_default_options_optionsr(   r)   r   s    r6   clear_optionsAerBackend.clear_options  s"    --/&(##% r8   c                    [         R                   " 5       nU R                  " U40 UD6u  pU R                  b&  [        XR	                  5       R
                  5      u  pO[        U5      u  pU(       a  U R                  XU	5      US'   O"[        S U 5       5      (       d  [        S5      e[        U5       H  u  pXl
        M     [        XR                  40 UD6n[        X5       VVs0 s H  u  pUR                  UR                  _M     nnnU R                  XU5      n[!        U["        5      (       dH  [$        R'                  SU R(                  5        U(       a  [$        R'                  SU5        [        S5      eXOS'   [*        R*                  R-                  5       R/                  5       US'   U R(                  US	'   U R	                  5       R0                  US
'   US    H%  nSU;  a  M  UUR3                  S5         US   S'   M'     [         R                   " 5       U-
  US'   UR5                  SS5      (       d(  SnSU;   a  USUS    3-  n[$        R7                  U5        U(       a  U R9                  U5      $ U$ s  snnf )z	Run a jobrP   c              3   R   #    U  H  n[        UR                  5      S :H  v   M     g7f)r   N)r>   r=   )r   r[   s     r6   r   3AerBackend._execute_circuits_job.<locals>.<genexpr>  s      JgS++,1s   %'z>circuits have parameters but parameter_binds is not specified.z%s: simulation failed.z
Output: %sz5simulation terminated without returning valid output.rr   dater%   r!   resultsheadercirc_idmetadata
time_takensuccessFzSimulation failedstatusz+ and returned the following error message:
)time_compiler*   r   r.   r{   re   allr   rV   r   r   optionszipr   _execute_circuitsr<   r   r   r   r   datetimenow	isoformatr!   popr   warning_format_results)r1   ra   rb   ri   rr   format_resultstartnoise_modelaer_circuitsrc   r   aer_circuitr|   r[   metadata_mapoutputresultmsgs                     r6   rp    AerBackend._execute_circuits_job  sZ   
 		 !%h F+ F<<#%6xASASAUAaAa%b"L(%6x%@"L/3/B/B80K+, JJJJ[\\$-l$; G") %< %X||K{K
 ),L(C
(C$ !1!11(C 	 
 ''6J &$''LL1499=\62RSS "x!**..0::<v!%~$($6$6$8$H$H ! Y'Fv%+7

98M+NF8Z( (  $yy{U2| zz)U++%C6!EfXFVEWXXNN3''//I
s   (#I>c                 <   U S    H~  nUR                  S0 5      nUR                  S0 5      nUR                  S0 5      nUR                  S0 5      nUR                  5        H  u  pgXd;   d  M  [        XtU   XV   5      X&'   M!     M     [        R                  " U 5      $ )z3Format C++ simulator output for constructing Resultr   rW   r   result_typesresult_subtypes)r   rx   r   r	   	from_dict)r  r  rW   r   
save_typessave_subtypesr}   r~   s           r6   r  AerBackend._format_results  s     Y'F::fb)Dzz*b1H!nb9J$LL):B?M JJL$ 0o}GY ZDI ) ( ''r8   c                     [        U[        5      (       a  U/nU Vs/ s H  n[        U5      PM     nn[        XS9u  pU R                  " X40 UD6u  pnX4$ s  snf )z Compile circuits and noise model)optypes)r<   r   r   r   _assemble_noise_model)r1   ra   ri   circr   r  s         r6   r  AerBackend._compile	  sp    h// zH5=>XT?4(X> ,HF .2-G-G.
!,.
*{ $$ ?s   Ac                    [         R                   " U5      nSnUR                  S[        U R                  SS5      5      nU(       a  [	        S U 5       5      (       a  UR                  5       nUb  [        U5       VV	s/ s H  u  p[        U	;   d  M  UPM     n
nn	UR                  U
 Vs/ s H  oU   PM	     sn5      n[        U[        5      (       a  U/n[        X5       H  u  pXU'   [        U5      X('   M     [        U5       GH_  u  p[        X(   ;   d  M  Sn/ nUR                   H  nUR                  UR                   UR"                  nnn[        U[        5      (       a  SnU(       d&  Uc  [%        5       nO[         R&                  " U5      nSnUR(                  n[+        U5      nUR-                  UUU45        X(   R/                  [*        5        UR0                  UR2                  ;  a  UR5                  UUR0                  5        M  M  UR-                  UUU45        M     U(       d  GM-  UR                  5       nUUl        UXH'   X(   R7                  [        5        GMb     XFU4$ s  sn	nf s  snf )z<Move quantum error instructions from circuits to noise modelFr  Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r<   r   )r   r"  s     r6   r   3AerBackend._assemble_noise_model.<locals>.<genexpr>'  s     YLDz$??Ls   T)rw   r   r   r  r  _pass_managerrV   r   rj   r<   r   r  r   r   rW   rX   r   clbitsr   deepcopy_quantum_errorr   r;   r   id_default_quantum_errorsadd_all_qubit_quantum_errordiscard)r1   ra   r   ri   run_circuitsupdated_noiser  npmrd   optypetranspile_idxsrJ   transpiled_circuitsr"  updated_circnew_datadatuminstqargscargsqerror
qerror_locnew_circs                          r6   r!   AerBackend._assemble_noise_model  s/    yy*
 !oomWT\\=Z^5_`
 3YLYYY++-C9B79K!_9K+#uX^#9K!_ '*gg.W1A.W&X#1>BB+>*?' "%^!IIC(,%#24#8GL "J
 #<0IC(GL8$!YYE).%,,%D!$(ABB'+,*2.8l.2mmK.H,0M!%!4!4%9&%A
 UE(BC(()=>!99K,O,OO'CCFFIIV P !ue(<=) '*  <#yy{H$,HM(0L%L(()BC; 1@ +55[ "` /Xs   ?I0I0+I6c                 F    SU;   a  US   $ [        U R                  SS5      $ )zGet the executorexecutorN)r   r   )r1   ri   s     r6   _get_executorAerBackend._get_executorZ  s(    $z**4==*d;;r8   c                     g)zExecute aer circuits on the backend.

Args:
    aer_circuits (List of AerCircuit): simulator input.
    noise_model (NoiseModel): noise model
    config (Dict): configuration for simulation

Returns:
    dict: return a dictionary of results.
Nr"   )r1   r  r  r|   s       r6   r  AerBackend._execute_circuitsa  s     	r8   c                    [        U R                  U5      (       a  U R                  X5        g[        U R                  U5      (       a  U R	                  X5        g[        U R
                  U5      (       d  [        SU 35      eUb  [        U R
                  X5        g[        U R
                  U[        U R                  5       U5      5        g)a  Special handling for setting backend options.

This method should be extended by sub classes to
update special option values.

Args:
    key (str): key to update
    value (any): value to update.

Raises:
    AerError: if key is 'method' and val isn't in available methods.
zInvalid option N)
rz   r'   r   r&   _set_properties_optionr   r   ry   r   r   r1   r}   values      r6   
set_optionAerBackend.set_optiono  s    " 4&&,,**36T%%s++''34==#..677 s2
 sGD4I4I4KS,QRr8   c                 X    UR                  5        H  u  p#U R                  X#5        M     g)zSet the simulator optionsN)rx   rI  )r1   fieldsr}   rH  s       r6   r-   AerBackend.set_options  s      ,,.JCOOC' )r8   c                 ~    Ub  X R                   U'   gXR                   ;   a  U R                   R                  U5        gg)z;Special handling for setting backend configuration options.N)r(   r  rG  s      r6   r   $AerBackend._set_configuration_option  s;    /4'',///''++C0 0r8   c                 ~    Ub  X R                   U'   gXR                   ;   a  U R                   R                  U5        gg)z8Special handling for setting backend properties options.N)r)   r  rG  s      r6   rF  !AerBackend._set_properties_option  s;    ,1$$S),,,$$((- -r8   c                 ^    U R                   R                  nSU R                   S3nU SU S3$ )z'String representation of an AerBackend.'())r5   __name__r   )r1   r   displays      r6   __repr__AerBackend.__repr__  s6    ~~&&dii["q	##r8   )	r'   r0   r,   r+   r   r(   r)   r&   r*   )NNNNr   ) T) rV  
__module____qualname____firstlineno____doc__r$   r^   re   rj   rh   r.   r2   propertyr   r3   r   r   rp   staticmethodr  r  r!  rA  r   r  rI  r-   r   rF  rX  __static_attributes____classcell__)r5   s   @r6   r   r   ,   s     [_/Pb!@!$'LR 
    BC& PT?B 
( 
(% ?6B<  SB(
1.$ $r8   r   )/r^  rw   r  loggingr  rn   r   abcr   r   qiskit.circuitr   r   r   qiskit.providersr   Backendqiskit.resultr	   qiskit.transpilerr
   r   r   aererrorr   jobsr   noise.noise_modelr   r   noise.errors.base_quantum_errorr   aer_compilerr   r   r   backend_utilsr   r   name_mappingr   controller_wrappersr   	getLoggerrV  r   r   r"   r8   r6   <module>rs     sr          # E E 1   ) +   @ G Q Q < & + 
		8	${	$# {	$r8   