
    z	i2)                        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JrJr  SS	KJr  SS
KJr  SSKJr   " S S\5      rg)z,Rescheduler pass to adjust node start times.    )annotations)	Generator)Gate)Delay)Measure)Reset)
DAGCircuit	DAGOpNode
DAGOutNode)AnalysisPass)TranspilerError)Targetc                  l   ^  \ rS rSrSr   S     S	U 4S jjjr\S
S j5       rSS jrSS jr	Sr
U =r$ )ConstrainedReschedule   u3  Rescheduler pass that updates node start times to conform to the hardware alignments.

This pass shifts DAG node start times previously scheduled with one of
the scheduling passes, e.g. :class:`ASAPScheduleAnalysis` or :class:`ALAPScheduleAnalysis`,
so that every instruction start time satisfies alignment constraints.

Examples:

    We assume executing the following circuit on a backend with 16 dt of acquire alignment.

    .. code-block:: text

             ┌───┐┌────────────────┐┌─┐
        q_0: ┤ X ├┤ Delay(100[dt]) ├┤M├
             └───┘└────────────────┘└╥┘
        c: 1/════════════════════════╩═
                                     0

    Note that delay of 100 dt induces a misalignment of 4 dt at the measurement.
    This pass appends an extra 12 dt time shift to the input circuit.

    .. code-block:: text

             ┌───┐┌────────────────┐┌─┐
        q_0: ┤ X ├┤ Delay(112[dt]) ├┤M├
             └───┘└────────────────┘└╥┘
        c: 1/════════════════════════╩═
                                     0

Notes:

    Your backend may execute circuits violating these alignment constraints.
    However, you may obtain erroneous measurement result because of the
    untracked phase originating in the instruction misalignment.
c                   > [         TU ]  5         Xl        X l        Ub>  UR	                  5       U l        X0l        UR                  U l        UR                  U l        gg)ac  Create new rescheduler pass.

The alignment values depend on the control electronics of your quantum processor.

Args:
    acquire_alignment: Integer number representing the minimum time resolution to
        trigger acquisition instruction in units of ``dt``.
    pulse_alignment: Integer number representing the minimum time resolution to
        trigger gate instruction in units of ``dt``.
    target: The :class:`~.Target` representing the target backend, if
        ``target`` is specified then this argument will take
        precedence and ``acquire_alignment`` and ``pulse_alignment`` will be ignored.
N)super__init__acquire_alignpulse_align	durationstargetacquire_alignmentpulse_alignment)selfr   r   r   	__class__s       s/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/scheduling/alignments/reschedule.pyr   ConstrainedReschedule.__init__@   sX    & 	.*#--/DN K!'!9!9D%55D	     c              #  r   #    UR                  U5       H  n[        U[        5      (       a  M  Uv   M      g7f)zGet next non-delay nodes.

Args:
    dag: DAG circuit to be rescheduled with constraints.
    node: Current node.

Returns:
    A list of non-delay successors.
N)
successors
isinstancer   )clsdagnode	next_nodes       r   _get_next_gate$ConstrainedReschedule._get_next_gate\   s,      -Ii44 .s   (7	7c           	        U R                   S   nU R                   R                  SS5      n[        UR                  [        5      (       a  U R
                  nO[        UR                  [        [        45      (       a  U R                  nOV[        UR                  [        5      (       d  [        UR                  SS5      (       a  SnO[        S[        U5       S35      eX2   nUb&  X2   U-  nUS:w  a  [        SXW-
  5      nOSnXh-  nXcU'   U R                  b_   U R                  R                  UR                  UR                    V	s/ s H  oR#                  U	5      R$                  PM     sn	5      n
Xj-   nO+UR&                  S	:X  a  XbR                  R(                  -   nOUn[+        UR                   5      n[        UR                  [        [        45      (       a  Un[+        UR,                  5      nOSn[+        5       nU R/                  X5       H  nX?   n[+        UR                   5      n[        UR                  [        [        45      (       a  UU-   n[+        UR,                  5      nOSn[+        5       n[1        UU-  5      (       a  UU-
  nOSn[1        UU-  5      (       a  UU-
  nOSn[        UU5      nX?   U-   X?'   M     gs  sn	f ! [         a    Sn
 GNkf = f)
a  Update the start time of the current node to satisfy alignment constraints.
Immediate successors are pushed back to avoid overlap and will be processed later.

.. note::

    This logic assumes the all bits in the qregs and cregs synchronously start and end,
    i.e. occupy the same time slot, but qregs and cregs can take
    different time slot due to classical I/O latencies.

Args:
    dag: DAG circuit to be rescheduled with constraints.
    node: Current node.
node_start_timeclbit_write_latencyr   
_directiveFNzUnknown operation type for .delay)property_setgetr"   opr   r   r   r   r   r   getattrr   reprmaxr   r   qargsfind_bitindexnamedurationsetcargsr'   any)r   r$   r%   r*   r+   	alignmentthis_t0misalignmentshiftxr9   new_t1qthis_qubitsnew_t1cthis_clbitsr&   next_t0qnext_qubitsnext_t0cnext_clbitsqreg_overlapcreg_overlapoverlaps                          r   _push_node_back%ConstrainedReschedule._push_node_backk   s    ++,=>"//334I1Mdggt$$((I'5!122**I''7477L%+P+PI!$?T
|1"MNN!' *09<Lq Ay78G$+D! ;;">>--dggW[WaWa7bWaRSQ8M8MWa7bc (GYY'! 0 00GG$**odgg/00Gdjj/KG%K ,,S7I&1Hioo.K),,%(899#&99!)//2!e;,--&1 ;,--&1  ,5G)8)Cg)MO&1 8' 8c" s$   .K 	$K
-K K K K c                Z   SU R                   ;  a0  [        SUR                   SU R                  R                   S35      eU R                   S   nUR                  5        HH  nUR                  U5      nUc  [        S[        U5       S35      eUS:X  a  M7  U R                  X5        MJ     g)	a  Run rescheduler.

This pass should perform rescheduling to satisfy:

    - All DAGOpNode nodes (except for compiler directives) are placed at start time
      satisfying hardware alignment constraints.
    - The end time of a node does not overlap with the start time of successor nodes.

Assumptions:

    - Topological order and absolute time order of DAGOpNode are consistent.
    - All bits in either qargs or cargs associated with node synchronously start.
    - Start time of qargs and cargs may different due to I/O latency.

Based on the configurations above, the rescheduler pass takes the following strategy:

1. The nodes are processed in the topological order, from the beginning of
    the circuit (i.e. from left to right). For every node (including compiler
    directives), the function ``_push_node_back`` performs steps 2 and 3.
2. If the start time of the node violates the alignment constraint,
    the start time is increased to satisfy the constraint.
3. Each immediate successor whose start_time overlaps the node's end_time is
    pushed backwards (towards the end of the wire). Note that at this point
    the shifted successor does not need to satisfy the constraints, but this
    will be taken care of when that successor node itself is processed.
4. After every node is processed, all misalignment constraints will be resolved,
    and there will be no overlap between the nodes.

Args:
    dag: DAG circuit to be rescheduled with constraints.

Raises:
    TranspilerError: If circuit is not scheduled.
r*   zThe input circuit zD is not scheduled. Call one of scheduling passes before running the z pass.NzStart time of z^ is not found. This node is likely added after this circuit is scheduled. Run scheduler again.r   )	r/   r   r8   r   __name__topological_op_nodesr0   r3   rM   )r   r$   r*   r%   
start_times        r   runConstrainedReschedule.run   s    H D$5$55!$SXXJ /&&*nn&=&=%>fF 
 ++,=>,,.D(,,T2J!%$T$ZL 1F F 
 Q  + /r   )r   r   r   r   )   rU   N)r   intr   rV   r   r   )r$   r	   r%   r
   returnz Generator[DAGOpNode, None, None])r$   r	   r%   r
   )r$   r	   )rP   
__module____qualname____firstlineno____doc__r   classmethodr'   rM   rS   __static_attributes____classcell__)r   s   @r   r   r      s`    "L "# 	66 6 	6 68    TNl:, :,r   r   N)r[   
__future__r   collections.abcr   qiskit.circuit.gater   qiskit.circuit.delayr   qiskit.circuit.measurer   qiskit.circuit.resetr   qiskit.dagcircuitr	   r
   r   qiskit.transpiler.basepassesr   qiskit.transpiler.exceptionsr   qiskit.transpiler.targetr   r    r   r   <module>rj      s9    3 " % $ & * & ? ? 5 8 +`,L `,r   