conversion
Perceval provides helper methods to convert the three types of results of the Sampler (namely the BSDistribution, BSCount and BSSamples) into each other.
>>> import perceval as pcvl
>>> distribution = pcvl.BSDistribution({pcvl.BasicState([1, 0]): 0.4, pcvl.BasicState([0, 1]): 0.6})
>>> print(pcvl.probs_to_sample_count(distribution, count=1000)) # Sampling noise is applied; results may vary
{
|1,0>: 392
|0,1>: 608
}
Note that for methods converting from probabilities, passing a kwarg with a count is mandatory. This can either be:
count
, in which case this is the number of resulting samples.max_shots
and/ormax_samples
, in which case the one defined or the minimum of the two is the number of resulting samples.
Conversion code reference
- perceval.utils.conversion.probs_to_sample_count(probs, **kwargs)
Convert a measured state probability distribution to a state count.
This conversion artificially adds random sampling noise, following a normal law, to the result.
- Parameters:
probs (
BSDistribution
) – the distribution to convertcount – (
int
) – The final number of samples to generate. Can be None if either of the remaining kwargs is defined.max_shots – (
int
) – If bothmax_shots
andmax_samples
are given, then the minimum of the two will be used. Else, the one defined will be used ifcount
is not given.max_samples – (
int
) – Seemax_shots
.
- Return type:
- Returns:
the state count
- perceval.utils.conversion.probs_to_samples(probs, **kwargs)
Convert a measured state probability distribution to a chronological list of samples
- Parameters:
probs (
BSDistribution
) – the distribution to convertcount – (
int
) – The final number of samples to generate. Can be None if either of the remaining kwargs is defined.max_shots – (
int
) – If bothmax_shots
andmax_samples
are given, then the minimum of the two will be used. Else, the one defined will be used ifcount
is not given.max_samples – (
int
) – Seemax_shots
.
- Return type:
- Returns:
the sample list
- perceval.utils.conversion.sample_count_to_probs(sample_count)
Convert a state count to a state probability distribution
- Parameters:
sample_count (
BSCount
) – the state count- Return type:
- Returns:
the state probability distribution
- perceval.utils.conversion.sample_count_to_samples(sample_count, **kwargs)
Convert a state count to a chronological list of samples, by randomly sampling on the count
- Parameters:
sample_count (
BSCount
) – the state countcount – (
int
) – The final number of samples to generate. Can be None to deduce it from the number of samples in the BSCount.max_shots – (
int
) – If bothmax_shots
andmax_samples
are given, then the minimum of the two will be used. Else, the one defined will be used ifcount
is not given.max_samples – (
int
) – Seemax_shots
.
- Return type:
- Returns:
the sample list