def random_bitstring_generator(bit_number, backend):
    Generate a bitstring with one qubit
    :param bit_number: number of bits that we want to generate
    :param backend: Qiskit hardware or simulator
    :return: string object with random bits

    bitstring = ""

    one_qubit_random_bit_generator_circuit = QuantumCircuit(1,1)

    for _ in range(bit_number):
        counts = execute(one_qubit_random_bit_generator_circuit, backend, shots=1000).result().get_counts()
        bit = list(counts.keys())[0]  # we have only one key because shots=1
        bitstring += bit

    return bitstring

backend = BasicAer.get_backend('qasm_simulator')

print(random_bitstring_generator(512, backend))

Executed the above code for QRNG using 1 qubit and received a 512 bit sequence 512 bit output

Executing the same circuit for 3 qubit code as given in image received an output of increased length as shown in image3 qubit qrng

increased length ouptut for 3 qubit qrng implementation of 512 bits

Also how can i change the backend from qasm_simulator to vigo/melbourne/any other quantum machine


2 Answers 2


The number of bits in the counts dictionary equals the number of qubits in the circuit. So in your first example, you have a 1-qubit circuit, therefore you're dictionary looks something like

counts = {'0': 400, '1': 600}  # for for 1000 shots
counts = {'0': 1} # for 1 shot

In the second example, the Jupyter notebook screenshot, you have three qubits. Therefore

counts = {'000': 70, '010': 80, '110': 70, ... }  # for for 1000 shots
counts = {'101': 1} # for 1 shot

Since you add these bitstrings together, the resulting bitstring will be three times longer in the second case.

Side note: You are actually running a 1000 shots, not one, you should change the argument shots=1000 to shots=1.

You might also want to have a look at the get_memory method of the job, which allows you to run e.g. 512 shots and get all single measurements, which should be more efficient than running a 1-shot experiment 512 times.


To execute on hardware backend:

provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_vigo') 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.