
    z	iM                    z    S r SSKJ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\5      rg)zQMap (with minimum effort) a DAGCircuit onto a ``coupling_map`` adding swap gates.    )annotationsN)TransformationPass)CouplingMap)TranspilerError)Layout)ApproximateTokenSwapper)Targetc                  L   ^  \ rS rSrSr  S       SU 4S jjjrS rSrU =r$ )LayoutTransformation   zAdds a Swap circuit for a given (partial) permutation to the circuit.

This circuit is found by a 4-approximation algorithm for Token Swapping.
More details are available in the routing code.
c                  > [         TU ]  5         X l        X0l        [	        U[
        5      (       a&  Xl        U R                  R                  5       U l        OSU l        Xl        U R                  c$  [        R                  " [        U5      5      U l        U R                  R                  R                  5       n[        Xd5      U l        XPl        g)a{  LayoutTransformation initializer.

Args:
    coupling_map:
        Directed graph representing a coupling map.

    from_layout (Union[Layout, str]):
        The starting layout of qubits onto physical qubits.
        If the type is str, look up `property_set` when this pass runs.

    to_layout (Union[Layout, str]):
        The final layout of qubits on physical qubits.
        If the type is str, look up ``property_set`` when this pass runs.

    seed (Union[int, np.random.default_rng]):
        Seed to use for random trials.

    trials (int):
        How many randomized trials to perform, taking the best circuit as output.
N)super__init__from_layout	to_layout
isinstancer	   targetbuild_coupling_mapcoupling_mapr   	from_fulllengraphto_undirectedr   token_swappertrials)selfr   r   r   seedr   r   	__class__s          p/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/routing/layout_transformation.pyr   LayoutTransformation.__init__!   s    8 	&"lF++&K $ > > @DDK ,$ + 5 5c)n ED!!''5574UA    c                   [        UR                  5      S:w  d  UR                  R                  SS5      c  [        S5      e[        UR                  5      [        U R
                  R                  5      :  a  [        S5      eU R                  n[        U[        5      (       a   U R                  U   nU R                  n[        U[        5      (       a   U R                  U   nUR                  5       R                  5        VVs0 s H  u  pVXdR                  5       U   _M     nnnU R                  R!                  XpR"                  5      n[%        UR&                  R                  5       S	 S
9 V	s/ s H  oR                  U	S      PM     n
n	UR)                  UR*                  U
S9  U$ ! [         a  n[        SU S35      UeSnAff = f! [         a  n[        SU S35      UeSnAff = fs  snnf s  sn	f )a_  Apply the specified partial permutation to the circuit.

Args:
    dag (DAGCircuit): DAG to transform the layout of.

Returns:
    DAGCircuit: The DAG with transformed layout.

Raises:
    TranspilerError: if the coupling map or the layout are not compatible with the DAG.
        Or if either of string from/to_layout is not found in `property_set`.
   qNz.LayoutTransform runs on physical circuits onlyz9The layout does not match the amount of qubits in the DAGzNo z (from_layout) in property_set.z (to_layout) in property_set.c                    U S   $ )Nr    )xs    r   <lambda>*LayoutTransformation.run.<locals>.<lambda>u   s    ]^_`]ar!   )keyr   )qubits)r   qregsgetr   r+   r   physical_qubitsr   r   strproperty_set	Exceptionr   get_virtual_bitsitemsr   permutation_circuitr   sortedinputmapcomposecircuit)r   dagr   exr   vqubitpqubitpermutation	perm_circir+   s              r   runLayoutTransformation.runL   s    syy>Q#))--T":"B!"RSSszz?S!2!2!B!BCC!"]^^&&k3''b"//< NN	i%%^ --i8	 #.">">"@"F"F"H
"H ..088"H 	 

 &&::;T	,293E3E3K3K3MSa,bc,bq**QqT",bcI%%f5
)  b%K=8W&XY_aab  ^%I;6S&TU[]]^
 ds<   %F+ G G/2G5+
G
5GG

G,G''G,)r   r   r   r   r   r   )N   )r   zCouplingMap | Target | Noner   Layout | strr   rC   r   z int | np.random.Generator | None)	__name__
__module____qualname____firstlineno____doc__r   r@   __static_attributes____classcell__)r   s   @r   r   r      sK     26)1) ")  	)
 /) )V+ +r!   r   )rH   
__future__r   numpynpqiskit.transpiler.basepassesr   qiskit.transpiler.couplingr   qiskit.transpiler.exceptionsr   qiskit.transpiler.layoutr   +qiskit.transpiler.passes.routing.algorithmsr   qiskit.transpiler.targetr	   r   r&   r!   r   <module>rT      s2    X "  ; 2 8 + O +]- ]r!   