
    z	in                         S r SSKrSSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SSKJrJr  S	\4S
 jrSS\S\R                   S\4S jjrS	\S\4S jrg)z:Utility functions for handling linear reversible circuits.    N)Callable)QuantumCircuit)QiskitError)CircuitError   )calc_inverse_matrixcheck_invertible_binary_matrixqcc                 B   [        U R                  U R                  U R                  S-   S9n[	        U R
                  5       HY  nUR                  R                  S:w  a  [        S5      eUR                  UR                  [	        UR                  5      S95        M[     U$ )aD  Takes a circuit having only CX gates, and calculates its transpose.
This is done by recursively replacing CX(i, j) with CX(j, i) in all instructions.

Args:
    qc: a :class:`.QuantumCircuit` containing only CX gates.

Returns:
    QuantumCircuit: the transposed circuit.

Raises:
    CircuitError: if qc has a non-CX gate.

_transpose)namecxz"The circuit contains non-CX gates.)qubits)
r   r   clbitsr   reverseddata	operationr   _appendreplace)r
   transposed_circinstructions      g/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/synthesis/linear/linear_circuits_utils.pytranspose_cx_circr      s     %RYY		,@VWO(  %%-CDD 3 38KDVDV;W 3 XY )     functionmatoptimize_countc                 h   [        U5      (       d  [        S5      eU " U5      nUnUR                  5       nUR                  5       S   n[	        SS5       GHX  n[
        R                  " U5      nUS:X  a$  [        U5      nU " U5      nUR                  5       nOxUS:X  a*  [        R                  " U5      nU " U5      n[        U5      nOHUS:X  aB  [        [        R                  " U5      5      nU " U5      n[        U5      nUR                  5       nUR                  5       n	UR                  5       S   n
U=(       a    Xj:  =(       d    U(       + =(       a    XY:H  =(       a    Xj:  nU(       + =(       a    XY:  =(       d    U=(       a    Xj:H  =(       a    XY:  nU(       d
  U(       d  GMR  U
nU	nUnGM[     U$ )a+  Get the best implementation of a circuit implementing a binary invertible matrix M,
by considering all four options: M,M^(-1),M^T,M^(-1)^T.
Optimizing either the CX count or the depth.

Args:
    function: the synthesis function.
    mat: a binary invertible matrix.
    optimize_count: True if the number of CX gates in optimize, False if the depth is optimized.

Returns:
    QuantumCircuit: an optimized :class:`.QuantumCircuit`, has the best depth or CX count of
        the four options.

Raises:
    QiskitError: if mat is not an invertible matrix.
zThe matrix is not invertible.r   r            )r	   r   depth	count_opsrangecopydeepcopyr   inversenp	transposer   )r   r   r   r
   best_qc
best_depth
best_countimat_cpy	new_depth	new_countbetter_countbetter_depths                r   optimize_cx_4_optionsr3   -   s}   " *#..9::	#BGJ%J1a[--$6)'2G'"BB!Vll7+G'"B"2&B!V)",,w*?@G'"B"2&BBHHJ	LLN4(	&A:+A 
U:#:Uz?U 	 +*Ez/E 
Qz6Q:;Q 	 <<"J"JG? B Nr   returnc                 v   U R                    H  nUR                  R                  S:  d  M  UR                  R                  S:X  ag  U R	                  UR
                  S   5      R                  nU R	                  UR
                  S   5      R                  n[        X#-
  5      nUS:w  a    gM  [        S5      e   g)a6  Check that the synthesized circuit qc fits linear nearest neighbor connectivity.

Args:
    qc: a :class:`.QuantumCircuit` containing only CX and single qubit gates.

Returns:
    bool: True if the circuit has linear nearest neighbor connectivity.

Raises:
    CircuitError: if qc has a non-CX two-qubit gate.
r   r   r   Fz3The circuit has two-qubits gates different than CX.T)	r   r   
num_qubitsr   find_bitr   indexabsr   )r
   r   q0q1dists        r   check_lnn_connectivityr=   j   s     ww  ++a/$$))T1[[!3!3A!67==[[!3!3A!67==27|19   ##XYY  r   )T)__doc__r%   typingr   numpyr(   qiskit.circuitr   qiskit.exceptionsr   qiskit.circuit.exceptionsr    r   r	   r   ndarrayboolr3   r=    r   r   <module>rH      s^    A    ) ) 2 A. *:H :2:: :t :z~ $ r   