§
    º³ÙgÕ0  ã                   óÎ   — d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ  ej        e¦  «        Z G d
„ de¦  «        Z G d„ de¦  «        ZdS )z:VF2Layout pass to find a layout using subgraph isomorphismé    )ÚEnumN)Úvf2_mapping)ÚLayout)ÚAnalysisPass)ÚTranspilerError)Ú	vf2_utils)Údeprecate_argc                   ó   — e Zd ZdZdZdZdZdS )ÚVF2LayoutStopReasonz Stop reasons for VF2Layout pass.zsolution foundznonexistent solutionz>2q gates in basisN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚSOLUTION_FOUNDÚNO_SOLUTION_FOUNDÚMORE_THAN_2Q© ó    ú`/granary/edev/quantum/lib/python3.11/site-packages/qiskit/transpiler/passes/layout/vf2_layout.pyr   r       s$   € € € € € Ø*Ð*à%€NØ.ÐØ'€L€L€Lr   r   c                   óf   ‡ — e Zd ZdZ eddddd¬¦  «        	 	 	 	 	 	 	 	 dˆ fd
„	¦   «         Zd„ Zˆ xZS )Ú	VF2Layoutad  A pass for choosing a Layout of a circuit onto a Coupling graph, as
    a subgraph isomorphism problem, solved by VF2++.

    If a solution is found that means there is a "perfect layout" and that no
    further swap mapping or routing is needed. If a solution is found the layout
    will be set in the property set as ``property_set['layout']``. However, if no
    solution is found, no ``property_set['layout']`` is set. The stopping reason is
    set in ``property_set['VF2Layout_stop_reason']`` in all the cases and will be
    one of the values enumerated in ``VF2LayoutStopReason`` which has the
    following values:

        * ``"solution found"``: If a perfect layout was found.
        * ``"nonexistent solution"``: If no perfect layout was found.
        * ``">2q gates in basis"``: If VF2Layout can't work with basis

    By default, this pass will construct a heuristic scoring map based on
    the error rates in the provided ``target`` (or ``properties`` if ``target``
    is not provided). However, analysis passes can be run prior to this pass
    and set ``vf2_avg_error_map`` in the property set with a :class:`~.ErrorMap`
    instance. If a value is ``NaN`` that is treated as an ideal edge
    For example if an error map is created as::

        from qiskit.transpiler.passes.layout.vf2_utils import ErrorMap

        error_map = ErrorMap(3)
        error_map.add_error((0, 0), 0.0024)
        error_map.add_error((0, 1), 0.01)
        error_map.add_error((1, 1), 0.0032)

    that represents the error map for a 2 qubit target, where the avg 1q error
    rate is ``0.0024`` on qubit 0 and ``0.0032`` on qubit 1. Then the avg 2q
    error rate for gates that operate on (0, 1) is 0.01 and (1, 0) is not
    supported by the target. This will be used for scoring if it's set as the
    ``vf2_avg_error_map`` key in the property set when :class:`~.VF2Layout` is run.
    Ú
propertiesz1.4ÚQiskitzin Qiskit 2.0a  The BackendProperties data structure has been deprecated and will be removed in Qiskit 2.0. The `target` input argument should be used instead. You can use Target.from_configuration() to build the target from the properties object, but in 2.0 you will need to generate a target directly.)ÚnameÚsinceÚpackage_nameÚremoval_timelineÚadditional_msgNFc	                 ó  •— t          ¦   «                              ¦   «          || _        |�#| j                             ¦   «         x}	�|	| _        n|| _        || _        || _        || _        || _        || _	        || _
        d| _        dS )aÛ  Initialize a ``VF2Layout`` pass instance

        Args:
            coupling_map (CouplingMap): Directed graph representing a coupling map.
            strict_direction (bool): If True, considers the direction of the coupling map.
                                     Default is False.
            seed (int): Sets the seed of the PRNG. -1 Means no node shuffling.
            call_limit (int): The number of state visits to attempt in each execution of
                VF2.
            time_limit (float): The total time limit in seconds to run ``VF2Layout``
            properties (BackendProperties): The backend properties for the backend. If
                :meth:`~qiskit.providers.models.BackendProperties.readout_error` is available
                it is used to score the layout.
            max_trials (int): The maximum number of trials to run VF2 to find
                a layout. If this is not specified the number of trials will be limited
                based on the number of edges in the interaction graph or the coupling graph
                (whichever is larger) if no other limits are set. If set to a value <= 0 no
                limit on the number of trials will be set.
            target (Target): A target representing the backend device to run ``VF2Layout`` on.
                If specified it will supersede a set value for ``properties`` and
                ``coupling_map`` if the :class:`.Target` contains connectivity constraints. If the value
                of ``target`` models an ideal backend without any constraints then the value of
                ``coupling_map``
                will be used.

        Raises:
            TypeError: At runtime, if neither ``coupling_map`` or ``target`` are provided.
        N)ÚsuperÚ__init__ÚtargetÚbuild_coupling_mapÚcoupling_mapr   Ústrict_directionÚseedÚ
call_limitÚ
time_limitÚ
max_trialsÚavg_error_map)Úselfr$   r%   r&   r'   r(   r   r)   r"   Útarget_coupling_mapÚ	__class__s             €r   r!   zVF2Layout.__init__M   s�   ø€ õb 	‰Œ×ÒÑÔÐØˆŒàÐØ(,¬×(FÒ(FÑ(HÔ(HÐHÐ$ÐUà 3ˆDÔÐà ,ˆDÔØ$ˆŒØ 0ˆÔØˆŒ	Ø$ˆŒØ$ˆŒØ$ˆŒØ!ˆÔÐÐr   c                 ót	  ‡‡— | j         €t          d¦  «        ‚| j        d         | _        | j        €*t	          j        | j        | j        | j         ¦  «        | _        t	          j        || j	        ¦  «        }|€t          j        | j        d<   dS |\  }}Š}t	          j        |¦  «        }t	          j        ||¦  «        }t	          j        | j         | j        | j	        ¦  «        \  }Š| j        �¡| j        j        �•t#          t$          j                             | j        j        ¦  «        ¦  «        }	t#          t+          t-          ‰¦  «        ¦  «        ¦  «                             |	¦  «        }
|
r!|                     ˆfd„|
D ¦   «         ¦  «         | j        €r| j        €k| j        €dt-          |                     ¦   «         ¦  «        }t-          | j         j                             ¦   «         ¦  «        }t=          ||¦  «        dz   | _        t>                                d¦  «         tC          ||dd	d	| j        ¬
¦  «        }d}d}tE          j"        ¦   «         }d}ˆfd„}|D �]µ}|dz  }t>                                d|¦  «         t          j#        }ˆfd„| $                    ¦   «         D ¦   «         }t-          |¦  «        t-          |¦  «        k    r ||¦  «        } �n:| j        € ||¦  «        } �n%t	          j%        | j        ||‰|| j	        ||¬¦  «        }|dk    r ||¦  «        } nìt>                                d||¦  «         |€ ||¦  «        }|}n3||k     r- ||¦  «        }t>                                d||||¦  «         |}|}| j        �9| j        dk    r.|| j        k    r#t>                                d|| j        ¦  «          nMtE          j"        ¦   «         |z
  }| j        �.|| j        k    r#t>                                d|| j        ¦  «          n�Œ·|€t          j&        }n�t	          j'        ||| (                    ¦   «         ‰| j        ¦  «        }|€t          j&        | j        d<   dS || j        d<   |j)         *                    ¦   «         D ]"}| j        d          +                    |¦  «         Œ#|| j        d<   dS )zrun the layout methodNz)coupling_map or target must be specified.Úvf2_avg_error_mapÚVF2Layout_stop_reasonc                 ó    •— g | ]
}‰|         ‘ŒS r   r   )Ú.0ÚiÚcm_nodess     €r   ú
<listcomp>z!VF2Layout.run.<locals>.<listcomp>©   s   ø€ Ð+KÐ+KÐ+K¸A¨H°Q¬KÐ+KÐ+KÐ+Kr   é   zRunning VF2 to find mappingsTF)ÚsubgraphÚid_orderÚinducedr'   r   c                 ó^   •— t          ˆfd„|                      ¦   «         D ¦   «         ¦  «        S )Nc                 ó(   •— i | ]\  }}‰|         |“ŒS r   r   )r2   ÚkÚvÚreverse_im_graph_node_maps      €r   ú
<dictcomp>z<VF2Layout.run.<locals>.mapping_to_layout.<locals>.<dictcomp>Ã   s%   ø€ Ð^Ð^Ð^¹t¸qÀ!Ð4°QÔ7¸Ð^Ð^Ð^r   )r   Úitems)Úlayout_mappingr>   s    €r   Úmapping_to_layoutz(VF2Layout.run.<locals>.mapping_to_layoutÂ   s2   ø€ ÝÐ^Ð^Ð^Ð^À~×G[ÒG[ÑG]ÔG]Ð^Ñ^Ô^Ñ_Ô_Ð_r   é   zRunning trial: %sc                 ó(   •— i | ]\  }}|‰|         “ŒS r   r   )r2   Úcm_iÚim_ir4   s      €r   r?   z!VF2Layout.run.<locals>.<dictcomp>É   s#   ø€ ÐUÐUÐU±z°t¸T˜d H¨T¤NÐUÐUÐUr   )Ú	edge_listÚbit_listg        zTrial %s has score %szAFound layout %s has a lower score (%s) than previous best %s (%s)z'Trial %s is >= configured max trials %sz<VF2Layout has taken %s which exceeds configured max time: %sÚlayout),r$   r   Úproperty_setr*   r   Úbuild_average_error_mapr"   r   Úbuild_interaction_graphr%   r   r   Úbuild_edge_listÚbuild_bit_listÚshuffle_coupling_graphr&   ÚqargsÚsetÚ	itertoolsÚchainÚfrom_iterableÚrangeÚlenÚ
differenceÚremove_nodes_fromr)   r'   r(   rG   ÚgraphÚmaxÚloggerÚdebugr   Útimer   r@   Úscore_layoutr   Úmap_free_qubitsÚ	num_nodesÚqregsÚvaluesÚadd_register)r+   ÚdagÚresultÚim_graphÚim_graph_node_mapÚ
free_nodesÚscoring_edge_listÚscoring_bit_listÚcm_graphÚhas_operationsÚ	to_removeÚim_graph_edge_countÚcm_graph_edge_countÚmappingsÚchosen_layoutÚchosen_layout_scoreÚ
start_timeÚtrialsrB   ÚmappingÚstop_reasonrA   Úlayout_scorerI   Úelapsed_timeÚregr4   r>   s                             @@r   ÚrunzVF2Layout.run�   s"  øø€ àÔÐ$Ý!Ð"MÑNÔNÐNØ!Ô.Ð/BÔCˆÔØÔÐ%Ý!*Ô!BØ”˜Tœ_¨dÔ.?ñ"ô "ˆDÔõ Ô2°3¸Ô8MÑNÔNˆØˆ>Ý9LÔ9YˆDÔÐ5Ñ6ØˆFØMSÑJˆÐ#Ð%>À
Ý%Ô5°hÑ?Ô?ÐÝ$Ô3°HÐ>OÑPÔPÐÝ&Ô=ØÔ˜tœy¨$Ô*?ñ
ô 
Ñˆ�(ð
 Œ;Ð" t¤{Ô'8Ð'DÝ ¥¤×!>Ò!>¸t¼{Ô?PÑ!QÔ!QÑRÔRˆNÝ�E¥# h¡-¤-Ñ0Ô0Ñ1Ô1×<Ò<¸^ÑLÔLˆIØð MØ×*Ò*Ð+KÐ+KÐ+KÐ+KÀÐ+KÑ+KÔ+KÑLÔLÐLð Œ?Ð" t¤Ð'>À4Ä?ÐCZÝ"% h×&8Ò&8Ñ&:Ô&:Ñ";Ô";ÐÝ"% dÔ&7Ô&=×&GÒ&GÑ&IÔ&IÑ"JÔ"JÐÝ!Ð"5Ð7JÑKÔKÈbÑPˆDŒOå�ŠÐ3Ñ4Ô4Ð4ÝØØØØØØ”ð
ñ 
ô 
ˆð ˆØ"ÐÝ”Y‘[”[ˆ
Øˆð	`ð 	`ð 	`ð 	`ð 	`ð  ð :	ñ :	ˆGØ�a‰KˆFÝ�LŠLÐ,¨fÑ5Ô5Ð5Ý-Ô<ˆKØUÐUÐUÐUÀWÇ]Â]Á_Ä_ÐUÑUÔUˆNõ
 �8‰}Œ}¥ H¡¤Ò-Ð-Ø 1Ð 1°.Ñ AÔ A�Ø‘ð Ô!Ð)Ø 1Ð 1°.Ñ AÔ A�Ø‘Ý$Ô1ØÔ"ØØ!Ø)ØØÔ%Ø+Ø)ð	ñ 	ô 	ˆLð ˜sÒ"Ð"Ø 1Ð 1°.Ñ AÔ A�Ø�Ý�LŠLÐ0°&¸,ÑGÔGÐGØÐ$Ø 1Ð 1°.Ñ AÔ A�Ø&2Ð#Ð#ØÐ 3Ò3Ð3Ø*Ð*¨>Ñ:Ô:�Ý—’ØWØØ Ø!Ø'ñô ð ð !'�Ø&2Ð#ØŒÐ*¨t¬ÀÒ/BÐ/BÀvÐQUÔQ`ÒG`ÐG`Ý—’ÐFÈÐPTÔP_Ñ`Ô`Ð`Ø�Ýœ9™;œ;¨Ñ3ˆLØŒÐ*¨|¸t¼Ò/NÐ/NÝ—’ØRØ Ø”Oñô ð ð
 �ùØÐ Ý-Ô?ˆKˆKå%Ô5ØØØ×"Ò"Ñ$Ô$Ø)ØÔ"ñô ˆMð Ð$Ý=PÔ=b�Ô!Ð"9Ñ:Ø�Ø*7ˆDÔ˜hÑ'Ø”y×'Ò'Ñ)Ô)ð >ð >�ØÔ! (Ô+×8Ò8¸Ñ=Ô=Ð=Ð=à5@ˆÔÐ1Ñ2Ð2Ð2r   )NFNNNNNN)r   r   r   r   r	   r!   rz   Ú__classcell__)r-   s   @r   r   r   (   s²   ø€ € € € € ð"ð "ðH €]ØØØØ(ðJð	ñ 	ô 	ð ØØØØØØØð6"ð 6"ð 6"ð 6"ð 6"ñ	ô 	ð6"ðpCAð CAð CAð CAð CAð CAð CAr   r   )r   Úenumr   rR   Úloggingr]   Ú	rustworkxr   Úqiskit.transpiler.layoutr   Úqiskit.transpiler.basepassesr   Úqiskit.transpiler.exceptionsr   Úqiskit.transpiler.passes.layoutr   Úqiskit.utilsr	   Ú	getLoggerr   r[   r   r   r   r   r   ú<module>r…      s.  ðð AÐ @Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ø €€€Ø €€€à !Ð !Ð !Ð !Ð !Ð !à +Ð +Ð +Ð +Ð +Ð +Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5à &Ð &Ð &Ð &Ð &Ð &à	ˆÔ	˜8Ñ	$Ô	$€ð(ð (ð (ð (ð (˜$ñ (ô (ð (ðjAð jAð jAð jAð jA�ñ jAô jAð jAð jAð jAr   