-
Notifications
You must be signed in to change notification settings - Fork 0
/
hadamard-random.py
36 lines (28 loc) · 954 Bytes
/
hadamard-random.py
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
36
# Random number generator
# using Hadamard logic gate
# Source: http://dataespresso.com/en/2018/07/22/Tutorial-Generating-random-numbers-with-a-quantum-computer-Python/
from projectq.ops import H, Measure
from projectq import MainEngine
"""
This Function creates a new qubit,
applies a Hadamard gate to put it in superposition,
and then measures the qubit to get a random
1 or 0.
"""
def get_random_number(quantum_engine):
qubit = quantum_engine.allocate_qubit()
H | qubit
Measure | qubit
random_number = int(qubit)
return random_number
# Storage for results
random_numbers_list = []
# Initialize engine
quantum_engine = MainEngine()
# Generate 10 random numbers
for i in range(10):
# calling the random number function and append the return to the list
random_numbers_list.append(get_random_number(quantum_engine))
# Flushes the quantum engine from memory
quantum_engine.flush()
print('Results', random_numbers_list)