
    z	iF                     \   S r SSKJr  SSKrSSKJrJrJrJrJ	r	J
r
  SSKJrJrJr  SSKJrJr  SSKJr  \
(       a  SSKJrJr  \	" S	\\\\4   \\\4   \R.                  4   S
9rSS jr\R4                   " S S5      5       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!g)zBPrivate utility functions that are used by the builder interfaces.    )annotationsN)IterableTupleSetUnionTypeVarTYPE_CHECKING)ClassicalRegisterClbitQuantumRegister)exprtypes)CircuitError)QuantumCircuitRegister_ConditionT)boundc                   [        U [        R                  5      (       aB  U R                  R                  [
        R                  La  [        SU R                   S35      eU $  U u  p[        U[        [        45      (       a  [        U[        5      (       a  X4$ [        SU < S35      e! [        [        4 a     N"f = f)a  Validate that a condition is in a valid format and return it, but raise if it is invalid.

Args:
    condition: the condition to be tested for validity.  Must be either the legacy 2-tuple
        format, or a :class:`~.expr.Expr` that has `Bool` type.

Raises:
    CircuitError: if the condition is not in a valid format.

Returns:
    The same condition as passed, if it was valid.
zFClassical conditions must be expressions with the type 'Bool()', not 'z'.z_A classical condition should be a 2-tuple of `(ClassicalRegister | Clbit, int)`, but received ')
isinstancer   Exprtypekindr   Boolr   r
   r   int	TypeError
ValueError)	conditionbitsvalues      c/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/controlflow/_builder_utils.pyvalidate_conditionr!   $   s     )TYY''>>ejj0"(,  d.677Juc<R<R=  	#b	*  z" s   #6B* *B=<B=c                  .    \ rS rSr% SrS\S'   S\S'   Srg)	LegacyResourcesD   zA pair of the :class:`.Clbit` and :class:`.ClassicalRegister` resources used by some other
object (such as a legacy condition or :class:`.expr.Expr` node).ztuple[Clbit, ...]clbitsztuple[ClassicalRegister, ...]cregs N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r'       r    r#   r#   D   s    H ((r/   r#   c                   0 n0 n[         R                  " U 5       H  n[        UR                  [        5      (       a  SXR                  '   M2  [        UR                  [
        5      (       d  MS  UR                  S UR                   5       5        SX#R                  '   M     [        [        U5      [        U5      5      $ )z{Get the legacy classical resources (:class:`.Clbit` and :class:`.ClassicalRegister`)
referenced by an :class:`~.expr.Expr`.Nc              3  (   #    U  H  oS 4v   M
     g 7f)Nr'   ).0bits     r    	<genexpr>!node_resources.<locals>.<genexpr>X   s     9#+s   )	r   	iter_varsr   varr   r
   updater#   tuple)noder%   r&   r7   s       r    node_resourcesr;   M   s    
 FE~~d#cggu%%"F77O!233MM999!E''N $ 5=%,77r/   c                    [        U [        R                  5      (       a  [        U 5      $ U u  p[        U[        5      (       a  [        [        U5      U45      $ [        U4S5      $ )zGet the legacy classical resources (:class:`.Clbit` and :class:`.ClassicalRegister`)
referenced by a legacy condition or an :class:`~.expr.Expr`.r'   )r   r   r   r;   r
   r#   r9   )r   target_s      r    condition_resourcesr?   ]   sW    
 )TYY''i((IF&+,,uV}vi88F9b))r/   c                    [        5       n[        5       nU  H`  n[        U[        5      (       a  UR                  U5        M+  [        U[        5      (       a  UR                  U5        MS  [        SU S35      e   X4$ )zKPartition a sequence of registers into its quantum and classical registers.zUnknown register: .)setr   r   addr
   r   )	registersqregsr&   registers       r    partition_registersrG   j   sp     EEEEh00IIh"344IIh !3H:Q?@@  <r/   c                P   [        U 5      n [        U 5      S:  a  U $ / n/ nU  H  nUR                  S[        U5       U:w  a  [        U 5      s  $ UR                  S[        U5       U:w  a  [        U 5      s  $ UR
                  [        U5      :  a  [        UR                  5      nUR                  [        U5      :  d  M  [        UR                  5      nM     U  H=  nUR                  XR
                  S 5        UR                  X#R                  S 5        M?     [        U 5      $ )a  
Ensure that all the given ``circuits`` have all the same qubits, clbits and registers, and
that they are defined in the same order.  The order is important for binding when the bodies are
used in the 3-tuple :obj:`.Instruction` context.

This function will preferentially try to mutate its inputs if they share an ordering, but if
not, it will rebuild two new circuits.  This is to avoid coupling too tightly to the inner
class; there is no real support for deleting or re-ordering bits within a :obj:`.QuantumCircuit`
context, and we don't want to rely on the *current* behavior of the private APIs, since they
are very liable to change.  No matter the method used, circuits with unified bits and registers
are returned.
   N)
r9   lenqubits _unify_circuit_resources_rebuildr%   
num_qubitslist
num_clbitsadd_bits_unify_circuit_registers)circuitsrK   r%   circuits       r    unify_circuit_resourcesrT   {   s    XH
8}qFF>>-CK(F23H==>>-CK(F23H==F+'..)FF+'..)F   2 2 456 2 2 456  $H--r/   c                   SSK Jn  [        5       [        5       p2U  H9  nUR                  UR                  5        UR                  UR
                  5        M;     [        U5      [        U5      p2/ nU  H  nU" UU/UR                  QUR                  Q7UR                  UR                  5       UR                  5       S.6nUR                  5        H  nUR                  U5        M     UR                  5        H  nUR                  U5        M     UR                    H  n	UR#                  U	5        M     UR%                  U5        M     ['        U5      $ )a;  
Ensure that all the given circuits have all the same qubits and clbits, and that they
are defined in the same order.  The order is important for binding when the bodies are used in
the 3-tuple :obj:`.Instruction` context.

This function will always rebuild the objects into new :class:`.QuantumCircuit` instances.
r   )r   )global_phaseinputscaptures)qiskit.circuitr   rB   r8   rK   r%   rN   rE   r&   rV   iter_input_varsiter_capturesiter_declared_varsadd_uninitialized_variter_declared_stretchesadd_stretchdata_appendappendrQ   )
rR   r   rK   r%   rS   out_circuitsoutr7   stretchinstructions
             r    rL   rL      s)    .UCEFgnn%gnn%  &\4<F L
 ]]
 ]]	

 !--**,**,
 --/C%%c* 0668GOOG$ 9"<<KKK$ (C ! " $L11r/   c                L   [        U 5      n [        5       nU  H9  nUR                  UR                  5        UR                  UR                  5        M;     U  HI  nU[        UR                  5      -
  [        UR                  5      -
   H  nUR                  U5        M     MK     U $ )z
Ensure that ``true_body`` and ``false_body`` have the same registers defined within them.  These
do not need to be in the same order between circuits.  The two input circuits are returned,
mutated to have the same registers.
)r9   rB   r8   rE   r&   add_register)rR   total_registersrS   rF   s       r    rQ   rQ      s     XHeOw}}-w}}-  '#gmm*<<s7==?QQH  * R  Or/   )r   r   returnr   )r:   z	expr.Exprrj   r#   )r   z=tuple[ClassicalRegister, int] | tuple[Clbit, int] | expr.Exprrj   r#   )rD   zIterable[Register]rj   z3Tuple[Set[QuantumRegister], Set[ClassicalRegister]])rR   Iterable[QuantumCircuit]rj   rk   )rR   zTuple[QuantumCircuit, ...]rj   z%Tuple[QuantumCircuit, QuantumCircuit])"r,   
__future__r   dataclassestypingr   r   r   r   r   r	   rY   r
   r   r   qiskit.circuit.classicalr   r   qiskit.circuit.exceptionsr   r   r   r   r   r   r!   	dataclassr#   r;   r?   rG   rT   rL   rQ   r'   r/   r    <module>rr      s    I "  F F 
 1 27u%6%;<eE3J>OQUQZQZZ[
@ ) ) )8 
*L
*
*!8".B&2(&2*&2Rr/   