
    z	i$                         S r SSKJ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	\5      r\ " S
 S5      5       rg)zMCheck if the DAG has reached a relative semi-stable point over previous runs.    )deepcopy)	dataclassN)Tuple)
DAGCircuit)TransformationPassc                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )MinimumPoint   a  Check if the DAG has reached a relative semi-stable point over previous runs

This pass is similar to the :class:`~.FixedPoint` transpiler pass and is intended
primarily to be used to set a loop break condition in the property set.
However, unlike the :class:`~.FixedPoint` class which only sets the
condition if 2 consecutive runs have the same value property set value
this pass is designed to find a local minimum and use that instead. This
pass is designed for an optimization loop where a fixed point may never
get reached (for example if synthesis is used and there are multiple
equivalent outputs for some cases).

This pass will track the state of fields in the property set over its past
executions and set a boolean field when either a fixed point is reached
over the backtracking depth or selecting the minimum value found if the
backtracking depth is reached. To do this it stores a deep copy of the
current minimum DAG in the property set and when ``backtrack_depth`` number
of executions is reached since the last minimum the output dag is set to
that copy of the earlier minimum.

Fields used by this pass in the property set are (all relative to the ``prefix``
argument):

* ``{prefix}_minimum_point_state`` - Used to track the state of the minimum point search
* ``{prefix}_minimum_point`` - This value gets set to ``True`` when either a fixed point
    is reached over the ``backtrack_depth`` executions, or ``backtrack_depth`` was exceeded
    and an earlier minimum is restored.
c                 b   > [         TU ]  5         Xl        U S3U l        U S3U l        X0l        g)a  Initialize an instance of this pass

Args:
    property_set_list (list): A list of property set keys that will
        be used to evaluate the local minimum. The values of these
        property set keys will be used as a tuple for comparison
    prefix (str): The prefix to use for the property set key that is used
        for tracking previous evaluations
    backtrack_depth (int): The maximum number of entries to store. If
        this number is reached and the next iteration doesn't have
        a decrease in the number of values the minimum of the previous
        n will be set as the output dag and ``minimum_point`` will be set to
        ``True`` in the property set
_minimum_point_state_minimum_pointN)super__init__property_set_listbacktrack_nameminimum_reachedbacktrack_depth)selfr   prefixr   	__class__s       f/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/utils/minimum_point.pyr   MinimumPoint.__init__5   s;     	!2!'(<="(8.    c                   ^  [        U 4S jT R                   5       5      nT R                  T R                     nUcI  [	        S[
        R                  4[        T R                  5      -  SS9T R                  T R                  '   U$ X#R                  :  an  U=R                  S-  sl	        UR                  T R                  :X  a<  ST R                  T R                  '   T R                  T R                     R                  $  U$ X#R                  :  a  SUl	        X#l        [        U5      Ul        U$ X#R                  :X  a  ST R                  T R                  '   U$ U$ )z#Run the MinimumPoint pass on `dag`.c              3   B   >#    U  H  nTR                   U   v   M     g 7f)N)property_set).0xr   s     r   	<genexpr>#MinimumPoint.run.<locals>.<genexpr>M   s     K4Jqd''*4Js   Nr   dagscoresince   T)tupler   r   r   _MinimumPointStatemathinflenr#   r$   r   r   r"   r   )r   r"   r#   states   `   r   runMinimumPoint.runK   s7   KD4J4JKK!!$"5"56
 =5Gc$2H2H.I IQR6Dd1122 
) [[ KK1K{{d222:>!!$"6"67(()<)<=AAA 3$ 
 [[ EKK EI 
	 kk!6:Dd223J
r   )r   r   r   r   )   )	__name__
__module____qualname____firstlineno____doc__r   r,   __static_attributes____classcell__)r   s   @r   r	   r	      s    8/," "r   r	   c                   B    \ rS rSr% Sr\\S'   \\S4   \S'   \	\S'   Sr
g)	r'   p   r!   r"   .r#   r$    N)r/   r0   r1   r2   	__slots__r   __annotations__r   floatintr4   r8   r   r   r'   r'   p   s     )I	OJr   r'   )r3   copyr   dataclassesr   r(   typingr   qiskit.dagcircuit.dagcircuitr   qiskit.transpiler.basepassesr   r	   r'   r8   r   r   <module>rB      sF    T  !   3 ;U% Up   r   