-
Notifications
You must be signed in to change notification settings - Fork 6
/
simulate.py
173 lines (132 loc) · 4.58 KB
/
simulate.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Simulation file for Qu[H]ack[Wo]man
"""
from qiskit import QuantumCircuit, Aer, execute, QuantumRegister, ClassicalRegister
import numpy as np
import math
class QuantumSimulation():
def __init__(self):
self.gates1 = []
self.gates2 = []
self.qc = QuantumCircuit(2, 2)
self.qc.h(0)
self.qc.cx(0,1)
self.output = []
self.shots_num = 1
self.result = None
self.did_win = None
self.game_gates1 = []
self.game_gates2 = []
def load_gates(self):
count1 = 0
for i in range(len(self.gates1)):
if self.gates1[i] == 'T':
self.qc.rx(np.pi/4,0)
count1 += 1
elif self.gates1[i] == 'S':
self.qc.rx(np.pi/2,0)
count1 += 1
elif self.gates1[i] == 'Z':
self.qc.x(0)
count1 += 1
count2 = 0
for i in range(len(self.gates2)):
if self.gates2[i] == 'T':
self.qc.rx(np.pi/4,1)
count2 += 1
elif self.gates2[i] == 'S':
self.qc.rx(np.pi/2,1)
count2 += 1
elif self.gates2[i] == 'Z':
self.qc.x(1)
count2 += 1
# add the identity
if count2 < count1:
for i in range(count1-count2):
self.qc.iden(1)
elif count2 > count1:
for i in range(count2-count1):
self.qc.iden(0)
def add_gate(self, player, gate):
if str(player) == "1":
self.gates1.append(gate.upper())
elif str(player) == "2":
self.gates2.append(gate.upper())
def add_game_gate(self, player, gate):
if str(player) == "1":
self.game_gates1.append(gate.upper())
elif str(player) == "2":
self.game_gates2.append(gate.upper())
def run(self):
simulator = Aer.get_backend('qasm_simulator')
self.qc.measure([0,1],[1,0])
# Execute the circuit on the qasm simulator
job = execute(self.qc, simulator, shots=self.shots_num)
# Grab results from the job
result = job.result()
# Returns counts
counts = result.get_counts(self.qc)
print("\nTotal count for 00 and 11 are:",counts)
self.output = counts
def measure(self, ra, rb, player_number):
# ra and rb are in degrees
print("SELF", player_number, ra, rb)
ra, rb = ra * math.pi/180, rb * math.pi/180
self.qc = QuantumCircuit(1, 1)
simulator = Aer.get_backend('qasm_simulator')
for i in range(len(self.game_gates1)):
if self.game_gates1[i] == 'T':
self.qc.rx(np.pi/4,0)
elif self.game_gates1[i] == 'S':
self.qc.rx(np.pi/2,0)
elif self.game_gates1[i] == 'Z':
self.qc.x(0)
for i in range(len(self.game_gates2)):
if self.game_gates2[i] == 'T':
self.qc.rx(np.pi/4,1)
elif self.game_gates2[i] == 'S':
self.qc.rx(np.pi/2,1)
elif self.game_gates2[i] == 'Z':
self.qc.x(1)
# self.qc.h(0)
# self.qc.cx(0,1)
# self.qc.barrier()
# self.qc.x(1)
self.qc.rx(math.pi*0.5+(ra if player_number == 1 else rb),0)
# self.qc.rx(rb,1)
self.qc.measure([0],[0])
print(self.qc.draw())
job = execute(self.qc, simulator, shots=1)
result = job.result()
counts = result.get_counts(self.qc)
counts.setdefault('0', 0)
counts.setdefault('1', 0)
zero_counts = counts['0']
one_counts = counts['1']
self.did_win = (player_number, zero_counts > one_counts)
class QuantumRandomizer():
def __init__(self):
self.rng_n_qubits = 2
def random_num_generator(self):
q = QuantumRegister(self.rng_n_qubits, 'q')
circ = QuantumCircuit(q)
c0 = ClassicalRegister(2, 'c0')
circ.add_register(c0)
for i in range(self.rng_n_qubits):
circ.h(q[i])
for i in range(self.rng_n_qubits):
circ.measure(q[i], c0)
backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
result = job.result()
output = result.get_statevector(circ, decimals=5)
n1 = 0
n2 = 0
n3 = 0
for i in range( output.size ):
if abs(output[i]) != 0:
n1 = i
n2 = np.real(output[i])
n3 = np.imag(output[i])
y = n1+n2+n3-1
return y