-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-number-generator.py
More file actions
35 lines (27 loc) · 959 Bytes
/
random-number-generator.py
File metadata and controls
35 lines (27 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from qiskit import *
backend = Aer.get_backend("qasm_simulator")
# generates random number2# between 0 and 2^lim - 1
def oneshotRandomNumber(lim):
li = [i for i in range(lim)]
qc = QuantumCircuit(lim, lim)
qc.h(li)
qc.measure(li, li)
ans = int(list(execute(qc, backend, shots=1).result().get_counts())[0], 2)
print("Random number between 0-"+str(pow(2, lim)-1)+" is ->", ans)
# generates random number between 0 and lim - 1
def bitwiseRandomNumber(lim):
ans = 0
multiplier = 1
while(ans < lim):
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0)
num = int(list(execute(qc, backend, shots=1).result().get_counts())[0])
if(ans+(num*multiplier) >= lim):
break
ans += num*multiplier
multiplier <<= 1
print("Random number between 0-"+str(lim-1)+" is ->", ans)
lim = int(input("Enter a number - "))
bitwiseRandomNumber(lim)
oneshotRandomNumber(lim)