
    i7                        S r SSKJr  SSKJr  SSKJrJrJrJ	r	J
r
  SSKrSSKJr  \
(       a  SSKJrJr  \" S	5      r\" S
5      R(                  R*                  rSrSr " S S5      rg)ziUtilities for working with circuit schedule timing information returned 
from the Qiskit Runtime service.    )annotations)cycle)TupleListSetDictTYPE_CHECKINGN   )plotly_module)FigureScatterz.graph_objectsz.colorsAWGRbarrierc                      \ rS rSrSr  SS jr\SS j5       rSS jr   S       SS jjr	SS jr
SS	 jrSS
 jrSS jrSS jrSrg)CircuitSchedule&   zmThe class encapsulates the data of a Qiskit circuit schedule and contains the functionality
to visualize it.
c                    SU l         SU l        SU l        U R                  U5      nU R	                  U5        [        5       U l        SU l        0 U l        / U l	        [        5       U l
        / U l        g)a  Initialize a CircuitSchedule object with the schedule data generated by the compiler.
The data is loaded as a single string of consecutive hardware instruction separated by
new line arguments, parsed into a Numpy array to allow efficient filtration and
preparation of the data for plotting.

Args:
    circuit_schedule: A schedule data as a string of hardware instructions as returned
    by the compiler.

Attributes:
    channels: A list of channels to be plotted (the rows in the plot).
    type_to_idx: A mapping from data type names to indices of the corresponding data in the
    circuit_scheduling Numpy array
    circuit_scheduling: A Numpy array container for holding and manipulating the data for
    plotting.
    instruction_set: A set of all the different instructions (gates + communication
    instructions) within the circuit scheduling.
    max_time: The duration of the scheduled circuit in cycles (a cycle is a global unit
    which may take a different amount of seconds on different backends).
    color_map: A color map for mapping instructions to colors.
    annotations: A list that contains annotations for traces.
    legend: A legend for the plot (a set of instructions).
    traces: A list of the plotly scatter traces to plot.
N)channelstype_to_idxcircuit_scheduling_load_parsesetinstruction_setmax_time	color_mapr   legendtraces)selfcircuit_scheduleraw_datas      c/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/utils/circuit_schedule.py__init__CircuitSchedule.__init__+   sf    8 #+/"&::./H),!)+') #%'    c                h    [        U[        5      (       a  UR                  S5      nU$ [        S5      e)zLoad the data from a file or a data object.

Args:
    circuit_schedule: A schedule data as a string of hardware instructions as returned
    by the compiler.

Returns:
    Circuit schedule data.

zCircuitSchedule expects a str.)
isinstancestrsplit	TypeError)clsr    datas      r"   r   CircuitSchedule._loadU   s7     &,,#))$/D  <==r%   c                   / nU H  nUS:X  a  M  UR                  S5      nSUS   ;   a  M'  [        U5      S:w  a  [        S5      eUR                  US   US   US   US	   [	        [        US	   5      [        US
   5      -   5      US   US   R                  S5      S   /5        M     / SQn[        U5       VVs0 s H  u  pgXv_M	     snnU l        [        R                  " U5      U l
        gs  snnf )zParse the raw circuit schedule data into a numpy array.

Args:
    raw_data: A list of instruction schedules as strings.

Return:
    None.
 ,shift_phaser      zCannot interpret timeline data that doesn't have the format \                        <Branch, Instruction, Channel, T0, Duration, Pulse>   r
            _)BranchInstructionChannelStartFinishPulseGateNameN)r*   len
ValueErrorappendr)   int	enumerater   nparrayr   )r   r!   r   linewords
data_namesidx	data_names           r"   r   CircuitSchedule._parseg   s     DrzJJsOEa(5zQ M  %%!H!H!H!HE!HE!H56!H!HNN3'*
 , b
AJ:AVWAV~sINAVW"$((+="> Xs   <C/Nc                   Ub^  [        U[        5      (       aI  [        R                  " U R                  SS2U R
                  S   4   U5      nU R                  U   U l        U(       aW  [        R                  R                  U R                  SS2U R
                  S   4   [        5      ) nU R                  U   U l        U(       a;  U R                  SS2U R
                  S   4   [        :g  nU R                  U   U l        U R                  [        R                  " U R                  SS2U R
                  S   4   5         U l        [        R                  " U R                  SS2U R
                  S   4   5      U l        U R                  R                  5         [        U R                  5      U l        [        [        U R                  SS2U R
                  S   4   5      5      U l        [        R                  " U R                  SS2U R
                  S   4   5      U l        [%        ['        U R"                  [)        [*        5      5      5      U l        g)au  Preprocess and filter the parsed circuit schedule data for visualization.

Args:
    filter_awgr: If ``True``, remove all readout channels from scheduling data.
    filter_barriers: If ``True``, remove all barriers from scheduling data.
    included_channels: If not ``None``, remove all channels from scheduling data
       that are not in the ``included_channels`` list.
Nr;   r:   r=   r?   )r(   listrE   isinr   r   char
startswithREADOUT_CHANNEL_PREFIXBARRIERargsortuniquer   sortrC   maxr   r   dictzipr   colorsr   )r   filter_awgrfilter_barriersincluded_channelsmasks        r"   
preprocessCircuitSchedule.preprocess   s    (Z8I4-P-P77''4+;+;I+F(FGIZD '+&=&=d&CD# GG&&''4+;+;I+F(FGI_ D '+&=&=d&CD# **1d.>.>}.M+MNRYYD&*&=&=d&CD#"&"9"9JJt..q$2B2B92M/MNO#
 		$"9"9!T=M=Mi=X:X"YZT]]+C 7 74;K;KH;U8U VWX!yy)@)@DDTDTU_D`A`)abc$"6"6fFGr%   c                H    US:X  a  gUS:X  a  gUS:X  a  g[        SU 35      e)ao  Return y-axis trace shift for a finite duration instruction schedule and its annotation.
The shifts are to distinguish static and dynamic (control-flow) parts of the circuit.

Args:
    branch: The branch type to get the shift for, 'main' / 'then' / 'else'.

Raises:
    ValueError for unsupported branch name.

Returns:
    A y-axis shifts for trace and annotation.
main)皙ٿ皙?r   then)r   rd   g      ?else)rc   r   g      пUnexpected branch provided: rA   r   branchs     r"   !get_trace_finite_duration_y_shift1CircuitSchedule.get_trace_finite_duration_y_shift   s8     V!v!v#;F8DEEr%   c                H    US:X  a  gUS:X  a  gUS:X  a  g[        SU 35      e)aK  Return y-axis trace shift for a zero duration instruction schedule.
The shifts are to distinguish static and dynamic (control-flow) parts of the circuit.

Args:
    branch: The branch type to get the shift for, 'main' / 'then' / 'else'.

Raises:
    ValueError for unsupported branch name.

Returns:
    A y-axis shifts for trace.
rb   r   re   皙?rf   gɿrg   rh   ri   s     r"   get_trace_zero_duration_y_shift/CircuitSchedule.get_trace_zero_duration_y_shift   s8     Vvv;F8DEEr%   c                   Uu  p#pEpgn[        U5      [        U5      peU R                  U5      u  pnU R                  R                  U5      nX-   nX-   nU[        :X  a
  U	S-  n	U
S-  n
[
        R                  UXV-   S-  UUXV-   S-  UUS/UUUUUUUS/SSUSR                  SU-   SU-   S	[        U5      -   S
[        U5      -   S[        Xe-
  5      -   /5      USS0SU R                  U   XR                  ;  S9nU R                  R                  U5        U[        :X  a  SOU SU 3nXV-   S-  X-   SSSS.USS.nU R                  R                  U5        g)zCreate a trace and annotation for a single finite duration instruction schedule.

Args:
    instruction_schedule: A single instruction schedule as a numpy array.
g?r
   Nmarkersx+text<br>Instruction: Pulse: Start: Finish: 
Duration: colorblacktoselfxymode	hoverinfonametextlegendgrouprG   fill	fillcolor
showlegendr0   r8   F
   rz   sizer   r~   r   	showarrowfontr   	textangle)rC   rk   r   indexrS   gor   joinr)   r   r   r   rB   r   )r   instruction_schedulerj   instructionchannelt_it_fpulse	gate_namey0y1annotation_ychannel_y_locy_lowy_hightracer   
annotations                     r"   !trace_finite_duration_instruction1CircuitSchedule.trace_finite_duration_instruction   s    FZBgC	s8SXS  $EEfM++G4"# $JB$JB
 

aa	 	 #k1%C(S) 3sy>1 "7#nnY/ 3K  &
N 	5! ')r)AeW/E)q-%r2

 	
+r%   c                   Uu  p#pEpgn[        U5      [        U5      peU R                  U5      n	U R                  R                  U5      n
X-   nUS-
  nUS-   n[        R                  XUS-   XUS-
  US/XXUS/SSUSR                  SU 3SU 3S	U 3S
U 3SXe-
   3/5      USS0SU R                  U   XR                  ;  S9nU R                  R                  U5        XV-   S-  USSSS.U SU 3SS.nU R                  R                  U5        g)zCreate a trace and annotation for a single zero duration instruction schedule.

Args:
    instruction_schedule: A single instruction schedule as a numpy array.
rn   r4   Nrr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r
   Tr   r   r8   r   r   )rC   ro   r   r   r   r   r   r   r   r   rB   r   )r   r   rj   r   r   r   r   r   r   y_shiftr   y_midr   r   r   r   s                   r"   trace_zero_duration_instruction/CircuitSchedule.trace_zero_duration_instruction/  sd    FZBgC	s8SXS66v>++G4' 

!GS'35VE48#K=1eW%cUOse$ , "7#nnY/ 3'  
* 	5! 9/%r2 k5'*

 	
+r%   c                ~   / nU R                    HQ  nUu          pEnSU;  a.  U R                  U5        U R                  R                  U5        M@  UR	                  U5        MS     U H8  nUu            pFU R                  U5        U R                  R                  U5        M:     UR                  U R                  5        U$ )zIterate through the processed circuit instruction schedules, generate
their traces and annotations, and add those to the figure.

Args:
    fig: A plotly figure to populate with traces.

Return:
    The populated figure.
r2   )r   r   r   addrB   r   
add_tracesr   )r   figshift_phase_instructionsr   r8   r   r   s          r"   populate_figureCircuitSchedule.populate_figureb  s     $& $($;$; 0D-Q1a9E)667KL	* )//0DE %< %= ,@)Q1aA001EFKKOOI& %=
 	t{{#
r%   )	r   r   r   r   r   r   r   r   r   )r    r)   )r    r)   return	List[str])r!   r   r   None)FFN)r[   boolr\   r   r]   rN   r   r   )rj   r)   r   zTuple[float, float, float])rj   r)   r   float)r   znp.arrayr   r   )r   PlotlyFigurer   r   )__name__
__module____qualname____firstlineno____doc__r#   classmethodr   r   r_   rk   ro   r   r   r   __static_attributes__ r%   r"   r   r   &   s    ((((T  ""?L " %"&	)H)H )H  	)H
 
)HVF,F,K,Z1,fr%   r   )r   
__future__r   	itertoolsr   typingr   r   r   r   r	   numpyrE   visualization.utilsr   plotly.graph_objectsr   r   r   r   qualitativePlotlyrZ   rR   rS   r   r   r%   r"   <module>r      sb   $ #  8 8  /D #$	y	!	-	-	4	4    Z Zr%   