
    z	iH                     P    S r SSKrSSKJr  SSKrSSKJr  SSKJ	r	   " S S5      r
g)au  
Directed graph object for representing coupling between physical qubits.

The nodes of the graph correspond to physical qubits (represented as integers) and the
directed edges indicate which physical qubits are coupled and the permitted direction of
CNOT gates. The object has a distance function that can be used to map quantum circuits
onto a device with this coupling.
    N)List)graphviz_draw)CouplingErrorc                   |   \ rS rSrSrSrS$S jrS rS rS r	S	 r
S
 r\S 5       rS rS r\S 5       rS rS rS r\S 5       rS rS rS%S jr\S%S&S jj5       r\S%S&S jj5       r\S%S&S jj5       r\S%S&S jj5       r\S%S&S jj5       r\S%S&S jj5       r\S%S&S jj5       rS r S\!S    4S jr"S  r#S! r$S" r%S#r&g)'CouplingMap   z
Directed graph specifying fixed coupling.

Nodes correspond to physical qubits (integers) and directed edges correspond
to permitted CNOT gates, with source and destination corresponding to control
and target qubits, respectively.
)descriptiongraph_dist_matrix_qubit_list_size_is_symmetricNc                     X l         [        R                  " 5       U l        SU l        SU l        SU l        SU l        Ub6  U R                  R                  U Vs/ s H  n[        U5      PM     sn5        ggs  snf )a  
Create coupling graph. By default, the generated coupling has no nodes.

Args:
    couplinglist (list or None): An initial coupling graph, specified as
        an adjacency list containing couplings, e.g. [[0,1], [0,2], [1,2]].
        It is required that nodes are contiguously indexed starting at 0.
        Missed nodes will be added as isolated nodes in the coupling map.
    description (str): A string to describe the coupling map.
N)
r	   rx	PyDiGraphr
   r   r   r   r   extend_from_edge_listtuple)selfcouplinglistr	   xs       T/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/coupling.py__init__CouplingMap.__init__1   si     '\\^
 
!#JJ,,-M1eAh-MN $-Ms   A7c                 h    U R                   c  [        U R                  5      U l         U R                   $ )z3Return the number of physical qubits in this graph.)r   lenr
   r   s    r   sizeCouplingMap.sizeJ   s%    ::TZZDJzz    c                 6    U R                   R                  5       $ )zu
Gets the list of edges in the coupling graph.

Returns:
    Tuple(int,int): Each edge is a pair of physical qubits.
)r
   	edge_listr   s    r   	get_edgesCouplingMap.get_edgesP   s     zz##%%r   c                 H    [        U R                  R                  5       5      $ )N)iterr
   r!   r   s    r   __iter__CouplingMap.__iter__Y   s    DJJ((*++r   c                     [        U[        5      (       d  [        S5      eXR                  ;   a  [        SU S35      eU R                  R                  U5        SU l        SU l        SU l        g)zAdd a physical qubit to the coupling graph as a node.

physical_qubit (int): An integer representing a physical qubit.

Raises:
    CouplingError: if trying to add duplicate qubit
z#Physical qubits should be integers.zThe physical qubit z! is already in the coupling graphN)	
isinstanceintr   physical_qubitsr
   add_noder   r   r   r   physical_qubits     r   add_physical_qubitCouplingMap.add_physical_qubit\   so     .#.. EFF111%n%55VW  	

N+ 
r   c                     XR                   ;  a  U R                  U5        X R                   ;  a  U R                  U5        U R                  R                  XS5        SU l        SU l        g)zn
Add directed edge to coupling graph.

src (int): source physical qubit
dst (int): destination physical qubit
N)r+   r/   r
   add_edger   r   )r   srcdsts      r   r2   CouplingMap.add_edgeo   s\     ***##C(***##C(

Cd+ !r   c                 r    U R                   c  U R                  R                  5       U l         U R                   $ )z(Returns a sorted list of physical_qubits)r   r
   node_indexesr   s    r   r+   CouplingMap.physical_qubits~   s1     ##zz668Dr   c                 x     [         R                  " U R                  5      $ ! [         R                   a     gf = f)zL
Test if the graph is connected.

Return True if connected, False otherwise
F)r   is_weakly_connectedr
   	NullGraphr   s    r   is_connectedCouplingMap.is_connected   s2    	))$**55|| 		s   " 99c                 8    U R                   R                  U5      $ )zReturn the nearest neighbors of a physical qubit.

Directionality matters, i.e. a neighbor must be reachable
by going one hop in the direction of an edge.
)r
   	neighborsr-   s     r   r?   CouplingMap.neighbors   s     zz##N33r   c                 :    U R                  5         U R                  $ )zReturn the distance matrix for the coupling map.

For any qubits where there isn't a path available between them the value
in this position of the distance matrix will be ``math.inf``.
)compute_distance_matrixr   r   s    r   distance_matrixCouplingMap.distance_matrix   s     	$$&   r   c                     U R                   c4  [        R                  " U R                  S[        R
                  S9U l         gg)a  Compute the full distance matrix on pairs of nodes.

The distance map self._dist_matrix is computed from the graph using
all_pairs_shortest_path_length. This is normally handled internally
by the :attr:`~qiskit.transpiler.CouplingMap.distance_matrix`
attribute or the :meth:`~qiskit.transpiler.CouplingMap.distance` method
but can be called if you're accessing the distance matrix outside of
those or want to pre-generate it.
NT)as_undirected
null_value)r   r   digraph_distance_matrixr
   mathinfr   s    r   rB   #CouplingMap.compute_distance_matrix   s7     $ " : :

$488!D %r   c                 &   XR                  5       :  a  [        U S35      eX R                  5       :  a  [        U S35      eU R                  5         U R                  X4   nU[        R
                  :X  a  [        SU SU 35      e[        U5      $ )a'  Returns the undirected distance between physical_qubit1 and physical_qubit2.

Args:
    physical_qubit1 (int): A physical qubit
    physical_qubit2 (int): Another physical qubit

Returns:
    int: The undirected distance

Raises:
    CouplingError: if the qubits do not exist in the CouplingMap
z not in coupling graphzNo path from z to )r   r   rB   r   rI   rJ   r*   )r   physical_qubit1physical_qubit2ress       r   distanceCouplingMap.distance   s     iik)?"33I JKKiik)?"33I JKK$$& @A$((?-/@_DU VWW3xr   c                     [         R                  " U R                  XSS9nU(       d$  [        S[	        U5       S[	        U5       S35      eX2   $ )aB  Returns the shortest undirected path between physical_qubit1 and physical_qubit2.

Args:
    physical_qubit1 (int): A physical qubit
    physical_qubit2 (int): Another physical qubit
Returns:
    List: The shortest undirected path
Raises:
    CouplingError: When there is no path between physical_qubit1, physical_qubit2.
T)sourcetargetrF   zNodes z and z are not connected)r   digraph_dijkstra_shortest_pathsr
   r   str)r   rM   rN   pathss       r   shortest_undirected_path$CouplingMap.shortest_undirected_path   s\     22JJVZ
 _-.eC4H3II[\  %%r   c                 ^    U R                   c  U R                  5       U l         U R                   $ )zL
Test if the graph is symmetric.

Return True if symmetric, False otherwise
)r   _check_symmetryr   s    r   is_symmetricCouplingMap.is_symmetric   s-     %!%!5!5!7D!!!r   c                     U R                  5       n[        U5      nU H*  u  p4XC4U;  d  M  U R                  R                  XCS5        M,     SU l        SU l        g)z4
Convert uni-directional edges into bi-directional.
N)r"   setr
   r2   r   r   )r   edgesedge_setr3   dests        r   make_symmetricCouplingMap.make_symmetric   sU      u:IC{(*

##Dt4  !!r   c                 6    U R                   R                  5       $ )zL
Calculates symmetry

Returns:
    Bool: True if symmetric, False otherwise
)r
   r\   r   s    r   r[   CouplingMap._check_symmetry   s     zz&&((r   c                 4   S/[        U5      S-   -  n[        U5       H	  u  pEXCU'   M     / nU R                  5        H6  nUS   U;   d  M  US   U;   d  M  UR                  X7S      X7S      /5        M8     [	        5       n[        [        U5      5       H  n	UR                  R                  U	5        M      UR                  R                  U V
s/ s H  n
[        U
5      PM     sn
5        U(       a   UR                  5       (       d  [        S5      eU$ s  sn
f )a  Returns a reduced coupling map that
corresponds to the subgraph of qubits
selected in the mapping.

Args:
    mapping (list): A mapping of reduced qubits to device
        qubits.
    check_if_connected (bool): if True, checks that the reduced
        coupling map is connected.

Returns:
    CouplingMap: A reduced coupling_map for the selected qubits.

Raises:
    CouplingError: Reduced coupling map must be connected.
N   r   zcoupling_map must be connected.)max	enumerater"   appendr   ranger   r
   r,   r   r   r<   r   )r   mappingcheck_if_connectedinv_mapidxvalreduced_cmapedgereduced_coupling_mapnoder   s              r   reduceCouplingMap.reduce   s   $ &CL1,-!'*HCCL + NN$DAw'!d1g&8##W!W%5wAw7G$HI %  +}#g,'D &&//5 (""88L9YLq%(L9YZ&:&G&G&I&I ABB## :Zs   Dreturnc                    U " SS9nU(       a&  [         R                  R                  U5      Ul        U$ / n[	        U5       H'  n[	        U5       H  nUR                  Xe45        M     M)     UR                  R                  U5        U$ )z2Return a fully connected coupling map on n qubits.fullr	   )r   
generatorsdirected_mesh_graphr
   rl   rk   r   )cls
num_qubitsbidirectionalcmapr!   ijs          r   	from_fullCouplingMap.from_full&  s{     v&:::FDJ  I:&qA$$aV, " ' JJ,,Y7r   c                 V    U " SS9n[         R                  R                  XS9Ul        U$ )z6Return a coupling map of n qubits connected in a line.liner{   r   )r   r|   directed_path_graphr
   r~   r   r   r   s       r   	from_lineCouplingMap.from_line4  s,     v&]]66z6_
r   c                 V    U " SS9n[         R                  R                  XS9Ul        U$ )zQReturn a coupling map of n qubits connected to each of their neighbors in a ring.ringr{   r   )r   r|   directed_cycle_graphr
   r   s       r   	from_ringCouplingMap.from_ring;  s,     v&]]77
7`
r   c                 X    U " SS9n[         R                  R                  XUS9Ul        U$ )zNReturn a coupling map of qubits connected on a grid of num_rows x num_columns.gridr{   r   )r   r|   directed_grid_graphr
   )r~   num_rowsnum_columnsr   r   s        r   	from_gridCouplingMap.from_gridB  s5     v&]]66 7 

 r   c                 V    U " SS9n[         R                  R                  XS9Ul        U$ )a  Return a heavy hexagon graph coupling map.

A heavy hexagon graph is described in:

https://journals.aps.org/prx/abstract/10.1103/PhysRevX.10.011022

Args:
    distance (int): The code distance for the generated heavy hex
        graph. The value for distance can be any odd positive integer.
        The distance relates to the number of qubits by:
        :math:`n = \frac{5d^2 - 2d - 1}{2}` where :math:`n` is the
        number of qubits and :math:`d` is the ``distance`` parameter.
    bidirectional (bool): Whether the edges in the output coupling
        graph are bidirectional or not. By default this is set to
        ``True``
Returns:
    CouplingMap: A heavy hex coupling graph
z	heavy-hexr{   r   )r   r|   directed_heavy_hex_graphr
   r~   rP   r   r   s       r   from_heavy_hexCouplingMap.from_heavy_hexK  s,    ( {+]];;H;b
r   c                 V    U " SS9n[         R                  R                  XS9Ul        U$ )a  Return a heavy square graph coupling map.

A heavy square graph is described in:

https://journals.aps.org/prx/abstract/10.1103/PhysRevX.10.011022

Args:
    distance (int): The code distance for the generated heavy square
        graph. The value for distance can be any odd positive integer.
        The distance relates to the number of qubits by:
        :math:`n = 3d^2 - 2d` where :math:`n` is the
        number of qubits and :math:`d` is the ``distance`` parameter.
    bidirectional (bool): Whether the edges in the output coupling
        graph are bidirectional or not. By default this is set to
        ``True``
Returns:
    CouplingMap: A heavy square coupling graph
zheavy-squarer{   r   )r   r|   directed_heavy_square_graphr
   r   s       r   from_heavy_squareCouplingMap.from_heavy_squarec  s3    ( ~.]]>> ? 

 r   c                 X    U " SS9n[         R                  R                  XUS9Ul        U$ )a  Return a hexagonal lattice graph coupling map.

Args:
    rows (int): The number of rows to generate the graph with.
    cols (int): The number of columns to generate the graph with.
    bidirectional (bool): Whether the edges in the output coupling
        graph are bidirectional or not. By default this is set to
        ``True``
Returns:
    CouplingMap: A hexagonal lattice coupling graph
zhexagonal-latticer{   r   )r   r|    directed_hexagonal_lattice_graphr
   )r~   rowscolsr   r   s        r   from_hexagonal_lattice"CouplingMap.from_hexagonal_lattice}  s7     23]]CCm D 

 r   c                 Z    [        [        R                  " U R                  5      [        S9$ )z:Return a set of qubits in the largest connected component.)key)ri   r   weakly_connected_componentsr
   r   r   s    r   largest_connected_component'CouplingMap.largest_connected_component  s    211$**=3GGr   c                 B   U R                   R                  5        H  nXR                   U'   M     [        R                  " U R                   5      n/ nU HG  n[	        5       nU R                   R                  [        U5      5      Ul         UR                  U5        MI     U$ )a)  Separate a :Class:`~.CouplingMap` into subgraph :class:`~.CouplingMap`
for each connected component.

The connected components of a :class:`~.CouplingMap` are the subgraphs
that are not part of any larger subgraph. For example, if you had a
coupling map that looked like::

    0 --> 1   4 --> 5 ---> 6 --> 7
    |     |
    |     |
    V     V
    2 --> 3

then the connected components of that graph are the subgraphs::

    0 --> 1
    |     |
    |     |
    V     V
    2 --> 3

and::

    4 --> 5 ---> 6 --> 7

For a connected :class:`~.CouplingMap` object there is only a single connected
component, the entire :class:`~.CouplingMap`.

This method will return a list of :class:`~.CouplingMap` objects, one for each connected
component in this :class:`~.CouplingMap`. The data payload of each node in the
:attr:`~.CouplingMap.graph` attribute will contain the qubit number in the original
graph. This will enables mapping the qubit index in a component subgraph to
the original qubit in the combined :class:`~.CouplingMap`. For example::

    from qiskit.transpiler import CouplingMap

    cmap = CouplingMap([[0, 1], [1, 2], [2, 0], [3, 4], [4, 5], [5, 3]])
    component_cmaps = cmap.connected_components()
    print(component_cmaps[1].graph[0])

will print ``3`` as index ``0`` in the second component is qubit 3 in the original cmap.

Returns:
    list: A list of :class:`~.CouplingMap` objects for each connected
        components. The order of this list is deterministic but
        implementation specific and shouldn't be relied upon as
        part of the API.
)r
   node_indicesr   r   r   subgraphsortedrk   )r   ru   
componentsoutput_list	componentnew_cmaps         r   connected_components CouplingMap.connected_components  s    d JJ++-D#JJt .33DJJ?
#I"}H!ZZ00	1BCHNx( $ r   c                     SnU R                  5       (       aH  US-  nUSR                  U R                  5        VVs/ s H  u  p#SU SU S3PM     snn5      -  nUS-  nU$ s  snnf )z5Return a string representation of the coupling graph. [z, ])r"   join)r   stringr3   r4   s       r   __str__CouplingMap.__str__  sm    >>cMFdiiT^^EU VEUz1SEC5!2EU VWWFcMF !Ws   A"c                     [        U[        5      (       d  g[        U R                  R	                  5       5      [        UR                  R	                  5       5      :H  $ )a.  Check if the graph in ``other`` has the same node labels and edges as the graph in
``self``.

This function assumes that the graphs in :class:`.CouplingMap` instances are connected.

Args:
    other (CouplingMap): The other coupling map.

Returns:
    bool: Whether or not other is isomorphic to self.
F)r)   r   r_   r
   r!   )r   others     r   __eq__CouplingMap.__eq__  sC     %--4::'')*c%++2G2G2I.JJJr   c                 *    [        U R                  SS9$ )zDraws the coupling map.

This function calls the :func:`~rustworkx.visualization.graphviz_draw` function from the
``rustworkx`` package to draw the :class:`CouplingMap` object.

Returns:
    PIL.Image: Drawn coupling map.

neato)method)r   r
   r   s    r   drawCouplingMap.draw  s     TZZ88r   )r   r   r   r   r	   r
   )NN)T)rx   r   )'__name__
__module____qualname____firstlineno____doc__	__slots__r   r   r"   r&   r/   r2   propertyr+   r<   r?   rC   rB   rP   rX   r\   rc   r[   rv   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__ r   r   r   r      sF   IO2&,&"    	4 ! !.&( " "")&$P          .  2  $H:d=&9 :xK 9r   r   )r   rI   typingr   	rustworkxr   rustworkx.visualizationr   qiskit.transpiler.exceptionsr   r   r   r   r   <module>r      s'       1 6U9 U9r   