§
    º³Ùgø  ã                   ó:  — d Z ddlmZ ddlmZmZ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mZ dd
lmZ  ed¬¦  «        	 ddededeeeef                  defd„¦   «         Z ed¬¦  «        	 ddededeeeef                  defd„¦   «         ZdS )zb
The most straightforward scheduling methods: scheduling **as early** or **as late** as possible.
é    )Údefaultdict)ÚListÚOptionalÚUnion)ÚQuantumCircuit)ÚBarrier)ÚSchedule)ÚScheduleConfig)Úlower_gates)Ú	BackendV1Ú	BackendV2)Údeprecate_pulse_dependencyT)Úmoving_to_dynamicsNÚcircuitÚschedule_configÚbackendÚreturnc                 ó"  ‡— t          t          ¦  «        Šddt          t                   dt          ddfˆfd„}g }t          | ||¦  «        }|D ]s}t	          ˆfd„|j        D ¦   «         ¦  «        }|}t          |j        t          ¦  «        s||j        j	        z  }| 
                    |¦  «          ||j        |¦  «         Œtd„ t          ||¦  «        D ¦   «         }	t          j        | ¦  «        }
|	D ]\  }}|
                     ||d	¬
¦  «         Œ|
S )a6  
    Return the pulse Schedule which implements the input circuit using an "as soon as possible"
    (asap) scheduling policy.

    Circuit instructions are first each mapped to equivalent pulse
    Schedules according to the command definition given by the schedule_config. Then, this circuit
    instruction-equivalent Schedule is appended at the earliest time at which all qubits involved
    in the instruction are available.

    Args:
        circuit: The quantum circuit to translate.
        schedule_config: Backend specific parameters used for building the Schedule.
        backend: A backend used to build the Schedule, the backend could be BackendV1
                 or BackendV2.

    Returns:
        A schedule corresponding to the input ``circuit`` with pulses occurring as early as
        possible.
    r   Úinst_qubitsÚtimer   Nc                 ó   •— | D ]}|‰|<   ŒdS ©z>Update the time tracker for all inst_qubits to the given time.N© ©r   r   ÚqÚqubit_time_availables      €úT/granary/edev/quantum/lib/python3.11/site-packages/qiskit/scheduler/methods/basic.pyÚupdate_timesz)as_soon_as_possible.<locals>.update_times8   ó*   ø€ àð 	+ð 	+ˆAØ&*Ð  Ñ#Ð#ð	+ð 	+ó    c              3   ó(   •K  — | ]}‰|         V — Œd S ©Nr   ©Ú.0r   r   s     €r   ú	<genexpr>z&as_soon_as_possible.<locals>.<genexpr>@   ó)   øè è € ÐPÐP°QÐ-¨aÔ0ÐPÐPÐPÐPÐPÐPr    c                 óZ   — g | ](\  }}t          |j        t          ¦  «        °||j        f‘Œ)S r   ©Ú
isinstanceÚscheduler   ©r$   r   Úcpds      r   ú
<listcomp>z'as_soon_as_possible.<locals>.<listcomp>H   óE   € ð ð ð áˆD�#Ý˜#œ,­Ñ0Ô0ðØ	ˆsŒ|Ððð ð r    T©Úinplace©r   )r   Úintr   r   ÚmaxÚqubitsr)   r*   r   ÚdurationÚappendÚzipr	   Úinitialize_fromÚinsert)r   r   r   r   Ústart_timesÚcirc_pulse_defsÚcirc_pulse_defÚ
start_timeÚ	stop_timeÚtimed_schedulesr*   r   Úinstr   s                @r   Úas_soon_as_possiblerA      s^  ø€ õ2 '¥sÑ+Ô+Ðð+ð +¥$¥s¤)ð +µ3ð +¸tð +ð +ð +ð +ð +ð +ð
 €KÝ! '¨?¸GÑDÔD€OØ)ð 7ð 7ˆÝÐPÐPÐPÐP¸.Ô:OÐPÑPÔPÑPÔPˆ
Øˆ	Ý˜.Ô1µ7Ñ;Ô;ð 	:Ø˜Ô0Ô9Ñ9ˆIà×Ò˜:Ñ&Ô&Ð&Øˆ�^Ô*¨IÑ6Ô6Ð6Ð6ðð å˜[¨/Ñ:Ô:ðñ ô €Oõ
 Ô'¨Ñ0Ô0€HØ%ð 2ð 2‰
ˆˆdØ�Š˜˜d¨DˆÑ1Ô1Ð1Ð1Ø€Or    c                 óÒ  ‡‡— t          t          ¦  «        Šddt          t                   dt          ddfˆfd„}g }t          | ||¦  «        }t	          |¦  «        D ]s}t          ˆfd„|j        D ¦   «         ¦  «        }|}t          |j        t          ¦  «        s||j        j
        z  }|                     |¦  «          ||j        |¦  «         Œt‰r+t          d„ ‰                     ¦   «         D ¦   «         ¦  «        ndŠˆfd	„t	          |¦  «        D ¦   «         }	d
„ t          |	|¦  «        D ¦   «         }
t          j        | ¦  «        }|
D ]\  }}|                     ||d¬¦  «         Œ|S )a  
    Return the pulse Schedule which implements the input circuit using an "as late as possible"
    (alap) scheduling policy.

    Circuit instructions are first each mapped to equivalent pulse
    Schedules according to the command definition given by the schedule_config. Then, this circuit
    instruction-equivalent Schedule is appended at the latest time that it can be without allowing
    unnecessary time between instructions or allowing instructions with common qubits to overlap.

    This method should improves the outcome fidelity over ASAP scheduling, because we may
    maximize the time that the qubit remains in the ground state.

    Args:
        circuit: The quantum circuit to translate.
        schedule_config: Backend specific parameters used for building the Schedule.
        backend: A backend used to build the Schedule, the backend could be BackendV1
                 or BackendV2.

    Returns:
        A schedule corresponding to the input ``circuit`` with pulses occurring as late as
        possible.
    r   r   r   r   Nc                 ó   •— | D ]}|‰|<   ŒdS r   r   r   s      €r   r   z)as_late_as_possible.<locals>.update_timesq   r   r    c              3   ó(   •K  — | ]}‰|         V — Œd S r"   r   r#   s     €r   r%   z&as_late_as_possible.<locals>.<genexpr>y   r&   r    c              3   ó   K  — | ]}|V — Œd S r"   r   )r$   Úts     r   r%   z&as_late_as_possible.<locals>.<genexpr>�   s"   è è € Ð=Ð=˜!�AÐ=Ð=Ð=Ð=Ð=Ð=r    c                 ó   •— g | ]}‰|z
  ‘ŒS r   r   )r$   rF   Ú	last_stops     €r   r-   z'as_late_as_possible.<locals>.<listcomp>‚   s   ø€ ÐCÐCÐC Q�9˜q‘=ÐCÐCÐCr    c                 óZ   — g | ](\  }}t          |j        t          ¦  «        °||j        f‘Œ)S r   r(   r+   s      r   r-   z'as_late_as_possible.<locals>.<listcomp>„   r.   r    Tr/   r1   )r   r2   r   r   Úreversedr3   r4   r)   r*   r   r5   r6   Úvaluesr7   r	   r8   r9   )r   r   r   r   Úrev_stop_timesr;   r<   r=   r>   r:   r?   r*   r   r@   rH   r   s                 @@r   Úas_late_as_possiblerM   S   s¼  øø€ õ8 '¥sÑ+Ô+Ðð+ð +¥$¥s¤)ð +µ3ð +¸tð +ð +ð +ð +ð +ð +ð
 €NÝ! '¨?¸GÑDÔD€OÝ" ?Ñ3Ô3ð 7ð 7ˆÝÐPÐPÐPÐP¸.Ô:OÐPÑPÔPÑPÔPˆ
Øˆ	Ý˜.Ô1µ7Ñ;Ô;ð 	:Ø˜Ô0Ô9Ñ9ˆIà×Ò˜iÑ(Ô(Ð(Øˆ�^Ô*¨IÑ6Ô6Ð6Ð6àAUÐ\•Ð=Ð=Ð3×:Ò:Ñ<Ô<Ð=Ñ=Ô=Ñ=Ô=Ð=Ð[\€IØCÐCÐCÐC­(°>Ñ*BÔ*BÐCÑCÔC€Kðð å˜[¨/Ñ:Ô:ðñ ô €Oõ
 Ô'¨Ñ0Ô0€HØ%ð 2ð 2‰
ˆˆdØ�Š˜˜d¨DˆÑ1Ô1Ð1Ð1Ø€Or    r"   )Ú__doc__Úcollectionsr   Útypingr   r   r   Úqiskit.circuit.quantumcircuitr   Úqiskit.circuit.barrierr   Úqiskit.pulse.scheduler	   Úqiskit.scheduler.configr
   Úqiskit.scheduler.loweringr   Úqiskit.providersr   r   Úqiskit.utils.deprecate_pulser   rA   rM   r   r    r   ú<module>rX      s¨  ððð ð $Ð #Ð #Ð #Ð #Ð #Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø *Ð *Ð *Ð *Ð *Ð *Ø *Ð *Ð *Ð *Ð *Ð *à 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø CÐ CÐ CÐ CÐ CÐ Cð Ð¨tÐ4Ñ4Ô4ð 6:ð2ð 2Øð2à#ð2ð �e˜I yÐ0Ô1Ô2ð2ð ð	2ð 2ð 2ñ 5Ô4ð2ðj Ð¨tÐ4Ñ4Ô4ð 6:ð8ð 8Øð8à#ð8ð �e˜I yÐ0Ô1Ô2ð8ð ð	8ð 8ð 8ñ 5Ô4ð8ð 8ð 8r    