
    z	iu                        S r SSKJ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JrJr  \R$                  " \5      r\r " S	 S
\5      r " S S\\5      r " S S\\5      rg)z:Baseclasses for the Qiskit passmanager optimization tasks.    )annotationsN)abstractmethodABC)IterableCallable	Generator)Any   )RunStatePassManagerStatePropertySetc                  @    \ rS rSrSr\ S       SS jj5       rSrg)Task   zAn interface of the pass manager task.

The task takes a Qiskit IR, and outputs new Qiskit IR after some operation on it.
A task can rely on the :class:`.PropertySet` to communicate intermediate data among tasks.
Nc                    g)aD  Execute optimization task for input Qiskit IR.

Args:
    passmanager_ir: Qiskit IR to optimize.
    state: State associated with workflow execution by the pass manager itself.
    callback: A callback function which is caller per execution of optimization task.

Returns:
    Optimized Qiskit IR and state of the workflow.
N )selfpassmanager_irstatecallbacks       W/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/passmanager/base_tasks.pyexecuteTask.execute%   s    " 	    r   Nr   PassManagerIRr   r   r   r   returnz&tuple[PassManagerIR, PassManagerState])__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__r   r   r   r   r      sF     
 "	%   	
 
0 r   r   c                  x    \ rS rSrSrS rS
S jr S       SS jjr      SS jr\	    SS j5       r
S	rg)GenericPass9   zBase class of a single pass manager task.

A pass instance can read and write to the provided :class:`.PropertySet`,
and may modify the input pass manager IR.
c                0    [        5       U l        / U l        g r   )r   property_setrequiresr   s    r   __init__GenericPass.__init__@   s    'M(*r   c                .    U R                   R                  $ )zName of the pass.)	__class__r   r+   s    r   nameGenericPass.nameD   s    ~~&&&r   Nc           
        UR                   U l         U R                  (       a)  SSKJn  U" U R                  5      R	                  UUUS9u  pS nS n[
        R
                  " 5       n XR                  R                  ;  a"  U R                  U5      n[        R                  nO[        R                  n U=(       d    UnU[        R                  :w  ak  [
        R
                  " 5       U-
  n[        R                  SU R!                  5       US-  5        Ub(  U" U UUR                   UUR                  R"                  S9  X`R%                  X%5      4$ ! [         a    [        R                  ne f = f! U=(       d    UnU[        R                  :w  am  [
        R
                  " 5       U-
  n[        R                  SU R!                  5       US-  5        Ub)  U" U UUR                   UUR                  R"                  S9  f f f = f)Nr
   )FlowControllerLinearr   r   r   zPass: %s - %.5f (ms)i  )taskr   r)   running_timecount)r)   r*   flow_controllersr3   r   timeworkflow_statuscompleted_passesrunr   SUCCESSSKIP	ExceptionFAILloggerinfor0   r7   update_status)	r   r   r   r   r3   	run_stateret
start_timer6   s	            r   r   GenericPass.executeH   s    "..==>$8$G$O$O-! %P %!N 	YY[
	00AAAhh~.$,,	$MM	
 'CHMM)#yy{Z72DIIKPTATU'!'*%*%7%7%1#3399 &&u888!  	 I	 'CHMM)#yy{Z72DIIKPTATU'!'*%*%7%7%1#3399 ( *s+   &:E  E- !E 1E- E**E- -BG;c                    X!R                   l        U[        R                  :X  aD  UR                   =R                  S-  sl        UR                   R
                  R                  U 5        U$ )zUpdate workflow status.

Args:
    state: Pass manager state to update.
    run_state: Completion status of current task.

Returns:
    Updated pass manager state.
r
   )r:   previous_runr   r=   r7   r;   add)r   r   rD   s      r   rC   GenericPass.update_statusx   sT     .7*(((!!''1,'!!2266t<r   c                    g)zlRun optimization task.

Args:
    passmanager_ir: Qiskit IR to optimize.

Returns:
    Optimized Qiskit IR.
Nr   )r   r   s     r   r<   GenericPass.run   s     	r   )r)   r*   )r   strr   r   )r   r   rD   r   r   r   )r   r   r   r   )r   r    r!   r"   r#   r,   r0   r   rC   r   r<   r$   r   r   r   r&   r&   9   s    +' "	.9%.9  .9 	.9
 
0.9`  
	( % 
 r   r&   c                  d    \ rS rSrSr S S	S jjr\    S
S j5       r S       SS jjrSr	g)BaseController   a%  Base class of controller.

A controller is built with a collection of pass manager tasks,
and a subclass provides a custom logic to choose next task to run.
Note a controller can be nested into another controller,
and a controller itself doesn't provide any subroutine to modify the input IR.
Nc                $    U=(       d    0 U l         g)zQCreate new flow controller.

Args:
    options: Option for this flow controller.
N_options)r   optionss     r   r,   BaseController.__init__   s      2r   c                    g)a  A custom logic to choose a next task to run.

Controller subclass can consume the state to build a proper task pipeline.  The updated
state after a task execution will be fed back in as the "return" value of any ``yield``
statements.  This indicates the order of task execution is only determined at running time.
This method is not allowed to mutate the given state object.

Args:
    state: The state of the passmanager workflow at the beginning of this flow controller's
        execution.

Receives:
    state: the state of pass manager after the execution of the last task that was yielded.
        The generator does not need to inspect this if it is irrelevant to its logic, nor
        update it.

Yields:
    Task: Next task to run.
Nr   )r   r   s     r   
iter_tasksBaseController.iter_tasks   s    0 	r   c                    U R                  U5      n UR                  S 5      n UR                  UUUS9u  p UR                  U5      nM'  ! [         a    X4s $ f = f! [         a     X4$ f = f)Nr4   )rX   sendStopIterationr   )r   r   r   r   task_generator	next_tasks         r   r   BaseController.execute   s     /	)&++D1I $-$5$5-! %6 %!N
 +//6	   	)!((	) ! $$s"   A A AA
A.-A.rS   r   )rU   zdict[str, Any] | None)r   r   r   z'Generator[Task, PassManagerState, None]r   )
r   r    r!   r"   r#   r,   r   rX   r   r$   r   r   r   rP   rP      st     *.	&&	&  
1 : "	%%%  % 	%
 
0% %r   rP   )r#   
__future__r   loggingr9   abcr   r   collections.abcr   r   r   typingr	   compilation_statusr   r   r   	getLoggerr   rA   r   r   r&   rP   r   r   r   <module>rg      sj    A "   # 9 9  G G			8	$ 3 6`$ `FJ%T3 J%r   