Generating a random 16-digits in a superposition state using Qiskit

Im trying to create a script using Qiskit for a days but somehow i couldn't seem to get it done!! Im new to QuantumComputing, so pardon me if i don't explain something properly. I need to create a simple script generating perfectly a random 16-digits (e. g 1548796654421354) in a superposition state using Hadamard gate (i think it requires 128 qubits i guess) and measuring them into a classical bits and printing the results different each time. (eg output: 5698744565414654)

Thank you King.

You need $$\lceil d\log_2(10)\rceil$$ qubits to store a $$d$$ decimal digits number.

So you can generate a superposition state of binary representation of a 16 decimal digits number using the following circuit:

from qiskit import QuantumRegister, QuantumCircuit, Aer
import numpy as np

num_of_decimal_digits = 16
num_of_qubits = int(np.ceil(num_of_decimal_digits * np.log2(10)))
qr = QuantumRegister(num_of_qubits, 'q')
circ = QuantumCircuit(num_of_qubits)
circ.h(qr)
circ.measure_all()


And to get the number:

simulator = Aer.get_backend('qasm_simulator')
result = simulator.run(circ, shots = 1).result()
counts = result.get_counts()
bitstring = next(iter(counts))
decimal = int(bitstring, 2)

print('Binary:', bitstring)
print('Decimal:', f'{decimal:016d}')


Note: You need all these qubits to have a superposition. If you just want to generate a random 16-digits number, you can use a single qubit and run the circuit repeatedly (by setting shots = number of bits) then append the result bits.

• thanks, sometimes it generates 16 digits and sometimes 17 digits, why is that happening? Apr 22 at 10:00
• This because $16$ decimal digits needs $16\cdot\log_2(10) = 53.1508495$ qubits. I chose $54$ qubits. You can choose $53$ qubits by using np.floor instead of np.ceil. But this will not cover all the possible $16$ decimal digits numbers. Apr 22 at 10:13
• thanks again mate! but what do you mean by this will not cover all the possible 16 decimal digits numbers? Apr 22 at 10:48
• $16$ digits decimal number ranges from $16$ zeros to $16$ nines. Using $53$ qubits, you can put the numbers between $0$ and $2^{53} - 1 = 9007199254740991$ in a superposition. That is, the values from $9007199254740992$ to $9,999,999,999,999,999$ will not be generated. Apr 22 at 11:11