
    {	iY!                       S r SSKJr  SSKrSSKJr  SSKJrJr  SSK	J
r
  SSKrSSKJrJrJrJr  SSKJr  SS	KJrJrJrJrJr  SS
KJr  SSKJr  SSKJr  SSK J!r!  \ " S S5      5       r" " S S\5      r#SS jr$SS jr%g)z
Sampler V2 class.
    )annotationsN)defaultdict)	dataclassfield)Iterable)_analyze_circuit_MeasureInfo_memory_array_samples_to_packed_array)BaseSamplerV2)BitArrayDataBinPrimitiveResultSamplerPubLikeSamplerPubResult)
SamplerPub)PrimitiveJob)Result)AerSimulatorc                  H    \ rS rSr% Sr\" \S9rS\S'    \" \S9r	S\S'   Sr
g)	Options.   z!Options for :class:`~.SamplerV2`.)default_factorydictbackend_optionsrun_options N)__name__
__module____qualname____firstlineno____doc__r   r   r   __annotations__r   __static_attributes__r       Z/home/james-whalen/.local/lib/python3.13/site-packages/qiskit_aer/primitives/sampler_v2.pyr   r   .   s)    +!$7OT7:d3K3-r%   r   c                      \ rS rSrSrSSSS.     SS jjr\S 5       r\SS j5       r	\SS	 j5       r
\SS
 j5       rSS.     SS jjrSS jrSS jrSS jr                SS jrSrg)	SamplerV29   a  
Aer implementation of SamplerV2 class.

Each tuple of ``(circuit, <optional> parameter values, <optional> shots)``, called a sampler
primitive unified bloc (PUB), produces its own array-valued result. The :meth:`~run` method can
be given many pubs at once.

* ``backend_options``: Options passed to AerSimulator.
  Default: {}.

* ``run_options``: Options passed to :meth:`AerSimulator.run`.
  Default: {}.

i   N)default_shotsseedoptionsc                   Xl         X l        U(       a  [        S0 UD6O	[        5       U l        [	        S0 U R
                  R                  D6U l        g)a9  
Args:
    default_shots: The default shots for the sampler if not specified during run.
    seed: The seed for random number generation.
        If None, a random seeded default RNG will be used.
    options:
        the backend options (``backend_options``), and
        the runtime options (``run_options``).

Nr   )_default_shots_seedr   _optionsr   r,   r   _backend)selfr*   r+   r,   s       r&   __init__SamplerV2.__init__I   s=    " ,
.5*'*79$Dt||'C'CDr%   c                    U " S0 UD6n[        U[        5      (       a  Xl        U$ [        R                  " U5      Ul        U$ )z+make new sampler that uses external backendr   )
isinstancer   r1   from_backend)clsbackendr,   samplers       r&   r7   SamplerV2.from_backend`   sC     ..g|,,&   ,88AGr%   c                    U R                   $ )zReturn the default shots)r.   r2   s    r&   r*   SamplerV2.default_shotsj   s     """r%   c                    U R                   $ )z-Return the seed for random number generation.)r/   r=   s    r&   r+   SamplerV2.seedo   s     zzr%   c                    U R                   $ )zReturn the options)r0   r=   s    r&   r,   SamplerV2.optionst   s     }}r%   )shotsc                   Uc  U R                   nU Vs/ s H  n[        R                  " X25      PM     nnU R                  U5        [	        U R
                  U5      nUR                  5         U$ s  snf )N)r.   r   coerce_validate_pubsr   _run_submit)r2   pubsrC   pubcoerced_pubsjobs         r&   runSamplerV2.runy   se     =''EAEF#
))#5FL)499l3
	 Gs    A/c                    [        U5       HI  u  p#[        UR                  R                  5      S:X  d  M*  [        R
                  " SU S3[        5        MK     g )Nr   zThe z-th pub's circuit has no output classical registers and so the result will be empty. Did you mean to add measurement instructions?)	enumeratelencircuitcregswarningswarnUserWarning)r2   rI   irJ   s       r&   rF   SamplerV2._validate_pubs   sK    oFA3;;$$%*1# S S &r%   c                n   [        [        5      n[        U5       H"  u  p4X$R                     R	                  U5        M$     S /[        U5      -  nUR                  5        HB  u  pgU R                  U Vs/ s H  o1U   PM	     snU5      n[        Xx5       H	  u  p9XU'   M     MD     [        USS0S9$ s  snf )Nversion   metadata)
r   listrP   rC   appendrQ   items	_run_pubszipr   )
r2   rI   pub_dictrW   rJ   resultsrC   lstpub_results
pub_results
             r&   rG   SamplerV2._run   s    t$oFAYY&&q) & &3t9$"..*JE..3)?3aq'3)?GK!$S!6'
 "7	 + w)Q@@	 *@s   4B2
c                   U Vs/ s H  o3R                   PM     nnU Vs/ s H  n[        U5      PM     nnU R                  R                  R	                  5       nS H  nXv;   d  M
  Xg	 M     U R
                  b	  SU;   a  US	 U R                  R                  " U4UU R
                  USS.UD6R                  5       n[        U5      n	/ n
SnU H  n[        UR                   5      u  pUR                  nXR                  -   nU
R                  U R                  XU UUR                  UUUR                   R                   UR                   5      5        UnM     U
$ s  snf s  snf )zFCompute results for pubs that all require the same value of ``shots``.)rC   parameter_bindsmemoryseed_simulatorT)rC   rl   rj   rk   r   )rR   _convert_parameter_bindingsr,   r   copyr/   r1   rM   result_prepare_memoryr   parameter_valuessizer_   _postprocess_pubshaper]   )r2   rI   rC   rJ   circuitsrj   r   keyro   result_memoryrd   start	meas_infomax_num_bytesp_vends                   r&   ra   SamplerV2._run_pubs   sj   +/04CKK40GKLt6s;tL ll..3359C!$ : ::!&6+&E,- ""
::+
 
 &( 	 (/ C'7'D$I&&C(("CNN%%!,II!KK((OO
 E " W 1Ls
   EEc           	     2   U Vs0 s H?  nUR                   [        R                  " X2UR                  4-   [        R                  S9_MA     n	n[        X5      n
[        U
[        R                  " U6 5       H?  u  pU H4  n[        XR                  UR                  5      nXUR                      U'   M6     MA     U Vs0 s H/  oR                   [        XR                      UR                  5      _M1     nn[        [        S0 UDSU0D6UUUS.S9$ s  snf s  snf )zOConverts the memory data into an array of bit arrays with the shape of the pub.)dtypert   )rC   circuit_metadatasimulator_metadatar\   r   )	creg_namenpzeros	num_bytesuint8r
   rb   ndindexr   num_bitsrx   r   r   r   )r2   rw   rC   rt   ry   rz   r   r   itemarraysmemory_arraysamplesindexarymeass                  r&   rs   SamplerV2._postprocess_pub   s    "
! NNBHHUT^^-D%DBHHUU! 	 
 %]B!,

E0BCNG!.wtzzR03t~~&u- " D Yb
XaPTNNHVNN%;T]]KKXa 	 
  (d(%($4&8
 	


s   AD<6D)r1   r.   r0   r/   )r*   intr+   
int | Noner,   zdict | None)returnr   )r   r   )r   r   )rI   zIterable[SamplerPubLike]rC   r   r   z/PrimitiveJob[PrimitiveResult[SamplerPubResult]])rI   list[SamplerPub])rI   r   r   z!PrimitiveResult[SamplerPubResult])rI   r   rC   r   r   zlist[SamplerPubResult])rw   list[list[str]]rC   r   rt   ztuple[int, ...]ry   zlist[_MeasureInfo]rz   r   r   r   r   r   r   r   )r   r   r    r!   r"   r3   classmethodr7   propertyr*   r+   r,   rM   rF   rG   ra   rs   r$   r   r%   r&   r(   r(   9   s   $ "#E E 	E
 E.   # #    
 FJ	,	8B		8	A-^ 
& 
  
 	 

 & 
  
  
 ! 
 
 
r%   r(   c                    U R                   nU R                  n0 nUR                  UR                  5      n[	        UR                  5       VVs0 s H  u  pVXdSU4   R                  5       _M     nnnU$ s  snnf )N.)rR   rq   as_array
parametersrP   ravel)rJ   rR   rq   rj   param_arrayrW   ps          r&   rm   rm      sv    kkG++O"++G,>,>?KBKGL^L^B_`B_$!qc1f-3355B_O` as   !A4c                (   / nU R                    H  n[        UR                  S5      (       aB  UR                  R                  (       a'  UR	                  UR                  R                  5        M`  UR	                  S/UR
                  -  5        M     U$ )z;There is no split of results due to max_experiments for Aerrk   0x0)rd   hasattrdatark   r_   rC   )ro   re   exps      r&   rp   rp      sd    
C~~388X&&388??JJsxx' JJw*+  Jr%   )rJ   r   r   r   )ro   r   r   r   )&r"   
__future__r   rT   collectionsr   dataclassesr   r   typingr   numpyr   $qiskit.primitives.backend_sampler_v2r   r	   r
   r   qiskit.primitives.baser   qiskit.primitives.containersr   r   r   r   r   (qiskit.primitives.containers.sampler_pubr   qiskit.primitives.primitive_jobr   qiskit.resultr   
qiskit_aerr   r   r(   rm   rp   r   r%   r&   <module>r      sx    #  # (    1  @ 8   # . . .r
 r
j	r%   