
    iH&                     N   % S r SSKJrJrJrJrJrJrJrJ	r	J
r
  SSKJ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Jr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&  \RN                  " \(5      r)\	S   r*SSSSSS.r+\\,\*4   \-S'    " S S\\\*4   \&5      r.g)zQiskit runtime job.    )	AnyOptionalDictTypeUnionSequenceListLiteralTuple)futuresN)Backend)PrimitiveResult)BasePrimitiveJob)qiskit_runtime_service   )EstimatorResultDecoder)RuntimeJobFailureErrorRuntimeInvalidStateErrorIBMRuntimeErrorRuntimeJobMaxTimeoutErrorRuntimeJobTimeoutError)ResultDecoder)RuntimeClient)RequestsApiError)BaseRuntimeJob)INITIALIZINGQUEUEDRUNNING	CANCELLEDDONEERRORr   r   r    r!   r   )r   r   	COMPLETEDFAILEDr   API_TO_JOB_STATUSc                      \ rS rSr% SrSr\\S4   \S'   Sr	       S'S\
S	\S
\S\SSS\\   S\\\\   \\\      4      S\\   S\\   S\\   S\\   S\\   SS4S jjr  S(S\\   S\\\      S\4S jjrS)S jrS\4S jrS\S\\\4   4S jrS\4S jrS\4S jrS\4S  jrS\4S! jr S\4S" jr!S\4S# jr" S*S\\   SS4S$ jjr#S*S\\   S\\
   4S% jjr$S&r%g)+RuntimeJobV23   z3Representation of a runtime V2 primitive execution.)r    r   r!   .JOB_FINAL_STATESr!   Nbackend
api_clientjob_id
program_idservicez+qiskit_runtime_service.QiskitRuntimeServicecreation_dateresult_decoderimage
session_idtagsversionprivatereturnc                 z    [         R                  " XS9  [        R                  " U UUUUUUUUU	U
UUS9  SU l        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.
r+   )r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r   N)r   __init__r   _status)selfr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   s                [/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/runtime_job_v2.pyr8   RuntimeJobV2.__init__9   sP    > 	!!$6!!')!	
 #1    timeoutdecoderc                     U=(       d    U R                   nU R                  US9  U R                  S:X  aR  U R                  (       a  U R                  OU R                  nU R
                  S:X  a  [        U5      e[        SU 35      eU R                  S:X  a(  [        SR                  U R                  5       5      5      eU R                  R                  U R                  5       S9nU(       a  UR                  U5      $ S$ )	a  Return the results of the job.

Args:
    timeout: Number of seconds to wait for job.
    decoder: A :class:`ResultDecoder` subclass used to decode job results.

Returns:
    Runtime job result.

Raises:
    RuntimeJobFailureError: If the job failed.
    RuntimeJobMaxTimeoutError: If the job does not complete within given timeout.
    RuntimeInvalidStateError: If the job was cancelled, and attempting to retrieve result.
r>   r!     zUnable to retrieve job result. r   z8Unable to retrieve result for job {}. Job was cancelled.r7   N)_final_result_decoderwait_for_final_stater9   _reason_error_message_reason_coder   r   r   formatr+   _api_clientjob_resultsdecode)r:   r>   r?   _decodererror_message
result_raws         r;   resultRuntimeJobV2.resultj   s    & 8d88!!'!2<<7",0LLDLLd>Q>QM  D(/>>(+J=/)Z[[<<;&*MTTUYU`U`Ubc  %%111G
.8xz*BdBr=   c                      U R                   R                  U R                  5       5        SU l        g! [         a3  nUR                  S:X  a  [        SU 35      Se[        SU 35      SeSnAff = f)zCancel the job.

Raises:
    RuntimeInvalidStateError: If the job is in a state that cannot be cancelled.
    IBMRuntimeError: If unable to cancel job.
i  zJob cannot be cancelled: NzFailed to cancel job: r   )rI   
job_cancelr+   r   status_coder   r   r9   )r:   exs     r;   cancelRuntimeJobV2.cancel   sv    	K''6
 #	   	K~~$.1J2$/OPVZZ!$:2$"?@dJ	Ks   )3 
A0.A++A0c                 :    U R                  5         U R                  $ )z@Return the status of the job.

Returns:
    Status of this job.
)_set_status_and_error_messager9   r:   s    r;   statusRuntimeJobV2.status   s     	**,||r=   responsec                     US   S   R                  5       nU[        ;   a#  [        U   nUS:X  a  U R                  S:X  a  SnU$ U$ )zReturns the job status from an API response.

Args:
    response: Job response from the runtime API.

Returns:
    Job status.
staterZ   r   rB   r!   )upperr$   rG   )r:   r\   
api_statusmapped_job_statuss       r;   _status_from_job_response&RuntimeJobV2._status_from_job_response   sW     g&x0668
** 1* = K/D4E4E4M$+!$$r=   c                 (    U R                  5       S:H  $ )z*Return whether the job has been cancelled.r   rZ   rY   s    r;   	cancelledRuntimeJobV2.cancelled   s    {{}++r=   c                 (    U R                  5       S:H  $ )z,Return whether the job has successfully run.r    re   rY   s    r;   doneRuntimeJobV2.done   s    {{}&&r=   c                 (    U R                  5       S:H  $ )z"Return whether the job has failed.r!   re   rY   s    r;   erroredRuntimeJobV2.errored   s    {{}''r=   c                 <    U R                  5       U R                  ;   $ )zMReturn whether the job is in a final job state such as ``DONE`` or ``ERROR``.)rZ   r(   rY   s    r;   in_final_stateRuntimeJobV2.in_final_state   s    {{} 5 555r=   c                 (    U R                  5       S:H  $ )z+Return whether the job is actively running.r   re   rY   s    r;   runningRuntimeJobV2.running   s    {{}	))r=   c                 (   U R                  5       U R                  ;  a  [        R                  S5         U R                  R                  U R                  5       5      $ ! [         a)  nUR                  S:X  a   SnAg[        SU 35      SeSnAff = f)zReturn job logs.

Note:
    Job logs are only available after the job finishes.

Returns:
    Job logs, including standard output and error.

Raises:
    IBMRuntimeError: If a network error occurred.
z3Job logs are only available after the job finishes.i  N zFailed to get job logs: )
rZ   r(   loggerwarningrI   job_logsr+   r   rS   r   )r:   errs     r;   logsRuntimeJobV2.logs   s~     ;;= 5 55NNPQ	N##,,T[[];; 	N#%!$<SE"BCM	Ns   (A 
B(B=BBc                     [         R                   " 5       nU R                  5       nX0R                  ;  ag  [         R                   " 5       U-
  nUb  XA:  a  [        SU S35      e[         R                  " S5        U R                  5       nX0R                  ;  a  Mf  gg! [
        R                   a    [        SU S35      ef = f)zPoll for the job status from the API until the status is in a final state.

Args:
    timeout: Seconds to wait for the job. If ``None``, wait indefinitely.

Raises:
    RuntimeJobTimeoutError: If the job does not complete within given timeout.
Nz,Timed out waiting for job to complete after z secs.g?)timerZ   r(   r   sleepr   TimeoutError)r:   r>   
start_timerZ   elapsed_times        r;   rD   !RuntimeJobV2.wait_for_final_state   s    	J[[]F 5 55#yy{Z7&<+B0FwivV  

3  5 55 ## 	(>wivN 	s   BB $Cc                 z   U R                   (       d~  U R                  US9   U R                  R                  U R	                  5       5      nUR                  S5      (       a#  U R                  R                  US   5      U l         U R                   $ U R                   $ ! [         a  n[        SU 35      SeSnAff = f)zReturn the backend where this job was executed. Retrieve data again if backend is None.

Raises:
    IBMRuntimeError: If a network error occurred.
rA   r)   zFailed to get job backend: N)
_backendrD   rI   job_getr+   get_servicer)   r   r   )r:   r>   raw_datary   s       r;   r)   RuntimeJobV2.backend   s     }}%%g%6U++33DKKMB<<	**$(MM$9$9(9:M$NDM }}t}} $ U%(CC5&IJPTTUs   A"B 
B:&B55B:)r   r9   )NNru   NNNF)NN)r5   N)N)&__name__
__module____qualname____firstlineno____doc__r(   r   	JobStatus__annotations__r!   r   r   strr   r   r   r   r   r	   intboolr8   floatr   rO   rU   rZ   r   rb   rf   ri   rl   ro   rr   rz   rD   r)   __static_attributes__ r=   r;   r&   r&   3   s   =.LeIsN+LE (,^b!$(#!%"'/1/1 "/1 	/1
 /1 ?/1  }/1 !tM':HT-EX<Y'Y!Z[/1 }/1 SM/1 tn/1 #/1 $/1 
/1f $(15!C%!C $}-.!C 
	!CF#	 $ 5C;P ",4 ,'d '( (6 6* *Nc N. $(% 
8x (7:K  r=   r&   )/r   typingr   r   r   r   r   r   r	   r
   r   
concurrentr   loggingr}   qiskit.providers.backendr   qiskit.primitives.containersr   )qiskit.primitives.base.base_primitive_jobr   qiskit_ibm_runtimer   utils.estimator_result_decoderr   
exceptionsr   r   r   r   r   utils.result_decoderr   api.clientsr   api.exceptionsr   base_runtime_jobr   	getLoggerr   rv   r   r$   r   r   r&   r   r=   r;   <module>r      s     S S S    , 8 F 6 B  0 & , ,			8	$UV	+ 4Y' V#OY$>? Vr=   