@@ -510,6 +510,7 @@ def sample(
510510 readout_error : Optional [Sequence [Any ]] = None ,
511511 format : Optional [str ] = None ,
512512 random_generator : Optional [Any ] = None ,
513+ status : Optional [Tensor ] = None ,
513514 ) -> Any :
514515 """
515516 batched sampling from state or circuit tensor network directly
@@ -526,6 +527,9 @@ def sample(
526527 :type format: Optional[str]
527528 :param random_generator: random generator, defaults to None
528529 :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]
529533 :return: List (if batch) of tuple (binary configuration tensor and correponding probability)
530534 if the format is None, and consitent with format when given
531535 :rtype: Any
@@ -578,21 +582,20 @@ def perfect_sampling(key: Any) -> Any:
578582 # readout error
579583 if readout_error is not None :
580584 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+ # )
589592 # confg = backend.mod(
590593 # backend.right_shift(
591594 # ch[..., None], backend.reverse(backend.arange(self._nqubits))
592595 # ),
593596 # 2,
594597 # )
595- if format is None :
598+ if format is None : # for backward compatibility
596599 confg = sample_int2bin (ch , self ._nqubits )
597600 prob = backend .gather1d (p , ch )
598601 r = list (zip (confg , prob )) # type: ignore
@@ -608,6 +611,7 @@ def sample_expectation_ps(
608611 z : Optional [Sequence [int ]] = None ,
609612 shots : Optional [int ] = None ,
610613 random_generator : Optional [Any ] = None ,
614+ status : Optional [Tensor ] = None ,
611615 readout_error : Optional [Sequence [Any ]] = None ,
612616 ** kws : Any ,
613617 ) -> Tensor :
@@ -635,7 +639,10 @@ def sample_expectation_ps(
635639 :param shots: number of measurement shots, defaults to None, indicating analytical result
636640 :type shots: Optional[int], optional
637641 :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]
639646 :param readout_error: readout_error, defaults to None
640647 :type readout_error: Optional[Sequence[Any]]. Tensor, List, Tuple
641648 :return: [description]
@@ -674,6 +681,7 @@ def sample_expectation_ps(
674681 counts = shots ,
675682 format = "count_vector" ,
676683 random_generator = random_generator ,
684+ status = status ,
677685 jittable = True ,
678686 is_prob = True ,
679687 )
@@ -684,6 +692,7 @@ def sample_expectation_ps(
684692 counts = shots ,
685693 format = "sample_bin" ,
686694 random_generator = random_generator ,
695+ status = status ,
687696 jittable = True ,
688697 is_prob = True ,
689698 )
0 commit comments