
    i}9                        S r SSKJr  SSKJrJrJrJrJrJ	r	  SSK
Jr  SSKrSSKJr  SSKJr  SSKJrJr  S	S
KJr  \	(       a  SSKJr   " S S5      r " S S5      r " S S5      rg)aZ  
==================================================================================
NoiseLearner result classes (:mod:`qiskit_ibm_runtime.utils.noise_learner_result`)
==================================================================================

.. autosummary::
   :toctree: ../stubs/
   :nosignatures:

   PauliLindbladError
   LayerError
    )annotations)AnyIteratorOptionalSequenceUnionTYPE_CHECKING)NDArrayN)	BackendV2)QuantumCircuit)	PauliListPauli   )	Embedding)Figurec                  |    \ rS rSrSrSS jr\SS j5       r\SS j5       r\SS j5       r	SS jr
SS jrSS	 jrS
rg)PauliLindbladError*   uK  A Pauli error channel generated by a Pauli Lindblad dissipators.

This operator represents an N-qubit quantum error channel
:math:`E(\rho) = e^{\sum_j r_j D_{P_j}}(\rho)` generated by Pauli Lindblad dissipators
:math:`D_P(\rho) = P \rho P - \rho`, where :math:`P_j` are N-qubit :class:`~.Pauli`
operators.

The list of Pauli generator terms are stored as a :class:`~.PauliList` and can be
accessed via the :attr:`~generators` attribute. The array of dissipator rates
:math:`r_j` can be accessed via the :attr:`~rates` attribute.

The equivalent Pauli error channel can be constructed as a composition
of single-Pauli channel terms

.. math::

    E = e^{\sum_j r_j D_{P_j}} = \prod_j e^{r_j D_{P_j}}
    = prod_j \left( (1 - p_j) S_I + p_j S_{P_j} \right)

where :math:`p_j = \frac12 - \frac12 e^{-2 r_j}` [1].

Args:
    generators: A list of the Pauli Lindblad generators for the error channel.
    rates: A list of the rates for the Pauli-Lindblad ``generators``.

Raises:
    ValueError: If ``generators`` and ``rates`` have different lengths.

References:
    1. E. van den Berg, Z. Minev, A. Kandala, K. Temme, *Probabilistic error
       cancellation with sparse Pauli–Lindblad models on noisy quantum processors*,
       Nature Physics volume 19, pages1116–1121 (2023).
       `arXiv:2201.09866 [quant-ph] <https://arxiv.org/abs/2201.09866>`_
c                    Xl         [        R                  " U[        S9U l        [        U5      4U R                  R                  :w  a/  [        S[        U5       SU R                  R                   S35      eg )N)dtypez``generators`` has length z but ``rates`` has shape .)_generatorsnpasarrayfloat_rateslenshape
ValueError)self
generatorsratess      g/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_ibm_runtime/utils/noise_learner_result.py__init__PauliLindbladError.__init__N   sn    %jje4
O!2!22,S_,= >++/;;+<+<*=Q@  3    c                    U R                   $ )zF
The Pauli Lindblad generators of this :class:`~.PauliLindbladError`.
)r   r    s    r#   r!   PauliLindbladError.generatorsX   s    
 r&   c                    U R                   $ )z5
The Lindblad generator rates of this quantum error.
)r   r(   s    r#   r"   PauliLindbladError.rates_   s    
 {{r&   c                .    U R                   R                  $ )z=
The number of qubits in this :class:`~.PauliLindbladError`.
)r!   
num_qubitsr(   s    r#   r-   PauliLindbladError.num_qubitsf   s    
 )))r&   c                    US:  a  [        S5      e[        R                  " U R                  R                  U R                  R
                  -  SS9U:H  n[        U R                  U   U R                  U   5      $ )aT  
The :class:`~.PauliLindbladError` containing only those terms acting on exactly
``num_qubits`` qubits.

Args:
    num_qubits: The number of qubits that the returned error acts on.

Returns:
    The error containing only those terms acting on exactly ``num_qubits`` qubits.

Raises:
    ValueError: If ``num_qubits`` is smaller than ``0``.
r   z'``num_qubits`` must be ``0`` or larger.   )axis)r   r   sumr!   xzr   r"   )r    r-   masks      r#   restrict_num_bodies&PauliLindbladError.restrict_num_bodiesm   sd     >FGGvvdoo''$//*;*;;!D
R!$//$"7D9IJJr&   c                4    U R                   U R                  S.$ )PReturn a dictionary containing all the information to re-initialize this object.r!   r"   r:   r(   s    r#   _jsonPauliLindbladError._json   s    "oo

CCr&   c                X    SU R                    SU R                  R                  5        S3$ )NzPauliLindbladError(generators=z, rates=))r!   r"   tolistr(   s    r#   __repr__PauliLindbladError.__repr__   s+    //@IZIZI\H]]^__r&   )r   r   N)r!   r   r"   zSequence[float]returnNone)rB   r   )rB   zNDArray[np.float64]rB   int)r-   rE   rB   r   rB   dictrB   str)__name__
__module____qualname____firstlineno____doc__r$   propertyr!   r"   r-   r6   r;   r@   __static_attributes__ r&   r#   r   r   *   s[    !F       * *K&D`r&   r   c                  B   \ rS rSrSr S       SS jjr\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
          S                       SS	 jjr            S                         SS
 jjrSS jrSS jrSrg)
LayerError   a  The error channel (in Pauli-Lindblad format) of a single layer of instructions.

Args:
    circuit: A circuit whose noise has been learnt.
    qubits: The labels of the qubits in the ``circuit``.
    error: The Pauli Lindblad error channel affecting the ``circuit``, or ``None`` if the error
        channel is either unknown or explicitly disabled.

Raises:
    ValueError: If ``circuit``, ``qubits``, and ``error`` have mismatching number of qubits.
Nc                *   Xl         [        U5      U l        X0l        [	        S5      n[        U R                  5      U R                  R                  :w  a  UeU R                  b0  [        U R                  5      U R                  R                  :w  a  Ueg g )NzMistmatching numbers of qubits.)
_circuitlist_qubits_errorr   r   qubitscircuitr-   error)r    r[   rZ   r\   errs        r#   r$   LayerError.__init__   sv      F|:;t{{t||666I::!c$++&6$**:O:O&OI 'P!r&   c                    U R                   $ )z,
The circuit in this :class:`.~LayerError`.
)rV   r(   s    r#   r[   LayerError.circuit   s    
 }}r&   c                    U R                   $ )z+
The qubits in this :class:`.~LayerError`.
)rX   r(   s    r#   rZ   LayerError.qubits   s    
 ||r&   c                    U R                   $ )z}
The error channel in this :class:`.~LayerError`, or ``None`` if the error channel is either
unknown or explicitly disabled.
)rY   r(   s    r#   r\   LayerError.error   s     {{r&   c                ,    [        U R                  5      $ )z5
The number of qubits in this :class:`~.LayerError`.
)r   rZ   r(   s    r#   r-   LayerError.num_qubits   s    
 4;;r&   c                0    SSK Jn  U" U UUUUUUUUU	U
US9$ )a  
Draw a map view of a this layer error.

Args:
    embedding: An :class:`~.Embedding` object containing the coordinates and coupling map
        to draw the layer error on, or a backend to generate an :class:`~.Embedding` for.
    colorscale: The colorscale used to show the rates of this layer error.
    color_no_data: The color used for qubits and edges for which no data is available.
    color_out_of_scale: The color used for rates with value greater than ``highest_rate``.
    num_edge_segments: The number of equal-sized segments that edges are made of.
    edge_width: The line width of the edges in pixels.
    height: The height of the returned figure.
    highest_rate: The highest rate, used to normalize all other rates before choosing their
        colors. If ``None``, it defaults to the highest value found in the ``layer_error``.
    background_color: The background color.
    radius: The radius of the pie charts representing the qubits.
    width: The width of the returned figure.

.. code:: python

    from qiskit import QuantumCircuit
    from qiskit.quantum_info import PauliList
    from qiskit_ibm_runtime.utils.embeddings import Embedding
    from qiskit_ibm_runtime.utils.noise_learner_result import LayerError, PauliLindbladError

    # A five-qubit 1-D embedding with nearest neighbouring connectivity
    coordinates1 = [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5)]
    coupling_map1 = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
    embedding1 = Embedding(coordinates1, coupling_map1)

    # A six-qubit horseshoe-shaped embedding with nearest neighbouring connectivity
    coordinates2 = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
    coupling_map2 = [(0, 1), (1, 2), (0, 3), (3, 4), (4, 5)]
    embedding2 = Embedding(coordinates2, coupling_map2)

    # A LayerError object
    circuit = QuantumCircuit(4)
    qubits = [1, 2, 3, 4]
    generators = PauliList(["IIIX", "IIXI", "IXII", "YIII", "ZIII", "XXII", "ZZII"])
    rates = [0.01, 0.01, 0.01, 0.005, 0.02, 0.01, 0.01]
    error = PauliLindbladError(generators, rates)
    layer_error = LayerError(circuit, qubits, error)

    # Draw the layer error on embedding1
    layer_error.draw_map(embedding1)

    # Draw the layer error on embedding2
    layer_error.draw_map(embedding2)
r   )draw_layer_error_map)layer_error	embedding
colorscalecolor_no_datacolor_out_of_scalenum_edge_segments
edge_widthheighthighest_ratebackground_colorradiuswidth)visualizationrh   )r    rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   rh   s                r#   draw_mapLayerError.draw_map   s9    B 	9#!'1/!%-
 	
r&   c                4    SSK Jn  U" U /UUUUUUUUU	U
UUS9$ )a  
Draw a swarm plot of the rates in this layer error.

This function plots the rates along a vertical axes, offsetting the rates along the ``x``
axis so that they do not overlap with each other.

.. note::
    To draw multiple layer errors at once, consider calling
    :meth:`~qiskit_ibm_runtime.visualization.draw_layer_errors_swarm` directly.

Args:
    num_bodies: The weight of the generators to include in the plot, or ``None`` if all the
        generators should be included.
    max_rate: The largest rate to include in the plot, or ``None`` if no upper limit should be
        set.
    min_rate: The smallest rate to include in the plot, or ``None`` if no lower limit should be
        set.
    connected: A list of generators whose markers are to be connected by lines.
    colors: A list of colors for the markers in the plot, or ``None`` if these colors are to be
        chosen automatically.
    num_bins: The number of bins to place the rates into when calculating the ``x``-axis
        offsets.
    opacities: A list of opacities for the markers.
    names: The names of the various layers as displayed in the legend. If ``None``, default
        names are assigned based on the layers' position inside the ``layer_errors`` list.
    x_coo: The ``x``-axis coordinates of the vertical axes that the markers are drawn around, or
        ``None`` if these axes should be placed at regular intervals.
    marker_size: The size of the marker in the plot.
    height: The height of the returned figure.
    width: The width of the returned figure.
r   )draw_layer_errors_swarm)layer_errors
num_bodiesmax_ratemin_rate	connectedcolorsnum_bins	opacitiesnamesx_coomarker_sizerp   rt   )ru   ry   )r    r{   r|   r}   r~   r   r   r   r   r   r   rp   rt   ry   s                 r#   
draw_swarmLayerError.draw_swarm  s>    ` 	<&!#
 	
r&   c                J    U R                   U R                  U R                  S.$ )r9   r[   rZ   r\   r   r(   s    r#   r;   LayerError._jsonW  s    <<4;;TTr&   c                t    S[        U R                  5       SU R                   SU R                   S3nSU S3$ )Nzcircuit=z	, qubits=z, error=r>   zLayerError()reprr[   rZ   r\   )r    rets     r#   r@   LayerError.__repr__[  s>    dll+,Idkk](4::,VWXSE##r&   )rV   rY   rX   N)r[   r   rZ   zSequence[int]r\   zOptional[PauliLindbladError]rB   rC   )rB   r   )rB   z	list[int])rB   zUnion[PauliLindbladError, None]rD   )
Bluered	lightgray
lightgreen        Nwhiteg      ?   )rj   zUnion[Embedding, BackendV2]rk   rI   rl   rI   rm   rI   rn   rE   ro   r   rp   rE   rq   Optional[float]rr   rI   rs   r   rt   rE   rB   PlotlyFigure)NNNNNNg?NNNr   r   )r{   Optional[int]r|   r   r}   r   r~   z'Optional[Union[list[Pauli], list[str]]]r   Optional[list[str]]r   r   r   zUnion[float, list[float]]r   r   r   zOptional[list[float]]r   r   rp   rE   rt   rE   rB   r   rF   rH   )rJ   rK   rL   rM   rN   r$   rO   r[   rZ   r\   r-   rv   r   r;   r@   rP   rQ   r&   r#   rS   rS      s   
  /3	  ,	
 
"           $(".!#(, 'P
.P
 P
 	P

  P
 P
 P
 P
 &P
 P
 P
 P
 
P
h %)$($(=A&*"&/2%)'+'+@
!@
 "@
 "	@

 ;@
 $@
  @
 -@
 #@
 %@
 %@
 @
 @
 
@
DU$r&   rS   c                  v    \ rS rSrSrSSS jjr\SS j5       r\SS j5       rSS jr	SS jr
SS	 jrSS
 jrSrg)NoiseLearnerResulti`  z:A container for the results of a noise learner experiment.Nc                d    [        U5      U l        Uc  0 U l        gUR                  5       U l        g)z
Args:
    data: The data of a noise learner experiment.
    metadata: Metadata that is common to all pub results; metadata specific to particular
        pubs should be placed in their metadata fields. Keys are expected to be strings.
N)rW   _datacopy	_metadata)r    datametadatas      r#   r$   NoiseLearnerResult.__init__c  s'     $Z
'/X]]_r&   c                    U R                   $ )z&The data of this noise learner result.)r   r(   s    r#   r   NoiseLearnerResult.datam  s     zzr&   c                    U R                   $ )z*The metadata of this noise learner result.)r   r(   s    r#   r   NoiseLearnerResult.metadatar  s     ~~r&   c                     U R                   U   $ r   )r   )r    indexs     r#   __getitem__NoiseLearnerResult.__getitem__w  s    yyr&   c                ,    [        U R                  5      $ r   )r   r   r(   s    r#   __len__NoiseLearnerResult.__len__z  s    499~r&   c                <    SU R                    SU R                   S3$ )NzNoiseLearnerResult(data=z, metadata=r>   )r   r   r(   s    r#   r@   NoiseLearnerResult.__repr__}  s    )$))KaPPr&   c                ,    [        U R                  5      $ r   )iterr   r(   s    r#   __iter__NoiseLearnerResult.__iter__  s    DIIr&   )r   r   r   )r   zSequence[LayerError]r   zdict[str, Any] | None)rB   zlist[LayerError])rB   zdict[str, Any])r   rE   rB   rS   rD   rH   )rB   zIterator[LayerError])rJ   rK   rL   rM   rN   r$   rO   r   r   r   r   r@   r   rP   rQ   r&   r#   r   r   `  sH    DE     Qr&   r   )rN   
__future__r   typingr   r   r   r   r   r	   numpy.typingr
   numpyr   qiskit.providers.backendr   qiskit.circuitr   qiskit.quantum_infor   r   utils.embeddingsr   plotly.graph_objectsr   r   r   rS   r   rQ   r&   r#   <module>r      sS    # J J    . ) 0 (;[` [`|U$ U$p! !r&   