
    z	ilD                    8   S r SSKJr  SrSSKrSSKJrJrJrJ	r	J
r
JrJrJr  SSKJrJr  SSKJr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  \(       a  SSKJ r    " S S5      r!\!" 5       r" " S S\5      r# " S S\5      r$ " S S5      r% " S S5      r&g)z<Circuit operation representing an ``switch/case`` statement.    )annotations)SwitchCaseOpCASE_DEFAULTN)UnionIterableAnyTupleOptionalListLiteralTYPE_CHECKING)ClassicalRegisterClbit)exprtypes)CircuitError   )InstructionPlaceholderInstructionResourcesControlFlowBuilderBlock)ControlFlowOp)unify_circuit_resourcespartition_registersnode_resourcesQuantumCircuitc                      \ rS rSrSrS rSrg)_DefaultCaseType"   aY  A special object that represents the "default" case of a switch statement.  If you use this
as a case target, it must be the last case, and will match anything that wasn't already matched.
When using the builder interface of :meth:`.QuantumCircuit.switch`, this can also be accessed as
the ``DEFAULT`` attribute of the bound case-builder object.c                    g)Nz<default case> selfs    `/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/controlflow/switch_case.py__repr___DefaultCaseType.__repr__+   s        r!   N)__name__
__module____qualname____firstlineno____doc__r%   __static_attributes__r!   r'   r$   r   r   "   s    C
 r'   r   c                  x   ^  \ rS rSrSrSS.     SU 4S jjjrU 4S jrSS jrS r\	S	 5       r
SS
 jrSrU =r$ )r   2   zA circuit operation that executes one particular circuit block based on matching a given
``target`` against an ordered list of ``values``.  The special value :data:`.CASE_DEFAULT` can
be used to represent a default condition.
Nlabelc               l  > SSK Jn  [        U[        R                  5      (       aR  UR
                  R                  [        R                  [        R                  4;  a  [        SUR
                   S35      eO&[        U[        [        45      (       d  [        S5      e[        U[        R                  5      (       a@  UR
                  R                  [        R                  L a  SOUR
                  R                  nO"[        U[        5      (       a  SO
[        U5      nSU-  S-
  n[        5       nSu  pXl        0 U l         / U l         / U l         [)        U5       GH  u  n
u  p[        U[*        [,        45      (       a  [+        U5      OU4nU H  nXR"                  ;   a  [        SU 35      e[.        U R"                  ;   a  [        S	5      eU[.        LaC  [        U[0        5      (       a  US:  a  [        S
5      eX:  a  [        SU SU SU SU S3	5      eXR"                  U'   M     U R$                  R3                  U5        [        X5      (       d  [        S5      e[5        U5      U;   a  [        S5      eUR7                  [5        U5      5        Uc  UR8                  UR:                  pUR8                  U:w  d  UR:                  U	:w  a  [        S5      eU R&                  R3                  U5        GM     U R&                  (       d  [        S5      e[<        TU ]}  SXU R&                  US9  g)a  
Args:
    target: the real-time value to switch on.
    cases: an ordered iterable of the corresponding value of the ``target`` and the circuit
        block that should be executed if this is matched.  There is no fall-through between
        blocks, and the order matters.
r   r   zNthe switch target must be an expression with type 'Uint(n)' or 'Bool()', not ''z5the switch target must be a classical bit or registerr   )NNzduplicate case value z'cases after the default are unreachablez5case values must be Booleans or non-negative integerszswitch target 'z' has z bit(s) of precision, but case z is larger than the maximum of .z,case blocks must be QuantumCircuit instancesz.ungrouped cases cannot point to the same blockNzincompatible bits between casesz"must have at least one case to runswitch_caser0   ) qiskit.circuitr   
isinstancer   Exprtypekindr   UintBoolr   r   r   widthlensettarget	_case_map_label_spec_params	enumeratetuplelistr   intappendidadd
num_qubits
num_clbitssuper__init__)r#   r@   casesr1   r   target_bits
target_maxcase_idsrK   rL   i
value_speccase_valuesvalue	__class__s                  r$   rN   SwitchCaseOp.__init__8   s    	2fdii(({{

EJJ'??"#[[M,  @
 FU,=$>??VWWfdii((%{{//5::=!6;;CTCTK)&%88!c&kK;&!+
5!+
	& QS	 	S '0&6"A"
*4Z%*O*OU:&V`UbFNN*&)>ug'FGG4>>1&'PQQ,%eS11UQY*+bcc)*-fXVK= I))./NzlZ[]  )*u%   ##F+e44"#QRR%yH$"#STTLLE#!).)9)95;K;KJ:-1A1AZ1O"#DEELL&5 '76 ||CDD
TYZr'   c                   > [         TU ]  U5      =(       aP    U R                  UR                  :H  =(       a0    [        S [	        U R
                  UR
                  5       5       5      $ )Nc              3  T   #    U  H  u  p[        U5      [        U5      :H  v   M      g 7fN)r?   ).0labels_selflabels_others      r$   	<genexpr>&SwitchCaseOp.__eq__.<locals>.<genexpr>   s)      1Y-K K C$551Ys   &()rM   __eq__r@   allziprB   )r#   otherrX   s     r$   rb   SwitchCaseOp.__eq__   sX     GN5! u||+ 14T5E5EuGXGX1Y 	
r'   c                B    [        U R                  U R                  5      $ )a  Return an iterable where each element is a 2-tuple whose first element is a tuple of
jump values, and whose second is the single circuit block that is associated with those
values.

This is an abstract specification of the jump table suitable for creating new
:class:`.SwitchCaseOp` instances.

.. seealso::
    :meth:`.SwitchCaseOp.cases`
        Create a lookup table that you can use for your own purposes to jump from values to
        the circuit that would be executed.)rd   rB   rC   r"   s    r$   cases_specifierSwitchCaseOp.cases_specifier   s     4##T\\22r'   c                    U R                   R                  5        VVs0 s H  u  pXR                  U   _M     snn$ s  snnf )a  Return a lookup table from case labels to the circuit that would be executed in that
case.  This object is not generally suitable for creating a new :class:`.SwitchCaseOp`
because any keys that point to the same object will not be grouped.

.. seealso::
    :meth:`.SwitchCaseOp.cases_specifier`
        An alternate method that produces its output in a suitable format for creating new
        :class:`.SwitchCaseOp` instances.
)rA   itemsrC   )r#   keyindexs      r$   rO   SwitchCaseOp.cases   s:     <@>>;O;O;QR;QZS\\%((;QRRRs   =c                ,    [        U R                  5      $ r\   )rE   rC   r"   s    r$   blocksSwitchCaseOp.blocks   s    T\\""r'   c                
   [        U5      n[        U5      [        U R                  5      :w  a-  [        S[        U R                  5       S[        U5       35      e[        U R                  [        U R                  U5      5      $ )Nzneeded z blocks but received )	rE   r>   rC   r   rA   r   r@   rd   rB   )r#   rp   s     r$   replace_blocksSwitchCaseOp.replace_blocks   si    vv;#dll++T^^)<(==RSVW]S^R_`aaDKKT-=-=v)FGGr'   )rA   rB   rC   r@   )r@   %Clbit | ClassicalRegister | expr.ExprrO   z$Iterable[Tuple[Any, QuantumCircuit]]r1   Optional[str])returnz&Iterable[Tuple[Tuple, QuantumCircuit]])rp   zIterable[QuantumCircuit]rw   z'SwitchCaseOp')r(   r)   r*   r+   r,   rN   rb   rh   rO   propertyrp   rs   r-   __classcell__rX   s   @r$   r   r   2   sn      $O[5O[ 4O[
 O[ O[b

3
S # #H Hr'   r   c                  T   ^  \ rS rSrSrSS.     S
U 4S jjjrS rS rS rS	r	U =r
$ )SwitchCasePlaceholder   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:`.SwitchContext` should do it when
it needs 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.
Nr0   c                  > Xl         X l        U R                  5       U l        [        TU ]  S[        U R                  R                  5      [        U R                  R                  5      / US9  g )Nr5   r0   )	_SwitchCasePlaceholder__target_SwitchCasePlaceholder__cases _calculate_placeholder_resources!_SwitchCasePlaceholder__resourcesrM   rN   r>   qubitsclbits)r#   r@   rO   r1   rX   s       r$   rN   SwitchCasePlaceholder.__init__   sc     @@B  ''(  ''( 	 	
r'   c                   [        5       n[        5       n[        5       n[        5       n[        U R                  [        5      (       a  UR	                  U R                  5        O[        U R                  [
        5      (       a7  UR                  U R                  5        UR	                  U R                  5        OK[        U R                  5      nUR                  UR                  5        UR                  UR                  5        U R                   HH  u  pgXR                  5       -  nX'R                  5       -  n[        UR                  5      u  pX8-  nXI-  nMJ     [        [        U5      [        U5      [        U5      [        U5      S9$ )Nr   r   qregscregs)r?   r7   r   r   rJ   r   updater   r   r   r   r   r   	registersr   rE   )
r#   r   r   r   r   	resources_body
body_qregs
body_cregss
             r$   r   6SwitchCasePlaceholder._calculate_placeholder_resources   s   dmmU++JJt}}%'899MM$--(IIdmm$&t}}5IMM)**+LL)||GAkkm#Fkkm#F%8%H"JEE $ $==,,	
 	
r'   c                    U R                   $ r\   )r   r"   s    r$   placeholder_resources+SwitchCasePlaceholder.placeholder_resources   s    r'   c           	       ^^ [        U R                  [        UU4S jU R                   5       5      5       VVVs/ s H  u  u  p4nX54PM     nnnnU(       aq  [        [	        US   S   R
                  5      [	        US   S   R                  5      [	        US   S   R                  5      [	        US   S   R                  5      S9nOU R                  n[        U R                  X`R                  S9U4$ s  snnnf )Nc              3  L   >#    U  H  u  pUR                  TT5      v   M     g 7fr\   )build)r]   r   r   r   r   s      r$   r`   =SwitchCasePlaceholder.concrete_instruction.<locals>.<genexpr>   s#     '_R^wq

66(B(BR^s   !$r   r   r   r0   )rd   r   r   r   rE   r   r   r   r   r   r   r   r1   )r#   r   r   labelsr   unified_bodyrO   r   s    ``     r$   concrete_instruction*SwitchCasePlaceholder.concrete_instruction   s     .1''_RVR^R^'__.
.)\ ". 	 
 ,U1Xa[//0U1Xa[//0E!HQK--.E!HQK--.	I ((IZZ@
 	
!
s   C5)__cases__resources__target)r@   ru   rO   z)List[Tuple[Any, ControlFlowBuilderBlock]]r1   rv   )r(   r)   r*   r+   r,   rN   r   r   r   r-   ry   rz   s   @r$   r|   r|      sG    $  $
5
 9

 
 
$
6 
 
r'   r|   c                  \    \ rS rSrSrSS.       SS jjrS r    SS jrS rS	 r	S
r
g)SwitchContexti  aS  A context manager for building up ``switch`` statements onto circuits in a natural order,
without having to construct the case bodies first.

The return value of this context manager can be used within the created context to build up the
individual ``case`` statements.  No other instructions should be appended to the circuit during
the `switch` context.

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

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.
Nr0   c               n   Xl         X l        [        U[        5      (       a  U4U l        SU l        O[[        U[        5      (       a  [        U5      U l        U4U l        O-[        U5      nUR                  U l        UR                  U l        X0l        SU l        X@l        / U l        [        5       U l        g )Nr!   F)circuit_targetr7   r   target_clbitstarget_cregsr   rE   r   r   r   in_loopcomplete	_op_label_casesr?   
_label_set)r#   r   r@   r   r1   r   s         r$   rN   SwitchContext.__init__"  s     fe$$5;ID?AD 122!&vD!'	D&v.I!*!1!1D )DMO%r'   c                    XR                   ;   $ )zMReturn whether a case label is already accounted for in the switch statement.)r   )r#   r1   s     r$   label_in_useSwitchContext.label_in_use<  s    ''r'   c                r    U R                   R                  U5        U R                  R                  X45        g)zAdd a sequence of conditions and the single block that should be run if they are
triggered to the context.  The labels are assumed to have already been validated using
:meth:`label_in_use`.N)r   r   r   rH   )r#   r   blocks      r$   add_caseSwitchContext.add_case@  s*     	v&F?+r'   c                J    U R                   R                  SS9  [        U 5      $ )Nz'Cannot have instructions outside a case)forbidden_message)r   _push_scopeCaseBuilderr"   s    r$   	__enter__SwitchContext.__enter__K  s#      3\ ]4  r'   c                   SU l         U R                  R                  5         Ub  g[        U R                  U R
                  U R                  S9nUR                  5       nU R                  (       a1  U R                  R                  XER                  UR                  5        gUR                  [        UR                  5      [        UR                  5      5      u  pgU R                  R                  XgR                  UR                  5        g)NTFr0   )r   r   
_pop_scoper|   r   r   r   r   r   rH   r   r   r   r?   )r#   exc_typeexc_valexc_tbplaceholderinitial_resources	operationr   s           r$   __exit__SwitchContext.__exit__O  s    ! ,DLL$++T^^\'==?<<LL-E-EGXG_G_` 	 $/#C#C%,,-s3D3K3K/L$ I LL	+;+;Y=M=MNr'   )	r   r   r   r   r   r   r   r   r   )r   r   r@   ru   r   boolr1   rv   )r   z-Tuple[Union[int, Literal[CASE_DEFAULT]], ...]r   r   )r(   r)   r*   r+   r,   rN   r   r   r   r   r-   r!   r'   r$   r   r     s[    0  $   6 
    4(	,C	,Lc	,!r'   r   c                  L    \ rS rSrSr\r SS jr\R                  S 5       r
Srg)r   id  a  A child context manager for building up the ``case`` blocks of ``switch`` statements onto
circuits in a natural order, without having to construct the case bodies first.

This context should never need to be created manually by a user; it is the return value of the
:class:`.SwitchContext` context manager, which in turn should only be created by suitable
:meth:`.QuantumCircuit.switch_case` calls.

.. warning::

    This is an internal interface and no part of it should be relied upon outside of Qiskit
    Terra.
c                    Xl         SU l        g )NF)switchentered)r#   parents     r$   rN   CaseBuilder.__init__u  s    r'   c              '  J  #    U R                   (       a  [        S5      eU R                  R                  (       a  [        S5      e[	        S U 5       5      (       d  [        S5      e[        5       nU HH  nU R                  R                  U5      (       d  X2;   a  [        SU S35      eUR                  U5        MJ     U R                  R                  R                  U R                  R                  U R                  R                  U R                  R                  S9   SU l         S v   S	U l         U R                  R                  R                  5       nU R                  R                  X5        g ! S	U l         U R                  R                  R                  5       nf = f7f)
NzCannot enter more than one case at once. If you want multiple labels to point to the same block, pass them all to a single case context, such as `with case(1, 2, 3):`.z6Cannot add a new case to a completed switch statement.c              3  ^   #    U  H#  o[         L =(       d    [        U[        5      v   M%     g 7fr\   )r   r7   rG   )r]   rW   s     r$   r`   'CaseBuilder.__call__.<locals>.<genexpr>  s$     WPVuL(BJuc,BBPVs   +-z.Case values must be integers or `CASE_DEFAULT`zduplicate case label: 'r3   )r   r   allow_jumpsTF)r   r   r   r   rc   r?   r   rJ   r   r   r   r   r   r   r   )r#   rV   seenrW   r   s        r$   __call__CaseBuilder.__call__y  sK    <<2  ;;WXXWPVWWWOPPuE{{''..%-"%<UG1#EFFHHUO  	'';;,,kk..++ 	( 	
	5DL DLKK''224E
 	V+ !DLKK''224Es   DF#!E3 ,AF#3-F  F#)r   r   N)r   r   )r(   r)   r*   r+   r,   r   DEFAULTrN   
contextlibcontextmanagerr   r-   r!   r'   r$   r   r   d  s0     GG !, !,r'   r   )'r,   
__future__r   __all__r   typingr   r   r   r	   r
   r   r   r   r6   r   r   qiskit.circuit.classicalr   r   qiskit.circuit.exceptionsr   builderr   r   r   control_flowr   _builder_utilsr   r   r   r   r   r   r   r|   r   r   r!   r'   r$   <module>r      s    C "
*  V V V 3 0 2 Z Z ' X X-
  
   !EH= EHPR
2 R
jR Rj7, 7,r'   