§
    ¹³Ùg—  ã                  ó¬   — d 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 ddlmZ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 )z0Circuit operation representing a ``while`` loop.é    )Úannotations)ÚTYPE_CHECKING)ÚClbitÚClassicalRegister)Úexpr)ÚCircuitErroré   )Úvalidate_conditionÚcondition_resources)ÚControlFlowOp©ÚQuantumCircuitc                  ó¶   ‡ — e Zd ZdZ	 ddˆ fd	„Zed
„ ¦   «         Zed„ ¦   «         Zej        d„ ¦   «         Zej        d„ ¦   «         Zed„ ¦   «         Z	d„ Z
d„ Zˆ xZS )ÚWhileLoopOpzæA circuit operation which repeatedly executes a subcircuit (``body``) until
    a condition (``condition``) evaluates as False.

    The classical bits used in ``condition`` must be a subset of those attached
    to ``body``.
    NÚ	conditionú=tuple[ClassicalRegister, int] | tuple[Clbit, int] | expr.ExprÚbodyr   Úlabelú
str | Nonec                óš   •— |j         }|j        }t          ¦   «                              d|||g|¬¦  «         t	          |¦  «        | _        dS )aÔ  
        Args:
            condition: A condition to be checked prior to executing ``body``. Can be
                specified as either a tuple of a ``ClassicalRegister`` to be tested
                for equality with a given ``int``, or as a tuple of a ``Clbit`` to
                be compared to either a ``bool`` or an ``int``.
            body: The loop body to be repeatedly executed.
            label: An optional label for identifying the instruction.
        Ú
while_loop©r   N)Ú
num_qubitsÚ
num_clbitsÚsuperÚ__init__r
   Ú
_condition)Úselfr   r   r   r   r   Ú	__class__s         €ú[/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/controlflow/while_loop.pyr   zWhileLoopOp.__init__%   sK   ø€ ð ”_ˆ
Ø”_ˆ
å‰Œ×Ò˜ z°:À¸vÈUÐÑSÔSÐSÝ,¨YÑ7Ô7ˆŒˆˆó    c                ó   — | j         S ©N©Ú_params©r   s    r    ÚparamszWhileLoopOp.params:   s
   € àŒ|Ðr!   c                ó   — | j         S r#   ©r   r&   s    r    r   zWhileLoopOp.condition>   s
   € àŒÐr!   c                ó   — || _         d S r#   r)   )r   Úvalues     r    r   zWhileLoopOp.conditionB   s   € àˆŒˆˆr!   c                ó*  — ddl m} |\  }t          ||¦  «        s t          dt	          |¦  «        › d�¦  «        ‚|j        | j        k    s|j        | j        k    r0t          d| j        › d| j        › d|j        › d|j        › d�	¦  «        ‚|g| _        d S )Nr   r   zJWhileLoopOp expects a body parameter of type QuantumCircuit, but received ú.zŒAttempted to assign a body parameter with a num_qubits or num_clbits different than that of the WhileLoopOp. WhileLoopOp num_qubits/clbits: ú/z" Supplied body num_qubits/clbits: )Úqiskit.circuitr   Ú
isinstancer   Útyper   r   r%   )r   Ú
parametersr   r   s       r    r'   zWhileLoopOp.paramsF   sô   € ð 	2Ð1Ð1Ð1Ð1Ð1à‰ˆå˜$ Ñ/Ô/ð 	Ýð>Ý04°T±
´
ð>ð >ð >ñô ð ð
 Œ?˜dœoÒ-Ð-°´ÀDÄOÒ1SÐ1SÝðYà26´/ðYð YàDHÄOðYð Yð 59´OðYð Yð GKÄoðYð Yð Yñô ð ð �vˆŒˆˆr!   c                ó   — | j         d         fS )Nr   r$   r&   s    r    ÚblockszWhileLoopOp.blocks]   s   € à”˜Q”Ð!Ð!r!   c                óB   — |\  }t          | j        || j        ¬¦  «        S )Nr   )r   r   r   )r   r4   r   s      r    Úreplace_blockszWhileLoopOp.replace_blocksa   s"   € Ø‰ˆÝ˜4œ?¨D¸¼
ÐCÑCÔCÐCr!   c                ó    — t          d¦  «        ‚)NzfWhileLoopOp cannot be classically controlled through Instruction.c_if. Please use an IfElseOp instead.)ÚNotImplementedError)r   Ú	classicalÚvals      r    Úc_ifzWhileLoopOp.c_ife   s   € Ý!ð.ñ
ô 
ð 	
r!   r#   )r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr'   r   Úsetterr4   r6   r;   Ú__classcell__)r   s   @r    r   r      sÿ   ø€ € € € € ðð ð !ð	8ð 8ð 8ð 8ð 8ð 8ð 8ð* ðð ñ „Xðð ðð ñ „Xðð Ôð ð  ñ Ôð ð „]ðð ñ „]ðð, ð"ð "ñ „Xð"ðDð Dð Dð
ð 
ð 
ð 
ð 
ð 
ð 
r!   r   c                  ó0   — e Zd ZdZdZddœdd„Zd„ Zd„ ZdS )ÚWhileLoopContextaI  A context manager for building up while 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.  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.while_loop` in its context-manager form, i.e. by not supplying a ``body``
    or sets of qubits and clbits.

    Example usage::

        from qiskit.circuit import QuantumCircuit, Clbit, Qubit
        bits = [Qubit(), Qubit(), Clbit()]
        qc = QuantumCircuit(bits)

        with qc.while_loop((bits[2], 0)):
            qc.h(0)
            qc.cx(0, 1)
            qc.measure(0, 0)

    .. warning::

        This is an internal interface and no part of it should be relied upon outside of Qiskit
        Terra.
    )Ú_circuitr   Ú_labelNr   Úcircuitr   r   r   r   r   c               óJ   — || _         t          |¦  «        | _        || _        d S r#   )rE   r
   r   rF   )r   rG   r   r   s       r    r   zWhileLoopContext.__init__Œ   s%   € ð  ˆŒÝ,¨YÑ7Ô7ˆŒØˆŒˆˆr!   c                óz   — t          | j        ¦  «        }| j                             |j        |j        ¬¦  «         d S )N)ÚclbitsÚ	registers)r   r   rE   Ú_push_scoperJ   Úcregs)r   Ú	resourcess     r    Ú	__enter__zWhileLoopContext.__enter__˜   s7   € Ý'¨¬Ñ8Ô8ˆ	ØŒ×!Ò!¨Ô)9ÀYÄ_Ð!ÑUÔUÐUÐUÐUr!   c                óf  — |�| j                              ¦   «          dS | j                              ¦   «         }|                     |                     ¦   «         |                     ¦   «         ¦  «        }| j                              t          | j        || j        ¬¦  «        |j        |j        ¦  «         dS )NFr   )	rE   Ú
_pop_scopeÚbuildÚqubitsrJ   Úappendr   r   rF   )r   Úexc_typeÚexc_valÚexc_tbÚscoper   s         r    Ú__exit__zWhileLoopContext.__exit__œ   sœ   € ØÐð ŒM×$Ò$Ñ&Ô&Ð&Ø�5Ø”×(Ò(Ñ*Ô*ˆð �{Š{˜5Ÿ<š<™>œ>¨5¯<ª<©>¬>Ñ:Ô:ˆØŒ×ÒÝ˜œ¨°T´[ÐAÑAÔAØŒKØŒKñ	
ô 	
ð 	
ð
 ˆur!   )rG   r   r   r   r   r   )r<   r=   r>   r?   Ú	__slots__r   rO   rY   © r!   r    rD   rD   l   si   € € € € € ðð ð: 5€Ið !ð
ð 
ð 
ð 
ð 
ð 
ðVð Vð Vðð ð ð ð r!   rD   N)r?   Ú
__future__r   Útypingr   Ú qiskit.circuit.classicalregisterr   r   Úqiskit.circuit.classicalr   Úqiskit.circuit.exceptionsr   Ú_builder_utilsr
   r   Úcontrol_flowr   r/   r   r   rD   r[   r!   r    ú<module>rc      s  ðð 7Ð 6à "Ð "Ð "Ð "Ð "Ð "à  Ð  Ð  Ð  Ð  Ð  à EÐ EÐ EÐ EÐ EÐ EÐ EÐ EØ )Ð )Ð )Ð )Ð )Ð )Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ 'Ð 'Ð 'Ð 'Ð 'Ð 'àð .Ø-Ð-Ð-Ð-Ð-Ð-ðL
ð L
ð L
ð L
ð L
�-ñ L
ô L
ð L
ð^?ð ?ð ?ð ?ð ?ñ ?ô ?ð ?ð ?ð ?r!   