
    in3                     B   S r SSKJrJr  SSKJrJrJrJrJ	r	J
r
JrJr  SSKrSSKJ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J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.  \R^                  " \05      r1 " S S\5      r2g)zBase runtime job class.    )ABCabstractmethod)AnyOptionalDictTypeUnionSequenceListTupleN)futures)datetime)Backend)	JobStatus)qiskit_runtime_service   )utc_to_localvalidate_job_tags)DEFAULT_DECODERSAPI_TO_JOB_ERROR_MESSAGE)IBMErrorIBMApiErrorIBMRuntimeError)ResultDecoder)issue_deprecation_msg)BackendProperties)RuntimeClient)RequestsApiError)ClientParametersc                   :   \ rS rSr% Sr\R                  " SS9rSr\	\
S4   \S'   Sr\\\4   \S	'          S7S
\S\S\S\SSS\\   S\\\\   \\\      4      S\\   S\\   S\\   S\\   S\\   SS4S jjr\S\4S j5       rS\4S jrS\4S jrS\\\
4   4S jr S\\   S\\   4S jr!S8S\S\\"   4S  jjr#S\\   4S! jr$S9S" jr%S#\SS4S$ jr&S#\SS4S% jr'\(S&\S\4S' j5       r)S&\S\4S( jr*\+S)\,RZ                  SS4S* j5       r.S\4S+ jr/\S\4S, j5       r0\S\4S- j5       r1\S\4S. j5       r2\S\\3   4S/ j5       r4\S\4S0 j5       r5\S\4S1 j5       r6\S\\\
4   4S2 j5       r7\S\\   4S3 j5       r8\(S\4S4 j5       r9\(S\4S5 j5       r:S6r;g):BaseRuntimeJob.   zBase Runtime Job class.runtime_job)thread_name_prefix .JOB_FINAL_STATESNERRORbackend
api_clientjob_id
program_idservicez+qiskit_runtime_service.QiskitRuntimeServicecreation_dateresult_decoderimage
session_idtagsversionprivatereturnc                    Xl         X0l        X l        X`l        X@l        SU l        SU l        SU l        Xl        XPl	        Xl
        Xl        0 U l        Xl        SU l        SU l        Xl        U=(       d$    ["        R$                  " US5      =(       d    [&        n[)        U[*        5      (       a
  Uu  ol        gXl        g)aR  RuntimeJob constructor.

Args:
    backend: The backend instance used to run this job.
    api_client: Object for connecting to the server.
    job_id: Job ID.
    program_id: ID of the program this job is for.
    creation_date: Job creation date, in UTC.
    result_decoder: A :class:`ResultDecoder` subclass used to decode job results.
    image: Runtime image used for this job: image_name:tag.
    service: Runtime service.
    session_id: Job ID of the first job in a runtime session.
    tags: Tags assigned to the job.
    version: Primitive version.
    private: Marks job as private.
N)_backend_job_id_api_client_creation_date_program_id_reason_reason_code_error_message_image_service_session_id_tags_usage_estimation_version_queue_info_status_privater   getr   
isinstancer
   _final_result_decoder)selfr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   decoder_s                  ]/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/base_runtime_job.py__init__BaseRuntimeJob.__init__6   s    >  %+%&*+/-1%
1359 [$4$8$8T$J[mgx((,3)A))0&    c                     U R                   $ )z?Returns a boolean indicating whether or not the job is private.)rF   rJ   s    rM   r3   BaseRuntimeJob.privatem   s     }}rP   c                     U R                   $ )z'Return a unique id identifying the job.)r7   rR   s    rM   r*   BaseRuntimeJob.job_idr   s    ||rP   c                      U R                   R                  U R                  5       5      nUR                  S0 5      R                  S5      $ ! [         a  n[        SU 35      SeSnAff = f)zReturn job usage in seconds.usagequantum_secondsFailed to get job metadata: N)r8   job_metadatar*   rG   r   r   )rJ   metricserrs      rM   rW   BaseRuntimeJob.usagev   sh    	R&&33DKKMBG;;w+//0ABB 	R!$@"FGTQ	Rs   A	A 
A*A%%A*c                      U R                   R                  U R                  5       5      $ ! [         a  n[	        SU 35      SeSnAff = f)a  Return job metrics.

Returns:
    A dictionary with job metrics including but not limited to the following:

    * ``timestamps``: Timestamps of when the job was created, started running, and finished.
    * ``usage``: Details regarding job usage, the measurement of the amount of
        time the QPU is locked for your workload.

Raises:
    IBMRuntimeError: If a network error occurred.
rY   N)r8   rZ   r*   r   r   )rJ   r\   s     rM   r[   BaseRuntimeJob.metrics~   sL    	R##00?? 	R!$@"FGTQ	Rs   (+ 
A	AA	new_tagsc                    [        U5      n[        U5        U R                  R                  U R	                  5       [        U5      S9nUR                  S:X  aL  U R                  R                  U R	                  5       5      nUR                  S/ 5      U l	        U R                  $ [        SR                  U R	                  5       5      5      e)a  Update the tags associated with this job.

Args:
    new_tags: New tags to assign to the job.

Returns:
    The new tags associated with this job.

Raises:
    IBMApiError: If an unexpected error occurred when communicating
        with the server or updating the job tags.
)r*   r1      r1   zfAn unexpected error occurred when updating the tags for job {}. The tags were not updated for the job.)setr   r8   update_tagsr*   liststatus_codejob_getpoprA   r   format)rJ   r`   tags_to_updateresponseapi_responses        rM   rd   BaseRuntimeJob.update_tags   s     X(###//t{{}4P^K_/`3&++33DKKMBL%))&"5DJ::!6$++-0 rP   refreshc                     U R                   nU R                  5       R                  S0 5      R                  S5      nU(       a  [        U5      nU R                  R                  X5      $ )a7  Return the backend properties for this job.

Args:
    refresh: If ``True``, re-query the server for the backend properties.
        Otherwise, return a cached version.

Returns:
    The backend properties used for this job, at the time the job started running,
    or ``None`` if properties are not available.

timestampsrunning)r-   r[   rG   r   r6   
properties)rJ   rn   job_datejob_running_dates       rM   rr   BaseRuntimeJob.properties   sV     %%<<>--lB?CCIN#$45H}}''::rP   c                 :    U R                  5         U R                  $ )zVReturns the reason if the job failed.

Returns:
    Error message string or ``None``.
)_set_status_and_error_messager=   rR   s    rM   error_messageBaseRuntimeJob.error_message   s     	**,"""rP   c                     U R                   U R                  ;  aJ  U R                  R                  U R	                  5       S9nU R                  U5        U R                  U5        gg)z'Fetch and set status and error message.r*   N)rE   r&   r8   rg   r*   _set_status_set_error_messagerJ   rk   s     rM   rw   ,BaseRuntimeJob._set_status_and_error_message   sT    <<t444''//t{{}/EHX&##H- 5rP   job_responsec                 ^    US   R                  S5      nUS   R                  S5      =(       d    US   R                  S5      nU(       a*  X l        U(       a  SU SU R                   3U l        X0l        U R                  U5      U l        g	! [
         a    [        SUS   S    35      ef = f)
zSet status.

Args:
    job_response: Job response from runtime API.

Raises:
    IBMError: If an unknown status is returned from the server.
statereason
reasonCodereason_codezError code z; zUnknown status: statusN)rG   r;   r<   _status_from_job_responserE   KeyErrorr   )rJ   r   r   r   s       rM   r|   BaseRuntimeJob._set_status   s    	Q!'*..x8F&w/33LA \RYEZE^E^FK %%0R~#NDL(3%99,GDL 	Q-l7.CH.M-NOPP	Qs   B
B B,c                 t    U R                   U R                  :X  a  U R                  U5      U l        gSU l        g)z]Set error message if the job failed.

Args:
    job_response: Job response from runtime API.
N)rE   r'   _error_msg_from_job_responser=   )rJ   r   s     rM   r}   !BaseRuntimeJob._set_error_message   s/     <<4::%"&"C"CL"QD"&DrP   rk   c                 .    US   S   R                  5       $ )z,Returns the job status from an API response.r   r   )upperr~   s     rM   r   (BaseRuntimeJob._status_from_job_response   s      *0022rP   c                    US   S   R                  5       nU R                  R                  U R                  5       S9nUR	                  S5      nUS:w  a  X4S nUS:X  a9  U R
                  S:X  a)  [        S	   nUR                  U R                  5       U5      $ [        S
   nUR                  U R                  5       U R                  =(       d    U5      $ )zReturns the error message from an API response.

Args:
    response: Job response from the runtime API.

Returns:
    Error message.
r   r   r{   	TracebackN	CANCELLEDi  zCANCELLED - RAN TOO LONGFAILED)	r   r8   job_resultsr*   rfindr<   r   ri   r;   )rJ   rk   r   job_result_rawindex	error_msgs         rM   r   +BaseRuntimeJob._error_msg_from_job_response   s     '"8,224))55T[[]5K$$[1B;+F3N[ T%6%6$%>01KLI##DKKM>BB0:I##DKKM4<<3Q>RRrP   result_queuec                 ^      U R                  5         M  ! [        R                   a     gf = f)zHEmpty the result queue.

Args:
    result_queue: Result queue to empty.
N)
get_nowaitqueueEmpty)r   s    rM   _empty_result_queue"BaseRuntimeJob._empty_result_queue  s1    	'') {{ 		s    ,,c                 j    SU R                   R                   SU R                   SU R                   S3$ )N<z('z', 'z')>)	__class____name__r7   r:   rR   s    rM   __repr__BaseRuntimeJob.__repr__  s4    4>>**+2dll^4@P@P?QQTUUrP   c                     U R                   $ )zReturn the runtime image used for the job.

Returns:
    Runtime image: image_name:tag or "" if the default
    image is used.
)r>   rR   s    rM   r/   BaseRuntimeJob.image  s     {{rP   c                 v    U R                   R                  U R                  5       SS9nUR                  S0 5      $ )zGJob input parameters.

Returns:
    Input parameters used in this job.
F)r*   exclude_paramsparams)r8   rg   r*   rG   r~   s     rM   inputsBaseRuntimeJob.inputs'  s7     ##++4;;=QV+W||Hb))rP   c                     U R                   $ )z8Primitive name.
Returns:
    Primitive this job is for.
)r:   rR   s    rM   primitive_idBaseRuntimeJob.primitive_id2  s     rP   c                     U R                   (       d>  U R                  R                  U R                  5       S9nUR	                  SS5      U l         U R                   (       d  g[        U R                   5      nU$ )zJob creation date in local time.

Returns:
    The job creation date as a datetime object, in local time, or
    ``None`` if creation date is not available.
r{   createdN)r9   r8   rg   r*   rG   r   )rJ   rk   creation_date_local_dts      rM   r-   BaseRuntimeJob.creation_date:  sb     ""''//t{{}/EH"*,,y$"?D""!-d.A.A!B%%rP   c                     U R                   (       d>  U R                  R                  U R                  5       S9nUR	                  SS5      U l         U R                   $ )zJSession ID.

Returns:
    Session ID. None if the backend is a simulator.
r{   r0   N)r@   r8   rg   r*   rG   r~   s     rM   r0   BaseRuntimeJob.session_idK  sK     ''//t{{}/EH'||L$?DrP   c                     U R                   $ )zQJob tags.

Returns:
    Tags assigned to the job that can be used for filtering.
)rA   rR   s    rM   r1   BaseRuntimeJob.tagsW  s     zzrP   c                     U R                   (       d@  U R                  R                  U R                  5       S9nSUR	                  SS5      0U l         U R                   $ )zReturn the usage estimation information for this job.

Returns:
    ``quantum_seconds`` which is the estimated system execution time
    of the job in seconds. Quantum time represents the time that
    the system is dedicated to processing your job.
r{   rX   estimated_running_time_secondsN)rB   r8   rg   r*   rh   r~   s     rM   usage_estimationBaseRuntimeJob.usage_estimation`  sV     %%''//t{{}/EH!8<<0PRV#W&D" %%%rP   c                 .    U R                   R                  $ )z"Return the IBM Cloud instance CRN.)r6   	_instancerR   s    rM   instanceBaseRuntimeJob.instanceq  s     }}&&&rP   c                     g)zMReturn whether the job is in a final job state such as ``DONE`` or ``ERROR``.Nr%   rR   s    rM   in_final_stateBaseRuntimeJob.in_final_statev       	rP   c                     g)z"Return whether the job has failed.Nr%   rR   s    rM   erroredBaseRuntimeJob.errored{  r   rP   )r8   r6   r9   r=   rI   r>   r7   rF   r:   rD   r;   r<   r?   r@   rE   rA   rB   rC   )NN NNNF)F)r4   N)<r   
__module____qualname____firstlineno____doc__r   ThreadPoolExecutor	_executorr&   r   r   __annotations__r'   r	   strRuntimeJobStatusr   r   r   r   r   r
   r   intboolrN   propertyr3   r*   floatrW   r   r[   rd   r   rr   rx   rw   r|   r}   r   r   r   staticmethodr   Queuer   r   r/   r   r   r   r-   r0   r1   r   r   r   r   __static_attributes__r%   rP   rM   r!   r!   .   sX   !**mLI(*eCHo**.E5&&'. (,^b!$(#!%"'5151 "51 	51
 51 ?51  }51 !tM':HT-EX<Y'Y!Z[51 }51 SM51 tn51 #51 $51 
51n    Ru RRc3h R$DI $s) :;$ ;8<M3N ;"#x} #.Q Q Q.	't 	' 	' 3$ 33 3 3ST Sc S0 
%++ 
$ 
 
V# V s   * * *  c     &x1 & &  	 C 	  	  d   &$sCx. & &  '(3- ' '      rP   r!   )3r   abcr   r   typingr   r   r   r   r	   r
   r   r   logging
concurrentr   r   r   qiskit.providers.backendr   qiskit.providers.jobstatusr   r   qiskit_ibm_runtimer   utilsr   r   	constantsr   r   
exceptionsr   r   r   utils.result_decoderr   utils.deprecationr   modelsr   api.clientsr   api.exceptionsr   api.client_parametersr   	getLoggerr   loggerr!   r%   rP   rM   <module>r      so     # J J J     , D 6 2 A 
 0 4 % & , 3			8	$PS PrP   