§
    º³ÙgJ  ã                   óz   — d 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 ddlmZ  G d	„ d
e¦  «        ZdS )zQMap (with minimum effort) a DAGCircuit onto a ``coupling_map`` adding swap gates.é    )ÚTransformationPass)ÚTranspilerError)Ú
DAGCircuit)ÚLayout)ÚSwapGate)ÚTarget)Údisjoint_utilsc                   ó0   ‡ — e Zd ZdZdˆ fd„	Zd„ Zd„ Zˆ xZS )Ú	BasicSwapa2  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.
    Fc                 óè   •— t          ¦   «                              ¦   «          t          |t          ¦  «        r&|| _        | j                             ¦   «         | _        nd| _        || _        || _        dS )a  BasicSwap 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      €úa/granary/edev/quantum/lib/python3.11/site-packages/qiskit/transpiler/passes/routing/basic_swap.pyr   zBasicSwap.__init__    sg   ø€ õ 	‰Œ×ÒÑÔÐÝ�l¥FÑ+Ô+ð 	-Ø&ˆDŒKØ $¤× >Ò >Ñ @Ô @ˆDÔÐàˆDŒKØ ,ˆDÔØ ˆŒˆˆó    c           	      ó~  — | j         r|                      |¦  «        S |                     ¦   «         }| j        €t	          d¦  «        ‚t          |j        ¦  «        dk    s|j                             dd¦  «        €t	          d¦  «        ‚t          |j        ¦  «        t          | j        j	        ¦  «        k    rt	          d¦  «        ‚t          j        || j        €| j        n| j        ¦  «         |j        d         }t          j        |¦  «        }|                     ¦   «         }|                     ¦   «         D �]½}|d         }|                     ¦   «         D �]k}||j        d                  }	||j        d                  }
| j                             |	|
¦  «        dk    �r"t)          ¦   «         }|                     |¦  «         | j                             |	|
¦  «        }t/          t          |¦  «        d	z
  ¦  «        D ]L}||         }||dz            }||         }||         }|                     t3          ¦   «         ||fd
d¬¦  «         ŒM|                     |j        ¦  «        }|                     ||¬¦  «         t/          t          |¦  «        d	z
  ¦  «        D ]'}|                     ||         ||dz            ¦  «         Œ(�Œm|                     |j        ¦  «        }|                     ||¬¦  «         �Œ¿| j        d         €|| j        d<   n.|                     | j        d         |j        ¦  «        | j        d<   |S )a8  Run 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 onlyú9The 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Úpathr7   Úconnected_wire_1Úconnected_wire_2Úqubit_1Úqubit_2Úorders                      r   ÚrunzBasicSwap.run1   s=  € ð Œ=ð 	'Ø—>’> #Ñ&Ô&Ð&à×%Ò%Ñ'Ô'ˆàÔÐ$Ý!Ð"OÑPÔPÐPåˆsŒy‰>Œ>˜QÒÐ #¤)§-¢-°°TÑ":Ô":Ð"BÝ!Ð"MÑNÔNÐNåˆsŒz‰?Œ?�S Ô!2Ô!BÑCÔCÒCÐCÝ!Ð"]Ñ^Ô^Ð^ÝÔ;Ø d¤kÐ&9�Ô"Ð"¸t¼{ñ	
ô 	
ð 	
ð !œY sœ^ÐÝÔ7Ð8JÑKÔKˆØ'×,Ò,Ñ.Ô.ˆà×&Ò&Ñ(Ô(ð !	2ñ !	2ˆEØ˜7”^ˆFà×,Ò,Ñ.Ô.ð Hñ H�Ø,¨T¬Z¸¬]Ô;�Ø,¨T¬Z¸¬]Ô;�ØÔ$×-Ò-¨k¸;ÑGÔGÈ1ÒLÑLå!+¡¤�JØ×'Ò'Ð(:Ñ;Ô;Ð;àÔ,×EÒEÀkÐS^Ñ_Ô_�DÝ %¥c¨$¡i¤i°!¡mÑ 4Ô 4ð 
ð 
˜Ø+/°¬:Ð(Ø+/°°q±¬>Ð(à"0Ð1AÔ"B˜Ø"0Ð1AÔ"B˜ð #×7Ò7Ý$™JœJ¨°'Ð(:À"ÈEð 8ñ ô ð ð ð
 +×7Ò7¸¼ÑGÔG�EØ—O’O J°u�OÑ=Ô=Ð=õ !&¥c¨$¡i¤i°!¡mÑ 4Ô 4ð Hð H˜Ø&×+Ò+¨D°¬J¸¸TÀA¹X¼ÑGÔGÐGÐGùà"×/Ò/°´Ñ?Ô?ˆEØ�OŠO˜F¨5ˆOÑ1Ô1Ð1Ñ1àÔ˜^Ô,Ð4Ø0>ˆDÔ˜nÑ-Ð-à0>×0FÒ0FØÔ! .Ô1°3´:ñ1ô 1ˆDÔ˜nÑ-ð ˆr   c           	      ó¶  — t          |j        ¦  «        dk    s|j                             dd¦  «        €t          d¦  «        ‚t          |j        ¦  «        t          | j        j        ¦  «        k    rt          d¦  «        ‚|j        d         }t          j        |¦  «        }| 	                    ¦   «         }| 
                    ¦   «         D ]È}|d         }|                     ¦   «         D ]©}||j        d                  }||j        d                  }	| j                             ||	¦  «        dk    rb| j                             ||	¦  «        }
t          t          |
¦  «        dz
  ¦  «        D ]'}|                     |
|         |
|dz            ¦  «         Œ(ŒªŒÉ| j        d	         €|| j        d	<   n.|                     | j        d	         |j        ¦  «        | j        d	<   |S )
a4  Do 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-   r.   r/   r0   r2   r3   r7   r8   r6   )r   r9   r;   r<   r=   r>   r?   r@   rA   rB   rD   r7   s               r   r$   zBasicSwap._fake_run~   sÕ  € õ ˆsŒy‰>Œ>˜QÒÐ #¤)§-¢-°°TÑ":Ô":Ð"BÝ!Ð"MÑNÔNÐNåˆsŒz‰?Œ?�S Ô!2Ô!BÑCÔCÒCÐCÝ!Ð"]Ñ^Ô^Ð^à œY sœ^ÐÝÔ7Ð8JÑKÔKˆØ'×,Ò,Ñ.Ô.ˆà×&Ò&Ñ(Ô(ð 		Hð 		HˆEØ˜7”^ˆFØ×,Ò,Ñ.Ô.ð Hð H�Ø,¨T¬Z¸¬]Ô;�Ø,¨T¬Z¸¬]Ô;�ØÔ$×-Ò-¨k¸;ÑGÔGÈ1ÒLÐLØÔ,×EÒEÀkÐS^Ñ_Ô_�Då %¥c¨$¡i¤i°!¡mÑ 4Ô 4ð Hð H˜Ø&×+Ò+¨D°¬J¸¸TÀA¹X¼ÑGÔGÐGÐGøðHð Ô˜^Ô,Ð4Ø0>ˆDÔ˜nÑ-Ð-à0>×0FÒ0FØÔ! .Ô1°3´:ñ1ô 1ˆDÔ˜nÑ-ð ˆ
r   )F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rJ   r$   Ú__classcell__)r   s   @r   r   r      si   ø€ € € € € ðð ð!ð !ð !ð !ð !ð !ð"Kð Kð KðZ(ð (ð (ð (ð (ð (ð (r   r   N)rO   Ú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>rX      sË   ðð XÐ Wà ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø (Ð (Ð (Ð (Ð (Ð (Ø +Ð +Ð +Ð +Ð +Ð +Ø :Ð :Ð :Ð :Ð :Ð :Ø +Ð +Ð +Ð +Ð +Ð +Ø :Ð :Ð :Ð :Ð :Ð :ðNð Nð Nð Nð NÐ"ñ Nô Nð Nð Nð Nr   