def sample_from_wavefunction( wavefunction: Wavefunction, n_samples: int, seed: Optional[int] = None, ) -> List[Tuple[int, ...]]: """Sample bitstrings from a wavefunction. Args: wavefunction: the wavefunction to sample from. n_samples: the number of samples taken. Needs to be greater than 0. seed: the seed of the sampler Returns: List[Tuple[int]]: A list of tuples where the each tuple is a sampled bitstring. """ assert isinstance(n_samples, int) and n_samples > 0 rng = np.random.default_rng(seed) outcomes_str, probabilities_np = zip( *wavefunction.get_outcome_probs().items()) probabilities = [ x[0] if isinstance(x, (list, np.ndarray)) else x for x in list(probabilities_np) ] samples_ndarray = rng.choice(a=outcomes_str, size=n_samples, p=probabilities) samples = [ tuple(int(y) for y in list(x)[::-1]) for x in list(samples_ndarray) ] return samples
def test_get_outcome_probs(self, wf_vec): wf = Wavefunction(wf_vec) probs_dict = wf.get_outcome_probs() assert all([len(key) == wf.n_qubits for key in probs_dict.keys()]) for key in probs_dict.keys(): assert len(key) == wf.n_qubits assert wf.probabilities()[int(key, 2)] == probs_dict[key]