
    iC                        S r SSKJr  SSKrSSKrSSK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  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  SSKJrJ r J!r!J"r"  SSK#J$r$  \RJ                  " \&5      r' " S S\
5      r(g)z 
Base class for dummy backends.
    )AnyN)QuantumCircuit)	BackendV2)	optionals)Target)Options)BasicSimulator)convert_to_target)decode_backend_configurationproperties_from_server_data   )QiskitRuntimeService)BackendEncoder)configuration_from_server_data)BackendPropertiesBackendConfigurationBackendStatusQasmBackendConfiguration)BackendPropertyErrorc                   j  ^  \ rS rSrSrSrSrSrSrSU 4S jjr	S\
S\4S jrSS jrS\4S	 jrSS
 jrS\4S jrS\
S\4S jrS\4S jrSS\S\4S jjrS\4S jrS\SS4S jr\S\4S j5       r\SS j5       r\ S\!4S j5       r"\S\#4S j5       r$S r%      S S jr&SS\'S\SS4S jjr(Sr)U =r*$ )!FakeBackendV27   zTA fake backend class for testing and noisy simulation using real backend
snapshots.
Nreturnc           	      D  > U R                  5       U l        SU l        [        TU ]  SU R                  R                  S5      U R                  R                  S5      U R                  R                  S5      U R                  R                  S5      S9  SU l        SU l        g)zFakeBackendV2 initializer.Nbackend_namedescriptiononline_datebackend_version)providernamer   r   r   )_get_conf_dict_from_json
_conf_dict_props_dictsuper__init__get_targetsim)self	__class__s    g/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/fake_provider/fake_backend.pyr%   FakeBackendV2.__init__B   s    779$$^4++M:++M: OO//0AB 	 	
     r    c                 &   US;   a/  [        SR                  U R                  R                  U5      5      e U R	                  5       R                  U5      $ ! [          a0    [        SR                  U R                  R                  U5      5      ef = f)zGets attribute from self or configuration

This magic method executes when user accesses an attribute that
does not yet exist on the class.
)r'   r"   r#   z!'{}' object has no attribute '{}')AttributeErrorformatr*   __name__configuration__getattribute__)r)   r    s     r+   __getattr__FakeBackendV2.__getattr__P   s     ;; 3::4>>;R;RTXY 
	%%'88>> 	 3::4>>;R;RTXY 	s   A :Bc                 *   [         R                  (       ao  SSKJn  U" 5       U l        U R
                  (       aK  U R                  (       a9  U R                  5       nU R                  R                  US9  U R                  US9  g g g [        5       U l        g )Nr   AerSimulator)noise_model)

_optionalsHAS_AER
qiskit_aerr8   r(   targetr#    _get_noise_model_from_backend_v2set_optionsr	   )r)   r8   r9   s      r+   
_setup_simFakeBackendV2._setup_simd   so    /#~DH{{t//"CCE$$$=   [ 9  0{ &'DHr-   c                     U R                   (       d  g U R                  U R                   5      n[        U5        U R                  US'   U$ )Nr   )conf_filename
_load_jsonr   r   )r)   	conf_dicts     r+   r!   &FakeBackendV2._get_conf_dict_from_jsons   s@    !!OOD$6$67	$Y/$($5$5	.!r-   c                     U R                   (       a-  U R                  U R                   5      n[        U5        Xl        g g N)props_filenamerD   r   r#   )r)   
props_dicts     r+   _set_props_dict_from_json'FakeBackendV2._set_props_dict_from_json{   s2    )<)<=J'
3) r-   c                 T    U R                   R                  S5      =(       d    / nSU;   $ )Nsupported_featuresqasm3r"   r&   )r)   rN   s     r+   _supports_dynamic_circuits(FakeBackendV2._supports_dynamic_circuits   s)    !__001EFL",,,r-   filenamec                     [        [        R                  R                  U R                  U5      5       n[
        R                  " U5      nS S S 5        U$ ! , (       d  f       W$ = frH   )openospathjoindirnamejsonload)r)   rS   f_jsonthe_jsons       r+   rD   FakeBackendV2._load_json   sO    GGLLx0
yy(H
 	
 
 s   A
A$c                 R    U R                   SSSSS.n[        R                  " U5      $ )zEReturn the backend status.

Returns:
    The status of the backend.

 activeTr   )r   r   
status_msgoperationalpending_jobs)r    r   	from_dict)r)   
api_statuss     r+   statusFakeBackendV2.status   s1     !II!"

 &&z22r-   refreshc                     U(       d  U R                   c  U R                  5         [        R                  " U R                   5      $ )zReturn the backend properties

Args:
    refresh: If ``True``, re-retrieve the backend properties from the local file.

Returns:
    The backend properties.
)r#   rK   r   re   )r)   ri   s     r+   
propertiesFakeBackendV2.properties   s5     t''/**, **4+;+;<<r-   c                 B    [         R                  " U R                  5      $ )z!Return the backend configuration.)r   re   r"   r)   s    r+   r2   FakeBackendV2.configuration   s    #--doo>>r-   circuitc                   ^ U R                  5       (       d  gU R                  5       R                  5       nU R                  5       R                  5       nU Vs/ s H3  n[        UR                  5      S:  d  M  [        UR                  5      PM5     nnTR                   H  nUR                  R                  S:X  a  M  [        U4S jUR                   5       5      nU H'  nX;   d  M
  [        STR                   SU SU S35      e   [        U5      S	:X  d  M|  Xu;   d  M  [        STR                   SU S
U 35      e   gs  snf )zCheck if the input circuit uses faulty qubits or edges.

Args:
    circuit: Circuit to check.

Raises:
    ValueError: If an instruction operating on a faulty qubit or edge is found.
N   barrierc              3   Z   >#    U  H   nTR                  U5      R                  v   M"     g 7frH   )find_bitindex).0xrp   s     r+   	<genexpr>-FakeBackendV2.check_faulty.<locals>.<genexpr>   s"     !R\'"2"21"5";";\s   (+zCircuit z contains instruction z operating on a faulty qubit .r   z operating on a faulty edge )
rk   faulty_qubitsfaulty_gateslenqubitstupledata	operationr    
ValueError)	r)   rp   r|   r}   gatefaulty_edgesinstrqubit_indices
circ_qubits	    `       r+   check_faultyFakeBackendV2.check_faulty   s-      )779(5577C\|ts4;;GWZ[G[*dkk*|\\\E##y0!!RU\\!RRM+
.$"7<<.0F '!>zl!M  , =!Q&=+H w||n,Bg9-J  " ]s   E9Ec                 N   U R                   c  U R                  5         U R                  c  U R                  5         [        R
                  " U R                  5      nSnU R                  b   [        R
                  " U R                  5      n[        UUSSS9U l         U R                   $ )zLA :class:`qiskit.transpiler.Target` object for the backend.

:rtype: Target
NTr2   rk   include_control_flowinclude_fractional_gates)	r'   r!   r#   rK   r   re   r"   r   r
   )r)   confpropss      r+   r=   FakeBackendV2.target   s     <<))+'..0'11$//BDE+)33D4D4DE,"  &*)-DL ||r-   c                     g)zThis property used to return the `max_experiments` value from the
backend configuration but this value is no longer an accurate representation
of backend circuit limits. New fields will be added to indicate new limits.
N rn   s    r+   max_circuitsFakeBackendV2.max_circuits   s     r-   c                     [         R                  (       a  SSKJn  UR	                  5       $ [
        R                  " 5       $ )a:  Return the default options

This method will return a :class:`qiskit.providers.Options`
subclass object that will be used for the default options. These
should be the default parameters to use for the options of the
backend.

Returns:
    qiskit.providers.Options: A options object with
        default values set
r   r7   )r:   r;   r<   r8   _default_optionsr	   )clsr8   s     r+   r   FakeBackendV2._default_options   s.     /0022!2244r-   c                 J    U R                   R                  S5      nUb  US-  $ g)ziReturn the system time resolution of output signals

Returns:
    The output signal timestep in seconds.
dtmNg&.>rP   )r)   r   s     r+   r   FakeBackendV2.dtm  s*     oo!!%(?:r-   c                     U R                   c  U R                  5         U R                  U R                   l        U R                   R                  " U40 UD6nU$ )a  Run on the fake backend using a simulator.

This method runs circuit jobs (an individual or a list of QuantumCircuit)
using BasicSimulator or Aer simulator and returns a
:class:`~qiskit.providers.Job` object.

If qiskit-aer is installed, jobs will be run using AerSimulator with
noise model of the fake backend. Otherwise, jobs will be run using
BasicSimulator without noise.

Currently noisy simulation of a pulse job is not supported yet in
FakeBackendV2.

Args:
    run_input (QuantumCircuit or list): An
        individual or a list of
        :class:`~qiskit.circuit.QuantumCircuit`
    options: Any kwarg options to pass to the backend for running the
        config. If a key is also present in the options
        attribute/object then the expectation is that the value
        specified will be used instead of what's set in the options
        object.

Returns:
    Job: The job object for the run
)r(   r@   _optionsrun)r)   	run_inputoptionsjobs       r+   r   FakeBackendV2.run  sD    6 88OO MMhhll900
r-   c           
      "   SSK Jn  SSKJn  SSKJn	Jn
Jn  SSKJ	n  U R                  c  U R                  5         [        R                  " U R                  5      nU R                  5       R                  nU R                   nU R"                  nU" US9nU(       a$  U" U5       H  u  nnUR%                  UU5        M     [&        R(                  " 5          [&        R*                  " SS	S
9  U
" UUUUUUS9nSSS5        W H  u  nnnUR-                  UUU5        M     U(       a   [/        U5       Vs/ s H  nU	" UR1                  U5      US9PM     nn U" [/        U5       Vs/ s H  nUR5                  U5      PM     sn[/        U5       Vs/ s H  nUR7                  U5      PM     snUUUS9nUR8                  R;                  U5        U$ U$ ! , (       d  f       N= fs  snf ! [2         a    Sn Nf = fs  snf s  snf ! [2         a     U$ f = f)zBuild noise model from BackendV2.

This is a temporary fix until qiskit-aer supports building noise model
from a BackendV2 object.
r   )Delay)
NoiseModel)_excited_populationbasic_device_gate_errorsbasic_device_readout_errors)RelaxationNoisePassN)basis_gatesignorezqiskit_aer.noise.device.models)module)
gate_errorthermal_relaxationgate_lengthsgate_length_unitstemperature)freqr   )t1st2sdtop_typesexcited_state_populations)qiskit.circuitr   qiskit_aer.noiser   qiskit_aer.noise.device.modelsr   r   r   qiskit_aer.noise.passesr   r#   rK   r   re   r2   r   
num_qubitsr   add_readout_errorwarningscatch_warningsfilterwarningsadd_quantum_errorrange	frequencyr   t1t2_custom_noise_passesappend)r)   r   readout_errorr   r   r   r   r   r   r   r   r   r   rk   r   r   r   r9   r   errorgate_errorsr    qr   
delay_passs                            r+   r>   .FakeBackendV2._get_noise_model_from_backend_v27  s    	)/	
 	

	
 #**,&001A1AB
((*66__
WW [9 !<Z!H--eV< "I $$&##7 3%#5)"3'K ' $/D&%))%v> $/ 1 #:.-. (Z-A-A!-DR]^. * -0383DE3Daq)3DE383DE3Daq)3DE".G
 0077
C {O '&&- ( 1,0)1 FE (  s`   !G#G$ 0 GG$ H  "G6=H  G;
'#H  
GG$ $G32G36
H   
HHserviceuse_fractional_gatesc                 V   [        U[        5      (       d  [        S5      eU R                  R	                  SS5      n UR                  X2S9nUS   nUR                  SS9n[        UR                  5       R                  USS9US9nUR                  5       nU R                  US	'   U(       a~  [        R                  R                  U R                  U R                  5      n	[!        U	S
SS9 n
U
R#                  [$        R&                  " UR                  5       [(        S95        SSS5        U(       a~  [        R                  R                  U R                  U R*                  5      n[!        US
SS9 n
U
R#                  [$        R&                  " UR                  5       [(        S95        SSS5        U R-                  5       U l        U R1                  5         [2        R4                  " U R.                  5      n[6        R4                  " U R8                  5      n[;        UUSSS9U l        [>        RA                  SU R                  5        g! , (       d  f       GN+= f! , (       d  f       N= f! [B         a4  n[>        RE                  SU R                  [G        U5      5         SnAgSnAff = f)a  Update the data files from its real counterpart

This method pulls the latest backend data files from their real counterpart and
overwrites the corresponding files in the local installation:

*  ``../fake_provider/backends/{backend_name}/conf_{backend_name}.json``
*  ``../fake_provider/backends/{backend_name}/defs_{backend_name}.json``
*  ``../fake_provider/backends/{backend_name}/props_{backend_name}.json``

The new data files will persist through sessions so the files will stay updated unless they
are manually reverted locally or when ``qiskit-ibm-runtime`` is upgraded or reinstalled.

Args:
    service: A :class:`QiskitRuntimeService` instance
    use_fractional_gates: Set True to allow for the backends to include
        fractional gates.

Raises:
    ValueError: if the provided service is a non-QiskitRuntimeService instance.
    Exception: If the real target doesn't exist or can't be accessed
zThe provided service to update the fake backend is invalid. A QiskitRuntimeService is required to retrieve the real backend's current properties and settings.fakeibm)r   r   T)ri   )
raw_configr   r   wzutf-8)encoding)r   Nr   zEThe backend %s has been updated with the latest data from the server.z#The refreshing of %s has failed: %s)$
isinstancer   r   r   replacebackendsrk   r   _get_api_clientbackend_configurationto_dictrV   rW   rX   rY   rC   rU   writerZ   dumpsr   rI   r!   r"   rK   r   re   r   r#   r
   r'   loggerinfo	Exceptionwarningstr)r)   r   r   	prod_namer   real_backend
real_propsreal_configupdated_configconfig_pathfd
props_pathupdated_configurationupdated_propertiesexs                  r+   ri   FakeBackendV2.refresh  s*   , '#788\ 
 %%--fe<	*	^''	']H#A;L%000>J8"224JJ9^bJc%9K
 )002N-1->->N>* ggll4<<9K9KL+sW=HHTZZ(;(;(=>RS > WW\\$,,8K8KL
*cG<HHTZZ
(:(:(<.QR = #;;=DO**,$8$B$B4??$S!!2!<!<T=M=M!N,3-%))-	DL KKW!!+ >=
 =<*  	^NN@$BSBSUXY[U\]]	^sJ   B.I* ,7I#AI* 17I(BI* 
II* 
I'#I* *
J(4*J##J()r"   r#   r'   r(   )r   N)F)TTTr   Nns)+r1   
__module____qualname____firstlineno____doc__rY   rC   rI   r   r%   r   r   r4   r@   dictr!   rK   boolrQ   rD   r   rg   r   rk   r   r2   r   r   propertyr   r=   r   classmethodr   r   floatr   r   r>   r   ri   __static_attributes____classcell__)r*   s   @r+   r   r   7   s]   
 GMNL  (($ *-D -3 4 3 3$=$ =3D =?7 ? N  t  D   2   5 5 5& U  F QfG^3 G^4 G^\` G^ G^r-   r   ))r   typingr   loggingr   rZ   rV   qiskitr   qiskit.providersr   qiskit.utilsr   r:   qiskit.transpilerr   r   qiskit.providers.basic_providerr	   *qiskit_ibm_runtime.utils.backend_converterr
   (qiskit_ibm_runtime.utils.backend_decoderr   r   r`   r   utils.backend_encoderr   utils.backend_decoderr   modelsr   r   r   r   models.exceptionsr   	getLoggerr1   r   r   r   r-   r+   <module>r
     sq        	 ! & 0 $ $ : H
 $ 2 B  
		8	$Z^I Z^r-   