
    z	iV                         S r SSKrSSKrSSKJrJr  SSKJrJrJ	r	J
r
  SSKrSSKJrJrJrJr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 " S S5      rg)zH_DAGDependencyV2 class for representing non-commutativity in a circuit.
    N)OrderedDict
namedtuple)DictList	GeneratorAny)QuantumRegisterClassicalRegisterQubitClbitParameterExpression)condition_resources)Bit)	DAGOpNode)DAGDependencyError)CommutationCheckerBitLocations)index	registersc                   N   \ rS rSrSrS r\S 5       r\R                  S 5       rS r	S r
S rS	 rS
 rS rS rS rS rS\S\4S jrS(S jrS rS rS rS rS rS rS rS)S\\\\4   4S jjr S)S\\\\4   4S jjr!S r"S r#S r$S  r%S! r&S" r'S# r(S$ r)S*S% jr*S+S& jr+S'r,g),_DAGDependencyV2(   u  Object to represent a quantum circuit as a Directed Acyclic Graph (DAG)
via operation dependencies (i.e. lack of commutation).

.. warning::

    This is not part of the public API.

The nodes in the graph are operations represented by quantum gates.
The edges correspond to non-commutation between two operations
(i.e. a dependency). A directed edge from node A to node B means that
operation A does not commute with operation B.
The object's methods allow circuits to be constructed.

**Example:**

Bell circuit with no measurement.

.. code-block:: text

          ┌───┐
    qr_0: ┤ H ├──■──
          └───┘┌─┴─┐
    qr_1: ─────┤ X ├
               └───┘

The dependency DAG for the above circuit is represented by two nodes.
The first one corresponds to Hadamard gate, the second one to the CNOT gate
as the gates do not commute there is an edge between the two nodes.

**Reference:**

[1] Iten, R., Moyard, R., Metger, T., Sutter, D. and Woerner, S., 2020.
Exact and practical pattern matching for quantum circuit optimization.
`arXiv:1909.05270 <https://arxiv.org/abs/1909.05270>`_

c                 ,   SU l         0 U l        0 U l        [        R                  " 5       U l        [        5       U l        [        5       U l        / U l	        / U l
        0 U l        0 U l        SU l        0 U l        SU l        SU l        [#        5       U l        g)z#
Create an empty _DAGDependencyV2.
Nr   dt)namemetadata
_key_cacherxPyDAG_multi_graphr   qregscregsqubitsclbits_qubit_indices_clbit_indices_global_phase	_op_namesdurationunitr   comm_checkerselfs    \/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/dagcircuit/dagdependency_v2.py__init___DAGDependencyV2.__init__N   s    
 	   HHJ !]
 ]
 $&#% :<9; 	.0    c                     U R                   $ )z'Return the global phase of the circuit.)r'   r,   s    r.   global_phase_DAGDependencyV2.global_phasex   s     !!!r1   c                     [        U[        5      (       a  Xl        g[        U5      nU(       d  SU l        gUS[        R
                  -  -  U l        g)zSSet the global phase of the circuit.

Args:
    angle (float, ParameterExpression)
r      N)
isinstancer   r'   floatmathpi)r-   angles     r.   r3   r4   }   sC     e011!& %LE%&"%*a$''k%:"r1   c                 ,    [        U R                  5      $ )z*Returns the number of gates in the circuit)lenr    r,   s    r.   size_DAGDependencyV2.size   s    4$$%%r1   c                 V    [         R                  " U R                  5      nUS:  a  U$ S$ )z>Return the circuit depth.
Returns:
    int: the circuit depth
r   )r   dag_longest_path_lengthr    )r-   depths     r.   rB   _DAGDependencyV2.depth   s+    
 **4+<+<=
u))r1   c                 X    [        U R                  5      [        U R                  5      -   $ )z?Return the total number of qubits + clbits used by the circuit.)r=   r#   r$   r,   s    r.   width_DAGDependencyV2.width   s    4;;#dkk"222r1   c                 ,    [        U R                  5      $ )z6Return the total number of qubits used by the circuit.)r=   r#   r,   s    r.   
num_qubits_DAGDependencyV2.num_qubits       4;;r1   c                 ,    [        U R                  5      $ )z>Return the total number of classical bits used by the circuit.)r=   r$   r,   s    r.   
num_clbits_DAGDependencyV2.num_clbits   rJ   r1   c                 `   [        S U 5       5      (       a  [        S5      e[        U R                  5      R	                  U5      nU(       a  [        SU 35      eU HM  nU R                  R                  U5        [        [        U R                  5      S-
  / 5      U R                  U'   MO     g)zAdd individual qubit wires.c              3   L   #    U  H  n[        U[        5      (       + v   M     g 7fN)r7   r   ).0qubits     r.   	<genexpr>._DAGDependencyV2.add_qubits.<locals>.<genexpr>        @:eU+++   "$znot a Qubit instance.zduplicate qubits    N)	anyr   setr#   intersectionappendr   r=   r%   )r-   r#   duplicate_qubitsrR   s       r.   
add_qubits_DAGDependencyV2.add_qubits       @@@@$%<==t{{+88@$'89I8J%KLLEKKu%)5c$++6F6JB)OD& r1   c                 `   [        S U 5       5      (       a  [        S5      e[        U R                  5      R	                  U5      nU(       a  [        SU 35      eU HM  nU R                  R                  U5        [        [        U R                  5      S-
  / 5      U R                  U'   MO     g)zAdd individual clbit wires.c              3   L   #    U  H  n[        U[        5      (       + v   M     g 7frP   )r7   r   )rQ   clbits     r.   rS   ._DAGDependencyV2.add_clbits.<locals>.<genexpr>   rU   rV   znot a Clbit instance.zduplicate clbits rW   N)	rX   r   rY   r$   rZ   r[   r   r=   r&   )r-   r$   duplicate_clbitsrb   s       r.   
add_clbits_DAGDependencyV2.add_clbits   r_   r1   c                 d   [        U[        5      (       d  [        S5      eUR                  U R                  ;   a  [        SUR                   35      eXR                  UR                  '   [        U R                  5      n[        UR                  5       H  nX   U R                  ;   a+  U R                  X      R                  R                  X45        X   U;  d  MJ  U R                  R                  X   5        [        [        U R                  5      S-
  X4/S9U R                  X   '   M     g)z!Add qubits in a quantum register.znot a QuantumRegister instance.duplicate register rW   r   N)r7   r	   r   r   r!   rY   r#   ranger>   r%   r   r[   r   r=   )r-   qregexisting_qubitsjs       r.   add_qreg_DAGDependencyV2.add_qreg   s    $00$%FGG99

"$':499+%FGG $

499dkk*tyy!Aw$---##DG,66==tiHwo-""47+/;$q(dYK0##DG, "r1   c                 d   [        U[        5      (       d  [        S5      eUR                  U R                  ;   a  [        SUR                   35      eXR                  UR                  '   [        U R                  5      n[        UR                  5       H  nX   U R                  ;   a+  U R                  X      R                  R                  X45        X   U;  d  MJ  U R                  R                  X   5        [        [        U R                  5      S-
  X4/S9U R                  X   '   M     g)z#Add clbits in a classical register.z!not a ClassicalRegister instance.rh   rW   ri   N)r7   r
   r   r   r"   rY   r$   rj   r>   r&   r   r[   r   r=   )r-   cregexisting_clbitsrm   s       r.   add_creg_DAGDependencyV2.add_creg   s    $ 122$%HII99

"$':499+%FGG $

499dkk*tyy!Aw$---##DG,66==tiHwo-""47+/;$q(dYK0##DG, "r1   bitreturnc                     [        U[        5      (       a  U R                  U   $ [        U[        5      (       a  U R                  U   $ [        S[        U5       35      e! [         a  n[        SU S35      UeSnAff = f)a  
Finds locations in the _DAGDependencyV2, by mapping the Qubit and Clbit to positional index
BitLocations is defined as: BitLocations = namedtuple("BitLocations", ("index", "registers"))

Args:
    bit (Bit): The bit to locate.

Returns:
    namedtuple(int, List[Tuple(Register, int)]): A 2-tuple. The first element (``index``)
        contains the index at which the ``Bit`` can be found (in either
        :obj:`~_DAGDependencyV2.qubits`, :obj:`~_DAGDependencyV2.clbits`, depending on its
        type). The second element (``registers``) is a list of ``(register, index)``
        pairs with an entry for each :obj:`~Register` in the circuit which contains the
        :obj:`~Bit` (and the index in the :obj:`~Register` at which it can be found).

  Raises:
    DAGDependencyError: If the supplied :obj:`~Bit` was of an unknown type.
    DAGDependencyError: If the supplied :obj:`~Bit` could not be found on the circuit.
z&Could not locate bit of unknown type: zCould not locate provided bit: z). Has it been added to the DAGDependency?N)r7   r   r%   r   r&   r   typeKeyError)r-   ru   errs      r.   find_bit_DAGDependencyV2.find_bit   s    (
	#u%%**3//C''**3//(+QRVWZR[Q\)]^^ 	$1#6_`	s"   #A! #A! 
A! !
B +A;;B c                     [        UUUS9nU R                  R                  U5      Ul        U R	                  5         U R                  UR                  5        g)a  Add a DAGOpNode to the graph and update the edges.

Args:
    operation (qiskit.circuit.Operation): operation as a quantum gate
    qargs (list[~qiskit.circuit.Qubit]): list of qubits on which the operation acts
    cargs (list[Clbit]): list of classical wires to attach to
)opqargscargsN)r   r    add_node_node_id_update_edges_increment_opr~   )r-   	operationr   r   new_nodes        r.   apply_operation_back%_DAGDependencyV2.apply_operation_back  sP     

 !--66x@8;;'r1   c           	      "   [        U R                  5      S-
  nU R                  U5      nS/U-  n[        US-
  SS5       GHA  nX4   (       a  U R                  U5      nU R                  R                  UR                  UR                  UR                  UR                  UR                  UR                  5      (       dp  U R                  R                  XASS05        [        U R                  U R                  U5      5       Vs/ s H  ofR                  PM     sn5      nU H  nSX8'   M	     M  M  [        U R                  U R                  U5      5       Vs/ s H  ofR                  PM     sn5      nU H  nSX8'   M	     GMD     gs  snf s  snf )a@  
Updates DagDependencyV2 by adding edges to the newly added node (max_node)
from the previously added nodes.
For each previously added node (prev_node), an edge from prev_node to max_node
is added if max_node is "reachable" from prev_node (this means that the two
nodes can be made adjacent by commuting them with other nodes), but the two nodes
themselves do not commute.

Currently. this function is only used when creating a new _DAGDependencyV2 from another
representation of a circuit, and hence there are no removed nodes (this is why
iterating over all nodes is fine).
rW   TcommuteFN)r=   r    	_get_noderj   r+   r   r~   r   r   add_edgesortedpredecessorsr   )	r-   max_node_idmax_node	reachableprev_node_id	prev_nodenodepredecessor_idspredecessor_ids	            r.   r   _DAGDependencyV2._update_edges  sf    $++,q0>>+.F[(	 "+/2r:L& NN<8	((00LLOOOOKKNNNN  %%..|9V[J\]&,373D3DT^^T`Ea3bc3b43bc'O +:49	1 +:!* #)/3/@/@P\A]/^_/^t]]/^_# '6N05I- '69 ;$ d `s   F
F
c                 8    U R                   R                  U5      $ )zd
Args:
    node_id (int): label of considered node.

Returns:
    node: corresponding to the label.
)r    get_node_data)r-   node_ids     r.   r   _DAGDependencyV2._get_nodeH  s       ..w77r1   c                     / nU R                   R                  5        H0  nUR                  R                  U;   d  M  UR	                  U5        M2     U$ )z.Get the set of "op" nodes with the given name.)r    nodesr~   r   r[   )r-   namesnamed_nodesr   s       r.   r   _DAGDependencyV2.named_nodesR  sF    %%++-Dww||u$""4( . r1   c                     UR                   U R                  ;   a"  U R                  UR                   ==   S-  ss'   g SU R                  UR                   '   g NrW   )r   r(   r-   r~   s     r.   r   _DAGDependencyV2._increment_opZ  s<    77dnn$NN277#q(#&'DNN277#r1   c                     U R                   UR                     S:X  a  U R                   UR                  	 g U R                   UR                  ==   S-  ss'   g r   )r(   r   r   s     r.   _decrement_op_DAGDependencyV2._decrement_op`  s?    >>"''"a'rww'NN277#q(#r1   c                     U R                   $ )z)Count the occurrences of operation names.)r(   r,   s    r.   	count_ops_DAGDependencyV2.count_opsf  s    ~~r1   c                 H    [        U R                  R                  5       5      $ )z<
Returns:
    generator(dict): iterator over all the nodes.
)iterr    r   r,   s    r.   op_nodes_DAGDependencyV2.op_nodesj  s    
 D%%++-..r1   Nc                 j   ^  U 4S jnUc  Un[        [        R                  " T R                  US95      $ )aU  
Yield nodes in topological order.

Args:
    key (Callable): A callable which will take a DAGNode object and
        return a string sort key. If not specified the
        :attr:`~qiskit.dagcircuit.DAGNode.sort_key` attribute will be
        used as the sort key for each node.

Returns:
    generator(DAGOpNode): node in topological order
c                    > SR                  U4S j[        R                  " U R                  U R                  5       5       5      $ )N,c              3   ^   >#    U  H"  nTR                  U5      R                  S  v   M$     g7f)04dN)r{   r   )rQ   qr-   s     r.   rS   C_DAGDependencyV2.topological_nodes.<locals>._key.<locals>.<genexpr>  s,      9ZA4==#))#./9Zs   *-)join	itertoolschainr   r   )xr-   s    r.   _key0_DAGDependencyV2.topological_nodes.<locals>._key  s7    88 9BRSRYRY9Z  r1   key)r   r    lexicographical_topological_sortr    )r-   r   r   s   `  r.   topological_nodes"_DAGDependencyV2.topological_nodesq  s2    	
 ;CB778I8IsSTTr1   c                 $    U R                  U5      $ )a  
Yield nodes in topological order. This is a wrapper for topological_nodes since
all nodes are op nodes. It's here so that calls to dag.topological_op_nodes can
use either DAGCircuit or _DAGDependencyV2.

Returns:
    generator(DAGOpNode): nodes in topological order.
)r   )r-   r   s     r.   topological_op_nodes%_DAGDependencyV2.topological_op_nodes  s     %%c**r1   c                 ^    [        U R                  R                  UR                  5      5      $ )z;Returns iterator of the successors of a node as DAGOpNodes.)r   r    
successorsr   r-   r   s     r.   r   _DAGDependencyV2.successors  s"    D%%00?@@r1   c                 ^    [        U R                  R                  UR                  5      5      $ )z=Returns iterator of the predecessors of a node as DAGOpNodes.)r   r    r   r   r   s     r.   r   _DAGDependencyV2.predecessors  s"    D%%224==ABBr1   c                 b    U R                   R                  UR                  UR                  5      $ )z5Checks if a second node is in the successors of node.r    has_edger   )r-   r   	node_succs      r.   is_successor_DAGDependencyV2.is_successor  s%      ))$--9K9KLLr1   c                 b    U R                   R                  UR                  UR                  5      $ )z7Checks if a second node is in the predecessors of node.r   )r-   r   	node_preds      r.   is_predecessor_DAGDependencyV2.is_predecessor  s%      )))*<*<dmmLLr1   c                     [         R                  " U R                  UR                  5       Vs1 s H  o R                  U   iM     sn$ s  snf )z5Returns set of the ancestors of a node as DAGOpNodes.)r   	ancestorsr    r   r-   r   r   s      r.   r   _DAGDependencyV2.ancestors  s:    .0ll4;L;Ldmm.\].\!!!$.\]]]   A	c                     [         R                  " U R                  UR                  5       Vs1 s H  o R                  U   iM     sn$ s  snf )z7Returns set of the descendants of a node as DAGOpNodes.)r   descendantsr    r   r   s      r.   r   _DAGDependencyV2.descendants  s=    .0nnT=N=NPTP]P].^_.^!!!$.^___r   c                 j    [        [        R                  " U R                  UR                  5      5      $ )z
Returns an iterator of tuples of (DAGOpNode, [DAGOpNodes]) where the DAGOpNode is the
current node and [DAGOpNode] is its successors in  BFS order.
)r   r   bfs_successorsr    r   r   s     r.   r   _DAGDependencyV2.bfs_successors  s&    
 B%%d&7&7GHHr1   c                 <   [        5       nU R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U R
                  Ul        UR                  U R                  5        UR                  U R                  5        U R                  Ul
        U R                  Ul        U R                  R                  5        H  nUR                  U5        M     U R                  R                  5        H  nUR!                  U5        M     U$ )a  Return a copy of self with the same structure but empty.

That structure includes:
    * name and other metadata
    * global phase
    * duration
    * all the qubits and clbits, including the registers.

Returns:
    _DAGDependencyV2: An empty copy of self.
)r   r   r'   r   r   r+   r]   r#   re   r$   r)   r*   r!   valuesrn   r"   rs   )r-   
target_dagrk   rq   s       r.   copy_empty_like _DAGDependencyV2.copy_empty_like  s     &'
))
#'#5#5
 "mm
 $
"&"3"3
dkk*dkk*"mm
))
JJ%%'D% (JJ%%'D% ( r1   c                     SSK Jn  U" XX#S9$ )a  
Draws the _DAGDependencyV2 graph.

This function needs `pydot <https://github.com/erocarrera/pydot>`, which in turn needs
Graphviz <https://www.graphviz.org/>` to be installed.

Args:
    scale (float): scaling factor
    filename (str): file path to save image to (format inferred from name)
    style (str): 'plain': B&W graph
                 'color' (default): color input/output/op nodes

Returns:
    Ipython.display.Image: if in Jupyter notebook and not saving to file, otherwise None.
r   )
dag_drawer)dagscalefilenamestyle)&qiskit.visualization.dag_visualizationr   )r-   r   r   r   r   s        r.   draw_DAGDependencyV2.draw  s      	Fd(PPr1   c           	        ^ [        5       n[        5       nU Vs/ s H  owR                  PM     nnU(       d  [        S5      eU Hs  n	U[        U	R                  5      -  nU[        U	R                  5      -  n[        U	R                  SS5      n
U
c  MO  UR                  [        U
5      R                  5        Mu     [        U[        UU4S jS9[        UU4S jS9U S9n U R                  R                  XUS9Ul        U R!                  U5        U H  n	U R#                  U	R                  5        M      U$ s  snf ! [        R                   a  n[        S	5      UeSnAff = f)
a  Replace a block of nodes with a single node.

This is used to consolidate a block of DAGOpNodes into a single
operation. A typical example is a block of CX and SWAP gates consolidated
into a LinearFunction. This function is an adaptation of a similar
function from DAGCircuit.

It is important that such consolidation preserves commutativity assumptions
present in _DAGDependencyV2. As an example, suppose that every node in a
block [A, B, C, D] commutes with another node E. Let F be the consolidated
node, F = A o B o C o D. Then F also commutes with E, and thus the result of
replacing [A, B, C, D] by F results in a valid _DAGDependencyV2. That is, any
deduction about commutativity in consolidated _DAGDependencyV2 is correct.
On the other hand, suppose that at least one of the nodes, say B, does not commute
with E. Then the consolidated _DAGDependencyV2 would imply that F does not commute
with E. Even though F and E may actually commute, it is still safe to assume that
they do not. That is, the current implementation of consolidation may lead to
suboptimal but not to incorrect results.

Args:
    node_block (List[DAGOpNode]): A list of dag nodes that represents the
        node block to be replaced
    op (qiskit.circuit.Operation): The operation to replace the
        block with
    wire_pos_map (Dict[~qiskit.circuit.Qubit, int]): The dictionary mapping the qarg to
        the position. This is necessary to reconstruct the qarg order
        over multiple gates in the combined single op node.
    cycle_check (bool): When set to True this method will check that
        replacing the provided ``node_block`` with a single node
        would introduce a cycle (which would invalidate the
        ``_DAGDependencyV2``) and will raise a ``DAGDependencyError`` if a cycle
        would be introduced. This checking comes with a run time
        penalty. If you can guarantee that your input ``node_block`` is
        a contiguous block and won't introduce a cycle when it's
        contracted to a single node, this can be set to ``False`` to
        improve the runtime performance of this method.
Raises:
    DAGDependencyError: if ``cycle_check`` is set to ``True`` and replacing
        the specified block introduces a cycle or if ``node_block`` is
        empty.
z!Can't replace an empty node_block	conditionNc                    > TU    $ rP    r   wire_pos_maps    r.   <lambda>8_DAGDependencyV2.replace_block_with_op.<locals>.<lambda>#  	    LOr1   r   c                    > TU    $ rP   r   r   s    r.   r   r   $  r   r1   )r   r   r   )check_cyclez:Replacing the specified node block would introduce a cycle)rY   r   r   r   r   getattrr~   updater   r$   r   r   r    contract_nodesr   DAGWouldCycler   r   )r-   
node_blockr~   r   cycle_checkblock_qargsblock_cargsr   	block_idsndcondr   exs      `         r.   replace_block_with_op&_DAGDependencyV2.replace_block_with_op  sP   T ee)34AZZ	4 $%HIIB3rxx=(K3rxx=(K255+t4D""#6t#<#C#CD  *CD*CD	
	 $ 1 1 @ @ !A !H 	2Bruu%  G 52  	$L	s   D7!D< <E!EE!)r&   r'   r   r    r(   r%   r$   r+   r"   r)   r   r   r!   r#   r*   )r   r   rP   )gffffff?Ncolor)T)-__name__
__module____qualname____firstlineno____doc__r/   propertyr3   setterr>   rB   rE   rH   rL   r]   re   rn   rs   r   r   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  __static_attributes__r   r1   r.   r   r   (   s   #J(1T " " ; ; &*3  PP""C L @("36j8()/UYy#s7J-K U0	+	)S#:M0N 	+ACMM^`I@Q(Or1   r   )r  r   r9   collectionsr   r   typingr   r   r   r   	rustworkxr   qiskit.circuitr	   r
   r   r   r   qiskit.circuit.controlflowr   r   qiskit.dagcircuit.dagnoder   qiskit.dagcircuit.exceptionsr   "qiskit.circuit.commutation_checkerr   r   r   r   r1   r.   <module>r     sT      / - -   ;  / ; A .*@AN Nr1   