Skip to content

Commit 94a88fe

Browse files
Generate random numbers using a quantum circuit.
1 parent 1947ef4 commit 94a88fe

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

hello.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import random
2-
from math import ceil, floor, log
3-
from lib.util import execute, generate
2+
from math import floor, log
3+
from lib.util import execute, num_qubits, random_letters
44
from lib.grover import grover
55
from lib.oracles.logic import oracle
66

77
def init(target, n):
88
# Initialize an array of random letters, including the target ones in the string.
9-
arr = generate(n)
9+
arr = random_letters(n)
1010

1111
# Store used indices so that we don't overwrite target letters.
1212
indices = [-1]
@@ -87,7 +87,7 @@ def main(phrase):
8787
execute.provider = None
8888

8989
# Calculate the number of qubits needed to represent the number of letters in the target.
90-
qubits = ceil(log(len(phrase), 2))
90+
qubits = num_qubits(len(phrase))
9191

9292
bits = 2 ** qubits
9393
arr = init(phrase, bits)

lib/util.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,57 @@
22
import random
33
import string
44
import time
5+
from math import ceil, log
56
from configparser import RawConfigParser
6-
from qiskit import qiskit, Aer, IBMQ
7+
from qiskit import qiskit, Aer, IBMQ, QuantumCircuit
78

8-
def generate(n):
9+
def random_letters(n):
910
# Generate an array of random letters.
1011
letters = []
1112
for i in range(n):
1213
letters.append(random.choice(string.ascii_letters))
1314
return letters
1415

16+
def random_number(minimum, maximum):
17+
# Uses a quantum circuit to generate a random number from minimum (inclusive) to maximum (exclusive).
18+
# Determine the number of qubits required.
19+
n = num_qubits(maximum-1)
20+
21+
# Create a quantum circuit with enough qubits for the max value.
22+
qc = QuantumCircuit(n)
23+
24+
# Place all qubits into superposition.
25+
qc.h(range(n))
26+
27+
# Measure the result.
28+
qc.measure_all()
29+
30+
# Continue executing the circuit until we obtain a value within range.
31+
r = -1
32+
count = 0
33+
max_count = 10
34+
while (r < minimum or r >= maximum) and count < max_count:
35+
# Execute the circuit.
36+
result = execute(qc)
37+
38+
# Get the resulting hit counts.
39+
counts = result.get_counts()
40+
41+
# Find the most frequent hit count.
42+
key = max(counts, key=counts.get)
43+
44+
# Since the quantum computer returns a binary string (one bit for each qubit), we need to convert it to an integer.
45+
r = int(key, 2)
46+
47+
# Increment the count before we break.
48+
count = count + 1
49+
50+
return r
51+
52+
def num_qubits(i):
53+
# Returns the number of qubits needed to represent the value i.
54+
return ceil(log(i, 2))
55+
1556
def execute(qc):
1657
# Setup the API key for the real quantum computer.
1758
parser = RawConfigParser()

magicball.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import random
2-
from math import ceil, log
3-
from lib.util import execute
1+
from lib.util import random_number, num_qubits, execute
42
from lib.grover import grover
53
from lib.oracles.numeric import oracle
64

@@ -47,10 +45,10 @@ def main():
4745
input('Please ask me a yes/no question and I will predict your future [PRESS ANY KEY] ...')
4846

4947
# Determine the number of qubits required.
50-
n = ceil(log(len(predictions), 2))
48+
n = num_qubits(len(predictions))
5149

52-
# Select a random prediction.
53-
r = random.choice(range(len(predictions)))
50+
# Generate a random number using a quantum circuit.
51+
r = random_number(0, len(predictions))
5452

5553
print()
5654
print('Selected random index ' + str(r))

0 commit comments

Comments
 (0)