
    iN5                       S r SSKJr  SSKJr  SSKJrJr  SSKr	SSK
Jr  SS	KJr  \(       a  SS
KJr  SSKJr           S                     SS jjr        S                   SS jjr     S                 SS jjr    S               SS jjrSS jrSS jrg)z>Functions to visualize :class:`~.EstimatorPubResult` ZNE data.    )annotations)product)SequenceTYPE_CHECKINGN   )plotly_module   )EstimatorPubResult)Figure)Scatterc
                   [        S5      R                  n
[        S5      R                  n[        U R                  5      nUc;  [        S U R                  R                   5       6  Vs/ s H  o(       d  M  UPM     nnUc)  U Vs/ s H  nS[        U5      S:w  a  UOUS    3PM     nn[        X5        US   nUS   nUS	   nU
" U	[        R                  " SS[        U5      5      5      n[        [        U5      U5      u  nnU" U(       a  UOUU[        U5      -   S
US9n[        5       n[        U5       GHf  u  nn[        UU5      u  nnUS-   nUS-   nU R                  R                   U   nUR#                  [%        UUU R                  R&                  U   UU   SS9UUS9  UR)                  UU[        R*                  " U5      U-
  [        R,                  " U5      U-   /S9  [        U5       H  u  nnU R                  R.                  U   U   nU R                  R0                  U   U   n[3        UU:  5      (       d  [3        [5        U5      U:  5      (       a  Mo  UR7                  [9        UUUUUUUU   UU;  5      UUS9  UR;                  U5        M     GMi     UR=                  UUSSS9  UR?                  S
SS
SSS9  UR)                  S
SS
SSS9  U$ s  snf s  snf )aJ  Plot the zero noise extrapolation data in an :class:`~.EstimatorPubResult`.

This function generates a subfigure for each estimated expectation value.

Args:
    result: An :class:`~.EstimatorPubResult`.
    indices: The indices of the expectation values to include in the plot. If ``None``, includes all
        values. See :class:`~.ZneOptions` for information on the indexing scheme.
    names: The names to assign to the expectation values. If ``None``, the names correspond to the
        indices.
    num_stds: The number of standard deviations to include around each fit.
    max_mag: The maximum magnitude of expectation values to include. If ``evs_extrapolated`` has a
        greater magnitude than this value, the expectation value is omitted from the plot.
    max_std: The maximum standard deviation to include. If ``stds_extrapolated`` is greater than
        this value for an expectation value and extrapolator, the fit is omitted from the plot.
    height: The height of the plot in pixels.
    width: The width of the plot in pixels.
    num_cols: The maximum number of columns in the figure.
    colorscale: The colorscale to use.

Returns:
    A plotly figure.

Raises:
    ValueError: If ``result`` does not contain zero noise extrapolation data.
    ValueError: If the length of ``names`` is not equal to the length of ``indices``.
.colors	.subplotsc              3  8   #    U  H  n[        U5      v   M     g 7fNrange.0ss     c/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/visualization/draw_zne.py	<genexpr>draw_zne_evs.<locals>.<genexpr>I        +P>OE!HH>O   evs_r   r   noise_factorsextrapolated_noise_factorsextrapolatorsT)colsrowsshared_xaxessubplot_titlesblack)namecolor)colrow)r'   r(   r   )r    r!   whiterightheightwidthplot_bgcolorhoverlabel_alignoutside	lightgreymirrorticksshowline	linecolor	gridcolor) r   sample_colorscalemake_subplots_validate_metadatametadatar   datashapelen_validate_namesnplinspacedivmodboolset	enumerateevs_noise_factors	add_trace_scatter_tracestds_noise_factorsupdate_yaxesminmaxevs_extrapolatedstds_extrapolatedanyabs
add_traces_line_fill_traceaddupdate_layoutupdate_xaxes)resultindicesnamesnum_stdsmax_magmax_stdr,   r-   num_cols
colorscaler8   r9   zne_metadataidxor   e_noise_factorsr   colorsdivmodfigmodelsir'   r(   evsidx_mmodelstdss                                 r   draw_zne_evsrl      s   N &i0BB!+.<<M%foo6L")+Pfkk>O>O+P"QY"Q3UX3"QY}>EFg4SVq[ad34gFG# 1M"#?@O 1Mz2;;q!S=O+PQFc'lH-HC
X#C$s)O$_dC UFG$3!X&SAgAgkk++C0..s31X  	 
	
 	Sc"&&+2GPSW^I^1_`%m4LE5++..s3E:C;;005e<D4'>""c#c(W*<&=&=NN #5M'	    JJu' 5# %L  	         J_ Z Gs   (
K6K#K!c	                2   [        S5      R                  n	[        S5      R                  n
[        U R                  5      nUc;  [        S U R                  R                   5       6  Vs/ s H  o(       d  M  UPM     nnUc)  U Vs/ s H  nS[        U5      S:w  a  UOUS    3PM     nn[        X5        US   nUS   nUS	   nU	" U[        R                  " SS[        U5      5      5      nU
" [        U5      US
9n[        U5       H  u  nnSnUU   n[        U5       H  u  nnU R                  R                  U   U   nU R                  R                  U   U   n[        UU:  5      (       d  [        [!        U5      U:  5      (       a  Mo  UR#                  [%        UU R                  R&                  U   U R                  R(                  U   UU   UUU5      /[+        UUUUUUUS9QSUS-   S9  SnM     M     UR-                  UUSSS9  UR/                  SSSSSS9  UR1                  [        R2                  " U R                  R&                  5      U-
  [        R4                  " U R                  R&                  5      U-   /SSSSSS9  U$ s  snf s  snf )a   Plot the zero noise extrapolation data in an :class:`~.EstimatorPubResult`.

This function generates a subfigure for each extrapolator.

Args:
    result: An :class:`~.EstimatorPubResult`.
    indices: The indices of the expectation values to include in the plot. If ``None``, includes all
        values. See :class:`~.ZneOptions` for information on the indexing scheme.
    names: The names to assign to the expectation values. If ``None``, the names correspond to the
        indices.
    num_stds: The number of standard deviations to include around each fit.
    max_mag: The maximum magnitude of expectation values to include. If ``evs_extrapolated`` has a
        greater magnitude than this value, the expectation value is omitted from the plot.
    max_std: The maximum standard deviation to include. If ``stds_extrapolated`` is greater than
        this value for an expectation value and extrapolator, the fit is omitted from the plot.
    height: The height of the plot in pixels.
    width: The width of the plot in pixels.
    colorscale: The colorscale to use.

Returns:
    A plotly figure.

Raises:
    ValueError: If ``result`` does not contain zero noise extrapolation data.
    ValueError: If the length of ``names`` is not equal to the length of ``indices``.
r   r   c              3  8   #    U  H  n[        U5      v   M     g 7fr   r   r   s     r   r   )draw_zne_extrapolators.<locals>.<genexpr>   r   r   r   r   r   r   r   r   )r    r#   T)r%   legend_groupr&   )r!   r    Fr)   r*   r+   r0   r$   r1   r2   )r   r3   r4   r5   r6   r7   )r   r8   r9   r:   r;   r   r<   r=   r>   r?   r@   rA   rE   rM   rN   rO   rP   rQ   rH   rF   rI   rR   rT   rU   rJ   rK   rL   )rV   rW   rX   rY   rZ   r[   r,   r-   r]   r8   r9   r^   r_   r`   r   ra   r   rb   re   rg   show_legendr&   ri   extrapolatorrh   rk   s                             r   draw_zne_extrapolatorsrs      s   J &i0BB!+.<<M%foo6L")+Pfkk>O>O+P"QY"Q3UX3"QY}>EFg4SVq[ad34gFG# 1M"#?@O 1Mz2;;q!S\+JKF
S/
NCG$3q	#,]#;E<++..s3E:C;;005e<D4'>""c#c(W*<&=&=NN"%55c:66s;a# &' )%&#( QY-  0  K; $< %D  	      FF6;;001G;FF6;;001G;
   
 JU Z Gs   (
J6J#Jc                   ^ ^^^^^	 [        S5      m	SU-   S-   nUS-  nT	R                  T TTUSST0UTUS9	/UU	UUU U4S j[        S	US	-   5       5       Q$ )
a  Return a list of traces for a line plot with a standard deviation fill.

Args:
    x_values: The values for the x-axis.
    y_values: The values for the y-axis.
    stds: The standard deviation for the ``y_values``.
    num_stds: The number of standard deviations to include around the line plot.
    name: The name of this trace.
    legend_group: The legend group that this trace belongs to.
    color: The color of the fill around the line.
    show_legend: Whether to show this trace on a figure.

Returns:
    A list of traces.
.graph_objectszextrapolator: z-<br>noise_factor: %{x}<br>ev_extrap: %{y:.4f}z)<br>std: %{customdata:.4f}<extra></extra>linesr&   )	xy
customdatar%   modelinehovertemplatelegendgroup
showlegendc              3     >#    U  HX  nTR                  TTS S S2   -   TUT-  -   R                  5       TUT-  -
  R                  5       S S S2   -   STSS0STSSS9	v   MZ     g 7f)	Ntoselfr&   zrgba(255,255,255,0)皙?skipF)	rw   rx   fill	fillcolorr{   opacityr}   	hoverinfor~   )r   tolist)r   rg   r&   gorp   rk   x_valuesy_valuess     r   r   #_line_fill_trace.<locals>.<genexpr>:  s      

 , JJXdd^+a$h&..0Hq4x4G3O3O3QRVTVRV3WW45(    
 ,s   A A#r   )r   r   r   )
r   r   rk   rY   r%   rp   r&   rq   r|   r   s
   ```  ``  @r   rR   rR     s    2 
'	(B$t+.]]M@@M


5!'$" 	 
	


 

 1hl+

     c                N    [        S5      nUR                  U USU0USSU0SUUS9	$ )a  Return a trace for a scatter plot with error bars.

Args:
    x_values: The values for the x-axis.
    y_values: The values for the y-axis.
    stds: The standard deviation for the ``y_values``.
    name: The name of this trace.
    legend_group: The legend group that this trace belongs to.
    color: The color of the markers.
    show_legend: Whether to show this trace on a legend.

Returns:
    A trace containing a scatter plot.
ru   arraymarkersr&   zOnoise_factor: %{x} <br>ev: %{y:.4f}<br>std: %{error_y.array:.4f}<extra></extra>)	rw   rx   error_yr%   rz   markerr|   r}   r~   )r   r   )r   r   rk   r%   rp   r&   rq   r   s           r   rH   rH   K  sI    . 
'	(B::

$g   
 
r   c                |    [        U 5      [        U5      :w  a$  [        S[        U5       S[        U 5       S35      eg)zValidate that the indices and names provided are compatible.

Raises:
    ValueError: If the indices and names are different lengths.
zLength of names z' is not equal to the length of indices .N)r>   
ValueError)rW   rX   s     r   r?   r?   p  sF     7|s5z!s5zl*QRUV]R^Q__`a
 	
 "r   c                    U R                  S5      =n(       d  [        S5      eUR                  S5      =n(       d  [        S5      eU$ )zValidate that the metadata contains ZNE metadata and return it.

Raises:
    ValueError: If metadata does not contain ZNE metadata.

Returns:
    The ZNE metadata.

resiliencezeResult does not contain resilience metadata. Enable ZNE options on an estimator to use this function.znezZResult does not contain ZNE data. Enable ZNE options on an estimator to use this function.)getr   )r;   r   r^   s      r   r:   r:   |  sX     #,,|44J4
 	

 'NN511L1h
 	
 r   )	NNr   
   r          Aggrnyl)rV   r
   rW    Sequence[tuple[int, ...]] | NonerX   Sequence[str] | NonerY   intrZ   floatr[   r   r,   r   r-   r   r\   r   r]   strreturnPlotlyFigure)NNr   r   r   r   r   r   )rV   r
   rW   r   rX   r   rY   r   rZ   r   r[   r   r,   r   r-   r   r]   r   r   r   )r   NNNF)r   np.arrayr   r   rk   r   rY   r   r%   
str | Nonerp   
int | Noner&   r   rq   rC   r   zlist[PlotlyScatter])NNNF)r   r   r   r   rk   r   r%   r   rp   r   r&   r   rq   rC   r   PlotlyScatter)rW   zSequence[tuple[int, ...]]rX   zSequence[str]r   None)r;   dictr   r   )__doc__
__future__r   	itertoolsr   typingr   r   numpyr@   utilsr   utils.estimator_pub_resultr
   plotly.graph_objectsr   r   r   r   rl   rs   rR   rH   r?   r:    r   r   <module>r      s!   E "  *    ;;=
 15"&{{-{  { 	{
 { { { { { { {@ 15"&tt-t  t 	t
 t t t t t tv #666 6 	6
 6 6 6 6 6z #""" " 	"
 " " " "J	
r   