§
    ¹³Ùg\!  ã                  ó    — d Z ddlmZ ddlZddlmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ erdd	lmZ  G d
„ de¦  «        Z G d„ d¦  «        ZdS )z.Circuit operation representing a ``for`` loop.é    )ÚannotationsN)ÚIterableÚOptionalÚUnionÚTYPE_CHECKING)Ú	Parameter)ÚCircuitErroré   )ÚControlFlowOp©ÚQuantumCircuitc                  óz   ‡ — e Zd ZdZ	 ddˆ fd„Zed„ ¦   «         Zej        d„ ¦   «         Zed„ ¦   «         Zd„ Z	ˆ xZ
S )Ú	ForLoopOpz¾A circuit operation which repeatedly executes a subcircuit
    (``body``) parameterized by a parameter ``loop_parameter`` through
    the set of integer values provided in ``indexset``.
    NÚindexsetúIterable[int]Úloop_parameterúUnion[Parameter, None]Úbodyr   ÚlabelúOptional[str]c                óv   •— |j         }|j        }t          ¦   «                              d|||||g|¬¦  «         dS )aa  
        Args:
            indexset: A collection of integers to loop over.
            loop_parameter: The placeholder parameterizing ``body`` to which
                the values from ``indexset`` will be assigned.
            body: The loop body to be repeatedly executed.
            label: An optional label for identifying the instruction.
        Úfor_loop©r   N)Ú
num_qubitsÚ
num_clbitsÚsuperÚ__init__)Úselfr   r   r   r   r   r   Ú	__class__s          €úY/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/controlflow/for_loop.pyr   zForLoopOp.__init__"   sQ   ø€ ð ”_ˆ
Ø”_ˆ
Ý‰Œ×ÒØ˜
 J°¸>È4Ð0PÐX]ð 	ñ 	
ô 	
ð 	
ð 	
ð 	
ó    c                ó   — | j         S ©N©Ú_params©r   s    r    ÚparamszForLoopOp.params7   s
   € àŒ|Ðr!   c                óŠ  — ddl m} |\  }}}t          |t          t	          d ¦  «        f¦  «        s t          dt	          |¦  «        › d�¦  «        ‚t          ||¦  «        s t          dt	          |¦  «        › d�¦  «        ‚|j        | j        k    s|j        | j        k    r0t          d| j        › d| j        › d|j        › d|j        › d�	¦  «        ‚|�@||j        vr7|j	        d	„ |j        D ¦   «         v rt          j        d
|j	        › d�d¬¦  «         t          |t          ¦  «        r|nt          |¦  «        }|||g| _        d S )Nr   r   zbForLoopOp expects a loop_parameter parameter to be either of type Parameter or None, but received ú.zNForLoopOp expects a body parameter to be of type QuantumCircuit, but received zˆAttempted to assign a body parameter with a num_qubits or num_clbits different than that of the ForLoopOp. ForLoopOp num_qubits/clbits: ú/z" Supplied body num_qubits/clbits: c              3  ó$   K  — | ]}|j         V — Œd S r#   )Úname)Ú.0Úps     r    ú	<genexpr>z#ForLoopOp.params.<locals>.<genexpr>Z   s$   è è € Ð'HÐ'H°1¨¬Ð'HÐ'HÐ'HÐ'HÐ'HÐ'Hr!   z²The Parameter provided as a loop_parameter was not found on the loop body and so no binding of the indexset to loop parameter will occur. A different Parameter of the same name (zs) was found. If you intended to loop over that Parameter, please use that Parameter instance as the loop_parameter.é   )Ú
stacklevel)Úqiskit.circuitr   Ú
isinstancer   Útyper	   r   r   Ú
parametersr,   ÚwarningsÚwarnÚrangeÚtupler%   )r   r5   r   r   r   r   s         r    r'   zForLoopOp.params;   sÚ  € ð 	2Ð1Ð1Ð1Ð1Ð1à)3Ñ&ˆ�. $å˜.­9µd¸4±j´jÐ*AÑBÔBð 	Ýð+å˜Ñ'Ô'ð+ð +ð +ñô ð õ ˜$ Ñ/Ô/ð 	Ýð>Ý04°T±
´
ð>ð >ð >ñô ð ð
 Œ?˜dœoÒ-Ð-°´ÀDÄOÒ1SÐ1SÝðYà04´ðYð YàBFÄ/ðYð Yð 59´OðYð Yð GKÄoðYð Yð Yñô ð ð Ð&Ø d¤oÐ5Ð5ØÔ#Ð'HÐ'H¸¼Ð'HÑ'HÔ'HÐHÐHåŒMð&ð #Ô'ð&ð &ð &ð ðñ ô ð õ  *¨(µEÑ:Ô:ÐO�8�8ÅÀhÁÄˆà  .°$Ð7ˆŒˆˆr!   c                ó   — | j         d         fS )Nr0   r$   r&   s    r    ÚblockszForLoopOp.blocksl   s   € à”˜Q”Ð!Ð!r!   c                óf   — |\  }t          | j        d         | j        d         || j        ¬¦  «        S )Nr   r
   r   )r   r'   r   )r   r;   r   s      r    Úreplace_blockszForLoopOp.replace_blocksp   s.   € Ø‰ˆÝ˜œ Qœ¨¬°Q¬¸ÀTÄZÐPÑPÔPÐPr!   r#   )r   r   r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr'   Úsetterr;   r=   Ú__classcell__)r   s   @r    r   r      s»   ø€ € € € € ðð ð  $ð
ð 
ð 
ð 
ð 
ð 
ð 
ð* ðð ñ „Xðð „]ð.8ð .8ñ „]ð.8ð` ð"ð "ñ „Xð"ðQð Qð Qð Qð Qð Qð Qr!   r   c                  ó8   — e Zd ZdZdZdZ	 dddœdd„Zd„ Zd„ ZdS )ÚForLoopContexta/  A context manager for building up ``for`` loops onto circuits in a natural order, without
    having to construct the loop body first.

    Within the block, a lot of the bookkeeping is done for you; you do not need to keep track of
    which qubits and clbits you are using, for example, and a loop parameter will be allocated for
    you, if you do not supply one yourself.  All normal methods of accessing the qubits on the
    underlying :obj:`~QuantumCircuit` will work correctly, and resolve into correct accesses within
    the interior block.

    You generally should never need to instantiate this object directly.  Instead, use
    :obj:`.QuantumCircuit.for_loop` in its context-manager form, i.e. by not supplying a ``body`` or
    sets of qubits and clbits.

    Example usage::

        import math
        from qiskit import QuantumCircuit
        qc = QuantumCircuit(2, 1)

        with qc.for_loop(range(5)) as i:
            qc.rx(i * math.pi/4, 0)
            qc.cx(0, 1)
            qc.measure(0, 0)
            qc.break_loop().c_if(0, True)

    This context should almost invariably be created by a :meth:`.QuantumCircuit.for_loop` call, and
    the resulting instance is a "friend" of the calling circuit.  The context will manipulate the
    circuit's defined scopes when it is entered (by pushing a new scope onto the stack) and exited
    (by popping its scope, building it, and appending the resulting :obj:`.ForLoopOp`).

    .. warning::

        This is an internal interface and no part of it should be relied upon outside of Qiskit
        Terra.
    r   )Ú_circuitÚ_generate_loop_parameterÚ_loop_parameterÚ	_indexsetÚ_labelÚ_usedNr   Úcircuitr   r   r   r   úOptional[Parameter]r   r   c               ó¦   — || _         |d u | _        || _        t          |t          ¦  «        r|nt          |¦  «        | _        || _        d| _        d S )NF)	rG   rH   rI   r3   r8   r9   rJ   rK   rL   )r   rM   r   r   r   s        r    r   zForLoopContext.__init__§   sT   € ð  ˆŒØ(6¸$Ð(>ˆÔ%Ø-ˆÔõ &0°½%Ñ%@Ô%@ÐU˜˜ÅeÈHÁoÄoˆŒØˆŒØˆŒ
ˆ
ˆ
r!   c                óü   — | j         rt          d¦  «        ‚d| _         | j                             ¦   «          | j        r9t          d| j        › �¦  «        | _        t          | ¦  «        xj        dz  c_        | j        S )Nz0A for-loop context manager cannot be re-entered.TÚ_loop_i_r
   )	rL   r	   rG   Ú_push_scoperH   r   Ú_generated_loop_parametersrI   r4   r&   s    r    Ú	__enter__zForLoopContext.__enter__¸   s�   € ØŒ:ð 	SÝÐQÑRÔRÐRØˆŒ
ØŒ×!Ò!Ñ#Ô#Ð#ØÔ(ð 	7Ý#,Ð-Y¸Ô8WÐ-YÐ-YÑ#ZÔ#ZˆDÔ Ý�‰JŒJÐ1Ô1°QÑ6Ð1Ô1ØÔ#Ð#r!   c                óÚ  — |�| j                              ¦   «          dS | j                              ¦   «         }|                     |                     ¦   «         |                     ¦   «         ¦  «        }| j        r| j        |j        vrd }n| j        }| j                              t          | j
        ||| j        ¬¦  «        t          |j        ¦  «        t          |j        ¦  «        ¦  «         dS )NFr   )rG   Ú
_pop_scopeÚbuildÚqubitsÚclbitsrH   rI   r5   Úappendr   rJ   rK   r9   )r   Úexc_typeÚexc_valÚexc_tbÚscoper   r   s          r    Ú__exit__zForLoopContext.__exit__Â   sÙ   € ØÐð ŒM×$Ò$Ñ&Ô&Ð&Ø�5Ø”×(Ò(Ñ*Ô*ˆð �{Š{˜5Ÿ<š<™>œ>¨5¯<ª<©>¬>Ñ:Ô:ˆð
 Ô(ð 	2¨TÔ-AÈÌÐ-XÐ-XØ!ˆNˆNà!Ô1ˆNØŒ×ÒÝ�d”n n°dÀ$Ä+ÐNÑNÔNÝ�$”+ÑÔÝ�$”+ÑÔñ	
ô 	
ð 	
ð
 ˆur!   r#   )rM   r   r   r   r   rN   r   r   )	r>   r?   r@   rA   rS   Ú	__slots__r   rT   r_   © r!   r    rF   rF   u   sy   € € € € € ð"ð "ðL "#Ðð€Ið /3ð	ð  $ðð ð ð ð ð ð"$ð $ð $ðð ð ð ð r!   rF   )rA   Ú
__future__r   r6   Útypingr   r   r   r   Úqiskit.circuit.parameterr   Úqiskit.circuit.exceptionsr	   Úcontrol_flowr   r2   r   r   rF   ra   r!   r    ú<module>rg      s  ðð 5Ð 4à "Ð "Ð "Ð "Ð "Ð "à €€€Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;à .Ð .Ð .Ð .Ð .Ð .Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'àð .Ø-Ð-Ð-Ð-Ð-Ð-ðVQð VQð VQð VQð VQ�ñ VQô VQð VQðrdð dð dð dð dñ dô dð dð dð dr!   