
    z	i                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  S
r " S S\5      rg)zHCancel the redundant (self-adjoint) gates through commutation relations.    )TransformationPass)StandardGateCommutations)U1Gate)	PhaseGate)RZGate)commutation_cancellation)CommutationChecker)trivial_recursegh㈵>c                   @   ^  \ rS rSrSrSU 4S jjr\S 5       rSrU =r	$ )CommutativeCancellation   zCancel the redundant (self-adjoint) gates through commutation relations.

Pass for cancelling self-inverse gates/rotations. The cancellation utilizes
the commutation relations in the circuit. Gates considered include::

    H, X, Y, Z, CX, CY, CZ
c                   > [         TU ]  5         U(       a  [        U5      U l        O[        5       U l        X l        Ub  [        UR
                  5      U l        [        [        [        S.U l	        1 SkU l
        SS1U l        1 SkU l        [        [        U R                  U R                  -  U R                  -  S9U l        g)a  
CommutativeCancellation initializer.

Args:
    basis_gates (list[str]): Basis gates to consider, e.g.
        ``['u3', 'cx']``. For the effects of this pass, the basis is
        the set intersection between the ``basis_gates`` parameter
        and the gates in the dag.
    target (Target): The :class:`~.Target` representing the target backend, if both
        ``basis_gates`` and ``target`` are specified then this argument will take
        precedence and ``basis_gates`` will be ignored.
N)rzpu1>   r   stzr   r   xrx>   hycxcycz)gates)super__init__setbasistargetoperation_namesr   r   r   
_var_z_map_z_rotations_x_rotations_gatesr	   r   _commutation_checker)selfbasis_gatesr!   	__class__s      x/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/optimization/commutative_cancellation.pyr    CommutativeCancellation.__init__%   s     	[)DJDJV334DJ!'ivF< $K2 %7$DKK$:K:K,KdN_N_,_%
!    c                 n    [         R                  " XR                  [        U R                  5      5        U$ )zRun the CommutativeCancellation pass on `dag`.

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

Returns:
    DAGCircuit: the optimized DAG.
)r   cancel_commutationsr'   sortedr    )r(   dags     r+   runCommutativeCancellation.runG   s-     	!44**F4::,>	
 
r-   )r'   r&   r#   r%   r$   r    r!   )NN)
__name__
__module____qualname____firstlineno____doc__r   r
   r2   __static_attributes____classcell__)r*   s   @r+   r   r      s"     
D  r-   r   N)r8   qiskit.transpiler.basepassesr   "qiskit.circuit.commutation_libraryr   (qiskit.circuit.library.standard_gates.u1r   'qiskit.circuit.library.standard_gates.pr   (qiskit.circuit.library.standard_gates.rzr   qiskit._accelerater   &qiskit._accelerate.commutation_checkerr	   +qiskit.transpiler.passes.utils.control_flowr
   _CUTOFF_PRECISIONr    r-   r+   <module>rE      s6    O ; G ; = ; 7 E G 80 8r-   