-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_number_quantum_computer.py
53 lines (41 loc) · 1.19 KB
/
random_number_quantum_computer.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from qiskit import IBMQ
import qiskit
import numpy as np
from qiskit import(
QuantumCircuit,
execute,
Aer)
from qiskit.visualization import plot_histogram
# Please get the API key from IBM Q Experience
IBMQ.save_account('<YOUR IBMQ Key Here>')
# Function to convert binary number to decimal number
def binaryToDecimal(binary1):
binary = int(binary1)
decimal, i, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
print(decimal)
# Use Aer's qasm_simulator
simulator = Aer.get_backend('qasm_simulator')
# Create a Quantum Circuit acting on the q register
circuit = QuantumCircuit(1, 1)
number = ''
for i in range(0, 15):
# Add a H gate on qubit 0
circuit.h(0)
# Map the quantum measurement to the classical bits
circuit.measure([0,], [0,])
# Execute the circuit on the qasm simulator
job = execute(circuit, simulator, shots=1000)
# Grab results from the job
result = job.result()
# Returns counts
counts = result.get_counts(circuit)
if counts.get('1') > counts.get('0'):
number+='1'
else:
number+='0'
binaryToDecimal(number)