
    z	i                    p    S r SSKJr  SSKJrJrJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr   " S S	5      rg
)zXCode from commutative_analysis pass that checks commutation relations between DAG nodes.    )annotations)ListUnionSetOptional)Sequence)	Operation)Qubit)CommutationCheckerc                      \ rS rSrSr  SSS.     SS jjjr  S     SS jjr  S                 SS jjrS rS	 r	          SS
 jr
Srg)r      a  Check commutations of two operations.

Two unitaries :math:`A` and :math:`B` on :math:`n` qubits commute if

.. math::

    \frac{2^n F_{\text{process}}(AB, BA) + 1}{2^n + 1} > 1 - \varepsilon,

where

.. math::

    F_{\text{process}}(U_1, U_2) = \left|\frac{\mathrm{Tr}(U_1 U_2^\dagger)}{2^n} \right|^2,

and we set :math:`\varepsilon` to :math:`10^{-12}` to account for round-off errors on
few-qubit systems. This metric is chosen for consistency with other closeness checks in
Qiskit.

When possible, commutation relations are queried from a lookup table. This is the case
for standard gates without parameters (such as :class:`.XGate` or :class:`.HGate`) or
gates with free parameters (such as :class:`.RXGate` with a :class:`.ParameterExpression` as
angle). Otherwise, a matrix-based check is performed, where two operations are said to
commute, if the average gate fidelity of performing the commutation is above a certain threshold
(see ``approximation_degree``). The result of this commutation is then added to the
cached lookup table.
N)gatesc               &    [        XU5      U l        g )N)RustCheckercc)selfstandard_gate_commutationscache_max_entriesr   s       \/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/commutation_checker.py__init__CommutationChecker.__init__4   s     8US    c                :    U R                   R                  XX45      $ )z!Checks if two DAGOpNodes commute.)r   commute_nodes)r   op1op2max_num_qubitsapproximation_degrees        r   r    CommutationChecker.commute_nodes=   s     ww$$S~TTr   c	           
         U R                   R                  U[        U5      [        U5      U[        U5      [        U5      UU5      $ )ay  
Checks if two Operations commute. The return value of `True` means that the operations
truly commute, and the return value of `False` means that either the operations do not
commute or that the commutation check was skipped (for example, when the operations
have conditions or have too many qubits).

Args:
    op1: first operation.
    qargs1: first operation's qubits.
    cargs1: first operation's clbits.
    op2: second operation.
    qargs2: second operation's qubits.
    cargs2: second operation's clbits.
    max_num_qubits: the maximum number of qubits to consider, the check may be skipped if
        the number of qubits for either operation exceeds this amount.
    approximation_degree: If the average gate fidelity in between the two operations
        is above this number (up to ``1e-12``) they are assumed to commute.

Returns:
    bool: whether two operations commute.
)r   commutetuple)	r   r   qargs1cargs1r   qargs2cargs2r   r   s	            r   r!   CommutationChecker.commuteG   sB    @ ww&M&M&M&M 	
 		
r   c                6    U R                   R                  5       $ )z Returns number of cached entries)r   num_cached_entriesr   s    r   r)   %CommutationChecker.num_cached_entriesr   s    ww))++r   c                8    U R                   R                  5         g)z1Clears the dictionary holding cached commutationsN)r   clear_cached_commutationsr*   s    r   r-   ,CommutationChecker.clear_cached_commutationsv   s    ))+r   c                N    U R                   R                  R                  XX45      $ )a  Returns stored commutation relation if any

Args:
    first_op: first operation.
    first_qargs: first operation's qubits.
    second_op: second operation.
    second_qargs: second operation's qubits.

Return:
    bool: True if the gates commute and false if it is not the case.
)r   librarycheck_commutation_entries)r   first_opfirst_qargs	second_opsecond_qargss        r   r1   ,CommutationChecker.check_commutation_entriesz   s$    $ ww889
 	
r   )r   )Ni@B )r   dictr   intr   zOptional[Set[str]])   g      ?)r   r8   r   floatreturnbool)r   r	   r#   Sequence[Qubit | int]r$   r=   r   r	   r%   r=   r&   r=   r   r8   r   r:   r;   r<   )
r2   r	   r3   r   r4   r	   r5   r   r;   zUnion[bool, None])__name__
__module____qualname____firstlineno____doc__r   r   r!   r)   r-   r1   __static_attributes__ r   r   r   r      s   : ,0!&T
 %)T$(T T
 "T  &)U 	U
 $U 
U$  &))
)
 &)
 &	)

 )
 &)
 &)
 )
 $)
 
)
V,,

 
 	

 
 

r   r   N)rB   
__future__r   typingr   r   r   r   collections.abcr   qiskit.circuit.operationr	   qiskit.circuitr
   &qiskit._accelerate.commutation_checkerr   r   rD   r   r   <module>rK      s,    _ " - - $ .   Tv
 v
r   