
    z	i7                     6    S r SSKJr  SSKJr   " S S\5      rg)z<Return a circuit with any adjacent barriers merged together.    )TransformationPass)Barrierc                   .    \ rS rSrSrS r\S 5       rSrg)MergeAdjacentBarriers   u3  Return a circuit with any adjacent barriers merged together.

Only barriers which can be merged without affecting the barrier structure
of the DAG will be merged.

Not all redundant barriers will necessarily be merged, only adjacent
barriers are merged.

For example, the circuit::

    qr = QuantumRegister(3, 'q')
    circuit = QuantumCircuit(qr)
    circuit.barrier(qr[0])
    circuit.barrier(qr[1])
    circuit.barrier(qr)

Will be transformed into a circuit corresponding to::

    circuit.barrier(qr[0])
    circuit.barrier(qr)

i.e,

.. code-block:: text

          ░  ░             ░  ░
    q_0: ─░──░─      q_0: ─░──░─
          ░  ░             ░  ░
    q_1: ─░──░─  =>  q_1: ────░─
          ░  ░                ░
    q_2: ────░─      q_2: ────░─
             ░

after one iteration of the pass. These two barriers were not merged by the
first pass as they are not adjacent in the initial circuit.

The pass then can be reapplied to merge the newly adjacent barriers.
c                 Z   [        UR                  5       VVs0 s H  u  p#X2_M	     nnnUR                  5        Vs/ s H  oUR                  S:X  d  M  UPM     nn[        R                  X5      nU(       d  U$ U H!  nX;   d  M
  Xx   u  pUR                  XUSS9  M#     U$ s  snnf s  snf )z,Run the MergeAdjacentBarriers pass on `dag`.barrierF)wire_pos_mapcycle_check)	enumeratequbitstopological_op_nodesnamer   _collect_potential_mergesreplace_block_with_op)selfdagindexqubitindicesndbarriersnode_to_barrier_qubitsr	   barrier_to_addnodess              p/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/utils/merge_adjacent_barriers.pyrunMergeAdjacentBarriers.run;   s    4=cjj4IJ4ILE5<4IJ "%!9!9!;T!;2ww)?SB!;T "7!P!PQT!_%JG0(>(G%))U *    
' K Us   B"B(B(c                    [        U5      S:  a  g0 nUS   nUnU/n[        UR                  5      nU R                  U5      nU R	                  U5      n[        [        U5      5      n	USS  GHx  n
U R                  UR                  U
R                  5      (       a  U R                  U
5       Vs1 s H  oU;  d  M
  UiM     nnU R	                  U
5       Vs1 s H  oU;  d  M
  UiM     nn[        U
R                  5      nUR                  U5      (       db  UR                  U5      (       aL  UR                  U5      (       a6  X|-  nX-  nXn-  n[        [        U5      5      n	U
nUR                  U5        GM  U	(       a  X4X$'   [        U
R                  5      nU R                  U
5      nU R	                  U
5      n[        [        U5      5      n	/ nU
nUR                  U5        GM{     U	(       a  X4X$'   U$ s  snf s  snf )zReturn the potential merges.

Returns a dict of DAGOpNode: (Barrier, [DAGOpNode]) objects, where the barrier needs to be
inserted where the corresponding index DAGOpNode appears in the main DAG, in replacement of
the listed DAGOpNodes.
   Nr      )
lensetqargs	ancestorsdescendantsr   	_has_edge_node_id
isdisjointappend)r   r   r   current_barrierend_of_barriercurrent_barrier_nodescurrent_qubitscurrent_ancestorscurrent_descendantsr   next_barrierr   next_ancestorsnext_descendantsnext_qubitss                  r   r   /MergeAdjacentBarriers._collect_potential_mergesR   s    x=1 "$ #1+(!0 1_223MM/:!ooo> ^!45$QRLL }}^44l6K6KLL "%|!<"!<2J_@_B!<  " "%!>$!>2LaBaB!> ! $ ","4"45 '11+>>)445EFF+66~FF ):(J%*=*P'%3%AN &-S-@%AN%1N)00@
 :H9`&6 !!3!34N #l ;"%//,"?$S%89N$&!)N!((8c )f 6D5\"2%%]"$s   /	G8<G8	G=$G= N)	__name__
__module____qualname____firstlineno____doc__r   staticmethodr   __static_attributes__r6       r   r   r      s#    %N. P& P&r>   r   N)r;   qiskit.transpiler.basepassesr   qiskit.circuit.barrierr   r   r6   r>   r   <module>rA      s     C ; *P&. P&r>   