
    z	iXV                       S r SSKJr  SSKJrJrJrJr  SSK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Jr  S	SKJr  S	SKJrJrJrJr  \(       a  SSK	Jr  Sr " S S\5      r " S S\5      r  " S S5      r! " S S5      r"g)z8Circuit operation representing an ``if/else`` statement.    )annotations)OptionalUnionIterableTYPE_CHECKINGN)ClassicalRegisterClbit)expr)InstructionSet)CircuitError   )ControlFlowBuilderBlockInstructionPlaceholderInstructionResources)ControlFlowOp)partition_registersunify_circuit_resourcesvalidate_conditioncondition_resourcesQuantumCircuit)IfElseOpc                     ^  \ rS rSrSr  S       SU 4S jjjr\S 5       r\S 5       r\R                  S 5       r\R                  S 5       r\S 5       r
SS	 jrS
rU =r$ )r   *   a  A circuit operation which executes a program (``true_body``) if a
provided condition (``condition``) evaluates to true, and
optionally evaluates another program (``false_body``) otherwise.

If provided, ``false_body`` must be of the same ``num_qubits`` and
``num_clbits`` as ``true_body``.

The classical bits used in ``condition`` must be a subset of those attached
to the circuit on which this ``IfElseOp`` will be appended.
c                   > SSK Jn  [        X%5      (       d  [        S[	        U5       S35      eUR
                  nUR                  n[        TU ]!  SXgX#/US9  [        U5      U l
        g)ae  
Args:
    condition: A condition to be evaluated in real time during circuit execution which,
        if true, will trigger the evaluation of ``true_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``.
    true_body: A program to be executed if ``condition`` evaluates
        to true.
    false_body: A optional program to be executed if ``condition``
        evaluates to false.
    label: An optional label for identifying the instruction.
r   r   LIfElseOp expects a true_body parameter of type QuantumCircuit, but received .if_elselabelN)qiskit.circuitr   
isinstancer   type
num_qubits
num_clbitssuper__init__r   
_condition)	self	condition	true_body
false_bodyr    r   r$   r%   	__class__s	           \/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/controlflow/if_else.pyr'   IfElseOp.__init__6   sw    * 	2 )4488<Y7HK 
 ))
))
JY<S[`a,Y7    c                    U R                   $ N)_paramsr)   s    r.   paramsIfElseOp.params\   s    ||r0   c                    U R                   $ )z(The condition for the if else operation.r(   r4   s    r.   r*   IfElseOp.condition`        r0   c                    Xl         g r2   r8   )r)   values     r.   r*   r9   e   s    r0   c                   SSK Jn  Uu  p4[        X25      (       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b  [        XB5      (       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X4/U l        g )
Nr   r   r   r   zAttempted to assign a true_body parameter with a num_qubits or num_clbits different than that of the IfElseOp. IfElseOp num_qubits/clbits: /z" Supplied body num_qubits/clbits: zMIfElseOp expects a false_body parameter of type QuantumCircuit, but received zAttempted to assign a false_body parameter with a num_qubits or num_clbits different than that of the IfElseOp. IfElseOp num_qubits/clbits: )r!   r   r"   r   r#   r$   r%   r3   )r)   
parametersr   r+   r,   s        r.   r5   r6   i   sn    	2 *	)44004Y/@C 
 4??2i6J6Jdoo6]//3.?q@Q R44=4H4H3I9K_K_J``ac  !j99"4484D3EQH 
 $$7:;P;PTXTcTc;c"337??2C1T__DU V88B8M8M7NaPZPePeOffgi  ".r0   c                ~    U R                   S   c  U R                   S   4$ U R                   S   U R                   S   4$ )Nr   r   )r5   r4   s    r.   blocksIfElseOp.blocks   s<    ;;q>!KKN$$KKNDKKN33r0   c                    S [         R                  " U[        S5      SS9 5       u  p#[        U R                  X#U R
                  S9$ )zReplace blocks and return new instruction.

Args:
    blocks: Iterable of circuits for "if" and "else" condition. If there is no "else"
        circuit it may be set to None or omitted.

Returns:
    New IfElseOp with replaced blocks.
c              3  *   #    U  H	  u  pUv   M     g 7fr2    ).0ablock_s      r.   	<genexpr>*IfElseOp.replace_blocks.<locals>.<genexpr>   s      !
$[yvF$[s      N)	fillvalue)r,   r    )	itertoolszip_longestranger   r(   r    )r)   rA   r+   r,   s       r.   replace_blocksIfElseOp.replace_blocks   sD    !
$-$9$9&%(VZ$[!
	 QUQ[Q[\\r0   )r(   r3   )NN)r*   =tuple[ClassicalRegister, int] | tuple[Clbit, int] | expr.Exprr+   r   r,   zQuantumCircuit | Noner    
str | None)rA   zIterable[QuantumCircuit]returnz
'IfElseOp')__name__
__module____qualname____firstlineno____doc__r'   propertyr5   r*   setterrA   rP   __static_attributes____classcell__r-   s   @r.   r   r   *   s    	 -1 $8P$8 "$8 *	$8
 $8 $8L         ]]#/ #/J 4 4] ]r0   r   c                  r   ^  \ rS rSrSr SSS.       SU 4S jjjjrSS jrS rSS jrS	 r	S
 r
SrU =r$ )IfElsePlaceholder   a  A placeholder instruction to use in control-flow context managers, when calculating the
number of resources this instruction should block is deferred until the construction of the
outer loop.

This generally should not be instantiated manually; only :obj:`.IfContext` and
:obj:`.ElseContext` should do it when they need to defer creation of the concrete instruction.

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.
Nr   c                  > X l         X0l        U R                  5       U l        [        TU ]  S[        U R                  R                  5      [        U R                  R                  5      / US9  [        U5      U l
        g)a  
Args:
    condition: the condition to execute the true block on.  This has the same semantics as
        the ``condition`` argument to :obj:`.IfElseOp`.
    true_block: the unbuilt scope block that will become the "true" branch at creation time.
    false_block: if given, the unbuilt scope block that will become the "false" branch at
        creation time.
    label: the label to give the operator when it is created.
r   r   N)_IfElsePlaceholder__true_block_IfElsePlaceholder__false_block _calculate_placeholder_resources_IfElsePlaceholder__resourcesr&   r'   lenqubitsclbitsr   r(   )r)   r*   
true_blockfalse_blockr    r-   s        r.   r'   IfElsePlaceholder.__init__   sq    $ '@K@@Bs4++223S9I9I9P9P5QSU]b 	 	
 -Y7r0   c                   U R                   b  [        SU R                    35      eU R                  R                  5       nUR	                  5       UR                  5       -  nUR	                  5       UR                  5       -  nUR                  XC-
  5        UR                  X4-
  5        [        U 5      " U R                  X!U R                  S9$ )a4  Return a new placeholder instruction, with the false block set to the given value,
updating the bits used by both it and the true body, if necessary.

It is an error to try and set the false block on a placeholder that already has one.

Args:
    false_block: The (unbuilt) instruction scope to set the false body to.

Returns:
    A new placeholder, with ``false_block`` set to the given input, and both true and false
    blocks expanded to account for all resources.

Raises:
    CircuitError: if the false block of this placeholder instruction is already set.
zfalse block is already set to r   )
rd   r   rc   copyrh   ri   add_bitsr#   r(   r    )r)   rk   rj   	true_bits
false_bitss        r.   with_false_block"IfElsePlaceholder.with_false_block   s      )!?@R@R?STUU&&++-
%%'**;*;*==	 '')K,>,>,@@
J23Y34Dz$//:$**UUr0   c                    U R                   c$  U R                  R                  R                  5       $ U R                  R                  U R                   R                  -  $ )z.Get the registers used by the interior blocks.)rd   rc   	registersrn   r4   s    r.   ru   IfElsePlaceholder.registers   sL    %$$..3355  **T-?-?-I-IIIr0   c           	        U R                   c  [        U R                  R                  5      u  p[	        [        U R                  R                  5       5      [        U R                  R                  5       5      [        U5      [        U5      S9$ [        U R                  R                  5      u  p4[        U R                   R                  5      u  pV[	        [        U R                  R                  5       U R                   R                  5       -  5      [        U R                  R                  5       U R                   R                  5       -  5      [        U5      [        U5      -   [        U5      [        U5      -   S9$ )a#  Get the placeholder resources (see :meth:`.placeholder_resources`).

This is a separate function because we use the resources during the initialization to
determine how we should set our ``num_qubits`` and ``num_clbits``, so we implement the
public version as a cache access for efficiency.
rh   ri   qregscregs)rd   r   rc   ru   r   tuplerh   ri   )r)   ry   rz   
true_qregs
true_cregsfalse_qregsfalse_cregss          r.   re   2IfElsePlaceholder._calculate_placeholder_resources   s,    %.t/@/@/J/JKLE'T..5578T..5578ElEl	  "5T5F5F5P5P!Q
#6t7I7I7S7S#T #**113d6H6H6O6O6QQR**113d6H6H6O6O6QQR
#eK&88
#eK&88	
 	
r0   c                    U R                   $ r2   )rf   r4   s    r.   placeholder_resources'IfElsePlaceholder.placeholder_resources  s    r0   c           	        U R                   R                  5       nU R                   R                  5       nU R                  b8  X0R                  R                  5       -  nX@R                  R                  5       -  nX-  nX4-  nXe-
  (       a  [	        SXe-
  < 35      eU R                   R                  X5      nU R                  c  S nO'[        XpR                  R                  X5      45      u  px[        U R                  XxU R                  S9[        [        UR                  5      [        UR                  5      [        UR                  5      [        UR                  5      S94$ )Nz<This block contains bits that are not in the operands sets: r   rx   )rc   rh   ri   rd   r   buildr   r   r(   r    r   r{   ry   rz   )	r)   rh   ri   current_qubitscurrent_clbitsall_bitscurrent_bitsr+   r,   s	            r.   concrete_instruction&IfElsePlaceholder.concrete_instruction  sF   **113**113)+.@.@.G.G.IIN+.@.@.G.G.IIN?%6"  +.0  %%++F;	%J
 %<..44VDE%!I T__i4::N Y--.Y--.IOO,IOO,	
 	
r0   )__false_block__resources__true_blockr(   r2   )r*   rR   rj   r   rk   zControlFlowBuilderBlock | Noner    zOptional[str])rk   r   rT   z'IfElsePlaceholder')rT   r   )rU   rV   rW   rX   rY   r'   rr   ru   re   r   r   r\   r]   r^   s   @r.   r`   r`      sg    " 7;	8  $8P8 ,8 4	8 8 86V2J
0 !
 !
r0   r`   c                      \ rS rSrSrSrSS.       SS jjr\SS j5       r\SS j5       r	\SS	 j5       r
\SS
 j5       rS rS rSrg)	IfContexti/  a  A context manager for building up ``if`` statements onto circuits in a natural order, without
having to construct the statement body first.

The return value of this context manager can be used immediately following the block to create
an attached ``else`` statement.

This context should almost invariably be created by a :meth:`.QuantumCircuit.if_test` 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:`.IfElseOp`).

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.
)_appended_instructions_circuitr(   _in_loop_labelNr   c               V    Xl         [        U5      U l        X@l        S U l        X0l        g r2   )r   r   r(   r   r   r   )r)   circuitr*   in_loopr    s        r.   r'   IfContext.__init__C  s'      ,Y7&*#r0   c                    U R                   $ )z9Get the circuit that this context manager is attached to.)r   r4   s    r.   r   IfContext.circuitU       }}r0   c                    U R                   $ )z9Get the expression that this statement is conditioned on.r8   r4   s    r.   r*   IfContext.conditionZ  r:   r0   c                    U R                   $ )zGet the instruction set that was created when this block finished.  If the block has not
yet finished, then this will be ``None``.)r   r4   s    r.   appended_instructionsIfContext.appended_instructions_  s     ***r0   c                    U R                   $ )z7Whether this context manager is enclosed within a loop.)r   r4   s    r.   r   IfContext.in_loope  r   r0   c                    [        U R                  5      nU R                  R                  UR                  UR
                  U R                  S9  [        U 5      $ )N)ri   ru   allow_jumps)r   r(   r   _push_scoperi   rz   r   ElseContext)r)   	resourcess     r.   	__enter__IfContext.__enter__j  sK    '8	!!##oo 	" 	

 4  r0   c           	        Ub  U R                   R                  5         gU R                   R                  5       nU R                  (       ad  [        U R                  X@R
                  S9nUR                  5       nU R                   R                  XVR                  UR                  5      U l
        gUR                  UR                  5       UR                  5       5      nU R                   R                  [        U R                  US U R
                  S9[        UR                  5      [        UR                  5      5      U l
        g)NFr   )r+   r,   r    )r   
_pop_scoper   r`   r(   r   r   appendrh   ri   r   r   r   r{   )r)   exc_typeexc_valexc_tbrj   	operationr   r+   s           r.   __exit__IfContext.__exit__s  s     MM$$&]]--/
==
 *$//:[[YI!779I*.--*>*>++Y-=-=+D'  #(():):)<j>O>O>QRI*.--*>*>I$VZVaVabi&&'i&&'+D'
 r0   )r   r   r*   rR   r   boolr    rS   )rT   r   )rT   rR   )rT   zUnion[InstructionSet, None])rT   r   )rU   rV   rW   rX   rY   	__slots__r'   rZ   r   r*   r   r   r   r   r\   rE   r0   r.   r   r   /  s    " [I !   Q 
    $     + +
  !r0   r   c                  2    \ rS rSrSrSrS	S jrS rS rSr	g)
r   i  a  A context manager for building up an ``else`` statements onto circuits in a natural order,
without having to construct the statement body first.

Instances of this context manager should only ever be gained as the output of the
:obj:`.IfContext` manager, so they know what they refer to.  Instances of this context are
"friends" of the circuit that created the :obj:`.IfContext` that in turn created this object.
The context will manipulate the circuit's defined scopes when it is entered (by popping the old
:obj:`.IfElseOp` if it exists and pushing a new scope onto the stack) and exited (by popping its
scope, building it, and appending the resulting :obj:`.IfElseOp`).

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.
_if_instruction_if_registers_if_context_usedc                :    S U l         S U l        Xl        SU l        g )NFr   )r)   
if_contexts     r.   r'   ElseContext.__init__  s!      $!%
r0   c                   U R                   (       a  [        S5      eSU l         U R                  R                  nU R                  R                  nUc  [        S5      e[        U5      S:w  a  [        S5      eUS   nUR                  5       nUR                  UR                  La  [        SU< SU< S	35      eUR                  5       U l	        [        U R                  R                  [        5      (       av  [        U R                  R                  R                  S   R                  5      R                  U R                  R                  R                  S   R                   5      U l        O)U R                  R                  R%                  5       U l        UR'                  U R                  R(                  U R                  R*                  U R"                  U R                  R,                  S
9  g )Nz Cannot re-use an 'else' context.Tz;Cannot attach an 'else' branch to an incomplete 'if' block.r   z4Cannot attach an 'else' to a broadcasted 'if' block.r   zTThe 'if' block is not the most recent instruction in the circuit. Expected to find: z, but instead found: r   )ru   r   )r   r   r   r   r   rg   #_peek_previous_instruction_in_scoper   "_pop_previous_instruction_in_scoper   r"   r   setrA   rz   unionry   r   ru   r   rh   ri   r   )r)   r   r   appendedinstructions        r.   r   ElseContext.__enter__  s   ::ABB
 $ 0 0 F F""** (\]]$%*UVV(+AAC[%:%::&&.\1F{oUVX   'IIKd**44h??!$T%9%9%C%C%J%J1%M%S%S!T!Z!Z$$..55a8>>"D "&!5!5!?!?!I!I!KD  ''  ''((((00	 	 	
r0   c           	     n   U R                   R                  nUb3  UR                  5         UR                  U R                  5        SU l        gUR                  5       n[        U R                  R                  [        5      (       a\  U R                  R                  R                  U5      nUR                  5       nUR                  XgR                  UR                  5        gU R                  R                  R                  S   nUR                  UR                  5       UR                  5       5      n	[!        X45      u  pUR                  [#        U R                   R$                  UU	U R                  R                  R&                  S9[)        UR                  5      [)        UR                  5      5        g)NFr   r   )r   r   r   _appendr   r   r"   r   r`   rr   r   r   rh   ri   rA   r   r   r   r*   r    r{   )
r)   r   r   r   r   rk   if_operationr   r+   r,   s
             r.   r   ElseContext.__exit__  si   ""**
  OOD001DJ((* d**446GHH//99JJ;WL$::<INN<)9)99;K;KL(  ,,66==a@I$**;+=+=+?ASASAUVJ$;Y<S$T!INN$$....88>>	 i&&'i&&'	 r0   )r   r   r   r   N)r   r   )
rU   rV   rW   rX   rY   r   r'   r   r   r\   rE   r0   r.   r   r     s      MI
@&r0   r   )#rY   
__future__r   typingr   r   r   r   rM   r!   r   r	   qiskit.circuit.classicalr
   qiskit.circuit.instructionsetr   qiskit.circuit.exceptionsr   builderr   r   r   control_flowr   _builder_utilsr   r   r   r   r   __all__r   r`   r   r   rE   r0   r.   <module>r      s~    ? " ; ;  3 ) 8 2 Z Z '  - z]} z]zE
. E
P^ ^Bb br0   