
    z	i=                     6   S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJr  \R&                  " \5      r\R,                  " S
S5      r \R,                  " SSSS9r " S S\5      rS rS rSS jrS rS rS rS r S r!g)zAMap input circuit onto a backend topology via insertion of SWAPs.    N)SwapGate)TransformationPass)TranspilerError)Layout)	DAGOpNode)Target)disjoint_utils_Step)stateswaps_addedgates_mappedgates_remaining_SystemState)layoutcoupling_mapregisterswapsN)defaultsc                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )LookaheadSwap2   aP  Map input circuit onto a backend topology via insertion of SWAPs.

Implementation of Sven Jandura's swap mapper submission for the 2018 Qiskit
Developer Challenge, adapted to integrate into the transpiler architecture.

The role of the swapper pass is to modify the starting circuit to be compatible
with the target device's topology (the set of two-qubit gates available on the
hardware.) To do this, the pass will insert SWAP gates to relocate the virtual
qubits for each upcoming gate onto a set of coupled physical qubits. However, as
SWAP gates are particularly lossy, the goal is to accomplish this remapping while
introducing the fewest possible additional SWAPs.

This algorithm searches through the available combinations of SWAP gates by means
of a narrowed best first/beam search, described as follows:

- Start with a layout of virtual qubits onto physical qubits.
- Find any gates in the input circuit which can be performed with the current
  layout and mark them as mapped.
- For all possible SWAP gates, calculate the layout that would result from their
  application and rank them according to the distance of the resulting layout
  over upcoming gates (see _calc_layout_distance.)
- For the four (search_width) highest-ranking SWAPs, repeat the above process on
  the layout that would be generated if they were applied.
- Repeat this process down to a depth of four (search_depth) SWAPs away from the
  initial layout, for a total of 256 (search_width^search_depth) prospective
  layouts.
- Choose the layout which maximizes the number of two-qubit which could be
  performed. Add its mapped gates, including the SWAPs generated, to the
  output circuit.
- Repeat the above until all gates from the initial circuit are mapped.

For more details on the algorithm, see Sven's blog post:
https://medium.com/qiskit/improving-a-quantum-compiler-48410d7a7084
c                    > [         TU ]  5         [        U[        5      (       a&  Xl        U R                  R                  5       U l        OSU l        Xl        X l        X0l        X@l	        g)a|  LookaheadSwap initializer.

Args:
    coupling_map (Union[CouplingMap, Target]): CouplingMap of the target backend.
    search_depth (int): lookahead tree depth when ranking best SWAP options.
    search_width (int): lookahead tree width when ranking best SWAP options.
    fake_run (bool): if true, it will only pretend to do routing, i.e., no
        swap is effectively added.
N)
super__init__
isinstancer   targetbuild_coupling_mapr   search_depthsearch_widthfake_run)selfr   r   r    r!   	__class__s        i/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/routing/lookahead_swap.pyr   LookaheadSwap.__init__V   sU     	lF++&K $ > > @DDK ,((     c                    U R                   c  [        S5      e[        UR                  5      S:w  d  UR                  R	                  SS5      c  [        S5      e[        U R                   R
                  5      n[        UR                  5      U:  a%  [        S[        UR                  5       SU S35      e[        R                  " XR                  c  U R                   OU R                  5        UR                  S   n[        [        R                  " U5      U R                   U5      n/ n[        UR                  5       5      nU(       a  [        R!                  S	[        U5      5        [#        UUU R$                  U R&                  5      nUc  [        S
5      e[        R!                  S[        UR(                  5      UR*                  5        UR,                  nUR(                  nUR.                  nUR1                  U5        U(       a  M  U R2                  S   c  UR4                  U R2                  S'   O@U R2                  S   R7                  UR4                  UR                  5      U R2                  S'   U R8                  (       a  U$ UR;                  5       n	U H3  n
U	R=                  U
R>                  U
R@                  U
RB                  SS9  M5     U	$ )aT  Run the LookaheadSwap pass on `dag`.

Args:
    dag (DAGCircuit): the directed acyclic graph to be mapped
Returns:
    DAGCircuit: A dag mapped to be compatible with the coupling_map in
        the property_set.
Raises:
    TranspilerError: if the coupling map or the layout are not
    compatible with the DAG, or if the coupling_map=None
Nz/LookaheadSwap cannot run with coupling_map=None   qz-Lookahead swap runs on physical circuits onlyzThe number of DAG qubits (z9) is greater than the number of available device qubits (z).z+Top-level routing step: %d gates remaining.zLLookahead failed to find a swap which mapped gates or improved layout score.z2Found best step: mapped %d gates. Added swaps: %s.final_layoutF)check)"r   r   lenqregsgetphysical_qubitsqubitsr	   $require_layout_isolated_to_componentr   r   r   generate_trivial_layoutlistserial_layersloggerdebug_search_forward_n_swapsr   r    r   r   r   r   extendproperty_setr   composer!   copy_empty_likeapply_operation_backopqargscargs)r"   dagnumber_of_available_qubitsr   current_statemapped_gatesr   	best_stepr   
mapped_dagnodes              r$   runLookaheadSwap.runl   sz    $!"STTsyy>Q#))--T":"B!"QRR%():):)J)J%K"szz?77!,S_,= >,,F+GrK  	;;kk&9""t{{	
 99S>$**84d6G6G
 s0023LLFOH\]/!!!!	I  %b  LLDI**+%% &OOM$11L'77O-3 o6 ^,40=0D0DDn- 150A0A.0Q0Y0Y$$cjj1Dn- ==J ((*
 D++DGGTZZSX+Y ! r&   )r   r!   r   r    r   )   rI   F)	__name__
__module____qualname____firstlineno____doc__r   rG   __static_attributes____classcell__)r#   s   @r$   r   r   2   s    !F!,S Sr&   r   c                   ^ ^ T R                   cE  T R                  T R                  R                  5        VVs1 s H  u  pEXE:  a  XE4OXT4iM     snnS9m [	        T T5      u  pg[        T / Xg5      nU(       a  US:X  a  U$ [        UU 4S jT R                    5       S S9n	[        R                  SUU	SUS-    V
VVs/ s H	  u  poU
4PM     snnn
5        SS[        R                  * pn[        U	5       H  u  nu  pn[        UXrS	-
  U5      nUc  M  [        U5      nUU:  a+  [        R                  S
UU/UR                  -   U5        UUUpnU[        U[!        U	5      S	-
  5      :  d  Mx  Uc  M}  [!        UR"                  5      U:  dR  [!        UR$                  5      [!        U5      :  d0  ['        UR$                  UR(                  5      ['        UU5      :  d  M    O   g[+        UT 5      n[        UR(                  U/UR                  -   UU-   UR"                  -   UR$                  5      n[        R                  SUUR                  5        U$ s  snnf s  snnn
f )ab  Search for SWAPs which allow for application of largest number of gates.

Args:
    state (_SystemState): The ``namedtuple`` collection containing the state of the physical
        system.  This includes the current layout, the coupling map, the canonical register and
        the possible swaps available.
    gates (list): Gates to be mapped.
    depth (int): Number of SWAP layers to search before choosing a result.
    width (int): Number of SWAPs to consider at each layer.
Returns:
    Optional(_Step): Describes the solution step found.  If ``None``, no swaps leading to an
    improvement were found.
N)r   r   c              3   >   >#    U  H  n[        UTT5      v   M     g 7fr   )_score_state_with_swap).0swapgatesr   s     r$   	<genexpr>*_search_forward_n_swaps.<locals>.<genexpr>   s     L	eU	3	3s   c                     U S   $ )Nr    )xs    r$   <lambda>)_search_forward_n_swaps.<locals>.<lambda>   s    adr&   )keyz*At depth %d, ranked candidate swaps: %s...   r(   z0At depth %d, updating best step: %s (score: %f).zAt depth %d, best_swap set: %s.)r   _replacer   	get_edges_map_free_gatesr
   sortedr5   r6   mathinf	enumerater7   _score_stepr   minr,   r   r   _calc_layout_distancer   _swap_ops_from_edge)r   rV   depthwidthabr   r   	base_stepranked_swapsscorerU   _	best_swaprD   
best_scorerank	new_state	next_step
next_scorebest_swap_gateouts   ``                    r$   r7   r7      sO    {{=B=O=O=Y=Y=[\=[TQaeQF!/=[\  
 %4E5$A!LeR?IeqjLLL LL4-9+EAI-FG-F>5-FG (,TDHH9*I&/&=""q	+IPQ	SXY	 +

"LLB...	 04Y
*I Cs<01455%I**+e3y001C4HH))*C*CY__U+OYGH C '>F (E:N
	i+++~%	(>(>>!!	C LL2E3??KJA ] 	Hs   I
<Ic                    [        5       n/ n/ nU R                  R                  nU GH`  nUS   (       dx  [        US   5      R                  nU(       d  M/  UR                  U5      (       a#  UR                  U5        UR                  U5        O[        X`5      nUR                  U5        M  US   S   nUR                  U5      (       a$  UR                  U5        UR                  U5        M  [        U5      S:X  a  [        X`5      nUR                  U5        M  U R                  R                  XWS      XWS      5      S:X  a  [        X`5      nUR                  U5        GM>  UR                  U5        UR                  U5        GMc     X44$ )a  Map all gates that can be executed with the current layout.

Args:
    state (_SystemState): The physical characteristics of the system, including its current
        layout and the coupling map.
    gates (list): Gates to be mapped.

Returns:
    tuple:
        mapped_gates (list): ops for gates that can be executed, mapped onto layout.
        remaining_gates (list): gates that cannot be executed on the layout.
	partitiongraphr   r(   )setr   _v2p_first_op_noder>   intersectionupdateappend_transform_gate_for_systemr,   r   distance)	r   rV   blocked_qubitsrC   remaining_gates
layout_mapgater0   mapped_gates	            r$   rb   rb     se    UNLO""J K #DM288F**622%%f-&&t,8E##K0k"1%&&v..!!&)""4([A4TAK,((1I)>
RS9@UVZ[[4TAK,!!&)""4(= @ ((r&   c                 8   Uc%  SS[        UR                  R                  5      -  -   nUR                  R                  nSnU SU  HP  nUS   (       d  M  US   S   n[        U5      S:X  d  M(  XAR                  R                  X6S      X6S      5      -  nMR     U$ )zsReturn the sum of the distances of two-qubit pairs in each CNOT in gates
according to the layout and the coupling.
Nr   
   r   r|   r_   r(   )r,   r   r/   r   r   r   )rV   r   	max_gatesr   rz   r   r0   s          r$   ri   ri   L  s     c%"4"4"D"DEEE	""J
Cjy!K k"1%v;!%%..z)/DjXYQZF[\\C " Jr&   c                     UR                   R                  5       nUR                  " U 6   UR                  US9n[	        X$5      X4$ )zCalculate the relative score for a given SWAP.

Returns:
    float: the score of the given swap.
    Tuple[int, int]: the input swap that should be performed.
    _SystemState: an updated system state with the new layout contained.
)r   )r   copyrU   r`   ri   )rU   r   rV   trial_layoutrv   s        r$   rS   rS   ^  sD     <<$$&Ltl3I 2DCCr&   c                     [        U R                   Vs/ s H   n[        UR                  5      S:X  d  M  UPM"     sn5      S[        U R                  5      -  -
  $ s  snf )zACount the mapped two-qubit gates, less the number of added SWAPs.r_      )r,   r   r>   r   )stepgs     r$   rg   rg   l  sM     4,,B,aAGG0A,BCa#dN^N^J_F___Bs
   AAc                    ^^ [         R                   " [        U S   5      5      nUR                  mUR                  R                  m[        UU4S jUR                   5       5      Ul        U$ )z6Return op implementing a virtual gate on given layout.r}   c              3   4   >#    U  H  nTTU      v   M     g 7fr   rZ   )rT   rm   device_qregr   s     r$   rW   -_transform_gate_for_system.<locals>.<genexpr>x  s      ZEYZ]!;EYs   )r   r   r   r   r   tupler>   )r   r   mapped_op_noder   r   s      @@r$   r   r   r  sQ    YY~d7m<=N..K""J  Z^EYEY ZZNr&   c                 n   ^ UR                   m[        U4S jU  5       5      n[        [        5       USS9/$ )zDGenerate list of ops to implement a SWAP gate along a coupling edge.c              3   .   >#    U  H
  nTU   v   M     g 7fr   rZ   )rT   ir   s     r$   rW   &_swap_ops_from_edge.<locals>.<genexpr>  s     3dk!nds   rZ   )r=   r>   r?   )r   r   r   r   )edger   	qreg_edger   s      @r$   rj   rj   }  s2    ..K3d33I 9B?@@r&   c                 B    [        S U R                  5        5       5      $ )z!Get the first op node from a DAG.c              3   T   #    U  H  n[        U[        5      (       d  M  Uv   M      g 7fr   )r   r   )rT   rF   s     r$   rW   !_first_op_node.<locals>.<genexpr>  s     L
40Ks   (	()nextnodes)r@   s    r$   r   r     s     LLLLr&   )"rN   collectionsr   loggingrd   %qiskit.circuit.library.standard_gatesr   qiskit.transpiler.basepassesr   qiskit.transpiler.exceptionsr   qiskit.transpiler.layoutr   qiskit.dagcircuitr   qiskit.transpiler.targetr   qiskit.transpiler.passes.layoutr	   	getLoggerrJ   r5   
namedtupler
   r   r   r7   rb   ri   rS   rg   r   rj   r   rZ   r&   r$   <module>r      s    H     : ; 8 + ' + :			8	$ 	w(cd %%3	M& M`Qh3)l$D`AMr&   