
    z	i                       S r SSKJr  SSKrSSKrSSKrSSKJrJrJrJ	r	J
r
JrJr  SSKJr  SSKJr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SSKJr  SSKJrJr  SSKJrJr  \R@                  (       a  SSK!r! " S S\RD                  5      r# " S S\RH                  5      r% " S S\\RD                  5      r& " S S\#5      r'g)z4Builder types for the basic control-flow constructs.    )annotationsN)
CollectionIterable	FrozenSetTupleUnionOptionalSequence)CircuitData)Register)expr)ClbitClassicalRegisterCircuitError)Instruction)CircuitInstruction)QubitQuantumRegister   )condition_resourcesnode_resourcesc                     \ rS rSrSrSr\\R                  SS j5       5       r	\R                  SS.   SS jj5       r
\R                  SS j5       r\R                      SS	 j5       r\R                  SS
 j5       r\R                  SS j5       r\R                  SS j5       r\R                  SS j5       r\R                  SS j5       r\R                  SS j5       r\R                  SS j5       rSrg)CircuitScopeInterface*   a  An interface that circuits and builder blocks explicitly fulfill, which contains the primitive
methods of circuit construction and object validation.

This allows core circuit methods to be applied to the currently open builder scope, and allows
the builders to hook into all places where circuit resources might be used.  This allows the
builders to track the resources being used, without getting in the way of
:class:`.QuantumCircuit` doing its own thing.
 c                    g)zIIndexable view onto the :class:`.CircuitInstruction`s backing this scope.Nr   selfs    \/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/controlflow/builder.pyinstructions"CircuitScopeInterface.instructions6           F_standard_gatec                   g)a  Low-level 'append' primitive; this may assume that the qubits, clbits and operation are
all valid for the circuit.

Abstraction of :meth:`.QuantumCircuit._append` (the low-level one, not the high-level).

Args:
    instruction: the resource-validated instruction context object.

Returns:
    the instruction context object actually appended.  This is not required to be the same
    as the object given (but typically will be).
Nr   )r   instructionr&   s      r    appendCircuitScopeInterface.append;   r#   r$   c                    g)z_Appends all instructions from ``data`` to the scope.

Args:
    data: The instruction listing.
Nr   )r   datas     r    extendCircuitScopeInterface.extendL   r#   r$   c                    g)a  Resolve a single bit-like classical-resource specifier.

A resource refers to either a classical bit or a register, where integers index into the
classical bits of the greater circuit.

This is called whenever a classical bit or register is being used outside the standard
:class:`.Clbit` usage of instructions in :meth:`append`, such as in a legacy two-tuple
condition.

Args:
    specifier: the classical resource specifier.

Returns:
    the resolved resource.  This cannot be an integer any more; an integer input is resolved
    into a classical bit.

Raises:
    CircuitError: if the resource cannot be used by the scope, such as an out-of-range index
        or a :class:`.Clbit` that isn't actually in the circuit.
Nr   )r   	specifiers     r    resolve_classical_resource0CircuitScopeInterface.resolve_classical_resourceT   r#   r$   c                    g)a  Add an uninitialized variable to the circuit scope.

The general circuit context is responsible for ensuring the variable is initialized.  These
uninitialized variables are guaranteed to be standalone.

Args:
    var: the variable to add, if valid.

Raises:
    CircuitError: if the variable cannot be added, such as because it invalidly shadows or
        redefines an existing name.
Nr   r   vars     r    add_uninitialized_var+CircuitScopeInterface.add_uninitialized_varm   r#   r$   c                    g)zAdd a stretch to the circuit scope.

Args:
    stretch: the stretch to add, if valid.

Raises:
    CircuitError: if the stretch cannot be added, such as because it invalidly shadows or
        redefines an existing name.
Nr   r   stretchs     r    add_stretch!CircuitScopeInterface.add_stretch|   r#   r$   c                    g)a  Called for every standalone classical real-time variable being used by some circuit
instruction.

The given variable is guaranteed to be a stand-alone variable; bit-like resource-wrapping
variables will have been filtered out and their resources given to
:meth:`resolve_classical_resource`.

Args:
    var: the variable to validate.

Raises:
    CircuitError: if the variable is not valid for this scope.
Nr   r4   s     r    use_varCircuitScopeInterface.use_var   r#   r$   c                    g)zCalled for every stretch being used by some circuit instruction.

Args:
    stretch: the stretch to validate.

Raises:
    CircuitError: if the stretch is not valid for this scope.
Nr   r9   s     r    use_stretch!CircuitScopeInterface.use_stretch   r#   r$   c                    g)a&  Get the variable (if any) in scope with the given name.

This should call up to the parent scope if in a control-flow builder scope, in case the
variable exists in an outer scope.

Args:
    name: the name of the symbol to lookup.

Returns:
    the variable if it is found, otherwise ``None``.
Nr   r   names     r    get_varCircuitScopeInterface.get_var   r#   r$   c                    g)a#  Get the stretch (if any) in scope with the given name.

This should call up to the parent scope if in a control-flow builder scope, in case the
stretch exists in an outer scope.

Args:
    name: the name of the symbol to lookup.

Returns:
    the stretch if it is found, otherwise ``None``.
Nr   rD   s     r    get_stretch!CircuitScopeInterface.get_stretch   r#   r$   c                    g)zCalled to mark that a :class:`~.circuit.Qubit` should be considered "used" by this scope,
without appending an explicit instruction.

The subclass may assume that the ``qubit`` is valid for the root scope.Nr   r   qubits     r    	use_qubitCircuitScopeInterface.use_qubit   r#   r$   N)returnzSequence[CircuitInstruction])r(   r   rP   r   r,   r   )r0   zClbit | ClassicalRegister | intrP   zClbit | ClassicalRegisterr5   zexpr.Varr:   zexpr.Stretch)rE   strrP   zOptional[expr.Var])rE   rT   rP   zOptional[expr.Stretch]rM   r   )__name__
__module____qualname____firstlineno____doc__	__slots__propertyabcabstractmethodr!   r)   r-   r1   r6   r;   r>   rA   rF   rI   rN   __static_attributes__r   r$   r    r   r   *   sY    IX  X 	AF-	   	  	8	" 0 	  		 	 	  	  	  	  	S Sr$   r   c                  R    \ rS rSr% SrSrS\S'   SrS\S'   SrS\S	'   Sr	S
\S'   Sr
g)InstructionResources   a  The quantum and classical resources used within a particular instruction.

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.

Attributes:
    qubits: A collection of qubits that will be used by the instruction.
    clbits: A collection of clbits that will be used by the instruction.
    qregs: A collection of quantum registers that are used by the instruction.
    cregs: A collection of classical registers that are used by the instruction.
r   zCollection[Qubit]qubitszCollection[Clbit]clbitszCollection[QuantumRegister]qregszCollection[ClassicalRegister]cregsN)rV   rW   rX   rY   rZ   rc   __annotations__rd   re   rf   r_   r   r$   r    ra   ra      s5     !#F" "F")+E&++-E(-r$   ra   c                  ~    \ rS rSrSrSr\R                        S	S j5       r\R                  S
S j5       r	S r
Srg)InstructionPlaceholder   a  A fake instruction that lies about its number of qubits and clbits.

These instances are used to temporarily represent control-flow instructions during the builder
process, when their lengths cannot be known until the end of the block.  This is necessary to
allow constructs like::

    with qc.for_loop(range(5)):
        qc.h(0)
        qc.measure(0, 0)
        with qc.if_test((0, 0)):
            qc.break_loop()

``qc.break_loop()`` needed to return a (mostly) functional
:obj:`~qiskit.circuit.Instruction` in order for the historical ``.InstructionSet.c_if``
to work correctly.

When appending a placeholder instruction into a circuit scope, you should create the
placeholder, and then ask it what resources it should be considered as using from the start by
calling :meth:`.InstructionPlaceholder.placeholder_instructions`.  This set will be a subset of
the final resources it asks for, but it is used for initializing resources that *must* be
supplied, such as the bits used in the conditions of placeholder ``if`` statements.

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.
Tc                    [         e)a$  Get a concrete, complete instruction that is valid to act over all the given resources.

The returned resources may not be the full width of the given resources, but will certainly
be a subset of them; this can occur if (for example) a placeholder ``if`` statement is
present, but does not itself contain any placeholder instructions.  For resource efficiency,
the returned :class:`.ControlFlowOp` will not unnecessarily span all resources, but only the
ones that it needs.

.. note::

    The caller of this function is responsible for ensuring that the inputs to this function
    are non-strict supersets of the bits returned by :meth:`placeholder_resources`.


Args:
    qubits: The qubits the created instruction should be defined across.
    clbits: The clbits the created instruction should be defined across.

Returns:
    A full version of the relevant control-flow instruction, and the resources that it uses.
    This is a "proper" instruction instance, as if it had been defined with the correct
    number of qubits and clbits from the beginning.
NotImplementedError)r   rc   rd   s      r    concrete_instruction+InstructionPlaceholder.concrete_instruction   s
    6 "!r$   c                    [         e)a  Get the qubit and clbit resources that this placeholder instruction should be considered
as using before construction.

This will likely not include *all* resources after the block has been built, but using the
output of this method ensures that all resources will pass through a
:meth:`.QuantumCircuit.append` call, even if they come from a placeholder, and consequently
will be tracked by the scope managers.

Returns:
    A collection of the quantum and classical resources this placeholder instruction will
    certainly use.
rl   r   s    r    placeholder_resources,InstructionPlaceholder.placeholder_resources  s
     "!r$   c                    [        S5      e)Nz(Cannot repeat a placeholder instruction.r   )r   ns     r    repeatInstructionPlaceholder.repeat+  s    EFFr$   r   N)rc   FrozenSet[Qubit]rd   FrozenSet[Clbit]rP   z(Tuple[Instruction, InstructionResources])rP   ra   )rV   rW   rX   rY   rZ   
_directiver]   r^   rn   rq   ru   r_   r   r$   r    ri   ri      s^    8 J"&"0@"	1" "8 	" "$Gr$   ri   c                  V   \ rS rSrSrSrSSSS.           S%S jjrS	 rS
 r\	S 5       r
\	S 5       r\S 5       rSS.     S&S jjrS'S jrS rS(S jrS)S jrS*S jrS*S jrS(S jrS)S jrS+S jrS rS rS rS rS,S jrS,S jrS-S  jrS.S! jr      S/S" jr S0S# jr!S$r"g)1ControlFlowBuilderBlocki/  a5  A lightweight scoped block for holding instructions within a control-flow builder context.

This class is designed only to be used by :obj:`.QuantumCircuit` as an internal context for
control-flow builder instructions, and in general should never be instantiated by any code other
than that.

Note that the instructions that are added to this scope may not be valid yet, so this elides
some of the type-checking of :obj:`.QuantumCircuit` until those things are known.

The general principle of the resource tracking through these builder blocks is that every
necessary resource should pass through an :meth:`.append` call, so that at the point that
:meth:`.build` is called, the scope knows all the concrete resources that it requires.  However,
the scope can also contain "placeholder" instructions, which may need extra resources filling in
from outer scopes (such as a ``break`` needing to know the width of its containing ``for``
loop).  This means that :meth:`.build` takes all the *containing* scope's resources as well.
This does not break the "all resources pass through an append" rule, because the containing
scope will only begin to build its instructions once it has received them all.

In short, :meth:`.append` adds resources, and :meth:`.build` may use only a subset of the extra
ones passed.  This ensures that all instructions know about all the resources they need, even in
the case of ``break``, but do not block any resources that they do *not* need.

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.
)_instructions	registersglobal_phase_allow_jumps_parent_built_forbidden_message_vars_local_vars_capture_stretches_local_stretches_capturer   TN)r}   allow_jumpsforbidden_messagec                   [        X5      U l        [        U5      U l        SU l        0 U l        0 U l        0 U l        0 U l        XPl	        X0l
        SU l        X`l        g)a  
Args:
    qubits: Any qubits this scope should consider itself as using from the beginning.
    clbits: Any clbits this scope should consider itself as using from the beginning.  Along
        with ``qubits``, this is useful for things such as ``if`` and ``while`` loop
        builders, where the classical condition has associated resources, and is known when
        this scope is created.
    registers: Any registers this scope should consider itself as using from the
        beginning.  This is useful for :obj:`.IfElseOp` and :obj:`.WhileLoopOp` instances
        which use a classical register as their condition.
    allow_jumps: Whether this builder scope should allow ``break`` and ``continue``
        statements within it.  This is intended to help give sensible error messages when
        dangerous behavior is encountered, such as using ``break`` inside an ``if`` context
        manager that is not within a ``for`` manager.  This can only be safe if the user is
        going to place the resulting :obj:`.QuantumCircuit` inside a :obj:`.ForLoopOp` that
        uses *exactly* the same set of resources.  We cannot verify this from within the
        builder interface (and it is too expensive to do when the ``for`` op is made), so we
        fail safe, and require the user to use the more verbose, internal form.
    parent: The scope interface of the containing scope.
    forbidden_message: If a string is given here, a :exc:`.CircuitError` will be raised on
        any attempts to append instructions to the scope with this message.  This is used by
        pseudo scopes where the state machine of the builder scopes has changed into a
        position where no instructions should be accepted, such as when inside a ``switch``
        but outside any cases.
g        FN)r   r|   setr}   r~   r   r   r   r   r   r   r   r   )r   rc   rd   parentr}   r   r   s          r    __init__ ControlFlowBuilderBlock.__init__Z  s]    F )8Y ""$'"3r$   c                @    [        U R                  R                  5      $ )z-The set of qubits associated with this scope.)r   r!   rc   r   s    r    rc   ControlFlowBuilderBlock.qubits      4$$++,,r$   c                @    [        U R                  R                  5      $ )z-The set of clbits associated with this scope.)r   r!   rd   r   s    r    rd   ControlFlowBuilderBlock.clbits  r   r$   c                    U R                   $ )a  Whether this builder scope should allow ``break`` and ``continue`` statements within it.

This is intended to help give sensible error messages when dangerous behavior is
encountered, such as using ``break`` inside an ``if`` context manager that is not within a
``for`` manager.  This can only be safe if the user is going to place the resulting
:obj:`.QuantumCircuit` inside a :obj:`.ForLoopOp` that uses *exactly* the same set of
resources.  We cannot verify this from within the builder interface (and it is too expensive
to do when the ``for`` op is made), so we fail safe, and require the user to use the more
verbose, internal form.
)r   r   s    r    r   #ControlFlowBuilderBlock.allow_jumps  s        r$   c                    U R                   $ N)r|   r   s    r    r!   $ControlFlowBuilderBlock.instructions  s    !!!r$   c                ~    SSK JnJn  SSKJnJn  XX44n[        X5      (       a  [        SU R                   S35      eg )Nr   )BreakLoopOpBreakLoopPlaceholder)ContinueLoopOpContinueLoopPlaceholderz)The current builder scope cannot take a 'z' because it is not in a loop.)	
break_loopr   r   continue_loopr   r   
isinstancer   rE   )	operationr   r   r   r   	forbiddens         r    _raise_on_jump&ControlFlowBuilderBlock._raise_on_jump  sI     	BJ `	i++;INN;K L0 0  ,r$   Fr%   c                  U R                   b  [        U R                   5      eU R                  (       d  U R                  UR                  5        UR
                   H  nU R                  R                  USS9  M     UR                   H  nU R                  R                  USS9  M     U R                  R                  U5        U$ NFstrict)r   r   r   r   r   rc   r!   	add_qubitrd   	add_clbitr|   r)   )r   r(   r&   bs       r    r)   ControlFlowBuilderBlock.append  s     "".t6677   5 56##A''%'8 $##A''%'8 $!!+.r$   c                   U R                   b  [        U R                   5      eU R                  (       d  UR                  U R                  5        UR                  5       u  p#UR                   H$  nXB;   d  M
  U R                  R                  USS9  M&     UR                   H$  nXC;   d  M
  U R                  R                  USS9  M&     U R                  R                  U5        g r   )r   r   r   
foreach_opr   active_bitsrc   r!   r   rd   r   r-   )r   r,   active_qubitsactive_clbitsr   s        r    r-   ControlFlowBuilderBlock.extend  s    "".t6677  OOD//0'+'7'7'9$A!!!++Ae+<  A!!!++Ae+<  	  &r$   c                    U R                   (       a  [        S5      eU R                  R                  U5      n[	        U[
        5      (       a  U R                  U45        U$ U R                  U5        U$ )N4Cannot add resources after the scope has been built.)r   r   r   r1   r   r   add_bitsadd_register)r   r0   resources      r    r1   2ControlFlowBuilderBlock.resolve_classical_resource  s`    ;;UVV<<::9Eh&&MM8+&  h'r$   c                   U R                   (       a  [        S5      eU R                  R                  UR                  5      =nb  [        SU SU S35      eU R
                  R                  UR                  5      =nb&  X!:X  a  [        SU S35      e[        SU SU S35      eUR                  U R                  ;   d  UR                  U R                  ;   a  [        SU SU S35      eXR
                  UR                  '   g Nr   zcannot add 'z$' as its name shadows the existing ''z!' is already present in the scope)r   r   r   getrE   r   r   r   )r   r5   previouss      r    r6   -ControlFlowBuilderBlock.add_uninitialized_var  s    ;;UVV --11#((;;HHcU2VW_V``abcc((,,SXX66HC"Qse+L#MNNcU2VW_V``abcc88t)))SXX9P9P-PcU2VW_V``abcc%("r$   c                   U R                   (       a  [        S5      eU R                  R                  UR                  5      =nb  [        SU SU S35      eU R
                  R                  UR                  5      =nb&  X!:X  a  [        SU S35      e[        SU SU S35      eUR                  U R                  ;   d  UR                  U R                  ;   a  [        SU SU S35      eXR
                  UR                  '   g r   )r   r   r   r   rE   r   r   r   )r   r:   r   s      r    r;   #ControlFlowBuilderBlock.add_stretch  s   ;;UVV ((,,W\\::HGwi'KH:UVW  --11',,??HL""Qwi/P#QRRwi'KH:UVW  <<4---AXAX1Xwi'KH:UVW  /6gll+r$   c                    XR                   ;   a  g U R                  R                  U5      =nb  U$ U R                  R	                  U5      $ r   )r   r   r   r   rF   r   rE   outs      r    rF   ControlFlowBuilderBlock.get_var  sG    (((##''--C:J||##D))r$   c                    XR                   ;   a  g U R                  R                  U5      =nb  U$ U R                  R	                  U5      $ r   )r   r   r   r   rI   r   s      r    rI   #ControlFlowBuilderBlock.get_stretch  sG    ###((,,T22C?J||''--r$   c                "   U R                   R                  UR                  5      =nb  [        SU SU S35      eU R                  R                  UR                  5      =nb  X!:X  a  g [        SU SU S35      eU R
                  R                  UR                  5      U:X  a  g U R                  R                  UR                  5      U:w  a  [        SU S35      eU R                  R                  U5        XR
                  UR                  '   g Nzcannot use 'z"' which is shadowed by the local 'r   zcannot close over 'z', which is not in scope)	r   r   rE   r   r   r   r   rF   r>   )r   r5   locals      r    r>   ControlFlowBuilderBlock.use_var
  s    **..sxx88EEcU2TUZT[[\]^^%%))#((33E@|cU2TUZT[[\]^^!!#((+s2<<)S0!4SE9QRSSS!'*388$r$   c                "   U R                   R                  UR                  5      =nb  [        SU SU S35      eU R                  R                  UR                  5      =nb  X!:X  a  g [        SU SU S35      eU R
                  R                  UR                  5      U:X  a  g U R                  R                  UR                  5      U:w  a  [        SU S35      eU R                  R                  U5        XR
                  UR                  '   g r   )	r   r   rE   r   r   r   r   rI   rA   )r   r:   r   s      r    rA   #ControlFlowBuilderBlock.use_stretch  s    %%))',,77EDgY6XY^X__`abb**..w||<<EIgY6XY^X__`abb""&&w||4?<<##GLL1W<!4WI=UVWW  )07-r$   c                8    U R                   R                  USS9  g r   )r|   r   rL   s     r    rN   !ControlFlowBuilderBlock.use_qubit&  s    $$U5$9r$   c                6    U R                   R                  5       $ )z=Iterator over the variables currently declared in this scope.)r   valuesr   s    r    iter_local_vars'ControlFlowBuilderBlock.iter_local_vars)  s    &&((r$   c                6    U R                   R                  5       $ )z=Iterator over the stretches currently declared in this scope.)r   r   r   s    r    iter_local_stretches,ControlFlowBuilderBlock.iter_local_stretches-  s    $$++--r$   c                6    U R                   R                  5       $ )z=Iterator over the variables currently captured in this scope.)r   r   r   s    r    iter_captured_vars*ControlFlowBuilderBlock.iter_captured_vars1  s    !!((**r$   c                6    U R                   R                  5       $ )z=Iterator over the stretches currently captured in this scope.)r   r   r   s    r    iter_captured_stretches/ControlFlowBuilderBlock.iter_captured_stretches5  s    &&--//r$   c                X    U R                   (       d  [        S5      eU R                   S   $ )zAGet the value of the most recent instruction tuple in this scope.$This scope contains no instructions.)r|   r   r   s    r    peekControlFlowBuilderBlock.peek9  s(    !!EFF!!"%%r$   c                n    U R                   (       d  [        S5      eU R                   R                  5       $ )z[Get the value of the most recent instruction in this scope, and remove it from this
object.r   )r|   r   popr   s    r    r   ControlFlowBuilderBlock.pop?  s.     !!EFF!!%%''r$   c                    U Hr  n[        U[        5      (       a  U R                  R                  USS9  M4  [        U[        5      (       a  U R                  R                  USS9  Me  [        SU S35      e   g)a?  Add extra bits to this scope that are not associated with any concrete instruction yet.

This is useful for expanding a scope's resource width when it may contain ``break`` or
``continue`` statements, or when its width needs to be expanded to match another scope's
width (as in the case of :obj:`.IfElseOp`).

Args:
    bits: The qubits and clbits that should be added to a scope.  It is not an error if
        there are duplicates, either within the iterable or with the bits currently in
        scope.

Raises:
    TypeError: if the provided bit is of an incorrect type.
Fr   z5Can only add qubits or classical bits, but received 'z'.N)r   r   r!   r   r   r   	TypeError)r   bitsbits      r    r    ControlFlowBuilderBlock.add_bitsF  sr     C#u%%!!++C+>C''!!++C+>"WX[W\\^ _`` r$   c                |    XR                   ;   a  gU R                   R                  U5        U R                  U5        g)zAdd a :obj:`.Register` to the set of resources used by this block, ensuring that
all bits contained within are also accounted for.

Args:
    register: the register to add to the block.
N)r}   addr   )r   registers     r    r   $ControlFlowBuilderBlock.add_register]  s/     ~~%8$hr$   c           	       ^ ^^^	^
^^^ SSK JnJm	  ST l        T R                  b  [        S5      e[        T5      T R                  5       -
  m[        T5      T R                  5       -
  mU" T R                  R                  T R                  R                  /T R                  Q7T R                  [        R                  " T R                  R                  5       T R                   R                  5       5      S.6m
T R"                  R                  5        H  nT
R%                  U5        M     T R&                  R                  5        H  nT
R)                  U5        M     0 mU	UUU
UUUU 4S jnT R                  R+                  U5        T R                  R-                  5       nUR/                  T
R                  T
R                  5        TR1                  5        H	  u  pxXU'   M     T
R3                  5       R5                  U5        T
$ )ab  Build this scoped block into a complete :obj:`.QuantumCircuit` instance.

This will build a circuit which contains all of the necessary qubits and clbits and no
others.

The ``qubits`` and ``clbits`` arguments should be sets that contains all the resources in
the outer scope; these will be passed down to inner placeholder instructions, so they can
apply themselves across the whole scope should they need to.  The resulting
:obj:`.QuantumCircuit` will be defined over a (nonstrict) subset of these resources.  This
is used to let ``break`` and ``continue`` span all resources, even if they are nested within
several :obj:`.IfElsePlaceholder` objects, without requiring :obj:`.IfElsePlaceholder`
objects *without* any ``break`` or ``continue`` statements to be full-width.

Args:
    all_qubits: all the qubits in the containing scope of this block.  The block may expand
        to use some or all of these qubits, but will never gain qubits that are not in this
        set.
    all_clbits: all the clbits in the containing scope of this block.  The block may expand
        to use some or all of these clbits, but will never gain clbits that are not in this
        set.

Returns:
    A circuit containing concrete versions of all the instructions that were in the scope,
    and using the minimal set of resources necessary to support them, within the enclosing
    scope.
r   )QuantumCircuitSwitchCaseOpTz<Cannot build a forbidden scope. Please report this as a bug.)r~   capturesc                  > [        U[        5      (       GaC  UR                  TT5      u  p[        UR                  5      n[        UR
                  5      n[        XU5      TU '   T(       aA  U(       a:  TR                  U5      nU(       a"  TR                  U5        TR                  U5        T(       aA  U(       a:  TR                  U5      nU(       a"  TR                  U5        TR                  U5        [        R                  " UR                  UR                  5       H7  nUTR                  ;  d  M  TR                  U5        TR                  U5        M9     [!        USS 5      b[  [#        UR$                  5      R                   H7  nUTR                  ;  d  M  TR                  U5        TR                  U5        M9     g [        UT
5      (       a  UR&                  n[        U[(        5      (       a  Sn	O.[        U[*        5      (       a  U4n	O[-        U5      R                  n	U	 H7  nUTR                  ;  d  M  TR                  U5        TR                  U5        M9     g g )N
_conditionr   )r   ri   rn   tuplerc   rd   r   intersectiondifference_updater   	itertoolschainre   rf   r}   r   getattrr   r   targetr   r   r   )indexop	resourcesrc   rd   
add_qubits
add_clbitsr   r   target_registersr   
all_clbits
all_qubitsr   placeholder_to_concretepotential_clbitspotential_qubitsr   s             r    update_registers7ControlFlowBuilderBlock.build.<locals>.update_registers  s   "455 " 7 7
J Oy//0y//01CBPV1W'. $!1!>!>v!FJ!(:::FZ0#!1!>!>v!FJ!(:::FZ0 )	 QHt~~5 ))(3((2 !R r<.: 3BMM B H HHt~~5))(3((2 !I B--fe,,')$(9::(.y$'5f'='C'C$ 0Ht~~5))(3((2 !1 .r$   )qiskit.circuitr   r   r   r   RuntimeErrorr   rc   rd   r|   r}   r~   r   r   r   r   r   r   r6   r   r;   foreach_op_indexedcopyreplace_bitsitems_current_scoper-   )r   r   r   r   r5   r  out_datair(   r   r   r  r  r  s   ```      @@@@@r    buildControlFlowBuilderBlock.buildj  s   < 	@
 "".]^^z?T[[]:z?T[[]: %%%%
 ^^
 **__T%7%7%>%>%@$BYBYB`B`Bbc
 ##**,C %%c* -
 ((//1COOC  2 #%(	3 (	3V 	--.>? %%**,cjj#**55;;=NA%QK > 	##H-
r$   c                H   [        U 5      R                  [        U 5      5      nU R                  R                  5       Ul        U R                  R                  5       Ul        U R
                  Ul        U R                  R                  5       Ul        U R                  R                  5       Ul        U R                  R                  5       Ul        U R                  R                  5       Ul	        U R                  Ul
        U R                  Ul        U R                  Ul        U$ )a  Return a semi-shallow copy of this builder block.

The instruction lists and sets of qubits and clbits will be new instances (so mutations will
not propagate), but any :obj:`.Instruction` instances within them will not be copied.

Returns:
    a semi-shallow copy of this object.
)type__new__r|   r	  r}   r~   r   r   r   r   r   r   r   )r   r   s     r    r	  ControlFlowBuilderBlock.copy  s     4j  d, ..335++-,,**//1 ..335#4499;!%!8!8!=!=!?ll,,!%!8!8
r$   )r   r   r   r|   r   r   r   r   r   r~   r}   )rc   zIterable[Qubit]rd   zIterable[Clbit]r   r   r}   zIterable[Register]r   boolr   zOptional[str])r(   r   r&   r  rP   r   rQ   rR   rS   )rE   rT   rU   )rP   r   )r   zIterable[Union[Qubit, Clbit]])r   r   )r   rw   r   rx   rP   z'qiskit.circuit.QuantumCircuit')rP   z'ControlFlowBuilderBlock')#rV   rW   rX   rY   rZ   r[   r   rc   rd   r\   r   r!   staticmethodr   r)   r-   r1   r6   r;   rF   rI   r>   rA   rN   r   r   r   r   r   r   r   r   r  r	  r_   r   r$   r    r{   r{   /  s@   8I( )+ +/-4-4  -4
 &-4 &-4 -4 )-4^-- ! ! " " 
 
 JO-BF	'	)6**.+8:).+0&(a. u*u8Hu	(unr$   r{   )(rZ   
__future__r   r]   r   typingr   r   r   r   r   r	   r
   qiskit._accelerate.circuitr   r  r   qiskit.circuit.classicalr   r   r   qiskit.circuit.exceptionsr   qiskit.circuit.instructionr   !qiskit.circuit.quantumcircuitdatar   r   r   _builder_utilsr   r   TYPE_CHECKINGqiskitABCr   
NamedTuplera   ri   r{   r   r$   r    <module>r#     s    ; # 
   T T T 2 # ) 3 2 2 @ 1 ?	ZSCGG ZSz.6,, .*PG[#'' PGfF3 Fr$   