
    z	i                     l    S 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
   " S S\5      r   SS	 jrS
 rg)zaProvides a general transpiler pass for collecting and consolidating blocks of nodes
in a circuit.    )TransformationPass)dag_to_dagdependencydagdependency_to_dag)BlockCollectorBlockCollapser)control_flowc                   V   ^  \ rS rSrSr SU 4S jjr\R                  S 5       rSr	U =r
$ )CollectAndCollapse   a  A general transpiler pass to collect and to consolidate blocks of nodes
in a circuit.

This transpiler pass depends on two functions: the collection function and the
collapsing function. The collection function ``collect_function`` takes a DAG
and returns a list of blocks. The collapsing function ``collapse_function``
takes a DAG and a list of blocks, consolidates each block, and returns the modified
DAG.

The input and the output DAGs are of type :class:`~qiskit.dagcircuit.DAGCircuit`,
however when exploiting commutativity analysis to collect blocks, the
:class:`~qiskit.dagcircuit.DAGDependency` representation is used internally.
To support this, the ``collect_function`` and ``collapse_function`` should work
with both types of DAGs and DAG nodes.

Other collection and consolidation transpiler passes, for instance
:class:`~.CollectLinearFunctions`, may derive from this pass, fixing
``collect_function`` and ``collapse_function`` to specific functions.
c                 F   > Xl         X l        X0l        [        TU ]  5         g)ah  
Args:
    collect_function (callable): a function that takes a DAG and returns a list
        of "collected" blocks of nodes
    collapse_function (callable): a function that takes a DAG and a list of
        "collected" blocks, and consolidates each block.
    do_commutative_analysis (bool): if True, exploits commutativity relations
        between nodes.
N)collect_functioncollapse_functiondo_commutative_analysissuper__init__)selfr   r   r   	__class__s       t/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/optimization/collect_and_collapse.pyr   CollectAndCollapse.__init__,   s"     !1!2'>$    c                     U R                   (       a  [        U5      nU R                  U5      nU R                  X5        U R                   (       a  [	        U5      nU$ )zRun the CollectLinearFunctions pass on `dag`.
Args:
    dag (DAGCircuit): the DAG to be optimized.
Returns:
    DAGCircuit: the optimized DAG.
)r   r   r   r   r   )r   dagblockss      r   runCollectAndCollapse.runA   sS     ''&s+C &&s+ 	s+ ''&s+C
r   )r   r   r   )F)__name__
__module____qualname____firstlineno____doc__r   r   trivial_recurser   __static_attributes____classcell__)r   s   @r   r
   r
      s,    0 !&	* !! "r   r
   Nc           	      <    [        U 5      R                  UUUUUUS9$ )zCorresponds to an important block collection strategy that greedily collects
maximal blocks of nodes matching a given ``filter_function``.
)	filter_fnsplit_blocksmin_block_sizesplit_layerscollect_from_backmax_block_width)r   collect_all_matching_blocks)r   filter_functionr&   r'   r(   r)   r*   s          r   collect_using_filter_functionr-   [   s3     #::!!%!+' ;  r   c                 6    [        U 5      R                  X5      $ )zCorresponds to an important block collapsing strategy that collapses every block
to a specific object as specified by ``collapse_function``.
)r   collapse_to_operation)r   r   r   s      r   r/   r/   q   s     #44VOOr   )FFN)r    qiskit.transpiler.basepassesr   qiskit.convertersr   r    qiskit.dagcircuit.collect_blocksr   r   qiskit.transpiler.passes.utilsr   r
   r-   r/    r   r   <module>r5      s>    < H K 7A+ AR ,Pr   