
    z	i>                     r    S 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   " S	 S
\5      rg)zQMap (with minimum effort) a DAGCircuit onto a ``coupling_map`` adding swap gates.    )TransformationPass)TranspilerError)
DAGCircuit)Layout)SwapGate)Target)disjoint_utilsc                   <   ^  \ rS rSrSrSU 4S jjrS rS rSrU =r	$ )	BasicSwap   a"  Map (with minimum effort) a DAGCircuit onto a ``coupling_map`` adding swap gates.

The basic mapper is a minimum effort to insert swap gates to map the DAG onto
a coupling map. When a cx is not in the coupling map possibilities, it inserts
one or more swaps in front to make it compatible.
c                    > [         TU ]  5         [        U[        5      (       a&  Xl        U R                  R                  5       U l        OSU l        Xl        X l        g)zBasicSwap initializer.

Args:
    coupling_map (Union[CouplingMap, Target]): Directed graph represented a coupling map.
    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_mapcoupling_mapfake_run)selfr   r   	__class__s      e/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/routing/basic_swap.pyr   BasicSwap.__init__    sI     	lF++&K $ > > @DDK ,     c           
      8   U R                   (       a  U R                  U5      $ UR                  5       nU R                  c  [	        S5      e[        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[        R                  " XR                  c  U R                  OU R                  5        UR                  S   n[        R                  " U5      nUR                  5       nUR!                  5        GHu  nUS   nUR#                  5        GH,  nXXR$                  S      n	XXR$                  S      n
U R                  R'                  X5      S:w  d  MG  [)        5       nUR+                  U5        U R                  R-                  X5      n[/        [        U5      S	-
  5       H2  nX   nXS-      nX^   nX_   nUR1                  [3        5       UU4S
SS9  M4     UR5                  UR                  5      nUR7                  UUS9  [/        [        U5      S	-
  5       H  nUR9                  X   XS-      5        M     GM/     UR5                  UR                  5      nUR7                  UUS9  GMx     U R:                  S   c  XPR:                  S'   U$ U R:                  S   R7                  XQR                  5      U R:                  S'   U$ )zRun the BasicSwap pass on `dag`.

Args:
    dag (DAGCircuit): DAG to map.

Returns:
    DAGCircuit: A mapped DAG.

Raises:
    TranspilerError: if the coupling map or the layout are not
    compatible with the DAG, or if the ``coupling_map=None``.
Nz+BasicSwap cannot run with coupling_map=None   q)Basic swap runs on physical circuits only9The layout does not match the amount of qubits in the DAGgraphr       F)cargscheck)qubitsfinal_layout)r   	_fake_runcopy_empty_liker   r   lenqregsgetr$   physical_qubitsr	   $require_layout_isolated_to_componentr   r   generate_trivial_layoutcopyserial_layerstwo_qubit_opsqargsdistancer   add_qregshortest_undirected_pathrangeapply_operation_backr   reorder_bitscomposeswapproperty_set)r   dagnew_dagcanonical_registertrivial_layoutcurrent_layoutlayersubdaggatephysical_q0physical_q1
swap_layerpathr9   connected_wire_1connected_wire_2qubit_1qubit_2orders                      r   runBasicSwap.run1   s    ==>>#&&%%'$!"OPPsyy>Q#))--T":"B!"MNNszz?S!2!2!B!BCC!"]^^;;kk&9""t{{	
 !YYs^778JK',,.&&(E7^F,,.,ZZ];,ZZ];$$--kG1L!+J''(:;,,EEk_D %c$i!m 4+/:(+/q>("0"B"0"B #77$J'(:"E 8  !5 +77GEOOJuO= !&c$i!m 4&++DJAXG !55 /: #//?EOOF5O1C )F ^,40>n- 	 150A0A.0Q0Y0Y

1Dn- r   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                  S   n[        R                  " U5      nUR                  5       nUR                  5        H  nUS   nUR                  5        H  nXGR                  S      nXGR                  S      n	U R
                  R                  X5      S:w  d  MF  U R
                  R                  X5      n
[        [        U
5      S-
  5       H  nUR!                  X   XS-      5        M     M     M     U R"                  S	   c  X@R"                  S	'   U$ U R"                  S	   R%                  XAR                  5      U R"                  S	'   U$ )
zDo a fake run the BasicSwap pass on `dag`.

Args:
    dag (DAGCircuit): DAG to improve initial layout.

Returns:
    DAGCircuit: The same DAG.

Raises:
    TranspilerError: if the coupling map or the layout are not
    compatible with the DAG.
r   r   Nr   r   r   r   r    r%   )r(   r)   r*   r   r$   r   r+   r   r-   r.   r/   r0   r1   r2   r4   r5   r9   r:   r8   )r   r;   r=   r>   r?   r@   rA   rB   rC   rD   rF   r9   s               r   r&   BasicSwap._fake_run   s    syy>Q#))--T":"B!"MNNszz?S!2!2!B!BCC!"]^^ YYs^778JK',,.&&(E7^F,,.,ZZ];,ZZ];$$--kG1L,,EEk_D %c$i!m 4&++DJAXG !5 / ) ^,40>n- 
 150A0A.0Q0Y0Y

1Dn- 
r   )r   r   r   )F)
__name__
__module____qualname____firstlineno____doc__r   rL   r&   __static_attributes____classcell__)r   s   @r   r   r      s    !"Ob, ,r   r   N)rT   qiskit.transpiler.basepassesr   qiskit.transpiler.exceptionsr   qiskit.dagcircuitr   qiskit.transpiler.layoutr   %qiskit.circuit.library.standard_gatesr   qiskit.transpiler.targetr   qiskit.transpiler.passes.layoutr	   r   r!   r   r   <module>r^      s/    X ; 8 ( + : + :V" Vr   