
    z	iz!                        S r SSKJr  SSKrSSKJrJrJrJr  SSK	J
r
  SSKJr  SSKJr  \(       a  SS	KJr   " S
 S\5      r " S S5      rg)z.Circuit operation representing a ``for`` loop.    )annotationsN)IterableOptionalUnionTYPE_CHECKING)	Parameter)CircuitError   )ControlFlowOpQuantumCircuitc                     ^  \ rS rSrSr S	       S
U 4S jjjr\S 5       r\R                  S 5       r\S 5       r	S r
SrU =r$ )	ForLoopOp   zA circuit operation which repeatedly executes a subcircuit
(``body``) parameterized by a parameter ``loop_parameter`` through
the set of integer values provided in ``indexset``.
c                Z   > UR                   nUR                  n[        TU ]  SXVXU/US9  g)a)  
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labelN)
num_qubits
num_clbitssuper__init__)selfindexsetloop_parameterbodyr   r   r   	__class__s          ]/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/controlflow/for_loop.pyr   ForLoopOp.__init__"   s8     __
__

40PX] 	 	
    c                    U R                   $ N_paramsr   s    r   paramsForLoopOp.params7   s    ||r    c                   SSK Jn  Uu  p4n[        U[        [	        S 5      45      (       d  [        S[	        U5       S35      e[        XR5      (       d  [        S[	        U5       S35      eUR                  U R                  :w  d  UR                  U R                  :w  a@  [        SU R                   SU R                   SUR                   SUR                   S3	5      eUbS  XER                  ;  aD  UR                  S	 UR                   5       ;   a#  [        R                  " S
UR                   S3SS9  [        U[        5      (       a  UO
[        U5      nX4U/U l        g )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 zAttempted 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  8   #    U  H  oR                   v   M     g 7fr"   )name).0ps     r   	<genexpr>#ForLoopOp.params.<locals>.<genexpr>Z   s     'H1s   zThe 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   r6   r   r   r   r   s         r   r&   r'   ;   sd    	2)3&$.9d4j*ABBE'(+  $//004T
|1> 
 ??doo-DOO1S004/@$//AR S448OO3DAdooEVVWY  &oo5##'H'HHMM #''( )&&   *(E::8h $7r    c                "    U R                   S   4$ )Nr1   r#   r%   s    r   blocksForLoopOp.blocksl   s    Q!!r    c                h    Uu  n[        U R                  S   U R                  S   X R                  S9$ )Nr   r
   r   )r   r&   r   )r   r<   r   s      r   replace_blocksForLoopOp.replace_blocksp   s,    QQZZPPr    r#   r"   )r   Iterable[int]r   zUnion[Parameter, None]r   r   r   Optional[str])__name__
__module____qualname____firstlineno____doc__r   propertyr&   setterr<   r?   __static_attributes____classcell__)r   s   @r   r   r      s      $

 /
 	

 
 
*   ]].8 .8` " "Q Qr    r   c                  R    \ rS rSrSrSrSr SSS.       SS jjjrS rS	 r	S
r
g)ForLoopContextu   a  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)
        with qc.if_test((0, True)):
            qc.break_loop()

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   c                   Xl         US L U l        X0l        [        U[        5      (       a  UO
[        U5      U l        X@l        SU l        g )NF)	rO   rP   rQ   r4   r9   r:   rR   rS   rT   )r   circuitr   r   r   s        r   r   ForLoopContext.__init__   sC      (6$(>%- &0%%@%@eHo
r    c                ,   U R                   (       a  [        S5      eSU l         U R                  R                  5         U R                  (       a;  [        SU R                   35      U l        [        U 5      =R                  S-  sl        U R                  $ )Nz0A for-loop context manager cannot be re-entered.T_loop_i_r
   )	rT   r	   rO   _push_scoperP   r   _generated_loop_parametersrQ   r5   r%   s    r   	__enter__ForLoopContext.__enter__   sq    ::QRR
!!#((#,x8W8W7X-Y#ZD J11Q61###r    c           	        Ub  U R                   R                  5         gU R                   R                  5       nUR                  UR                  5       UR	                  5       5      nU R
                  (       a  U R                  UR                  ;  a  S nOU R                  nU R                   R                  [        U R                  XeU R                  S9[        UR                  5      [        UR                  5      5        g)NFr   )rO   
_pop_scopebuildqubitsclbitsrP   rQ   r6   appendr   rR   rS   r:   )r   exc_typeexc_valexc_tbscoper   r   s          r   __exit__ForLoopContext.__exit__   s     MM$$&((* {{5<<>5<<>:
 ((T-A-A-X!N!11Ndnnn$++N$++$++	

 r    )rO   rP   rR   rS   rQ   rT   r"   )rV   r   r   rA   r   zOptional[Parameter]r   rB   )rC   rD   rE   rF   rG   r[   	__slots__r   r\   rh   rJ    r    r   rM   rM   u   s[    #N "#I /3	  $   ,	 "$r    rM   )rG   
__future__r   r7   typingr   r   r   r   qiskit.circuit.parameterr   qiskit.circuit.exceptionsr	   control_flowr   r3   r   r   rM   rk   r    r   <module>rq      sB    5 "  ; ; . 2 '-VQ VQre er    