Skip to content

Commit d88df33

Browse files
committed
changed sahred dictionary to a singleton object
1 parent d46a557 commit d88df33

2 files changed

Lines changed: 176 additions & 172 deletions

File tree

eqsn/gates.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
from eqsn.qubit_thread import SINGLE_GATE, MERGE_SEND, MERGE_ACCEPT, MEASURE,\
44
MEASURE_NON_DESTRUCTIVE, GIVE_QUBITS_AND_TERMINATE, \
55
CONTROLLED_GATE, QubitThread
6-
from eqsn.shared_dict import get_queues_for_ids, set_thread_with_id, \
7-
send_all_threads, stop_all_threads, \
8-
change_thread_and_queue_of_ids_and_join, delete_id_and_check_to_join_thread
6+
from eqsn.shared_dict import SharedDict
97

108

119
class EQSN(object):
@@ -17,6 +15,7 @@ class EQSN(object):
1715

1816
def __init__(self):
1917
self.manager = multiprocessing.Manager()
18+
self.shared_dict = SharedDict.get_instance()
2019

2120
def new_qubit(self, id):
2221
"""
@@ -25,70 +24,70 @@ def new_qubit(self, id):
2524
q = multiprocessing.Queue()
2625
thread = QubitThread(id, q)
2726
p = multiprocessing.Process(target=thread.run, args=())
28-
set_thread_with_id(id, p, q)
27+
self.shared_dict.set_thread_with_id(id, p, q)
2928
p.start()
3029

3130
def stop_all(self):
3231
"""
3332
Stops the simulator from running.
3433
"""
35-
send_all_threads(None)
36-
stop_all_threads()
34+
self.shared_dict.send_all_threads(None)
35+
self.shared_dict.stop_all_threads()
3736

3837
def X_gate(self, q_id):
3938
"""
4039
Applies the Pauli X gate to the Qubit with q_id.
4140
"""
4241
x = np.array([[0, 1], [1, 0]], dtype=np.csingle)
43-
q = get_queues_for_ids([q_id])[0]
42+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
4443
q.put([SINGLE_GATE, x, q_id])
4544

4645
def Y_gate(self, q_id):
4746
"""
4847
Applies the Pauli Y gate to the Qubit with q_id.
4948
"""
5049
x = np.array([[0, 0 - 1j], [0 + 1j, 0]], dtype=np.csingle)
51-
q = get_queues_for_ids([q_id])[0]
50+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
5251
q.put([SINGLE_GATE, x, q_id])
5352

5453
def Z_gate(self, q_id):
5554
"""
5655
Applies the Pauli Z gate to the Qubit with q_id.
5756
"""
5857
x = np.array([[1, 0], [0, -1]], dtype=np.csingle)
59-
q = get_queues_for_ids([q_id])[0]
58+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
6059
q.put([SINGLE_GATE, x, q_id])
6160

6261
def H_gate(self, q_id):
6362
"""
6463
Applies the Hadamard gate to the Qubit with q_id.
6564
"""
6665
x = (1 / 2.0) ** 0.5 * np.array([[1, 1], [1, -1]], dtype=np.csingle)
67-
q = get_queues_for_ids([q_id])[0]
66+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
6867
q.put([SINGLE_GATE, x, q_id])
6968

7069
def T_gate(self, q_id):
7170
"""
7271
Applies the T gate to the Qubit with q_id.
7372
"""
7473
x = np.array([[1, 0], [0, (0.7071067811865476 + 0.7071067811865475j)]], dtype=np.csingle)
75-
q = get_queues_for_ids([q_id])[0]
74+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
7675
q.put([SINGLE_GATE, x, q_id])
7776

7877
def S_gate(self, q_id):
7978
"""
8079
Applies the S gate to the Qubit with q_id.
8180
"""
8281
x = np.array([[1, 0], [0, 1j]], dtype=np.csingle)
83-
q = get_queues_for_ids([q_id])[0]
82+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
8483
q.put([SINGLE_GATE, x, q_id])
8584

8685
def K_gate(self, q_id):
8786
"""
8887
Applies the K gate to the Qubit with q_id.
8988
"""
9089
x = 0.5 * np.array([[1+1j, 1-1j], [-1+1j, -1-1j]], dtype=np.csingle)
91-
q = get_queues_for_ids([q_id])[0]
90+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
9291
q.put([SINGLE_GATE, x, q_id])
9392

9493
def RX_gate(self, q_id, rad):
@@ -98,7 +97,7 @@ def RX_gate(self, q_id, rad):
9897
mid = np.cos(rad / 2)
9998
other = -1j * np.sin(rad / 2)
10099
x = np.array([[mid, other], [other, mid]], dtype=np.csingle)
101-
q = get_queues_for_ids([q_id])[0]
100+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
102101
q.put([SINGLE_GATE, x, q_id])
103102

104103
def RY_gate(self, q_id, rad):
@@ -108,7 +107,7 @@ def RY_gate(self, q_id, rad):
108107
mid = np.cos(rad / 2)
109108
other = np.sin(rad / 2)
110109
x = np.array([[mid, -1.0 * other], [other, mid]], dtype=np.csingle)
111-
q = get_queues_for_ids([q_id])[0]
110+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
112111
q.put([SINGLE_GATE, x, q_id])
113112

114113
def RZ_gate(self, q_id, rad):
@@ -118,15 +117,15 @@ def RZ_gate(self, q_id, rad):
118117
top = np.exp(-1j * (rad / 2))
119118
bot = np.exp(1j * (rad / 2))
120119
x = np.array([[top, 0], [0, bot]], dtype=np.csingle)
121-
q = get_queues_for_ids([q_id])[0]
120+
q = self.shared_dict.get_queues_for_ids([q_id])[0]
122121
q.put([SINGLE_GATE, x, q_id])
123122

124123
def merge_qubits(self, q_id1, q_id2):
125124
"""
126125
Merges two qubits to one process, if they are not already
127126
running in the same process.
128127
"""
129-
l = get_queues_for_ids([q_id1, q_id2])
128+
l = self.shared_dict.get_queues_for_ids([q_id1, q_id2])
130129
if len(l) == 1:
131130
return # Already merged
132131
else:
@@ -139,7 +138,7 @@ def merge_qubits(self, q_id1, q_id2):
139138
qubits_q = self.manager.Queue()
140139
q1.put([GIVE_QUBITS_AND_TERMINATE, qubits_q])
141140
qubits = qubits_q.get()
142-
change_thread_and_queue_of_ids_and_join(qubits, q_id2)
141+
self.shared_dict.change_thread_and_queue_of_ids_and_join(qubits, q_id2)
143142

144143
def cnot_gate(self, q_id1, q_id2):
145144
"""
@@ -148,7 +147,7 @@ def cnot_gate(self, q_id1, q_id2):
148147
"""
149148
x = np.array([[0, 1], [1, 0]], dtype=np.csingle)
150149
self.merge_qubits(q_id1, q_id2)
151-
q = get_queues_for_ids([q_id1])[0]
150+
q = self.shared_dict.get_queues_for_ids([q_id1])[0]
152151
q.put([CONTROLLED_GATE, x, q_id1, q_id2])
153152

154153
def cphase_gate(self, q_id1, q_id2):
@@ -158,7 +157,7 @@ def cphase_gate(self, q_id1, q_id2):
158157
"""
159158
x = np.array([[0, 1], [0, -1]], dtype=np.csingle)
160159
self.merge_qubits(q_id1, q_id2)
161-
q = get_queues_for_ids([q_id1])[0]
160+
q = self.shared_dict.get_queues_for_ids([q_id1])[0]
162161
q.put([CONTROLLED_GATE, x, q_id1, q_id2])
163162

164163
def measure(self, id, non_destructive=False):
@@ -168,12 +167,12 @@ def measure(self, id, non_destructive=False):
168167
measurement, but its wavefunction collapses.
169168
"""
170169
ret = self.manager.Queue()
171-
q = get_queues_for_ids([id])[0]
170+
q = self.shared_dict.get_queues_for_ids([id])[0]
172171
if non_destructive:
173172
q.put([MEASURE_NON_DESTRUCTIVE, id, ret])
174173
else:
175174
q.put([MEASURE, id, ret])
176175
res = ret.get()
177176
if not non_destructive:
178-
delete_id_and_check_to_join_thread(id)
177+
self.shared_dict.delete_id_and_check_to_join_thread(id)
179178
return res

0 commit comments

Comments
 (0)