qiskit_rng.Generator

class Generator(backend: qiskit.providers.basebackend.BaseBackend, wsr_generator: Optional[Callable] = None, noise_model: Optional[Any] = None, save_local: bool = False)[source]

Class for generating random numbers.

You can use the sample() method to run quantum circuits on a backend to generate random bits. When the circuit jobs finish and their results processed, you can examine the generated random bits as well as the bell values. An example of this flow:

from qiskit import IBMQ
from qiskit_rng import Generator

provider = IBMQ.load_account()
generator = Generator(backend=provider.backends.ibmq_valencia)
output = generator.sample(num_raw_bits=1024).block_until_ready()
print("Mermin correlator value is {}".format(output.mermin_correlator))
print("first 8 raw bits are {}".format(output.raw_bits[:8]))

Note that due to the way the circuits are constructed and executed, the resulting size of raw_bits might be slightly larger than the input num_raw_bits.

The bits generated by the sample() method are only weakly random. You will need to apply a randomness extractor to this output in order to obtain a more uniformly distributed string of random bits.

Generator constructor.

Parameters
  • backend – Backend to use for generating random numbers.

  • wsr_generator – Function used to generate WSR. It must take the number of bits as the input and a list of random bits (0s and 1s) as the output. If None, qiskit_rng.generate_wsr() is used.

  • noise_model – Noise model to use. Only applicable if backend is a simulator.

  • save_local – If True, the generated WSR and other metadata is saved into a pickle file in the local directory. The file can be used to recover and resume a sampling if needed. The file name will be in the format of qiskit_rng_<backend>_<num_raw_bits>_<id>. The file will be deleted automatically when the corresponding GeneratorJob.block_until_ready() method is invoked. Only supported if backend is an IBMQBackend.

__init__(backend: qiskit.providers.basebackend.BaseBackend, wsr_generator: Optional[Callable] = None, noise_model: Optional[Any] = None, save_local: bool = False) → None[source]

Generator constructor.

Parameters
  • backend – Backend to use for generating random numbers.

  • wsr_generator – Function used to generate WSR. It must take the number of bits as the input and a list of random bits (0s and 1s) as the output. If None, qiskit_rng.generate_wsr() is used.

  • noise_model – Noise model to use. Only applicable if backend is a simulator.

  • save_local – If True, the generated WSR and other metadata is saved into a pickle file in the local directory. The file can be used to recover and resume a sampling if needed. The file name will be in the format of qiskit_rng_<backend>_<num_raw_bits>_<id>. The file will be deleted automatically when the corresponding GeneratorJob.block_until_ready() method is invoked. Only supported if backend is an IBMQBackend.

Methods

__init__(backend[, wsr_generator, …])

Generator constructor.

recover(file_name, provider)

Recover a previously saved sampling run.

sample(num_raw_bits)

Use the target system to generate raw bit strings.

classmethod recover(file_name: str, provider: qiskit.providers.ibmq.accountprovider.AccountProvider) → qiskit_rng.generator_job.GeneratorJob[source]

Recover a previously saved sampling run.

Parameters
  • file_name – Name of the file containing saved context.

  • provider – Provider used to do the sampling.

Returns

Recovered output of the original sample() call.

sample(num_raw_bits: int) → qiskit_rng.generator_job.GeneratorJob[source]

Use the target system to generate raw bit strings.

Parameters

num_raw_bits – Number of raw bits to sample. Note that the raw bits are only weakly random and needs to go through extraction to generate highly random output.

Returns

A GeneratorJob instance that contains all the information

needed to extract randomness.

Raises

ValueError – If an input argument is invalid.