1818
1919
2020class QubitThread (object ):
21+ """
22+ The Qubit thread is the smallest object in EQSN.
23+ It consists of a statevector and the Qubit IDs of the state vector.
24+ Most operations here can be applid asynchronously.
25+ """
2126
2227 def __init__ (self , q_id , queue ):
2328 """
@@ -42,15 +47,15 @@ def __init__(self, q_id, queue):
4247
4348 logging .debug ("Qubit thread with qubit %s has been created." , q_id )
4449
45- def apply_single_gate (self , mat , q_id ):
50+ def apply_single_gate (self , gate , q_id ):
4651 """
4752 Applys a single gate to a qubit.
4853
4954 Args:
50- mat (np.array): 2x2 unitary array.
55+ gate (np.array): 2x2 unitary array.
5156 id (String): Qubit on which the gate should be applied to.
5257 """
53- apply_mat = mat
58+ apply_mat = gate
5459 nr = self .qubits .index (q_id )
5560 total_amount = len (self .qubits )
5661 before = nr
@@ -62,11 +67,17 @@ def apply_single_gate(self, mat, q_id):
6267 self .qubit = np .dot (apply_mat , self .qubit )
6368
6469 def give_statevector (self , channel ):
70+ """
71+ Sends the Qubit IDs and their state vectors over a channel.
72+
73+ Args:
74+ channel(Queue): Channel to return the requested data to.
75+ """
6576 channel .put ((dp (self .qubits ), dp (self .qubit )))
6677
6778 def apply_controlled_gate (self , mat , q_id1 , q_id2 ):
6879 """
69- Apply a controlled gate to
80+ Applies a controlled gate to q_id1
7081 """
7182 first_mat = 1
7283 second_mat = 1
@@ -111,7 +122,11 @@ def apply_controlled_gate(self, mat, q_id1, q_id2):
111122
112123 def merge_accept (self , channel ):
113124 """
114- Receive another process to merge it with this one.
125+ Receive the statevector and qubit information of another
126+ thread with this thread and merge the vectors.
127+
128+ Args:
129+ channel(Queue): channel to receive qubit ids and statevectors from.
115130 """
116131 ids = channel .get ()
117132 vector = channel .get ()
@@ -122,6 +137,11 @@ def merge_accept(self, channel):
122137 def merge_send (self , channel , chanel2 ):
123138 """
124139 Send own process data to another process and suicide.
140+
141+ Args:
142+ channel(Queue): Channel to send own data to other Qubit Thread.
143+ channel2(Queue): Channel to send qubit ids to parent, to update
144+ the qubit ids in its dictionary.
125145 """
126146 channel .put (dp (self .qubits ))
127147 channel .put (dp (self .qubit ))
@@ -132,6 +152,9 @@ def swap_qubits(self, q_id1, q_id2):
132152 """
133153 Swaps the position of qubit q_id1 with q_id2
134154 in the statevector.
155+
156+ q_id1(String): Qubit id of one of the qubits to swap.
157+ q_id2(String): Qubit id of the other qubit to swap.
135158 """
136159 def cnot (q_id1 , q_id2 ):
137160 mat = np .asarray ([[0 , 1 ], [1 , 0 ]])
@@ -150,7 +173,15 @@ def cnot(q_id1, q_id2):
150173 i2 = self .qubits .index (q_id2 )
151174 self .qubits [i1 ], self .qubits [i2 ] = self .qubits [i2 ], self .qubits [i1 ]
152175
153- def apply_two_qubit_gate (self , mat , q_id1 , q_id2 ):
176+ def apply_two_qubit_gate (self , gate , q_id1 , q_id2 ):
177+ """
178+ Applies a two qubit gate to the statevector.
179+
180+ Args:
181+ gate(np.ndarray): 4x4 unitary matrix
182+ q_id1(String): First qubit id.
183+ q_id2(String): Second qubit id.
184+ """
154185 # Bring the qubits in the right order
155186 i2 = self .qubits .index (q_id2 )
156187 if i2 > 0 :
@@ -159,7 +190,7 @@ def apply_two_qubit_gate(self, mat, q_id1, q_id2):
159190 else :
160191 self .swap_qubits (q_id1 , self .qubits [0 ])
161192 self .swap_qubits (q_id2 , self .qubits [1 ])
162- apply_mat = mat
193+ apply_mat = gate
163194 nr1 = self .qubits .index (q_id1 )
164195 total_amount = len (self .qubits )
165196 before = nr1
@@ -170,9 +201,13 @@ def apply_two_qubit_gate(self, mat, q_id1, q_id2):
170201 apply_mat = np .kron (apply_mat , np .eye (2 ** after ))
171202 self .qubit = np .dot (apply_mat , self .qubit )
172203
173- def measure_non_destructive (self , q_id , ret_channel ):
204+ def measure_non_destructive (self , q_id , channel ):
174205 """
175206 Perform a non destructive measurement on qubit with the id.
207+
208+ Args:
209+ q_id(String): ID of the Qubit to measure.
210+ channel(Queue): Channel to transmit measurement result to.
176211 """
177212 # determine probability for |1>
178213 measure_vec = np .array ([1 , 0 ], dtype = np .csingle )
@@ -194,11 +229,11 @@ def measure_non_destructive(self, q_id, ret_channel):
194229 reduction_mat = None
195230 if meas_res == 0 :
196231 # |0> has been measured
197- ret_channel .put (0 )
232+ channel .put (0 )
198233 reduction_mat = np .array ([[1 , 0 ], [0 , 0 ]], dtype = np .csingle )
199234 else :
200235 # |1> has been measured
201- ret_channel .put (1 )
236+ channel .put (1 )
202237 reduction_mat = np .array ([[0 , 0 ], [0 , 1 ]], dtype = np .csingle )
203238 if before > 0 :
204239 reduction_mat = np .kron (
@@ -212,9 +247,13 @@ def measure_non_destructive(self, q_id, ret_channel):
212247 norm = np .linalg .norm (self .qubit )
213248 self .qubit = self .qubit / norm
214249
215- def measure (self , q_id , ret_channel ):
250+ def measure (self , q_id , channel ):
216251 """
217252 Perform a destructive measurement on qubit with the id.
253+
254+ Args:
255+ q_id(String): ID of the Qubit to measure.
256+ channel(Queue): Channel to transmit measurement result to.
218257 """
219258 # determine probability for |1>
220259 measure_vec = np .array ([1 , 0 ], dtype = np .csingle )
@@ -236,11 +275,11 @@ def measure(self, q_id, ret_channel):
236275 reduction_mat = None
237276 if meas_res == 0 :
238277 # |0> has been measured
239- ret_channel .put (0 )
278+ channel .put (0 )
240279 reduction_mat = np .array ([1 , 0 ], dtype = np .csingle )
241280 else :
242281 # |1> has been measured
243- ret_channel .put (1 )
282+ channel .put (1 )
244283 reduction_mat = np .array ([0 , 1 ], dtype = np .csingle )
245284 if before > 0 :
246285 reduction_mat = np .kron (
@@ -278,6 +317,7 @@ def run(self):
278317 elif item [0 ] == MERGE_ACCEPT :
279318 self .merge_accept (item [1 ])
280319 elif item [0 ] == MERGE_SEND :
320+ # After merge, this thread is not needed anymore
281321 self .merge_send (item [1 ], item [2 ])
282322 return
283323 elif item [0 ] == MEASURE_NON_DESTRUCTIVE :
0 commit comments