@@ -510,6 +510,7 @@ def sample(
510
510
readout_error : Optional [Sequence [Any ]] = None ,
511
511
format : Optional [str ] = None ,
512
512
random_generator : Optional [Any ] = None ,
513
+ status : Optional [Tensor ] = None ,
513
514
) -> Any :
514
515
"""
515
516
batched sampling from state or circuit tensor network directly
@@ -526,6 +527,9 @@ def sample(
526
527
:type format: Optional[str]
527
528
:param random_generator: random generator, defaults to None
528
529
:type random_generator: Optional[Any], optional
530
+ :param status: external randomness given by tensor uniformly from [0, 1],
531
+ if set, can overwrite random_generator
532
+ :type status: Optional[Tensor]
529
533
:return: List (if batch) of tuple (binary configuration tensor and correponding probability)
530
534
if the format is None, and consitent with format when given
531
535
:rtype: Any
@@ -578,21 +582,20 @@ def perfect_sampling(key: Any) -> Any:
578
582
# readout error
579
583
if readout_error is not None :
580
584
p = self .readouterror_bs (readout_error , p )
581
-
582
- a_range = backend .arange (2 ** self ._nqubits )
583
- if random_generator is None :
584
- ch = backend .implicit_randc (a = a_range , shape = [nbatch ], p = p )
585
- else :
586
- ch = backend .stateful_randc (
587
- random_generator , a = a_range , shape = [nbatch ], p = p
588
- )
585
+ ch = backend .probability_sample (nbatch , p , status , random_generator )
586
+ # if random_generator is None:
587
+ # ch = backend.implicit_randc(a=a_range, shape=[nbatch], p=p)
588
+ # else:
589
+ # ch = backend.stateful_randc(
590
+ # random_generator, a=a_range, shape=[nbatch], p=p
591
+ # )
589
592
# confg = backend.mod(
590
593
# backend.right_shift(
591
594
# ch[..., None], backend.reverse(backend.arange(self._nqubits))
592
595
# ),
593
596
# 2,
594
597
# )
595
- if format is None :
598
+ if format is None : # for backward compatibility
596
599
confg = sample_int2bin (ch , self ._nqubits )
597
600
prob = backend .gather1d (p , ch )
598
601
r = list (zip (confg , prob )) # type: ignore
@@ -608,6 +611,7 @@ def sample_expectation_ps(
608
611
z : Optional [Sequence [int ]] = None ,
609
612
shots : Optional [int ] = None ,
610
613
random_generator : Optional [Any ] = None ,
614
+ status : Optional [Tensor ] = None ,
611
615
readout_error : Optional [Sequence [Any ]] = None ,
612
616
** kws : Any ,
613
617
) -> Tensor :
@@ -635,7 +639,10 @@ def sample_expectation_ps(
635
639
:param shots: number of measurement shots, defaults to None, indicating analytical result
636
640
:type shots: Optional[int], optional
637
641
:param random_generator: random_generator, defaults to None
638
- :type random_general: Optional[Any]
642
+ :type random_generator: Optional[Any]
643
+ :param status: external randomness given by tensor uniformly from [0, 1],
644
+ if set, can overwrite random_generator
645
+ :type status: Optional[Tensor]
639
646
:param readout_error: readout_error, defaults to None
640
647
:type readout_error: Optional[Sequence[Any]]. Tensor, List, Tuple
641
648
:return: [description]
@@ -674,6 +681,7 @@ def sample_expectation_ps(
674
681
counts = shots ,
675
682
format = "count_vector" ,
676
683
random_generator = random_generator ,
684
+ status = status ,
677
685
jittable = True ,
678
686
is_prob = True ,
679
687
)
@@ -684,6 +692,7 @@ def sample_expectation_ps(
684
692
counts = shots ,
685
693
format = "sample_bin" ,
686
694
random_generator = random_generator ,
695
+ status = status ,
687
696
jittable = True ,
688
697
is_prob = True ,
689
698
)
0 commit comments