
    z	i'                     \    S r SSKJr  SSKJrJr  S\S\\   S\\   4S jr	 " S S	\5      r
g
)z?Remove final measurements and barriers at the end of a circuit.    )TransformationPass)
DAGCircuit	DAGOpNodedagfinal_op_namesreturnc           	      @   / nU R                    Vs/ s H*  n[        U R                  U R                  U   5      5      PM,     nn0 nU(       a  UR	                  5       n[        U[        5      (       d  M.  UR                  R                  S:X  aA  Xe;  a#  [        S U R                  U5       5       5      XV'   XV   S-
  S:  a  XV==   S-  ss'   M  UR                  U;   a1  UR                  U5        UR                  U R                  U5      5        U(       a  M  U$ s  snf )a  Find the final operations of a circuit of a given type.
Args:
    dag: the DAG circuit
    final_op_names: names of the operations to find at the end of the circuit.

Returns:
List of nodes corresponding the the relevant operations at the end of the circuit.
barrierc              3   &   #    U  H  nS v   M	     g7f)   N ).0_s     r/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/utils/remove_final_measurements.py	<genexpr>!calc_final_ops.<locals>.<genexpr>+   s     8aD`qD`s   r   r   )qubitsnextpredecessors
output_mappop
isinstancer   opnamesumquantum_successorsappendextendquantum_predecessors)r   r   	final_opsqubitto_visitbarrier_encounters_remainingnodes          r   calc_final_opsr%      s     IKN::V:%S%%cnnU&;<=:HV#% 
||~$	**77<<9$ 7588aCDZDZ[_D`8a5a,2+1A59 -2a7299&T"OOC44T:;' (* 1 Ws   1Dc                       \ rS rSrSrS rSrg)RemoveFinalMeasurements9   a  Remove final measurements and barriers at the end of a circuit.

This pass removes final barriers and final measurements, as well as all
unused classical registers and bits they are connected to.
Measurements and barriers are considered final if they are
followed by no other operations (aside from other measurements or barriers.)

Classical registers are removed iff they reference at least one bit
that has become unused by the circuit as a result of the operation, and all
of their other bits are also unused. Separately, classical bits are removed
iff they have become unused by the circuit as a result of the operation,
or they appear in a removed classical register, but do not appear
in a classical register that will remain.
c                 (   [        USS15      nU(       d  U$ [        5       nU H8  nUR                   H  nUR                  U5        M     UR	                  U5        M:     [        UR                  5       5      nX6-  nU(       d  U$ [        5       n[        5       nUR                  R                  5        HD  n	[        U	5      n
U
R                  U5      (       d  U
R                  U5      (       a  Xz-  nM@  X-  nMF     X7-  U-
  nUR                  " U6   U$ )zRun the RemoveFinalMeasurements pass on `dag`.

Args:
    dag (DAGCircuit): the DAG to be optimized.

Returns:
    DAGCircuit: the optimized DAG.
measurer
   )r%   setcargsaddremove_op_node
idle_wirescregsvalues
isdisjointissubsetremove_clbits)selfr   r    clbits_with_final_measuresr$   cargr/   idle_register_bitsbusy_register_bitscregclbitsbits_to_removes               r   runRemoveFinalMeasurements.runI   s	    #3I(>?	J &)U"D

*..t4 #t$  )*
"0")J !U UII$$&DYF$$%?@@V__U_E`E`"," #," ' 5IM__>*
    r   N)__name__
__module____qualname____firstlineno____doc__r=   __static_attributes__r   r?   r   r'   r'   9   s    0r?   r'   N)rD   qiskit.transpiler.basepassesr   qiskit.dagcircuitr   r   r+   strlistr%   r'   r   r?   r   <module>rJ      sC    F ; 3#
 #CH #i #L@0 @r?   