
    z	i|2                        S r SSKJr  SSKrSSKJrJr  SSKJrJ	r	  SSK
Jr  SSKJr  SSKrSSKJrJr  S	S
KJrJr  S	SKJr  S	SKJr  S	SKJrJrJr  \R:                  " \5      r " S S\5      r SS.       SS jjr!          SS jr"g)zFManager for a set of Passes and their scheduling during transpilation.    )annotationsN)ABCabstractmethod)CallableIterable)chain)Any)parallel_mapshould_run_in_parallel   )TaskPassManagerIR)PassManagerError)FlowControllerLinear)PropertySetWorkflowStatusPassManagerStatec                      \ rS rSrSr  S   SS jjr    SS jr      SS jrSS jrS r	S r
S	 rS
 r\    SS j5       r\      SS j5       r  SSS.         SS jjjrSS jrSS jrSrg)BasePassManager!   zPass manager base class.c                n    / U l         X l        [        5       U l        U(       a  U R	                  U5        gg)zInitialize an empty pass manager object.

Args:
    tasks: A pass set to be added to the pass manager schedule.
    max_iteration: The maximum number of iterations the schedule will be looped if the
        condition is not met.
N)_tasksmax_iterationr   property_setappend)selftasksr   s      X/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/passmanager/passmanager.py__init__BasePassManager.__init__$   s1     * (MKK     c                    [        U[        5      (       a  U/n[        S U 5       5      (       a  [        S5      eU R                  R                  U5        g)zAppend tasks to the schedule of passes.

Args:
    tasks: A set of pass manager tasks to be added to schedule.

Raises:
    TypeError: When any element of tasks is not a subclass of passmanager Task.
c              3  L   #    U  H  n[        U[        5      (       + v   M     g 7fN)
isinstancer   ).0ts     r   	<genexpr>)BasePassManager.append.<locals>.<genexpr>G   s     61:a&&&s   "$z6Added tasks are not all valid pass manager task types.N)r%   r   any	TypeErrorr   r   )r   r   s     r   r   BasePassManager.append9   sG     eT""GE6666TUU5!r!   c                f     X R                   U'   g! [         a  n[        SU S35      UeSnAff = f)aF  Replace a particular pass in the scheduler.

Args:
    index: Task index to replace, based on the position in :meth:`tasks`
    tasks: A set of pass manager tasks to be added to schedule.

Raises:
    TypeError: When any element of tasks is not a subclass of passmanager Task.
    PassManagerError: If the index is not found.
Index to replace  does not existsNr   
IndexErrorr   )r   indexr   exs       r   replaceBasePassManager.replaceL   s?    	X!&KK 	X"%6ug=M#NOUWW	Xs    
0+0c                d     U R                   U	 g! [         a  n[        SU S35      UeSnAff = f)zRemoves a particular pass in the scheduler.

Args:
    index: Pass index to remove, based on the position in :meth:`passes`.

Raises:
    PassManagerError: If the index is not found.
r.   r/   Nr0   )r   r2   r3   s      r   removeBasePassManager.remove`   s?    	XE" 	X"%6ug=M#NOUWW	Xs    
/*/c                &    U R                  X5        g r$   )r4   )r   r2   items      r   __setitem__BasePassManager.__setitem__n   s    U!r!   c                ,    [        U R                  5      $ r$   )lenr   )r   s    r   __len__BasePassManager.__len__q   s    4;;r!   c                `    U R                  U R                  S9nU R                  U   Ul        U$ )Nr   )	__class__r   r   )r   r2   new_passmanagers      r   __getitem__BasePassManager.__getitem__t   s/    ..t7I7I.J!%U!3r!   c                d   U R                  U R                  S9nU R                  Ul        [        XR                   5      (       a!  U=R                  UR                  -  sl        U$  UR	                  U5        U$ ! [
         a+  n[        SU R                    SUR                    35      UeS nAff = f)NrB   zunsupported operand type + for z and )rC   r   r   r%   r   r   r+   )r   otherrD   r3   s       r   __add__BasePassManager.__add__y   s    ..t7I7I.J!%e^^,,""ell2"""&&u-&&# 5dnn5EU5??J[\s   'A: :
B/&B**B/c                    g)zqConvert input program into pass manager IR.

Args:
    in_program: Input program.

Returns:
    Pass manager IR.
N )r   input_programkwargss      r   _passmanager_frontend%BasePassManager._passmanager_frontend   s     	r!   c                    g)a*  Convert pass manager IR into output program.

Args:
    passmanager_ir: Pass manager IR after optimization.
    in_program: The input program, this can be used if you need
        any metadata about the original input for the output.
        It should not be mutated.

Returns:
    Output program.
NrL   )r   passmanager_ir
in_programrN   s       r   _passmanager_backend$BasePassManager._passmanager_backend   s    $ 	r!   N)r   c                  U R                   (       d  U(       d  Uc  U$ Sn[        U[        5      (       d  U/nSn[        U5      S:X  d  [	        U5      (       d=  U Vs/ s H  n[        SUU UUS.UD6PM     nn[        U5      S:X  a  U(       d  US   $ U$ A[        [        U[        R                  " U 5      [        R                  " U5      US.US9$ s  snf )	a'	  Run all the passes on the specified ``in_programs``.

Args:
    in_programs: Input programs to transform via all the registered passes.
        A single input object cannot be a Python builtin list object.
        A list object is considered as multiple input objects to optimize.
    callback: A callback function that will be called after each pass execution. The
        function will be called with 4 keyword arguments::

            task (GenericPass): the pass being run
            passmanager_ir (Any): depending on pass manager subclass
            property_set (PropertySet): the property set
            running_time (float): the time to execute the pass
            count (int): the index for the pass execution

        The exact arguments pass expose the internals of the pass
        manager and are subject to change as the pass manager internals
        change. If you intend to reuse a callback function over
        multiple releases be sure to check that the arguments being
        passed are the same.

        To use the callback feature you define a function that will
        take in kwargs dict and access the variables. For example::

            def callback_func(**kwargs):
                task = kwargs['task']
                passmanager_ir = kwargs['passmanager_ir']
                property_set = kwargs['property_set']
                running_time = kwargs['running_time']
                count = kwargs['count']
                ...
    num_processes: The maximum number of parallel processes to launch if parallel
        execution is enabled. This argument overrides ``num_processes`` in the user
        configuration file, and the ``QISKIT_NUM_PROCS`` environment variable. If set
        to ``None`` the system default or local user configuration will be used.
    property_set: If given, the initial value to use as the :class:`.PropertySet` for the
        pass manager pipeline.  This can be used to persist analysis from one run to
        another, in cases where you know the analysis is safe to share.  Beware that some
        analysis will be specific to the input circuit and the particular :class:`.Target`,
        so you should take a lot of care when using this argument.
    kwargs: Arbitrary arguments passed to the compiler frontend and backend.

Returns:
    The transformed program(s).
TFr   )programpass_managercallbackinitial_property_setr   )pass_manager_binrY   rZ   )valuestask_kwargsnum_processesrL   )
r   r%   listr>   r   _run_workflowr
   _run_workflow_in_new_processdilldumps)	r   in_programsrY   r^   r   rN   is_listrW   outs	            r   runBasePassManager.run   s    l {{6h.>+t,,&-KG {q (>}(M(M  +	  +G  #!%%)5	
   +  	 ;1$W1vJ
 ($(JJt$4 JJx0(4
 (	
 		
%	s   Cc                `    [        U R                  U R                  5      5      n[        U5      $ )zLinearize this manager into a single :class:`.FlowControllerLinear`,
so that it can be nested inside another pass manager.

Returns:
    A linearized pass manager.
)r_   _flatten_tasksr   r   )r   flatten_taskss     r   to_flow_controller"BasePassManager.to_flow_controller
  s(     T00=>#M22r!   c                j    [        U[        5      (       d  U/$ [        [        U R                  U5      6 $ )z;A helper method to recursively flatten a nested task chain.)r%   r   r   maprj   )r   elementss     r   rj   BasePassManager._flatten_tasks  s/    (H--:c$--x899r!   )r   r   r   )rL   i  )r   Task | list[Task]r   int)r   rr   returnNone)r2   rs   r   rr   rt   ru   )r2   rs   rt   ru   )rM   r	   rt   r   )rR   r   rS   r	   rt   r	   )NN)
rd   zAny | list[Any]rY   r   r^   rs   r   dict[str, object] | Nonert   r	   )rt   r   )rp   zIterable | Taskrt   r   )__name__
__module____qualname____firstlineno____doc__r   r   r4   r7   r;   r?   rE   rI   r   rO   rT   rg   rl   rj   __static_attributes__rL   r!   r   r   r   !   s)   " $&!  *" " 
"&XX !X 
	X(X" 
  
	  % 
 
 , "!	\
 26\
$\
 \
 	\
 /\
 
\
|3:r!   r   )rZ   c               R   UR                  5       n[        5       nUc
  [        5       O
[        U5      nXal        UR                  " SSU 0UD6nUR                  U[        XQR                  S9UR                  SS5      S9u  pxUR                  Ul        UR                  " SUU S.UD6n	U	$ )zRun single program optimization with a pass manager.

Args:
    program: Arbitrary program to optimize.
    pass_manager: Pass manager with scheduled passes.
    **kwargs: Keyword arguments for IR conversion.

Returns:
    Optimized program.
NrM   )workflow_statusr   rY   )rR   staterY   )rR   rS   rL   )	rl   r   r   r   rO   executer   getrT   )
rW   rX   rZ   rN   flow_controllerinitial_statusr   rR   final_stateout_programs
             r   r`   r`     s    " #557O#%N .5;G[;\  !-!77 
N #2"9"9%*9R9R
 J- #: #N !, 8 8L33 % K r!   c               j    [        U [        R                  " U5      U[        R                  " U5      S9$ )zRun single program optimization in new process.

Args:
    program: Arbitrary program to optimize.
    pass_manager_bin: Binary of the pass manager with scheduled passes.

Returns:
      Optimized program.
)rW   rX   rZ   rY   )r`   rb   loads)rW   r[   rZ   rY   s       r   ra   ra   O  s0      ZZ 011H%	 r!   )rW   r	   rX   r   rZ   rv   rt   r	   )
rW   r	   r[   bytesrZ   rv   rY   r   rt   r	   )#r{   
__future__r   loggingabcr   r   collections.abcr   r   	itertoolsr   typingr	   rb   qiskit.utils.parallelr
   r   
base_tasksr   r   
exceptionsr   flow_controllersr   compilation_statusr   r   r   	getLoggerrw   loggerr   r`   ra   rL   r!   r   <module>r      s    M "  # .    F + ( 2 M M			8	$w:c w:| 6:	11!1 3	1 	1h 3	
  	r!   