-
Notifications
You must be signed in to change notification settings - Fork 2
/
flappyq.py
123 lines (95 loc) · 3.54 KB
/
flappyq.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
from copy import deepcopy
from random import randint, shuffle
import qiskit
import numpy as np
from qiskit import Aer
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import execute
import qiskit.extensions.standard as gates
from qiskit.tools.visualization import plot_bloch_multivector
levels = {1: {'qubits': 1, 'states': ['0', '1'], 'gates': [gates.x, gates.y, gates.z, gates.iden]},
2: {'qubits': 2, 'states': ['00', '01', '10', '11'],
'gates': [gates.x, gates.y, gates.z, gates.iden, gates.h]}}
# qiskit.IBMQ.load_accounts()
backend = Aer.get_backend('qasm_simulator')
shots = 100
def get_desired_state(level):
states = levels[level]['states']
shuffle(states)
return states[0]
def get_random_gates(level):
gates = levels[level]['gates']
shuffle(gates)
target = randint(1, levels[level]['qubits'])
return (gates[0], target), (gates[1], target)
def check(desierd_state, user_circut, qr, cr):
circuit = deepcopy(user_circut)
if '+x' == desierd_state:
circuit.h(qr)
desierd_state = '0'
if '-x' == desierd_state:
circuit.h(qr)
desierd_state = '1'
circuit.measure(qr, cr)
job = execute(circuit, backend=backend, shots=shots)
res = job.result().get_counts()
print(res)
if desierd_state in res:
return res[desierd_state] / shots, read_state(res)
else:
return 0, read_state(res)
def read_state(res):
if 1 == len(res):
return list(res.keys())[0]
keys = list(res.keys())
out = keys[0]
for k in keys:
for i in range(len(k)):
if out[i] != k[i]:
out = out[:i] + '?' + out[i + 1:]
return out
def getGoalBlochs(state):
if state == '00':
psi = np.array([1,0,0,0])
return plot_bloch_multivector(psi, title="Goal Qubits")
elif state == '01':
psi = np.array([0,1,0,0])
return plot_bloch_multivector(psi, title="Goal Qubits")
elif state == '10':
psi = np.array([0,0,1,0])
return plot_bloch_multivector(psi, title="Goal Qubits")
elif state == '11':
psi = np.array([0,0,0,1])
return plot_bloch_multivector(psi, title="Goal Qubits")
elif state == '1':
psi = np.array([0,1])
return plot_bloch_multivector(psi, title="Goal Qubits")
elif state == '0':
psi = np.array([1,0])
return plot_bloch_multivector(psi, title="Goal Qubits")
def getCurrentBlochs(psi):
return plot_bloch_multivector(psi, title="Current Qubits")
if '__main__' == __name__:
LEVEL = 1
q = QuantumRegister(levels[LEVEL]['qubits']) # |0>
c = ClassicalRegister(levels[LEVEL]['qubits'])
circuit = QuantumCircuit(q, c)
desired_state = get_desired_state(LEVEL)
goal_blochs = getGoalBlochs(desired_state)
goal_blochs.savefig("desierd_sphere.png")
print("Desired state is: " + desired_state)
p = 0.0
while p < 0.9:
rgates = get_random_gates(LEVEL)
gateno = int(input("Select gate [1: " + rgates[0][0].__name__
+ str(rgates[0][1]) + "] or [2: "
+ rgates[1][0].__name__ + str(rgates[0][1]) + "] > "))
gate = rgates[gateno - 1][0]
target = rgates[gateno - 1][1] - 1
gate(circuit, q[target])
p, cstate = check(desired_state, circuit, q, c)
print("Probabilaty for desired state is: " + str(p))
print("Current state: |" + cstate + ">")
cblochs = getGoalBlochs(cstate)
cblochs.savefig("current_sphere.png")
print("Success")