-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
141 lines (100 loc) · 3.24 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %% [markdown]
# #### imports
# %%
# Import qiskit modules.
from qiskit import QuantumCircuit, assemble, Aer, execute, IBMQ
from qiskit.tools import job_monitor
from qiskit.providers.ibmq import least_busy
from qiskit.visualization import plot_bloch_multivector
from qiskit_textbook.tools import array_to_latex
# Import other modules.
import numpy as np
from plyer import notification
from time import time
# %% [markdown]
# #### create quantum circuit
# %%
# Get # of qubits for quantum circuit and range of random num
def init_gen():
n_qubits = int(input('Choose # of qubits (limit: 5) -> '))
_min = int(input('Choose min'))
_max = int(input('Choose max'))
if n_qubits > 6:
init_gen()
else:
return n_qubits, _min, _max
n_qubits, _min, _max = init_gen()
qc = QuantumCircuit(n_qubits, n_qubits)
for i in range(n_qubits):
qc.h(i)
qc.measure(range(n_qubits), range(n_qubits))
qobj = assemble(qc)
qc.draw(output='mpl')
# %% [markdown]
# #### run circuit on quantum computer
# %%
IBMQ.load_account()
provider = IBMQ.get_provider('ibm-q')
start_time = time()
# Get the computers with amount of qubits.
qcomp = provider.backends(filters=lambda x: x.configuration().n_qubits >= n_qubits, simulator=False)
# Get least busy computer.
qcomp = least_busy(qcomp)
print('Running on', qcomp)
# NOTE: To choose the best backend, check: https://quantum-computing.ibm.com/services?systems=yours
# Run circuit.
job = execute(qc, backend=qcomp, memory=True)
job_monitor(job)
result = job.result()
state = result.get_memory(qc)[0]
elapsed = (time() - start_time)/60
print('state ->', state)
print(f'elapsed -> {elapsed:.2f} min.')
statevectors = [
[1, 0],
[0, 1]
]
for i, qubit_state in enumerate(state):
qc.initialize(statevectors[int(qubit_state)], i)
qc.draw()
qobj = assemble(qc)
# # NOTE: The reason why we re-initialize the state as the result of the simulation
# # is so that we can represent that state on a bloch sphere.
simulator = Aer.get_backend('statevector_simulator')
job = simulator.run(qobj)
statevector = job.result().get_statevector(qc)
plot_bloch_multivector(statevector)
notification.notify(
title='Quantum Circuit Sim Results',
message=f'elapsed -> {elapsed:.2f} min.\nstate -> {state}',
app_icon='qiskit_icon.ico'
)
# %% [markdown]
# #### convert statevector to random number
# %%
# Produce random number based on the results from experiment.
def real_map(value, left_min, left_max, right_min, right_max):
# Figure out how 'wide' each range is.
left_span = left_max - left_min
right_span = right_max - right_min
# Convert the left range into a 0-1 range (float) NOTE: This is the equation that finalizes the num.
value_scaled = float(value - left_min) / float(left_span)
# Convert the 0-1 range into a value in the right range.
return right_min + (value_scaled * right_span)
n1, n2, n3 = (0, 0, 0)
for i in range(statevector.size):
if abs(statevector[i]) != 0:
n1 = i
n2 = np.real(statevector[i])
n3 = np.imag(statevector[i])
print(
real_map(
n1 + n2+ n3,
-n_qubits,
len(statevector) - 1 + n_qubits,
_min,
_max
)
)