Generator
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:
sample()
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.
raw_bits
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.
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.
None
qiskit_rng.generate_wsr()
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.
True
qiskit_rng_<backend>_<num_raw_bits>_<id>
GeneratorJob.block_until_ready()
IBMQBackend
__init__
Methods
__init__(backend[, wsr_generator, …])
recover(file_name, provider)
recover
Recover a previously saved sampling run.
sample(num_raw_bits)
sample
Use the target system to generate raw bit strings.
file_name – Name of the file containing saved context.
provider – Provider used to do the sampling.
Recovered output of the original sample() call.
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.
GeneratorJob
needed to extract randomness.
ValueError – If an input argument is invalid.