Generate a random bit sequence of 512 bits [duplicate]

How can I generate a random bit sequence of 512 bits on IBM Q experience using 3 or 5 qubits? Putting a hadammard gate and measuring would only give me smaller bit sequence due to limitation in number of qubits.

How to solve this problem?

Here is a code that will generate random bitstring. Althoug in the code, I have used qasm_simulator, it can be replaced with any available quantum hardware from IBM.

from qiskit import *

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)
one_qubit_random_bit_generator_circuit.h(0)
one_qubit_random_bit_generator_circuit.measure(0, 0)

for _ in range(bit_number):
counts = execute(one_qubit_random_bit_generator_circuit, backend, shots=1).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))

The output:

00000000000000000000000000000000000000000000000011111111111111111111111100000000111111111111111111111111000000000000000000000000000000001111111111111111000000000000000011111111000000000000000011111111111111111111111111111111111111111111111111111111111111111111111100000000000000001111111111111111111111111111111100000000111111110000000000000000000000000000000000000000111111110000000000000000000000001111111111111111000000001111111111111111000000001111111111111111111111111111111111111111000000001111111100000000

As one can see the generated bitstring doesn't look random. Maybe it is because of qasm_simulator. I think on real hardware this problem will not exist. Also the code uses only one qubit of the hardware that is not optimal if we have multiqubit hardware. We can use all available qubits in order to decrease the number of iterations in the for loop.

• If instead of using a single qubit circuit, you do it with a bigger one (24 is the maximum in BasicAer). You can get a more "random looking number" qc=QuantumCircuit(24);qc.h(range(24));qc.measure_all(); – luciano Sep 20 '20 at 13:49

Qiskit 0.21 has qiskit-ibmq-provider 0.9. This new provider comes with a connector to the RNG service in IBMQ. From the release notes:

You can now access the IBMQ random number services, such as the CQC randomness extractor, using the new package qiskit.providers.ibmq.random. Note that this feature is still in beta, and not all accounts have access to it. It is also subject to heavy modification in both functionality and API without backward compatibility.

You can find the documentation here and here.