§
    º³Ùgn  ã                   ó–   — d Z ddlZddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZ d	efd
„Zddedej        defd„Zd	edefd„ZdS )z:Utility functions for handling linear reversible circuits.é    N)ÚCallable)ÚQuantumCircuit)ÚQiskitError)ÚCircuitErroré   )Úcalc_inverse_matrixÚcheck_invertible_binary_matrixÚqcc                 ó0  — t          | j        | j        | j        dz   ¬¦  «        }t	          | j        ¦  «        D ]\}|j        j        dk    rt          d¦  «        ‚|                     | 	                    t	          |j        ¦  «        ¬¦  «        ¦  «         Œ]|S )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      úc/granary/edev/quantum/lib/python3.11/site-packages/qiskit/synthesis/linear/linear_circuits_utils.pyÚtranspose_cx_circr      s—   € õ % R¤Y°´	ÀÄÈ,Ñ@VÐWÑWÔW€OÝ ¤Ñ(Ô(ð Zð ZˆØÔ Ô%¨Ò-Ð-ÝÐCÑDÔDÐDØ×Ò × 3Ò 3½8ÀKÔDVÑ;WÔ;WÐ 3Ñ XÔ XÑYÔYÐYÐYØÐó    TÚfunctionÚmatÚoptimize_countc                 óT  — t          |¦  «        st          d¦  «        ‚ | |¦  «        }|}|                     ¦   «         }|                     ¦   «         d         }t	          dd¦  «        D �]<}t          j        |¦  «        }|dk    r/t          |¦  «        } | |¦  «        }|                     ¦   «         }nŠ|dk    r/t          j
        |¦  «        } | |¦  «        }t          |¦  «        }nU|dk    rOt          t          j
        |¦  «        ¦  «        } | |¦  «        }t          |¦  «        }|                     ¦   «         }|                     ¦   «         }	|                     ¦   «         d         }
|r||
k    p| o||	k    o||
k    }| r||	k    p|o||
k    o||	k    }|s|r|
}|	}|}�Œ>|S )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å�1�a‰[Œ[ð ñ ˆÝ”- Ñ$Ô$ˆà�Š6ˆ6Ý)¨'Ñ2Ô2ˆGØ�˜'Ñ"Ô"ˆBØ—’‘”ˆBˆBØ�!ŠVˆVÝ”l 7Ñ+Ô+ˆGØ�˜'Ñ"Ô"ˆBÝ" 2Ñ&Ô&ˆBˆBØ�!ŠVˆVÝ)­"¬,°wÑ*?Ô*?Ñ@Ô@ˆGØ�˜'Ñ"Ô"ˆBÝ" 2Ñ&Ô&ˆBØ—’‘”ˆBà—H’H‘J”Jˆ	Ø—L’L‘N”N 4Ô(ˆ	à&ÐA¨:¸	Ò+Að 
ØÐÐU :°Ò#:ÐU¸zÈIÒ?Uð 	ð +Ð*ÐE¨z¸IÒ/Eð 
ØÐQ˜z¨YÒ6ÐQ¸:È	Ò;Qð 	ð ð 	˜<ð 	Ø"ˆJØ"ˆJØˆGùà€Nr   Úreturnc                 óD  — | j         D ]—}|j        j        dk    r…|j        j        dk    rf|                      |j        d         ¦  «        j        }|                      |j        d         ¦  «        j        }t          ||z
  ¦  «        }|dk    r dS Œˆt          d¦  «        ‚Œ˜dS )aR  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²   € ð ”wð 	Zð 	ZˆØÔ Ô+¨aÒ/Ð/ØÔ$Ô)¨TÒ1Ð1Ø—[’[ Ô!3°AÔ!6Ñ7Ô7Ô=�Ø—[’[ Ô!3°AÔ!6Ñ7Ô7Ô=�Ý˜2 ™7‘|”|�Ø˜1’9�9Ø ˜5˜5ð õ #Ð#XÑYÔYÐYð 0ð ˆ4r   )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Ð 2Ð 2Ð 2Ð 2Ð 2Ø AÐ AÐ AÐ AÐ AÐ AÐ AÐ Að˜.ð ð ð ð ð*:ð : Hð :°2´:ð :Ètð :ð :ð :ð :ðz˜~ð °$ð ð ð ð ð ð r   