§
    º³Ùgi  ã                  óp   — d Z ddlmZ ddlZddlZddlmZ ddlm	Z	 ej
        rddlmZ dd„Zdd„Zdd„ZdS )zIA collection of functions to convert ScheduleBlock to DAG representation.é    )ÚannotationsN)ÚChannel)ÚUnassignedReferenceError)ÚScheduleBlockÚblockr   Úreturnúrx.PyDAGc                óV   — | j         j        rt          | ¦  «        S t          | ¦  «        S )a)  Convert schedule block instruction into DAG.

    ``ScheduleBlock`` can be represented as a DAG as needed.
    For example, equality of two programs are efficiently checked on DAG representation.

    .. code-block:: python

        from qiskit import pulse

        my_gaussian0 = pulse.Gaussian(100, 0.5, 20)
        my_gaussian1 = pulse.Gaussian(100, 0.3, 10)

        with pulse.build() as sched1:
            with pulse.align_left():
                pulse.play(my_gaussian0, pulse.DriveChannel(0))
                pulse.shift_phase(1.57, pulse.DriveChannel(2))
                pulse.play(my_gaussian1, pulse.DriveChannel(1))

        with pulse.build() as sched2:
            with pulse.align_left():
                pulse.shift_phase(1.57, pulse.DriveChannel(2))
                pulse.play(my_gaussian1, pulse.DriveChannel(1))
                pulse.play(my_gaussian0, pulse.DriveChannel(0))

    Here the ``sched1 `` and ``sched2`` are different implementations of the same program,
    but it is difficult to confirm on the list representation.

    Another example is instruction optimization.

    .. code-block:: python

        from qiskit import pulse

        with pulse.build() as sched:
            with pulse.align_left():
                pulse.shift_phase(1.57, pulse.DriveChannel(1))
                pulse.play(my_gaussian0, pulse.DriveChannel(0))
                pulse.shift_phase(-1.57, pulse.DriveChannel(1))

    In above program two ``shift_phase`` instructions can be cancelled out because
    they are consecutive on the same drive channel.
    This can be easily found on the DAG representation.

    Args:
        block ("ScheduleBlock"): A schedule block to be converted.

    Returns:
        Instructions in DAG representation.

    Raises:
        PulseError: When the context is invalid subclass.
    )Úalignment_contextÚis_sequentialÚ_sequential_allocationÚ_parallel_allocation)r   s    úQ/granary/edev/quantum/lib/python3.11/site-packages/qiskit/pulse/transforms/dag.pyÚblock_to_dagr      s/   € ðj ÔÔ,ð -Ý% eÑ,Ô,Ð,Ý Ñ&Ô&Ð&ó    c                óþ   — t          j        ¦   «         }g }d}| j        D ]H}|                     |¦  «        }|                     ¦   «         dk    r|                     ||f¦  «         |}ŒI|                     |¦  «         |S )zDA helper function to create a DAG of a sequential alignment context.Né   )ÚrxÚPyDAGÚblocksÚadd_nodeÚ	num_nodesÚappendÚadd_edges_from_no_data)r   ÚdagÚedgesÚprev_idÚelmÚnode_ids         r   r   r   U   sƒ   € å
Œ(‰*Œ*€Cà#%€EØ€GØŒ|ð ð ˆØ—,’,˜sÑ#Ô#ˆØ�=Š=‰?Œ?˜QÒÐØ�LŠL˜' 7Ð+Ñ,Ô,Ð,ØˆˆØ×Ò˜uÑ%Ô%Ð%Ø€Jr   c                ó  — t          j        ¦   «         }i }t          ¦   «         }d}| j        D ]°}|                     |¦  «        }	 |j        D ]6}|                     ||¦  «        }|�|                     ||f¦  «         |||<   Œ7ŒW# t          $ rM | 	                    ¦   «          
                    ¦   «         D ]!\  }}|                     ||f¦  «         |||<   Œ"|}Y Œ­w xY w|                     t          |¦  «        ¦  «         |S )zBA helper function to create a DAG of a parallel alignment context.N)r   r   Úsetr   r   ÚchannelsÚpopÚaddr   ÚcopyÚitemsr   Úlist)	r   r   Úslotsr   Úprev_referencer   r   Úchanr   s	            r   r   r   d   s(  € å
Œ(‰*Œ*€Cà "€EÝ"%¡%¤%€EØ€NØŒ|ð %ð %ˆØ—,’,˜sÑ#Ô#ˆð	%Øœð &ð &�ØŸ)š) D¨.Ñ9Ô9�ØÐ&Ø—I’I˜w¨Ð0Ñ1Ô1Ð1Ø%��d‘�ð	&øõ
 (ð 	%ð 	%ð 	%à!&§¢¡¤×!3Ò!3Ñ!5Ô!5ð &ð &‘��gØ—	’	˜7 GÐ,Ñ-Ô-Ð-Ø%��d‘�Ø$ˆNˆNˆNð	%øøøð ×Ò�t E™{œ{Ñ+Ô+Ð+Ø€Js   Á>BÂACÃC)r   r   r   r	   )r   r	   )Ú__doc__Ú
__future__r   ÚtypingÚ	rustworkxr   Úqiskit.pulse.channelsr   Úqiskit.pulse.exceptionsr   ÚTYPE_CHECKINGÚqiskit.pulser   r   r   r   © r   r   ú<module>r4      s¿   ðð PÐ OØ "Ð "Ð "Ð "Ð "Ð "à €€€à Ð Ð Ð ð *Ð )Ð )Ð )Ð )Ð )Ø <Ð <Ð <Ð <Ð <Ð <à	Ôð +Ø*Ð*Ð*Ð*Ð*Ð*ð7'ð 7'ð 7'ð 7'ðtð ð ð ðð ð ð ð ð r   