§
    ¹³Ùg   ã                  óT  — d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ e
rdd
lmZ  e	deeeef         eeef         ej        f         ¬¦  «        Zd"d„Zej         G d„ d¦  «        ¦   «         Zd#d„Zd$d„Z d%d„Z!d&d„Z"d'd „Z#d&d!„Z$dS )(zBPrivate utility functions that are used by the builder interfaces.é    )ÚannotationsN)ÚIterableÚTupleÚSetÚUnionÚTypeVarÚTYPE_CHECKING)ÚexprÚtypes)ÚCircuitError)ÚRegister)ÚClassicalRegisterÚClbit)ÚQuantumRegister©ÚQuantumCircuitÚ_ConditionT)ÚboundÚ	conditionÚreturnc                óf  — t          | t          j        ¦  «        r2| j        j        t
          j        urt          d| j        › d�¦  «        ‚| S 	 | \  }}t          |t          t          f¦  «        rt          |t          ¦  «        r||fS n# t          t          f$ r Y nw xY wt          d| ›d�¦  «        ‚)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)r   ÚbitsÚvalues      ú_/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/controlflow/_builder_utils.pyÚvalidate_conditionr#   "   sí   € õ �)�TœYÑ'Ô'ð ØŒ>Ô¥e¤jÐ0Ð0Ýð,Ø"œð,ð ,ð ,ñô ð ð ÐðØ‰ˆˆeÝ�dÕ.µÐ6Ñ7Ô7ð 	!½JÀuÍcÑ<RÔ<Rð 	!Ø˜%�=Ð øøÝ•zÐ"ð ð ð Øˆðøøøå
ð	*Ø#ð	*ð 	*ð 	*ñô ð s   Á9B	 Â	BÂBc                  ó(   — e Zd ZU dZded<   ded<   dS )ÚLegacyResourceszŸA 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, ...]ÚcregsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    r"   r%   r%   B   s8   € € € € € € ðHð Hð ÐÐÑØ(Ð(Ð(Ñ(Ð(Ð(r.   r%   Únodeú	expr.Exprc                óf  — i }i }t          j        | ¦  «        D ]o}t          |j        t          ¦  «        rd||j        <   Œ't          |j        t
          ¦  «        r.|                     d„ |j        D ¦   «         ¦  «         d||j        <   Œpt          t          |¦  «        t          |¦  «        ¦  «        S )zGet the legacy classical resources (:class:`.Clbit` and :class:`.ClassicalRegister`)
    referenced by an :class:`~.expr.Expr`.Nc              3  ó   K  — | ]}|d fV — Œ	d S )Nr-   )Ú.0Úbits     r"   ú	<genexpr>z!node_resources.<locals>.<genexpr>V   s&   è è € Ð9Ð9¨#˜3 ˜+Ð9Ð9Ð9Ð9Ð9Ð9r.   )	r
   Ú	iter_varsr   Úvarr   r   Úupdater%   Útuple)r/   r&   r'   r7   s       r"   Únode_resourcesr:   K   s¨   € ð
 €FØ€EÝŒ~˜dÑ#Ô#ð "ð "ˆÝ�c”g�uÑ%Ô%ð 	"Ø"ˆF�3”7‰OˆOÝ˜œÕ!2Ñ3Ô3ð 	"Ø�MŠMÐ9Ð9°´Ð9Ñ9Ô9Ñ9Ô9Ð9Ø!ˆE�#”'‰NøÝ�5 ™=œ=­%°©,¬,Ñ7Ô7Ð7r.   ú=tuple[ClassicalRegister, int] | tuple[Clbit, int] | expr.Exprc                óæ   — t          | t          j        ¦  «        rt          | ¦  «        S | \  }}t          |t          ¦  «        rt          t          |¦  «        |f¦  «        S t          |fd¦  «        S )z•Get 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?   [   sl   € õ
 �)�TœYÑ'Ô'ð )Ý˜iÑ(Ô(Ð(Ø�I€FˆAÝ�&Õ+Ñ,Ô,ð 9Ý�u V™}œ}¨v¨iÑ8Ô8Ð8Ý˜F˜9 bÑ)Ô)Ð)r.   Ú	registersúIterable[Register]ú3Tuple[Set[QuantumRegister], Set[ClassicalRegister]]c                ó  — t          ¦   «         }t          ¦   «         }| D ]j}t          |t          ¦  «        r|                     |¦  «         Œ-t          |t          ¦  «        r|                     |¦  «         ŒXt          d|› d�¦  «        ‚||fS )zKPartition a sequence of registers into its quantum and classical registers.zUnknown register: ú.)Úsetr   r   Úaddr   r   )r@   Úqregsr'   Úregisters       r"   Úpartition_registersrI   h   sž   € õ ‰EŒE€EÝ‰EŒE€EØð Að AˆÝ�h¥Ñ0Ô0ð 	AØ�IŠI�hÑÔÐÐÝ˜Õ"3Ñ4Ô4ð 	AØ�IŠI�hÑÔÐÐõ Ð?°HÐ?Ð?Ð?Ñ@Ô@Ð@Ø�%ˆ<Ðr.   ÚcircuitsúIterable[QuantumCircuit]c                ó€  — t          | ¦  «        } t          | ¦  «        dk     r| S g }g }| D ]¼}|j        dt          |¦  «        …         |k    rt          | ¦  «        c S |j        dt          |¦  «        …         |k    rt          | ¦  «        c S |j        t          |¦  «        k    rt          |j        ¦  «        }|j        t          |¦  «        k    rt          |j        ¦  «        }Œ½| D ]F}|                     ||j        d…         ¦  «         |                     ||j        d…         ¦  «         ŒGt          | ¦  «        S )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)rJ   rO   r&   Úcircuits       r"   Úunify_circuit_resourcesrW   y   sI  € õ �X‰Œ€HÝ
ˆ8�}„}�qÒÐØˆØ€FØ€FØð *ð *ˆØŒ>˜-�C ™KœK˜-Ô(¨FÒ2Ð2Ý3°HÑ=Ô=Ð=Ð=Ð=ØŒ>˜-�C ™KœK˜-Ô(¨FÒ2Ð2Ý3°HÑ=Ô=Ð=Ð=Ð=ØÔ¥ F¡¤Ò+Ð+Ý˜'œ.Ñ)Ô)ˆFØÔ¥ F¡¤Ò+Ð+Ý˜'œ.Ñ)Ô)ˆFøØð 7ð 7ˆØ×Ò˜ Ô 2Ð 4Ð 4Ô5Ñ6Ô6Ð6Ø×Ò˜ Ô 2Ð 4Ð 4Ô5Ñ6Ô6Ð6Ð6Ý# HÑ-Ô-Ð-r.   úTuple[QuantumCircuit, ...]ú%Tuple[QuantumCircuit, QuantumCircuit]c                óh  — ddl m} t          ¦   «         t          ¦   «         }}| D ]6}|                     |j        ¦  «         |                     |j        ¦  «         Œ7t          |¦  «        t          |¦  «        }}g }| D ]¦} |||g|j        ¢|j        ¢R |j	        | 
                    ¦   «         |                     ¦   «         dœŽ}|                     ¦   «         D ]}|                     |¦  «         Œ|j        D ]}|                     |¦  «         Œ|                     |¦  «         Œ§t#          |¦  «        S )aO  
    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   rE   r8   rO   r&   rR   rG   r'   r[   Úiter_input_varsÚiter_captured_varsÚiter_declared_varsÚadd_uninitialized_varÚdataÚ_appendÚappendrU   )	rJ   r   rO   r&   rV   Úout_circuitsÚoutr7   Úinstructions	            r"   rP   rP   š   so  € ð .Ð-Ð-Ð-Ð-Ð-å‘U”U�C™EœEˆF€FØð &ð &ˆØ�Š�g”nÑ%Ô%Ð%Ø�Š�g”nÑ%Ô%Ð%Ð%Ý˜&‘\”\¥4¨¡<¤<ˆF€Fð €LØð !ð !ˆØˆnØØð
ð Œ]ð
ð Œ]ð	
ð 
ð
 !Ô-Ø×*Ò*Ñ,Ô,Ø×/Ò/Ñ1Ô1ð
ð 
ð 
ˆð ×-Ò-Ñ/Ô/ð 	+ð 	+ˆCØ×%Ò% cÑ*Ô*Ð*Ð*Ø"œ<ð 	%ð 	%ˆKØ�KŠK˜Ñ$Ô$Ð$Ð$Ø×Ò˜CÑ Ô Ð Ð Ý# LÑ1Ô1Ð1r.   c                óD  — t          | ¦  «        } t          ¦   «         }| D ]6}|                     |j        ¦  «         |                     |j        ¦  «         Œ7| D ]F}|t          |j        ¦  «        z
  t          |j        ¦  «        z
  D ]}|                     |¦  «         ŒŒG| S )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   rE   r8   rG   r'   Úadd_register)rJ   Útotal_registersrV   rH   s       r"   rU   rU   Á   s³   € õ �X‰Œ€HÝ‘e”e€OØð .ð .ˆØ×Ò˜wœ}Ñ-Ô-Ð-Ø×Ò˜wœ}Ñ-Ô-Ð-Ð-Øð +ð +ˆØ'­#¨g¬mÑ*<Ô*<Ñ<½sÀ7Ä=Ñ?QÔ?QÑQð 	+ð 	+ˆHØ× Ò  Ñ*Ô*Ð*Ð*ð	+à€Or.   )r   r   r   r   )r/   r0   r   r%   )r   r;   r   r%   )r@   rA   r   rB   )rJ   rK   r   rK   )rJ   rX   r   rY   )%r+   Ú
__future__r   ÚdataclassesÚtypingr   r   r   r   r   r	   Úqiskit.circuit.classicalr
   r   Úqiskit.circuit.exceptionsr   Úqiskit.circuit.registerr   Ú qiskit.circuit.classicalregisterr   r   Úqiskit.circuit.quantumregisterr   r^   r   r   r   r   r#   Ú	dataclassr%   r:   r?   rI   rW   rP   rU   r-   r.   r"   ú<module>ru      sì  ðð IÐ Hà "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ Fà 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø EÐ EÐ EÐ EÐ EÐ EÐ EÐ EØ :Ð :Ð :Ð :Ð :Ð :àð .Ø-Ð-Ð-Ð-Ð-Ð-àˆgØ˜˜uÐ%6¸Ð%;Ô<¸eÀEÈ3ÀJÔ>OÐQUÔQZÐZÔ[ðñ ô €ð
ð ð ð ð@ Ôð)ð )ð )ð )ð )ñ )ô )ñ Ôð)ð8ð 8ð 8ð 8ð 
*ð 
*ð 
*ð 
*ðð ð ð ð".ð .ð .ð .ðB$2ð $2ð $2ð $2ðNð ð ð ð ð r.   