§
    º³Ùg6  ã                  ó   — d Z ddlmZ ddlZddlmZmZ ddlZddl	m
Z
mZ ddlmZ ddlmZmZ ddlmZ  G d	„ d
ej        ¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        ZdS )zXA collection of passes to reallocate the timeslots of instructions according to context.é    )ÚannotationsN)ÚCallableÚTuple)ÚParameterExpressionÚParameterValueType)Ú
PulseError)ÚScheduleÚScheduleComponent)Úinstruction_duration_validationc                  ó|   — e Zd ZdZdd„Zej        dd„¦   «         Zeej        dd
„¦   «         ¦   «         Z	dd„Z
d„ ZdS )ÚAlignmentKindz)An abstract class for schedule alignment.Úcontext_paramsúTuple[ParameterValueType, ...]c                ó.   — t          |¦  «        | _        dS )zCreate new context.N)ÚtupleÚ_context_params)Úselfr   s     úX/granary/edev/quantum/lib/python3.11/site-packages/qiskit/pulse/transforms/alignments.pyÚ__init__zAlignmentKind.__init__   s   € å$ ^Ñ4Ô4ˆÔÐÐó    Úscheduler	   Úreturnc                ó   — dS )á=  Reallocate instructions according to the policy.

        Only top-level sub-schedules are aligned. If sub-schedules are nested,
        nested schedules are not recursively aligned.

        Args:
            schedule: Schedule to align.

        Returns:
            Schedule with reallocated instructions.
        N© )r   r   s     r   ÚalignzAlignmentKind.align    s	   € ð 	ˆr   Úboolc                ó   — dS )u#  Return ``True`` if this is sequential alignment context.

        This information is used to evaluate DAG equivalency of two :class:`.ScheduleBlock`s.
        When the context has two pulses in different channels,
        a sequential context subtype intends to return following scheduling outcome.

        .. code-block:: text

                â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”�
            D0: â”¤ pulse1 â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€
                â””â”€â”€â”€â”€â”€â”€â”€â”€â”˜  â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”�
            D1: â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¤ pulse2 â”œ
                            â””â”€â”€â”€â”€â”€â”€â”€â”€â”˜

        On the other hand, parallel context with ``is_sequential=False`` returns

        .. code-block:: text

                â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”�
            D0: â”¤ pulse1 â”œ
                â”œâ”€â”€â”€â”€â”€â”€â”€â”€â”¤
            D1: â”¤ pulse2 â”œ
                â””â”€â”€â”€â”€â”€â”€â”€â”€â”˜

        All subclasses must implement this method according to scheduling strategy.
        Nr   ©r   s    r   Úis_sequentialzAlignmentKind.is_sequential/   s	   € ð: 	ˆr   ÚotherÚobjectc                ój   — t          | ¦  «        t          |¦  «        urdS | j        |j        k    rdS dS )z!Check equality of two transforms.FT)Útyper   )r   r!   s     r   Ú__eq__zAlignmentKind.__eq__N   s9   € å�‰:Œ:�T %™[œ[Ð(Ð(Ø�5ØÔ 5Ô#8Ò8Ð8Ø�5Øˆtr   c                óV   — | j         j        › dd                     | j        ¦  «        › d�S )Nú(z, ú))Ú	__class__Ú__name__Újoinr   r   s    r   Ú__repr__zAlignmentKind.__repr__V   s-   € Ø”.Ô)ÐNÐN¨D¯IªI°dÔ6JÑ,KÔ,KÐNÐNÐNÐNr   N)r   r   ©r   r	   r   r	   ©r   r   )r!   r"   r   r   )r*   Ú
__module__Ú__qualname__Ú__doc__r   ÚabcÚabstractmethodr   Úpropertyr    r%   r,   r   r   r   r   r      s¨   € € € € € Ø3Ð3ð5ð 5ð 5ð 5ð 	Ôðð ð ñ Ôðð ØÔðð ð ñ Ôñ „Xðð:ð ð ð ðOð Oð Oð Oð Or   r   c                  óZ   ‡ — e Zd ZdZˆ fd„Zedd„¦   «         Zdd„Zedd„¦   «         Z	ˆ xZ
S )Ú	AlignLeftztAlign instructions in as-soon-as-possible manner.

    Instructions are placed at earliest available timeslots.
    c                óL   •— t          ¦   «                              d¬¦  «         dS )z"Create new left-justified context.r   ©r   N©Úsuperr   ©r   r)   s    €r   r   zAlignLeft.__init__`   ó$   ø€ å‰Œ×Ò¨ÐÑ+Ô+Ð+Ð+Ð+r   r   r   c                ó   — dS ©NFr   r   s    r   r    zAlignLeft.is_sequentiald   ó   € àˆur   r   r	   c                ót   — t          j        |¦  «        }|j        D ]\  }}|                      ||¦  «         Œ|S ©r   )r	   Úinitialize_fromÚchildrenÚ_push_left_append©r   r   ÚalignedÚ_Úchilds        r   r   zAlignLeft.alignh   sH   € õ Ô*¨8Ñ4Ô4ˆØ Ô)ð 	3ð 	3‰HˆAˆuØ×"Ò" 7¨EÑ2Ô2Ð2Ð2àˆr   Úthisr!   r
   c                ó–  ‡ ‡— t          ‰ j        ¦  «        }t          ‰j        ¦  «        }t          ||z  ¦  «        }ˆˆ fd„|D ¦   «         }|rF|t          j        |¦  «                 }‰                      |¦  «        ‰                     |¦  «        z
  }nd} ‰j        ||z
  Ž }t          ||¦  «        }	‰                      |	‰d¬¦  «        S )aC  Return ``this`` with ``other`` inserted at the maximum time over
        all channels shared between ```this`` and ``other``.

        Args:
            this: Input schedule to which ``other`` will be inserted.
            other: Other schedule to insert.

        Returns:
            Push left appended schedule.
        c                óv   •— g | ]5}‰j         ‰                     |¦  «        z
  ‰                     |¦  «        z   ‘Œ6S r   )Ú	stop_timeÚch_stop_timeÚch_start_time©Ú.0Úchannelr!   rI   s     €€r   ú
<listcomp>z/AlignLeft._push_left_append.<locals>.<listcomp>‰   sQ   ø€ ð 
ð 
ð 
àð ŒN˜T×.Ò.¨wÑ7Ô7Ñ7¸%×:MÒ:MÈgÑ:VÔ:VÑVð
ð 
ð 
r   r   T©Úinplace)	ÚsetÚchannelsÚlistÚnpÚargminrM   rN   ÚmaxÚinsert)
rI   r!   Úthis_channelsÚother_channelsÚshared_channelsÚ	ch_slacksÚ
slack_chanÚshared_insert_timeÚother_only_insert_timeÚinsert_times
   ``        r   rD   zAlignLeft._push_left_appendz   sò   øø€ õ ˜DœMÑ*Ô*ˆÝ˜Uœ^Ñ,Ô,ˆÝ˜}¨~Ñ=Ñ>Ô>ˆð
ð 
ð 
ð 
ð 
à*ð
ñ 
ô 
ˆ	ð
 ð 	#Ø(­¬°9Ñ)=Ô)=Ô>ˆJØ!%×!2Ò!2°:Ñ!>Ô!>À×ATÒATÐU_ÑA`ÔA`Ñ!`ÐÐà!"Ðð "5 Ô!4°~ÈÑ7UÐ!WÐåÐ,Ð.DÑEÔEˆà�{Š{˜;¨°tˆ{Ñ<Ô<Ð<r   r.   r-   ©rI   r	   r!   r
   r   r	   )r*   r/   r0   r1   r   r4   r    r   ÚstaticmethodrD   Ú__classcell__©r)   s   @r   r6   r6   Z   s–   ø€ € € € € ðð ð
,ð ,ð ,ð ,ð ,ð ðð ð ñ „Xððð ð ð ð$ ð=ð =ð =ñ „\ð=ð =ð =ð =ð =r   r6   c                  óZ   ‡ — e Zd ZdZˆ fd„Zedd„¦   «         Zdd„Zedd„¦   «         Z	ˆ xZ
S )Ú
AlignRightzrAlign instructions in as-late-as-possible manner.

    Instructions are placed at latest available timeslots.
    c                óL   •— t          ¦   «                              d¬¦  «         dS )z#Create new right-justified context.r   r8   Nr9   r;   s    €r   r   zAlignRight.__init__£   r<   r   r   r   c                ó   — dS r>   r   r   s    r   r    zAlignRight.is_sequential§   r?   r   r   r	   c                óŽ   — t          j        |¦  «        }t          |j        ¦  «        D ]\  }}|                      ||¦  «        }Œ|S rA   )r	   rB   ÚreversedrC   Ú_push_right_prependrE   s        r   r   zAlignRight.align«   sN   € õ Ô*¨8Ñ4Ô4ˆÝ  Ô!2Ñ3Ô3ð 	?ð 	?‰HˆAˆuØ×.Ò.¨w¸Ñ>Ô>ˆGˆGàˆr   rI   r!   r
   c                óœ  ‡ ‡— t          ‰ j        ¦  «        }t          ‰j        ¦  «        }t          ||z  ¦  «        }ˆˆ fd„|D ¦   «         }|rt          |¦  «        ‰j        z   }n‰ j        ‰j        z
  ‰j        z   }|dk     r1‰                      | d¬¦  «         ‰                      d‰d¬¦  «         n‰                      |‰d¬¦  «         ‰ S )aÄ  Return ``this`` with ``other`` inserted at the latest possible time
        such that ``other`` ends before it overlaps with any of ``this``.

        If required ``this`` is shifted  to start late enough so that there is room
        to insert ``other``.

        Args:
           this: Input schedule to which ``other`` will be inserted.
           other: Other schedule to insert.

        Returns:
           Push right prepended schedule.
        c                óf   •— g | ]-}‰                      |¦  «        ‰                     |¦  «        z
  ‘Œ.S r   )rN   rM   rO   s     €€r   rR   z2AlignRight._push_right_prepend.<locals>.<listcomp>Ï   sG   ø€ ð 
ð 
ð 
ØJQˆD×Ò˜wÑ'Ô'¨%×*<Ò*<¸WÑ*EÔ*EÑEð
ð 
ð 
r   r   TrS   )rU   rV   rW   ÚminÚ
start_timerL   Úshiftr[   )rI   r!   r\   r]   r^   r_   rc   s   ``     r   rn   zAlignRight._push_right_prepend½   sò   øø€ õ ˜DœMÑ*Ô*ˆÝ˜Uœ^Ñ,Ô,ˆÝ˜}¨~Ñ=Ñ>Ô>ˆð
ð 
ð 
ð 
ð 
ØUdð
ñ 
ô 
ˆ	ð ð 	NÝ˜i™.œ.¨5Ô+;Ñ;ˆKˆKàœ.¨5¬?Ñ:¸UÔ=MÑMˆKà˜Š?ˆ?Ø�JŠJ˜�|¨TˆJÑ2Ô2Ð2Ø�KŠK˜˜5¨$ˆKÑ/Ô/Ð/Ð/à�KŠK˜ U°DˆKÑ9Ô9Ð9àˆr   r.   r-   rd   )r*   r/   r0   r1   r   r4   r    r   re   rn   rf   rg   s   @r   ri   ri   �   s–   ø€ € € € € ðð ð
,ð ,ð ,ð ,ð ,ð ðð ð ñ „Xððð ð ð ð$ ð ð  ð  ñ „\ð ð  ð  ð  ð  r   ri   c                  óB   ‡ — e Zd ZdZˆ fd„Zed	d„¦   «         Zd
d„Zˆ xZS )ÚAlignSequentialz­Align instructions sequentially.

    Instructions played on different channels are also arranged in a sequence.
    No buffer time is inserted in between instructions.
    c                óL   •— t          ¦   «                              d¬¦  «         dS )zCreate new sequential context.r   r8   Nr9   r;   s    €r   r   zAlignSequential.__init__è   r<   r   r   r   c                ó   — dS ©NTr   r   s    r   r    zAlignSequential.is_sequentialì   ó   € àˆtr   r   r	   c                ó‚   — t          j        |¦  «        }|j        D ]"\  }}|                     |j        |d¬¦  «         Œ#|S )r   TrS   )r	   rB   rC   r[   ÚdurationrE   s        r   r   zAlignSequential.alignð   sO   € õ Ô*¨8Ñ4Ô4ˆØ Ô)ð 	Bð 	B‰HˆAˆuØ�NŠN˜7Ô+¨U¸DˆNÑAÔAÐAÐAàˆr   r.   r-   )	r*   r/   r0   r1   r   r4   r    r   rf   rg   s   @r   ru   ru   á   sv   ø€ € € € € ðð ð,ð ,ð ,ð ,ð ,ð ðð ð ñ „Xððð ð ð ð ð ð ð r   ru   c                  óZ   ‡ — e Zd ZdZdˆ fd„Zedd„¦   «         Zed„ ¦   «         Zdd„Zˆ xZ	S )ÚAlignEquispacedzôAlign instructions with equispaced interval within a specified duration.

    Instructions played on different channels are also arranged in a sequence.
    This alignment is convenient to create dynamical decoupling sequences such as PDD.
    r{   úint | ParameterExpressionc                óN   •— t          ¦   «                              |f¬¦  «         dS )ao  Create new equispaced context.

        Args:
            duration: Duration of this context. This should be larger than the schedule duration.
                If the specified duration is shorter than the schedule duration,
                no alignment is performed and the input schedule is just returned.
                This duration can be parametrized.
        r8   Nr9   )r   r{   r)   s     €r   r   zAlignEquispaced.__init__
  s(   ø€ õ 	‰Œ×Ò¨¨ÐÑ4Ô4Ð4Ð4Ð4r   r   r   c                ó   — dS rx   r   r   s    r   r    zAlignEquispaced.is_sequential  ry   r   c                ó   — | j         d         S ©zReturn context duration.r   ©r   r   s    r   r{   zAlignEquispaced.duration  ó   € ð Ô# AÔ&Ð&r   r   r	   c                ó0  — t          | j        ¦  «         t          d„ |j        D ¦   «         ¦  «        }| j        |k     r|S | j        |z
  }t	          |j        ¦  «        dk    r.t          j        |t	          |j        ¦  «        dz
  ¦  «        \  }}nd}|}t          j        |d¦  «        \  }}t          j        |¦  «        }t          |j
        |z   |z   ¦  «        }|j        D ]4\  }	}
|                     ||
d¬¦  «         t          |j
        |z   ¦  «        }Œ5|S )r   c              3  ó*   K  — | ]\  }}|j         V — Œd S )N)r{   )rP   rG   rH   s      r   ú	<genexpr>z(AlignEquispaced.align.<locals>.<genexpr>,  s(   è è € ÐNÐN±°°5˜Uœ^ÐNÐNÐNÐNÐNÐNr   é   r   é   TrS   )r   r{   ÚsumrC   ÚlenrX   Údivmodr	   rB   ÚintrL   r[   )r   r   Útotal_durationÚtotal_delayÚintervalÚmodÚdelayrF   Ú_t0rG   rH   s              r   r   zAlignEquispaced.align  s%  € õ 	(¨¬Ñ6Ô6Ð6åÐNÐN¸HÔ<MÐNÑNÔNÑNÔNˆØŒ=˜>Ò)Ð)ØˆOà”m nÑ4ˆåˆxÔ Ñ!Ô! AÒ%Ð%õ œI kµ3°xÔ7HÑ3IÔ3IÈAÑ3MÑNÔN‰MˆH�c�càˆHØˆCõ ”Y˜s AÑ&Ô&‰
ˆˆsåÔ*¨8Ñ4Ô4ˆå�'Ô# eÑ+¨cÑ1Ñ2Ô2ˆØ Ô)ð 	4ð 	4‰HˆAˆuØ�NŠN˜3 ¨tˆNÑ4Ô4Ð4Ý�gÔ'¨(Ñ2Ñ3Ô3ˆCˆCàˆr   )r{   r~   r.   r-   )
r*   r/   r0   r1   r   r4   r    r{   r   rf   rg   s   @r   r}   r}     s–   ø€ € € € € ðð ð	5ð 	5ð 	5ð 	5ð 	5ð 	5ð ðð ð ñ „Xðð ð'ð 'ñ „Xð'ð'ð 'ð 'ð 'ð 'ð 'ð 'ð 'r   r}   c                  óp   ‡ — e Zd ZdZdˆ fd„Zedd	„¦   «         Zed
„ ¦   «         Zed„ ¦   «         Zdd„Z	ˆ xZ
S )Ú	AlignFuncaù  Allocate instructions at position specified by callback function.

    The position is specified for each instruction of index ``j`` as a
    fractional coordinate in [0, 1] within the specified duration.

    Instructions played on different channels are also arranged in a sequence.
    This alignment is convenient to create dynamical decoupling sequences such as UDD.

    For example, UDD sequence with 10 pulses can be specified with following function.

    .. code-block:: python

        import numpy as np

        def udd10_pos(j):
            return np.sin(np.pi*j/(2*10 + 2))**2

    .. note::

        This context cannot be QPY serialized because of the callable. If you use this context,
        your program cannot be saved in QPY format.

    r{   r~   Úfuncr   c                óP   •— t          ¦   «                              ||f¬¦  «         dS )ab  Create new equispaced context.

        Args:
            duration: Duration of this context. This should be larger than the schedule duration.
                If the specified duration is shorter than the schedule duration,
                no alignment is performed and the input schedule is just returned.
                This duration can be parametrized.
            func: A function that takes an index of sub-schedule and returns the
                fractional coordinate of of that sub-schedule. The returned value should be
                defined within [0, 1]. The pulse index starts from 1.
        r8   Nr9   )r   r{   r–   r)   s      €r   r   zAlignFunc.__init__a  s+   ø€ õ 	‰Œ×Ò¨°4Ð(8ÐÑ9Ô9Ð9Ð9Ð9r   r   r   c                ó   — dS rx   r   r   s    r   r    zAlignFunc.is_sequentialo  ry   r   c                ó   — | j         d         S r‚   rƒ   r   s    r   r{   zAlignFunc.durations  r„   r   c                ó   — | j         d         S )z"Return context alignment function.rˆ   rƒ   r   s    r   r–   zAlignFunc.funcx  r„   r   r   r	   c                ó¤  — t          | j        ¦  «         | j        |j        k     r|S t          j        |¦  «        }t	          |j        ¦  «        D ]€\  }\  }}| j        |                      |dz   ¦  «        z  }t          |d|j        z  z
  ¦  «        }|dk     s|| j        k    rt          d|› d|› �¦  «        ‚| 	                    ||d¬¦  «         Œ�|S )r   rˆ   g      à?r   zInvalid schedule position t=z is specified at index=TrS   )
r   r{   r	   rB   Ú	enumeraterC   r–   r�   r   r[   )r   r   rF   ÚindrG   rH   Ú	_t_centerr“   s           r   r   zAlignFunc.align}  så   € õ 	(¨¬Ñ6Ô6Ð6àŒ=˜8Ô,Ò,Ð,ØˆOåÔ*¨8Ñ4Ô4ˆÝ(¨Ô):Ñ;Ô;ð 	5ð 	5‰OˆC‘�!�UØœ¨¯	ª	°#¸±'Ñ(:Ô(:Ñ:ˆIÝ�i #¨¬Ñ"6Ñ6Ñ7Ô7ˆCØ�QŠwˆw˜# ¤Ò-Ð-Ý Ð!aÀÐ!aÐ!aÐ\_Ð!aÐ!aÑbÔbÐbØ�NŠN˜3 ¨tˆNÑ4Ô4Ð4Ð4àˆr   )r{   r~   r–   r   r.   r-   )r*   r/   r0   r1   r   r4   r    r{   r–   r   rf   rg   s   @r   r•   r•   H  s±   ø€ € € € € ðð ð0:ð :ð :ð :ð :ð :ð ðð ð ñ „Xðð ð'ð 'ñ „Xð'ð ð'ð 'ñ „Xð'ðð ð ð ð ð ð ð r   r•   )r1   Ú
__future__r   r2   Útypingr   r   ÚnumpyrX   Ú"qiskit.circuit.parameterexpressionr   r   Úqiskit.pulse.exceptionsr   Úqiskit.pulse.scheduler	   r
   Úqiskit.pulse.utilsr   ÚABCr   r6   ri   ru   r}   r•   r   r   r   ú<module>r§      sØ  ðð _Ð ^Ø "Ð "Ð "Ð "Ð "Ð "Ø 
€
€
€
Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð à VÐ VÐ VÐ VÐ VÐ VÐ VÐ VØ .Ð .Ð .Ð .Ð .Ð .Ø =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ø >Ð >Ð >Ð >Ð >Ð >ð>Oð >Oð >Oð >Oð >O�C”Gñ >Oô >Oð >OðB@=ð @=ð @=ð @=ð @=�ñ @=ô @=ð @=ðFAð Að Að Að A�ñ Aô Að AðHð ð ð ð �mñ ô ð ðDBð Bð Bð Bð B�mñ Bô Bð BðJNð Nð Nð Nð N�ñ Nô Nð Nð Nð Nr   