
    z	i.                         S 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Jr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJrJr  SSKJr  \R4                  " \5      r " S S\5      rg)z@Routing via SWAP insertion using the SABRE method from Li et al.    N)TransformationPass)TranspilerError)Layout)Target_FakeTarget)CouplingMap)disjoint_utils)default_num_processes)sabre_routing	Heuristic
SetScalingRoutingTarget)NLayoutc                   ~   ^  \ rS rSrSrSU 4S jjr\R                  S 5       r\R                  S 5       r	S r
SrU =r$ )		SabreSwap!   a  Map input circuit onto a backend topology via insertion of SWAPs.

Implementation of the SWAP-based heuristic search from the SABRE qubit
mapping paper [2] (Algorithm 1) with the modifications from the LightSABRE
paper [1]. The heuristic aims to minimize the number of lossy SWAPs inserted
and the depth of the circuit.

This algorithm starts from an initial layout of virtual qubits onto physical
qubits, and iterates over the circuit DAG until all gates are exhausted,
inserting SWAPs along the way. It only considers 2-qubit gates as only those
are germane for the mapping problem (it is assumed that 3+ qubit gates are
already decomposed).

In each iteration, it will first check if there are any gates in the
``front_layer`` that can be directly applied. If so, it will apply them and
remove them from ``front_layer``, and replenish that layer with new gates
if possible. Otherwise, it will try to search for SWAPs, insert the SWAPs,
and update the mapping.

The search for SWAPs is restricted, in the sense that we only consider
physical qubits in the neighborhood of those qubits involved in
``front_layer``. These give rise to a ``swap_candidate_list`` which is
scored according to some heuristic cost function. The best SWAP is
implemented and ``current_layout`` updated.

This transpiler pass adds onto the SABRE algorithm in that it will run
multiple trials of the algorithm with different seeds. The best output,
determined by the trial with the least amount of SWAPed inserted, will
be selected from the random trials.

**References:**

[1] Henry Zou and Matthew Treinish and Kevin Hartman and Alexander Ivrii and Jake Lishman.
"LightSABRE: A Lightweight and Enhanced SABRE Algorithm"
`arXiv:2409.08368 <https://doi.org/10.48550/arXiv.2409.08368>`__
[2] Li, Gushu, Yufei Ding, and Yuan Xie. "Tackling the qubit mapping problem
for NISQ-era quantum devices." ASPLOS 2019.
`arXiv:1809.02573 <https://arxiv.org/pdf/1809.02573.pdf>`_
c                 z  > [         TU ]  5         SU l        [        U[        5      (       a  [        U[
        5      (       d  Xl        ONUc  SU l        OC[        U[
        5      (       a  UR                  5       OUn[        R                  " SS/US9U l        X l	        X0l
        Uc
  [        5       OUU l        X@l        g)a	  SabreSwap initializer.

Args:
    coupling_map (Union[CouplingMap, Target]): CouplingMap of the target backend.
    heuristic (str): The type of heuristic to use when deciding best
        swap strategy ('basic' or 'lookahead' or 'decay').
    seed (int): random seed used to tie-break among candidate swaps.
    fake_run (bool): if true, it only pretend to do routing, i.e., no
        swap is effectively added.
    trials (int): The number of seed trials to run sabre with. These will
        be run in parallel (unless the PassManager is already running in
        parallel). If not specified this defaults to the number of physical
        CPUs on the local system. For reproducible results it is recommended
        that you set this explicitly, as the output will be deterministic for
        a fixed number of trials.

Raises:
    TranspilerError: If the specified heuristic is not valid.

Additional Information:

    The search space of possible SWAPs on physical qubits is explored
    by assigning a score to the layout that would result from each SWAP.
    The goodness of a layout is evaluated based on how viable it makes
    the remaining virtual gates that must be applied. A few heuristic
    cost functions are supported

    - 'basic':

    The sum of distances for corresponding physical qubits of
    interacting virtual qubits in the front_layer.

    .. math::

        H_{basic} = \sum_{gate \in F} D[\pi(gate.q_1)][\pi(gate.q2)]

    - 'lookahead':

    This is the sum of two costs: first is the same as the basic cost.
    Second is the basic cost but now evaluated for the
    extended set as well (i.e. :math:`|E|` number of upcoming successors to gates in
    front_layer F). This is weighted by some amount EXTENDED_SET_WEIGHT (W) to
    signify that upcoming gates are less important that the front_layer.

    .. math::

        H_{decay}=\frac{1}{\left|{F}\right|}\sum_{gate \in F} D[\pi(gate.q_1)][\pi(gate.q2)]
            + W*\frac{1}{\left|{E}\right|} \sum_{gate \in E} D[\pi(gate.q_1)][\pi(gate.q2)]

    - 'decay':

    This is the same as 'lookahead', but the whole cost is multiplied by a
    decay factor. This increases the cost if the SWAP that generated the
    trial layout was recently used (i.e. it penalizes increase in depth).

    .. math::

        H_{decay} = max(decay(SWAP.q_1), decay(SWAP.q_2)) {
            \frac{1}{\left|{F}\right|} \sum_{gate \in F} D[\pi(gate.q_1)][\pi(gate.q2)]\\
            + W *\frac{1}{\left|{E}\right|} \sum_{gate \in E} D[\pi(gate.q_1)][\pi(gate.q2)]
            }
Nucx)basis_gatescoupling_map)super__init___routing_target
isinstancer   r   targetbuild_coupling_mapfrom_configuration	heuristicseedr
   trialsfake_run)selfr   r   r    r"   r!   	__class__s         e/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/routing/sabre_swap.pyr   SabreSwap.__init__J   s    ~ 	#lF++J|[4Y4Y&K! DK lK88 //1!  !33 $KlDK #	17+-V     c                 T    U R                   c  S $ U R                   R                  5       $ N)r   distance_matrixr#   s    r%   dist_matrixSabreSwap.dist_matrix   s)     ++3t_9M9M9]9]9__r'   c                 f    U R                   c  S $ [        U R                   R                  5       5      $ r)   )r   r   coupling_listr+   s    r%   r   SabreSwap.coupling_map   s8    
 ##+ 	
 T11??AB	
r'   c                    U R                   c  [        S5      eU R                  c%  [        R                  " U R                   5      U l        [        UR                  5      S:w  d  UR                  R                  SS5      c  [        S5      e[        UR                  5      nU R                   R                  nX#:  a  [        SU SU S35      eX#:  a  [        S	U S
U S35      e[        U R                  [        5      (       a  U R                  nGOU R                  S:X  a+  [        SU-  S9R                  S[        R                  5      nOU R                  S:X  aJ  [        SU-  S9R                  S[        R                   5      R#                  SS[        R                  5      nOU R                  S:X  aZ  [        SU-  S9R                  S[        R                   5      R#                  SS[        R                  5      R%                  SS5      nO[        SU R                   S35      e[&        R(                  " XR                   5        [*        R,                  " U5      n[.        R0                  " 5       n[3        XR                  XEU R4                  U R6                  5      u  p[.        R0                  " 5       n[8        R;                  SX-
  5        [=        U5       V	s/ s H#  n	UR?                  URA                  U	5      5      PM%     n
n	[C        [E        [G        UR                  U
5      5      5      nU RH                  S   =nc  UOURK                  XR                  5      U RH                  S'   U$ s  sn	f )a5  Run the SabreSwap 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.
Raises:
    TranspilerError: if the coupling map or the layout are not
    compatible with the DAG, or if the coupling_map=None
Nz+SabreSwap cannot run with coupling_map=None   qz*Sabre swap runs on physical circuits only.zFewer qubits in the circuit (z) than the coupling map (z). Have you run a layout pass and then expanded your DAG with ancillas? See `FullAncillaAllocation`, `EnlargeWithAncilla` and `ApplyLayout`.zMore qubits in the circuit (z&) than available in the coupling map (z0). This circuit cannot be routed to this device.basic
   )attempt_limitg      ?	lookaheadg      ?   decaygMbP?   z
Heuristic z not recognized.z.Sabre swap algorithm execution complete in: %sfinal_layout)&r   r   r   r   from_targetlenqregsgetqubits
num_qubitsr   r   r   
with_basicr   SizeConstantwith_lookahead
with_decayr	   $require_layout_isolated_to_componentr   generate_trivial_layouttimeperf_counterr   r!   r    LOGdebugrangevirtual_to_physicalphysical_to_virtualr   dictzipproperty_setcompose)r#   dagnum_dag_qubitsnum_coupling_qubitsr   initial_layoutsabre_startr;   
sabre_stopipermutationlayoutprevs                r%   runSabreSwap.run   s    ;;!"OPP'#0#<#<T[[#ID syy>Q#))--T":"B!"NOOSZZ"kk44/!//? @() *XX  /!.~.> ?() *AA  dnni00I^^w&!^0CDOOZ__I ^^{*^(;<C!4!45R9 
 ^^w&^(;<C!4!45R9E1%	  "Jt~~.>>N"OPP;;CM 88H''))%%y$++tyy
 &&(
		BJD\] >*
* ,,^-O-OPQ-RS* 	 
 S[9:; )).99B  fjj1 	.) 

s   *M/)r   r"   r   r    r   r!   )r4   NFN)__name__
__module____qualname____firstlineno____doc__r   	functoolscached_propertyr,   r   r^   __static_attributes____classcell__)r$   s   @r%   r   r   !   sR    &PU!n ` ` 
 
O Or'   r   )rd   re   loggingrI   qiskit.transpiler.basepassesr   qiskit.transpiler.exceptionsr   qiskit.transpiler.layoutr   qiskit.transpiler.targetr   r   qiskit.transpiler.couplingr   qiskit.transpiler.passes.layoutr	   qiskit.utilsr
   qiskit._accelerate.sabrer   r   r   r   qiskit._accelerate.nlayoutr   	getLoggerr`   rK   r    r'   r%   <module>ru      sQ    G    ; 8 + 8 2 : . X X .!]" ]r'   