§
    ¹³ÙgD(  ã                  ó°   — d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ d	d
lmZmZmZmZ d	dlmZ dd„Z	 	 	 ddd„Zd„ Zd„ Zd„ ZdS ) z&Add control to operation if supported.é    )Úannotations)Úpi)ÚCircuitError)ÚUnitaryGate)ÚPassManager)ÚBasisTranslatorÚUnrollCustomDefinitions)ÚSessionEquivalenceLibraryé   )ÚControlledGateÚGateÚQuantumRegisterÚQuantumCircuit)Ú_ctrl_state_to_intÚ	operationúGate | ControlledGateÚnum_ctrl_qubitsÚintÚlabelú
str | NoneÚ
ctrl_stateústr | int | NoneÚreturnr   c                óê   — t          | t          ¦  «        r|                      ¦   «          t          | |||¬¦  «        }| j        �/|j                             ¦   «         |_        | j        |j        _        |S )aõ  For standard gates, if the controlled version already exists in the
    library, it will be returned (e.g. XGate.control() = CnotGate().

    For more generic gates, this method implements the controlled
    version by first decomposing into the ['u1', 'u3', 'cx'] basis, then
    controlling each gate in the decomposition.

    Open controls are implemented by conjugating the control line with
    X gates. Adds num_ctrl_qubits controls to operation.

    This function is meant to be called from the
    :method:`qiskit.circuit.gate.Gate.control()` method.

    Args:
        operation: The operation to be controlled.
        num_ctrl_qubits: The number of controls to add to gate.
        label: An optional gate label.
        ctrl_state: The control state in decimal or as a bitstring
            (e.g. '111'). If specified as a bitstring the length
            must equal num_ctrl_qubits, MSB on left. If None, use
            2**num_ctrl_qubits-1.

    Returns:
        Controlled version of gate.

    )r   r   r   )Ú
isinstancer   Ú_defineÚcontrolr   Ú	base_gateÚ
to_mutable)r   r   r   r   Úcgates        úP/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/add_control.pyÚadd_controlr"      sn   € õ@ �)�[Ñ)Ô)ð à×ÒÑÔÐÝ�I¨ÀeÐXbÐcÑcÔc€EØ„Ð"Øœ/×4Ò4Ñ6Ô6ˆŒØ )¤ˆŒÔØ€Ló    Nú
int | Nonec           
     óx  ‡‡— ddl m} t          ||¦  «        }t          |d¬¦  «        }t          | j        d¬¦  «        Št          |‰d| j        › �¬¦  «        }t          | |j        ¦  «        r"| j	        }|  
                    ¦   «         } d| _	        d}g d¢}	| j        |	v rt          || |‰d         ¦  «         �n	t          | |j        ¦  «        r|  
                    ¦   «         } d| _	        t          | |	¬	¦  «        }
|
j        j        r||
j        j        z  }|
j        }d
„ |j        |j        fD ¦   «         Š|j        D ]†}|j        |j        }}t'          |¦  «        dk    r‰‰|d                           }nˆˆfd„|D ¦   «         }t          ||||¦  «         |j        �&|j        j        r|j        dk    r||j        j        z  }Œ‡|rOt'          |¦  «        dk     r|                     ||¦  «         n%|                     ||dd…         |d         ¦  «         t          | |j        ¦  «        r2|| _	        || j        z   }| j	        |z  |z  }| j        j        }| j        }n|}|}| j        }| }|dk    rd|d›�}nd|z                       d¦  «        }|› |› �}|                     ||j        | j        |||||¬¦  «        }|S )a�  Return controlled version of gate using controlled rotations. This function
    first checks the name of the operation to see if it knows of a method from which
    to generate a controlled version. Currently, these are ``x``, ``rx``, ``ry``, and ``rz``.
    If a method is not directly known, it calls the unroller to convert to `u1`, `u3`,
    and `cx` gates.

    Args:
        operation: The gate used to create the ControlledGate.
        num_ctrl_qubits: The number of controls to add to gate (default=1).
        label: An optional gate label.
        ctrl_state: The control state in decimal or as
            a bitstring (e.g. '111'). If specified as a bitstring the length
            must equal num_ctrl_qubits, MSB on left. If None, use
            2**num_ctrl_qubits-1.

    Returns:
        Controlled version of gate.

    Raises:
        CircuitError: gate contains non-gate in definition
    r   )Úcontrolledgater   ©ÚnameÚtargetÚc_N)ÚpÚuÚxÚzÚrxÚryÚrzÚcx©Úbasis_gatesc                ó>   — i | ]}t          |¦  «        D ]\  }}||“Œ	ŒS © )Ú	enumerate)Ú.0ÚbitsÚindexÚbits       r!   ú
<dictcomp>zcontrol.<locals>.<dictcomp>~   sK   € ð 
ð 
ð 
àÝ'¨™oœoð
ð 
ñ ��sð �ð
ð 
ð 
ð 
r#   r   c                ó,   •— g | ]}‰‰|                  ‘ŒS r6   r6   )r8   ÚqargÚbit_indicesÚq_targets     €€r!   ú
<listcomp>zcontrol.<locals>.<listcomp>Š   s#   ø€ ÐHÐHÐH¸$˜( ;¨tÔ#4Ô5ÐHÐHÐHr#   r1   é   éÿÿÿÿÚcÚdz{0})r   r   Ú
definitionr   r   )Úqiskit.circuitr&   r   r   Ú
num_qubitsr   r(   r   r   r   r   Úapply_basic_controlled_gateÚ_unroll_gaterF   Úglobal_phaseÚqubitsÚclbitsÚdatar   Úlenr+   Úmcpr   r   ÚformatÚparams)r   r   r   r   r&   Ú	q_controlÚcontrolled_circÚoriginal_ctrl_staterK   ÚbasisÚunrolled_gaterF   ÚinstructionÚgateÚqargsr)   Únew_num_ctrl_qubitsÚnew_ctrl_stateÚ	base_namer   Úctrl_substrÚnew_namer    r?   r@   s                          @@r!   r   r   E   s^  øø€ ð: .Ð-Ð-Ð-Ð-Ð-å# J°Ñ@Ô@€Jå °iÐ@Ñ@Ô@€IÝ˜yÔ3¸(ÐCÑCÔC€HÝ$ Y°Ð?TÀIÄNÐ?TÐ?TÐUÑUÔU€OÝ�)˜^Ô:Ñ;Ô;ð $Ø'Ô2ÐØ×(Ò(Ñ*Ô*ˆ	Ø#ˆ	Ôà€Là8Ð8Ð8€Eà„~˜ÐÐÝ# O°YÀ	È8ÐTUÌ;ÑWÔWÐWÑWå�i Ô!>Ñ?Ô?ð 	(Ø!×,Ò,Ñ.Ô.ˆIØ#'ˆIÔ å$ Y¸EÐBÑBÔBˆØÔ#Ô0ð 	BØ˜MÔ4ÔAÑAˆLà"Ô-ˆ
ð
ð 
à#Ô*¨JÔ,=Ð>ð
ñ 
ô 
ˆð &œ?ð 	=ð 	=ˆKØ%Ô/°Ô1C�%ˆDå�5‰zŒz˜QŠˆØ! +¨e°A¬hÔ"7Ô8��àHÐHÐHÐHÐHÀ%ÐHÑHÔH�å'¨¸¸yÈ&ÑQÔQÐQàŒÐ*¨t¬Ô/KÐ*ÐPTÔPYÐ]aÒPaÐPaØ ¤Ô <Ñ<�øð ð MÝˆy‰>Œ>˜AÒÐØ×Ò˜l¨IÑ6Ô6Ð6Ð6à×Ò ¨i¸¸¸¬n¸iÈ¼mÑLÔLÐLÝ�)˜^Ô:Ñ;Ô;ð 
Ø2ˆ	ÔØ-°	Ô0IÑIÐØ"Ô-°Ñ@À:ÑMˆØÔ'Ô,ˆ	ØÔ'ˆ	ˆ	à-ÐØ#ˆØ”Nˆ	Øˆ	ð ˜QÒÐØ1Ð-Ð1Ð1Ð1ˆˆàÐ2Ñ2×:Ò:¸3Ñ?Ô?ˆØÐ*˜yÐ*Ð*€HØ×)Ò)ØØÔ"ØÔØØ+Ø"Ø!Øð *ñ 	ô 	€Eð €Lr#   c                óD  — t          |¦  «        }|j        dk    r|                      ||¦  «         dS |j        dk    r;|                      |j        j        d         j        j        d         ||d¬¦  «         dS |j        dk    r<|                      |j        j        d         j        j        d         ||dd¬¦  «         dS |j        d	k    r;|  	                    |j        j        d         j        j        d         ||d¬¦  «         dS |j        d
k    r?ddl
m} |                       ||j        d         |¦  «        |dd…         |gz   ¦  «         dS |j        dk    r0|                      |dd…         |d         gz   |d         ¦  «         dS |j        dk    �r+|j        \  }}}|dk    rM|dk    r%|dk    r|                      ||d         |¦  «         dS |                      |||d|d         |¦  «         dS |t           dz  k    r)|t          dz  k    r|                      |||d¬¦  «         dS |dk    r!|dk    r|                      |||d¬¦  «         dS |dk    r|dk    r|                      |||¦  «         dS |                      |||¦  «         |                      |||d¬¦  «         |                      |||¦  «         dS |j        dk    rB|                      |¦  «         |                      ||¦  «         |                      |¦  «         dS t%          d|› d�¦  «        ‚)z¿Apply a controlled version of ``gate`` to the circuit.

    This implements multi-control operations for the following basis gates:

        ["p", "u", "x", "z", "rx", "ry", "rz", "cx"]

    r-   r/   r   F)Úuse_basis_gatesr0   Ú	noancilla)Úmodera   r1   r+   )ÚMCPhaseGateNr2   r   r,   rB   Tr.   zGate z not in supported basis.)rO   r(   ÚmcxÚmcrxrF   rN   r   rR   ÚmcryÚmcrzÚqiskit.circuit.libraryrd   ÚappendÚcpÚcur   rP   Úhr   )	ÚcircuitrY   Úcontrolsr)   r   rd   ÚthetaÚphiÚlambs	            r!   rI   rI   ¹   s×  € õ ˜(‘m”m€Oà„y�CÒÐØ�Š�H˜fÑ%Ô%Ð%Ð%Ð%à	Œ�dÒ	Ð	Ø�ŠØŒOÔ  Ô#Ô-Ô4°QÔ7ØØØ!ð	 	ñ 	
ô 	
ð 	
ð 	
ð 	
ð 
Œ�dÒ	Ð	Ø�ŠØŒOÔ  Ô#Ô-Ô4°QÔ7ØØØØ!ð 	ñ 	
ô 	
ð 	
ð 	
ð 	
ð 
Œ�dÒ	Ð	Ø�ŠØŒOÔ  Ô#Ô-Ô4°QÔ7ØØØ!ð	 	ñ 	
ô 	
ð 	
ð 	
ð 	
ð 
Œ�cÒ	Ð	Ø6Ð6Ð6Ð6Ð6Ð6à�ŠØˆK˜œ Aœ¨Ñ8Ô8Ø�Q�Q�QŒK˜6˜(Ñ"ñ	
ô 	
ð 	
ð 	
ð 	
ð 
Œ�dÒ	Ð	Ø�ŠØ�Q�Q�QŒK˜6 !œ9˜+Ñ%Ø�1ŒIñ	
ô 	
ð 	
ð 	
ð 	
ð 
Œ�cÒ	Ñ	Øœ;Ñˆˆs�DØ˜aÒÐØ˜Šzˆz˜c Qšh˜hØ—
’
˜4 ¨!¤¨fÑ5Ô5Ð5Ð5Ð5à—
’
˜5 # t¨Q°¸´¸VÑDÔDÐDÐDÐDà•r�c˜A‘gŠ~ˆ~ $­"¨q©&¢. .Ø—’˜U H¨fÀd�ÑKÔKÐKÐKÐKØ˜’�˜d aši˜iØ—’ØØØØ$(ð	 ñ ô ð ð ð ð ˜!’�  q¢ Ø—’˜D (¨FÑ3Ô3Ð3Ð3Ð3à—’˜D (¨FÑ3Ô3Ð3Ø—’ØØØØ$(ð	 ñ ô ð ð —’˜C ¨6Ñ2Ô2Ð2Ð2Ð2à	Œ�cÒ	Ð	Ø�	Š	�&ÑÔÐØ�Š�H˜fÑ%Ô%Ð%Ø�	Š	�&ÑÔÐÐÐõ ÐA 4ÐAÐAÐAÑBÔBÐBr#   c                óÂ   — t          | d¦  «        r| j        �| j        S t          | j        ¦  «        }t	          || j        ¬¦  «        }|                     | |¦  «         |S )z,Converts a gate instance to a QuantumCircuitrF   Nr'   )ÚhasattrrF   r   rH   r   r(   rj   )r   ÚqrÚqcs      r!   Ú_gate_to_circuitrw     sb   € åˆy˜,Ñ'Ô'ð $¨IÔ,@Ð,LØÔ#Ð#å	˜Ô-Ñ	.Ô	.€BÝ	˜ ¤Ð	0Ñ	0Ô	0€BØ‡I‚Iˆi˜ÑÔÐØ€Ir#   c                óä   — t          | ¦  «        }t          t          t          |¬¦  «        t	          t          |¬¦  «        g¦  «        }|                     |¦  «        }|                     ¦   «         S )z7Unrolls a gate, possibly composite, to the target basisr3   )Útarget_basis)rw   r   r	   Úselr   ÚrunÚto_gate)r   r4   ÚcircÚpmÚopqcs        r!   rJ   rJ     sb   € å˜IÑ&Ô&€DÝ	å#¥C°[ÐAÑAÔAÝ�C¨kÐ:Ñ:Ô:ð	
ñ
ô 
€Bð �6Š6�$‰<Œ<€DØ�<Š<‰>Œ>Ðr#   )
r   r   r   r   r   r   r   r   r   r   )r   NN)
r   r   r   r$   r   r   r   r   r   r   )Ú__doc__Ú
__future__r   Úmathr   Úqiskit.circuit.exceptionsr   ri   r   Úqiskit.transpilerr   Úqiskit.transpiler.passes.basisr   r	   Ú"qiskit.circuit.equivalence_libraryr
   rz   Ú r   r   r   r   Ú_utilsr   r"   r   rI   rw   rJ   r6   r#   r!   ú<module>r‰      sD  ðð -Ð ,Ø "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø .Ð .Ð .Ð .Ð .Ð .Ø )Ð )Ð )Ð )Ð )Ð )Ø SÐ SÐ SÐ SÐ SÐ SÐ SÐ SØ OÐ OÐ OÐ OÐ OÐ Oà CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ &Ð &Ð &Ð &Ð &Ð &ð'ð 'ð 'ð 'ðX #$ØØ#'ð	qð qð qð qð qðhSCð SCð SCðlð ð ð
ð 
ð 
ð 
ð 
r#   