§
    ¹³ÙgF  ã                  ó‚   — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
  G d„ d	ej        ej                 ¦  «        ZdS )
zYShared helper utility for mapping classical resources from one circuit or DAG to another.é    )ÚannotationsNé   )ÚBit)Úexpr)ÚClassicalRegisterÚClbitc                  ón   — e Zd ZdZdZ	 dddœd d„Zd!d„Zddœd„Zd„ Zd"d„Z	d„ Z
d„ Zd„ Zd„ Zd„ Zd„ ZdS )#ÚVariableMappera®  Stateful helper class that manages the mapping of variables in conditions and expressions.

    This is designed to be used by both :class:`.QuantumCircuit` and :class:`.DAGCircuit` when
    managing operations that need to map classical resources from one circuit to another.

    The general usage is to initialise this at the start of a many-block mapping operation, then
    call its :meth:`map_condition`, :meth:`map_target` or :meth:`map_expr` methods as appropriate,
    which will return the new object that should be used.

    If an ``add_register`` callable is given to the initializer, the mapper will use it to attempt
    to add new aliasing registers to the outer circuit object, if there is not already a suitable
    register for the mapping available in the circuit.  If this parameter is not given, a
    ``ValueError`` will be raised instead.  The given ``add_register`` callable may choose to raise
    its own exception.)Útarget_cregsÚregister_mapÚbit_mapÚvar_mapÚadd_registerN)r   r   ú"typing.Iterable[ClassicalRegister]r   útyping.Mapping[Bit, Bit]r   ú)typing.Mapping[expr.Var, expr.Var] | Noner   ú1typing.Callable[[ClassicalRegister], None] | Nonec               ój   — t          |¦  «        | _        i | _        || _        |pi | _        || _        d S ©N)Útupler   r   r   r   r   )Úselfr   r   r   r   s        ú\/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/_classical_resource_map.pyÚ__init__zVariableMapper.__init__*   s<   € õ " ,Ñ/Ô/ˆÔØˆÔØˆŒØ�} "ˆŒØ(ˆÔÐÐó    Útheirsr   Úreturnc                óR  ‡ — ‰ j                              |j        ¦  «        x}�|S ˆ fd„|D ¦   «         }‰ j        D ]}|t	          |¦  «        k    r|} nEŒ‰ j        €t          d|j        › d�¦  «        ‚t          |¬¦  «        }‰                      |¦  «         |‰ j         |j        <   |S )z}Map the target's registers to suitable equivalents in the destination, adding an
        extra one if there's no exact match.Nc                ó*   •— g | ]}‰j         |         ‘ŒS © ©r   ©Ú.0Úbitr   s     €r   ú
<listcomp>z0VariableMapper._map_register.<locals>.<listcomp>=   s    ø€ Ð;Ð;Ð;¨S�t”| CÔ(Ð;Ð;Ð;r   ú
Register 'ú(' has no counterpart in the destination.©Úbits)r   ÚgetÚnamer   Úlistr   Ú
ValueErrorr   )r   r   Úmapped_theirsÚmapped_bitsÚourss   `    r   Ú_map_registerzVariableMapper._map_register8   sÔ   ø€ ð "Ô.×2Ò2°6´;Ñ?Ô?Ð?ˆMÐLØ Ð Ø;Ð;Ð;Ð;°FÐ;Ñ;Ô;ˆØÔ%ð 	-ð 	-ˆDØ�d 4™jœjÒ(Ð(Ø $�Ø�ð )ð Ô Ð(Ý Ð!c¨f¬kÐ!cÐ!cÐ!cÑdÔdÐdÝ-°;Ð?Ñ?Ô?ˆMØ×Ò˜mÑ,Ô,Ð,Ø)6ˆÔ˜&œ+Ñ&ØÐr   F)Úallow_reorderc              óê  ‡ ‡
‡— |€dS t          |t          j        ¦  «        r‰                      |¦  «        S |\  }}t          |t          ¦  «        r‰ j        |         |fS |s‰                      |¦  «        |fS ˆ fd„|D ¦   «         }t          |¦  «        }‰ j        D ]}|t          |¦  «        k    r|} nKŒ‰ j	        €‰  
                    d|j        › d�¦  «        ‚t          |¬¦  «        }‰  	                    |¦  «         d„ t          |¦  «        D ¦   «         Š
|dt          |¦  «        › d�›ddd	…         Št          d
                     ˆ
ˆfd„|D ¦   «         ¦  «        ddd	…         d¦  «        }	||	fS )aç  Map the given ``condition`` so that it only references variables in the destination
        circuit (as given to this class on initialization).

        If ``allow_reorder`` is ``True``, then when a legacy condition (the two-tuple form) is made
        on a register that has a counterpart in the destination with all the same (mapped) bits but
        in a different order, then that register will be used and the value suitably modified to
        make the equality condition work.  This is maintaining legacy (tested) behavior of
        :meth:`.DAGCircuit.compose`; nowhere else does this, and in general this would require *far*
        more complex classical rewriting than Terra needs to worry about in the full expression era.
        Nc                ó*   •— g | ]}‰j         |         ‘ŒS r   r    r!   s     €r   r$   z0VariableMapper.map_condition.<locals>.<listcomp>a   s    ø€ ÐAÐAÐA°3˜Tœ\¨#Ô.ÐAÐAÐAr   r%   r&   r'   c                ó   — i | ]\  }}||“Œ	S r   r   )r"   Úir#   s      r   ú
<dictcomp>z0VariableMapper.map_condition.<locals>.<dictcomp>n   s   € ÐGÐGÐG¡  3�S˜!ÐGÐGÐGr   Ú0ÚbéÿÿÿÿÚ c              3  ó4   •K  — | ]}‰‰|                  V — Œd S r   r   )r"   r#   Ú	new_orderÚ
value_bitss     €€r   ú	<genexpr>z/VariableMapper.map_condition.<locals>.<genexpr>p   s,   øè è € Ð"WÐ"WÀ# :¨i¸¬nÔ#=Ð"WÐ"WÐ"WÐ"WÐ"WÐ"Wr   é   )Ú
isinstancer   ÚExprÚmap_exprr   r   r0   Úsetr   r   Úexc_typer*   r   Ú	enumerateÚlenÚintÚjoin)r   Ú	conditionr1   ÚtargetÚvalueÚmapped_bits_orderÚmapped_bits_setÚregisterr-   Úmapped_valuer<   r=   s   `         @@r   Úmap_conditionzVariableMapper.map_conditionJ   sÌ  øøø€ ð ÐØ�4Ý�i¥¤Ñ+Ô+ð 	,Ø—=’= Ñ+Ô+Ð+Ø!‰ˆ�Ý�f�eÑ$Ô$ð 	1Ø”L Ô(¨%Ð0Ð0Øð 	7Ø×&Ò& vÑ.Ô.°Ð6Ð6ð BÐAÐAÐA¸&ÐAÑAÔAÐÝÐ/Ñ0Ô0ˆØÔ)ð 
	-ð 
	-ˆHØ¥# h¡-¤-Ò/Ð/Ø (�Ø�ð 0ð Ô Ð(Ø—m’mØV ¤ÐVÐVÐVñô ð õ .Ð3DÐEÑEÔEˆMØ×Ò˜mÑ,Ô,Ð,ØGÐG­)Ð4EÑ*FÔ*FÐGÑGÔGˆ	ØÐ/¥ F¡¤Ð/Ð/Ð/Ð/°°°"°Ô5ˆ
Ý˜2Ÿ7š7Ð"WÐ"WÐ"WÐ"WÐ"WÈÐ"WÑ"WÔ"WÑWÔWÐX\ÐX\ÐZ\ÐX\Ô]Ð_`ÑaÔaˆØ˜|Ð,Ð,r   c               óÄ   — t          |t          ¦  «        r| j        |         S t          |t          ¦  «        r|                      |¦  «        S |                      |¦  «        S )z¬Map the real-time variables in a ``target`` of a :class:`.SwitchCaseOp` to the new
        circuit, as defined in the ``circuit`` argument of the initializer of this class.)r@   r   r   r   r0   rB   )r   rJ   s     r   Ú
map_targetzVariableMapper.map_targets   s\   € õ �f�eÑ$Ô$ð 	(Ø”< Ô'Ð'Ý�fÕ/Ñ0Ô0ð 	.Ø×%Ò% fÑ-Ô-Ð-Ø�}Š}˜VÑ$Ô$Ð$r   Únodeú	expr.Exprc               ó,   — |                      | ¦  «        S )zEMap the variables in an :class:`~.expr.Expr` node to the new circuit.)Úaccept©r   rS   s     r   rB   zVariableMapper.map_expr|   s   € à�{Š{˜4Ñ Ô Ð r   c               óX  — t          |j        t          ¦  «        r*t          j        | j        |j                 |j        ¦  «        S t          |j        t          ¦  «        r2t          j        |                      |j        ¦  «        |j        ¦  «        S | j	         
                    ||¦  «        S r   )r@   Úvarr   r   ÚVarr   Útyper   r0   r   r)   rW   s     r   Ú	visit_varzVariableMapper.visit_var€   s…   € Ý�d”h¥Ñ&Ô&ð 	?Ý”8˜DœL¨¬Ô2°D´IÑ>Ô>Ð>Ý�d”hÕ 1Ñ2Ô2ð 	EÝ”8˜D×.Ò.¨t¬xÑ8Ô8¸$¼)ÑDÔDÐDØŒ|×Ò  dÑ+Ô+Ð+r   c               ó@   — t          j        |j        |j        ¦  «        S r   )r   ÚValuerK   r[   rW   s     r   Úvisit_valuezVariableMapper.visit_value‡   s   € ÝŒz˜$œ* d¤iÑ0Ô0Ð0r   c               ór   — t          j        |j        |j                             | ¦  «        |j        ¦  «        S r   )r   ÚUnaryÚopÚoperandrV   r[   rW   s     r   Úvisit_unaryzVariableMapper.visit_unaryŠ   s*   € ÝŒz˜$œ' 4¤<×#6Ò#6°tÑ#<Ô#<¸d¼iÑHÔHÐHr   c               ó¤   — t          j        |j        |j                             | ¦  «        |j                             | ¦  «        |j        ¦  «        S r   )r   ÚBinaryrb   ÚleftrV   Úrightr[   rW   s     r   Úvisit_binaryzVariableMapper.visit_binary�   s>   € ÝŒ{˜4œ7 D¤I×$4Ò$4°TÑ$:Ô$:¸D¼J×<MÒ<MÈdÑ<SÔ<SÐUYÔU^Ñ_Ô_Ð_r   c               ót   — t          j        |j                             | ¦  «        |j        |j        ¬¦  «        S )N)Úimplicit)r   ÚCastrc   rV   r[   rk   rW   s     r   Ú
visit_castzVariableMapper.visit_cast�   s-   € ÝŒy˜œ×,Ò,¨TÑ2Ô2°D´IÈÌÐVÑVÔVÐVr   c               ó˜   — t          j        |j                             | ¦  «        |j                             | ¦  «        |j        ¦  «        S r   )r   ÚIndexrJ   rV   Úindexr[   rW   s     r   Úvisit_indexzVariableMapper.visit_index“   s8   € ÝŒz˜$œ+×,Ò,¨TÑ2Ô2°D´J×4EÒ4EÀdÑ4KÔ4KÈTÌYÑWÔWÐWr   r   )r   r   r   r   r   r   r   r   )r   r   r   r   )rS   rT   r   rT   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r0   rP   rR   rB   r\   r_   rd   ri   rm   rq   r   r   r   r
   r
      s  € € € € € ðð ð W€Ið >Bð	)ð KOð)ð )ð )ð )ð )ð )ðð ð ð ð$ <Að '-ð '-ð '-ð '-ð '-ðR%ð %ð %ð!ð !ð !ð !ð,ð ,ð ,ð1ð 1ð 1ðIð Ið Ið`ð `ð `ðWð Wð WðXð Xð Xð Xð Xr   r
   )ru   Ú
__future__r   Útypingr#   r   Ú	classicalr   Úclassicalregisterr   r   ÚExprVisitorrA   r
   r   r   r   ú<module>r|      s·   ðð `Ð _à "Ð "Ð "Ð "Ð "Ð "à €€€à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7ð|Xð |Xð |Xð |Xð |X�TÔ% d¤iÔ0ñ |Xô |Xð |Xð |Xð |Xr   