Skip to content

Commit 1947ef4

Browse files
Added magic eight ball.
1 parent c8c98b9 commit 1947ef4

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

even.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from lib.oracles.even import oracle
44

55
def main():
6+
# Find all even numbers in a given range.
67
execute.provider = None
78

89
# Generate the quantum circuit.

lib/oracles/numeric.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from qiskit import QuantumCircuit
2+
from qiskit.circuit.library.standard_gates.z import ZGate
3+
4+
def oracle(i, n):
5+
# Returns a quantum circuit that recognizes a single number i.
6+
# Upon starting, all qubits are assumed to have a value of 1.
7+
# We use a circuit of size n+1 to include an output qubit.
8+
qc = QuantumCircuit(n+1)
9+
10+
# Convert i to a binary string.
11+
bin_str = bin(i)[2:]
12+
13+
# Pad the binary string with zeros to the length of the qubits.
14+
bin_str = bin_str.zfill(n)
15+
16+
print('Encoding ' + bin_str)
17+
18+
# Reverse the bits since qiskit represents the qubits from right to left.
19+
bin_str = bin_str[::-1]
20+
21+
# Flip each qubit to zero to match the bits in the target number i.
22+
for j in range(len(bin_str)):
23+
if bin_str[j] == '0':
24+
qc.x(j)
25+
26+
# Apply a controlled Z-gate on all qubits, setting the phase.
27+
qc.append(ZGate().control(n), range(n+1))
28+
29+
# Undo each inverted qubit.
30+
for j in range(len(bin_str)):
31+
if bin_str[j] == '0':
32+
qc.x(j)
33+
34+
print(qc.draw())
35+
36+
# Convert the oracle to a gate.
37+
gate = qc.to_gate()
38+
gate.name = "oracle"
39+
40+
return gate

magicball.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import random
2+
from math import ceil, log
3+
from lib.util import execute
4+
from lib.grover import grover
5+
from lib.oracles.numeric import oracle
6+
7+
predictions = [
8+
'It is certain.',
9+
'It is decidedly so.',
10+
'Without a doubt.',
11+
'Yes definitely.',
12+
'You may rely on it.',
13+
'As I see it, yes.',
14+
'Most likely.',
15+
'Outlook good.',
16+
'Yes.',
17+
'Signs point to yes.',
18+
'Reply hazy, try again.',
19+
'Ask again later.',
20+
'Better not tell you now.',
21+
'Cannot predict now.',
22+
'Concentrate and ask again.',
23+
'Don''t count on it.',
24+
'My reply is no.',
25+
'My sources say no.',
26+
'Outlook not so good.',
27+
'Very doubtful.'
28+
]
29+
30+
def main():
31+
"""
32+
Return a random prediction from a dictionary, selecting the index using Grover's search.
33+
Example:
34+
Please ask me a yes/no question and I will predict your future [PRESS ANY KEY] ...
35+
36+
Selected random index 4
37+
Encoding 00100
38+
39+
{'01000': 27, '01111': 27, '01110': 26, '11010': 31, '11001': 32, '00010': 27, '11101': 25, '10100': 27, '11100': 31, '11110': 24, '00111': 35, '01100': 31, '01010': 36, '10001': 30, '00011': 32, '00100': 135, '10011': 23, '00110': 25, '11111': 27, '10010': 23, '10110': 34, '10111': 34, '11000': 34, '00101': 36, '01001': 31, '10000': 19, '10101': 29, '00000': 26, '01011': 28, '00001': 23, '01101': 26, '11011': 30}
40+
Result: 4 (00100)
41+
42+
You may rely on it.
43+
"""
44+
45+
execute.provider = None
46+
47+
input('Please ask me a yes/no question and I will predict your future [PRESS ANY KEY] ...')
48+
49+
# Determine the number of qubits required.
50+
n = ceil(log(len(predictions), 2))
51+
52+
# Select a random prediction.
53+
r = random.choice(range(len(predictions)))
54+
55+
print()
56+
print('Selected random index ' + str(r))
57+
58+
# Generate the quantum circuit.
59+
qc = grover(oracle, r, n)
60+
print(qc.draw())
61+
62+
# Execute the quantum circuit.
63+
result = execute(qc)
64+
65+
# Get the resulting hit counts.
66+
counts = result.get_counts()
67+
print(counts)
68+
69+
# Find the most frequent hit count.
70+
key = max(counts, key=counts.get)
71+
72+
# Since the quantum computer returns a binary string (one bit for each qubit), we need to convert it to an integer.
73+
index = int(key, 2)
74+
75+
print()
76+
print('Result: ' + str(index) + ' (' + key + ')')
77+
78+
# Print the resulting prediction from the quantum circuit.
79+
print()
80+
print(predictions[index])
81+
82+
main()

odd.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from lib.oracles.odd import oracle
44

55
def main():
6+
# Find all odd numbers in a given range.
67
execute.provider = None
78

89
# Generate the quantum circuit.

0 commit comments

Comments
 (0)