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/or max_samples, in which case the one defined or the minimum of the two is the number of resulting samples.

Conversion code reference

class perceval.utils.conversion.ConversionHelper

Helper to makes the conversion between probs, samples and sample_count easy

static convert_to(target, original, **kwargs)
Parameters:
  • target (str) – the target name (probs, samples, sample_count)

  • original (BSDistribution | BSSamples | BSCount) – The BSSamples, BSCount or BSDistribution to convert

  • kwargs – The parameters to pass to the conversion (shots, samples)

Return type:

BSDistribution | BSSamples | BSCount

Returns:

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 convert

  • count – (int) – The final number of samples to generate. Can be None if either of the remaining kwargs is defined.

  • max_shots – (int) – If both max_shots and max_samples are given, then the minimum of the two will be used. Else, the one defined will be used if count is not given.

  • max_samples – (int) – See max_shots.

Return type:

BSCount

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 convert

  • count – (int) – The final number of samples to generate. Can be None if either of the remaining kwargs is defined.

  • max_shots – (int) – If both max_shots and max_samples are given, then the minimum of the two will be used. Else, the one defined will be used if count is not given.

  • max_samples – (int) – See max_shots.

Return type:

BSSamples

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:

BSDistribution

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 count

  • count – (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 both max_shots and max_samples are given, then the minimum of the two will be used. Else, the one defined will be used if count is not given.

  • max_samples – (int) – See max_shots.

Return type:

BSSamples

Returns:

the sample list

perceval.utils.conversion.samples_to_probs(sample_list)

Convert a chronological measured sample list to a state distribution

Parameters:

sample_list (BSSamples) – the list to convert

Return type:

BSDistribution

Returns:

the state distribution

perceval.utils.conversion.samples_to_sample_count(sample_list)

Convert a chronological measured sample list to a state count

Parameters:

sample_list (BSSamples) – the list to convert

Return type:

BSCount

Returns:

the state count