
    z	iL0                       S 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
Jr  SSKrSSKJr  SSKJr  SS	KJ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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      5       r)\\*\+   \*\*\,      \*\*\*\,         4   r-  " S S\5      r.SS jr/SS jr0SS jr1        SS jr2g) z:Sampler V2 implementation for an arbitrary Backend object.    )annotationsN)defaultdict)	dataclass)AnyIterableUnion)NDArray)QuantumCircuit)QiskitError)_run_circuits)BaseSamplerV2)BitArrayDataBinPrimitiveResultSamplerPubLikeSamplerPubResult)_min_num_bytes)
SamplerPub)PrimitiveJob)	BackendV2)Resultc                  H    \ rS rSr% SrSrS\S'    SrS\S'    SrS	\S
'   Sr	g)Options+   z'Options for :class:`~.BackendSamplerV2`i   intdefault_shotsN
int | Noneseed_simulatorzdict[str, Any] | Nonerun_options )
__name__
__module____qualname____firstlineno____doc__r   __annotations__r   r   __static_attributes__r        ^/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/primitives/backend_sampler_v2.pyr   r   +   s7    1M3 "&NJ% *.K&-r(   r   c                  >    \ rS rSr% S\S'   S\S'   S\S'   S\S'   Srg	)
_MeasureInfo?   str	creg_namer   num_bits	num_bytesstartr    N)r!   r"   r#   r$   r&   r'   r    r(   r)   r+   r+   ?   s    NMNJr(   r+   c                      \ rS rSrSrSS.   SS jj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)BackendSamplerV2Q   a  Evaluates bitstrings for provided quantum circuits

The :class:`~.BackendSamplerV2` class is a generic implementation of the
:class:`~.BaseSamplerV2` interface that is used to wrap a :class:`~.BackendV2`
object in the class :class:`~.BaseSamplerV2` API. It
facilitates using backends that do not provide a native
:class:`~.BaseSamplerV2` implementation in places that work with
:class:`~.BaseSamplerV2`. However,
if you're using a provider that has a native implementation of
:class:`~.BaseSamplerV2`, it is a better choice to leverage that native
implementation as it will likely include additional optimizations and be
a more efficient implementation. The generic nature of this class
precludes doing any provider- or backend-specific optimizations.

This class does not perform any measurement or gate mitigation.

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.

The options for :class:`~.BackendSamplerV2` consist of the following items.

* ``default_shots``: The default shots to use if none are specified in :meth:`~run`.
  Default: 1024.

* ``seed_simulator``: The seed to use in the simulator. If None, a random seed will be used.
  Default: None.

* ``run_options``: A dictionary of options to pass through to the ``run()``
  method of the wrapped :class:`~.BackendV2` instance.

.. note::

    This class requires a backend that supports ``memory`` option.

N)optionsc               ^    Xl         U(       a  [        S0 UD6U l        g[        5       U l        g)z
Args:
    backend: The backend to run the primitive on.
    options: The options to control the default shots (``default_shots``) and
        the random seed for the simulator (``seed_simulator``).
Nr    )_backendr   _options)selfbackendr5   s      r)   __init__BackendSamplerV2.__init__w   s"      .5*'*79r(   c                    U R                   $ )z7Returns the backend which this sampler object based on.)r7   r9   s    r)   r:   BackendSamplerV2.backend        }}r(   c                    U R                   $ )zReturn the options)r8   r>   s    r)   r5   BackendSamplerV2.options   r@   r(   )shotsc                   Uc  U R                   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)r8   r   r   coerce_validate_pubsr   _run_submit)r9   pubsrC   pubcoerced_pubsjobs         r)   runBackendSamplerV2.run   si     =MM//EAEF#
))#5FL)499l3
	 Gs    A9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)r9   rI   irJ   s       r)   rF   BackendSamplerV2._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   )
r9   rI   pub_dictrW   rJ   resultsrC   lstpub_results
pub_results
             r)   rG   BackendSamplerV2._run   s    t$oFAYY&&q) & &3t9$"..*JE..3)?3aq'3)?GK!$S!6'
 "7	 + w)Q@@	 *@s   4B2
c                2   U Vs/ s H'  o3R                   R                  UR                  5      PM)     nn/ nU H6  nUR                  [        R
                  " U5      R                  5       5        M8     U R                  R                  =(       d    0 n[        UU R                  4SSUU R                  R                  S.UD6u  p[        U5      n
/ nSnU R                  R                  c  SO$U R                  R                  R                  S5      n[        X5       Hr  u  p=[        UR                  5      u  pXR                   -   nUR#                  U R%                  XU UUR&                  UUUR                  R(                  U5      5        UnMt     U$ s  snf )zFCompute results for pubs that all require the same value of ``shots``.FT)clear_metadatamemoryrC   r   r   N
meas_level)parameter_valuesbind_allrR   extendnpraveltolistr8   r   r   r7   r   _prepare_memorygetrb   _analyze_circuitsizer_   _postprocess_pubshaper]   )r9   rI   rC   rJ   bound_circuitsflatten_circuitscircuitsrun_optsrd   _result_memoryr1   rl   bound	meas_infomax_num_bytesends                    r)   ra   BackendSamplerV2._run_pubs   s    QUUPT..77DPTU&H##BHHX$6$=$=$?@ ' ==,,2"MM
 !==77
 

 (0  }}((0 **..|< 	
 d3JC'7'D$I**$CNN%%!,KK!KK((
 E 4  U Vs   .Fc           	     J   US:X  d  Uc  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 H0  nUR                   [        XR                      UR                  5      _M2     nnO~US:X  aj  [        R                  " U5      nUS   SUS   -  -   n[        R                  " U/ UQUR                  SS Q75      nU Vs0 s H  oR                   U_M     nnO[        SU 35      e[!        [#        S0 UDS	U0D6X&S
.S9$ s  snf s  snf s  snf )zConverts the memory data into a sampler pub result

For level 2 data, the memory data are stored in an array of bit arrays
with the shape of the pub. For level 1 data, the data are stored in a
complex numpy array.
r[   Ndtype   ).r   y              ?).r   zUnsupported meas_level: rx   )rC   circuit_metadatar\   r    )r.   rp   zerosr0   uint8_memory_arrayrb   ndindex_samples_to_packed_arrayr/   r1   r   arrayreshaperx   r   r   r   )r9   r~   rC   rx   r   r   r   rl   itemarraysmemory_arraysamplesindexarymeasrawcplxs                    r)   rw   !BackendSamplerV2._postprocess_pub   s     ?j0 &%D 1H)HPRPXPX YY%   )FL"%lBJJ4F"G%D27MM4::VC474>>*51 & #H &%D )? OO%  D 1_((=)Cv;c&k!11D::d$=e$=djjn$=>D5>?YTNND(YD?D 8EFF(d(%($K
 	
- @s   AF7FF )r7   r8   )r:   r   r5   zdict | None)returnr   )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])r~   list[ResultMemory]rC   r   rx   ztuple[int, ...]r   zlist[_MeasureInfo]r   r   r   dictrl   r   r   r   )r!   r"   r#   r$   r%   r;   propertyr:   r5   rM   rF   rG   ra   rw   r'   r    r(   r)   r3   r3   Q   s    #R  $	E E 	E    
 FJ	,	8B		8	A-^*
)*
 *
 	*

 &*
 *
 *
 *
 
*
r(   r3   c           
     ,   / nSnU R                    Ht  nUR                  nUR                  nUS:w  a  U R                  US   5      R                  nOSnUR                  [        UU[        U5      US95        [        X&U-   5      nMv     U[        U5      4$ )z4Analyzes the information for each creg in a circuit.r   )r.   r/   r0   r1   )	rS   namerv   find_bitr   r_   r+   r   max)rR   r   max_num_bitscregr   r/   r1   s          r)   ru   ru     s    ILyy99q=$$T!W-33EE!(2		
 <)9:   n\222r(   c                :   / nU  H  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     M     U$ )z3Joins splitted results if exceeding max_experimentsrk   0x0)rd   hasattrdatark   r_   rC   )rd   re   resexps       r)   rs   rs   &  sn    
C;;Csxx**sxx

388??+ 

E7SYY./   Jr(   c                  ^ / nU  H  nTS:  aO  SR                  U4S jU 5       5      n[        R                  " U[        R                  S9R	                  ST5      nO.[        R
                  " [        U5      T4[        R                  S9nUR                  U5        M     [        R                  " U5      n[        R                  " USSS9$ )z:Converts the memory data into an array in an unpacked way.r   r(   c              3  \   >#    U  H!  n[        US 5      R                  TS5      v   M#     g7f)   bigN)r   to_bytes).0rW   r0   s     r)   	<genexpr> _memory_array.<locals>.<genexpr>8  s'     R6aC2J//	5AA6s   ),r   r   axisbitorder)
joinrp   
frombufferr   r   r   rQ   r_   asarray
unpackbits)rd   r0   re   rk   r   r   s    `    r)   r   r   3  s    
Cq=88R6RRD==RXX6>>r9MD 88S[)4BHHED

4  **S/C==266r(   c                    US:X  a  U SS2U* U-
  S24   nOU SS2U* U-
  U* 24   nU* S-  n[         R                  " USUS44SS9n[         R                  " USSS9nU$ )	zBConverts an unpacked array of the memory data into a packed array.r   N   )r   r   )constant_valuesr   r   r   )rp   padpackbits)r   r/   r1   r   pad_sizes        r)   r   r   B  s     za%(*,,-a%(*eV334 y1}H
&&v!}-q
AC
++cU
3CJr(   )rR   r
   r   ztuple[list[_MeasureInfo], int])rd   zlist[Result]r   r   )rd   zlist[list[str]]r0   r   r   NDArray[np.uint8])r   r   r/   r   r1   r   r   r   )3r%   
__future__r   rT   collectionsr   dataclassesr   typingr   r   r   numpyrp   numpy.typingr	   qiskit.circuitr
   qiskit.exceptionsr   &qiskit.primitives.backend_estimator_v2r   qiskit.primitives.baser   qiskit.primitives.containersr   r   r   r   r   &qiskit.primitives.containers.bit_arrayr   (qiskit.primitives.containers.sampler_pubr   qiskit.primitives.primitive_jobr   qiskit.providers.backendr   qiskit.resultr   r   r+   r^   r-   floatResultMemoryr3   ru   rs   r   r   r    r(   r)   <module>r      s    A "  # ! ' '    ) ) @ 0  B ? 8 .     &    T#YT%[ 14T%[8I3JJK{
} {
|3.
7*-69r(   