@@ -179,7 +179,21 @@ def Get_Rsl_matrix(Xsk_op_list, N_Qubits):
179
179
return Rs_l_matrix
180
180
181
181
182
- def SeqRot_linalg_Energy (anti_commuting_sets , S_key_dict , N_Qubits , atol = 1e-8 , rtol = 1e-05 , check_reduction = False ):
182
+ def SeqRot_linalg_Energy_matrix (anti_commuting_sets , S_key_dict , N_Qubits , atol = 1e-8 , rtol = 1e-05 , check_reduction = False ):
183
+ """
184
+ Function giving ground state energy of Hamiltonian given as a dictionary of anti-commuting sets.
185
+ Note this function builds up full matrix iteratively. See SeqRot_linalg_Energy for symbolic method.
186
+
187
+
188
+ Args:
189
+ anti_commuting_sets (dict): dictionary of int keys with list of anti commuting QubitOperators sets
190
+ S_key_dict(dict): dictionary keys match that of anti_commuting_sets. Value gives index of P_s operator
191
+ N_Qubits(int): number of qubits
192
+
193
+ returns:
194
+ FCI_Energy(float): Ground state energy
195
+
196
+ """
183
197
# TODO: could return reduced_H_matrix sparse matrix!
184
198
185
199
reduced_H_matrix = csc_matrix ((2 ** N_Qubits , 2 ** N_Qubits ), dtype = complex )
@@ -211,4 +225,85 @@ def SeqRot_linalg_Energy(anti_commuting_sets, S_key_dict, N_Qubits, atol=1e-8, r
211
225
else :
212
226
eig_values , eig_vectors = eigsh (reduced_H_matrix , k = 1 , which = 'SA' ) # < solves eigenvalue problem for a complex Hermitian matrix.
213
227
FCI_Energy = min (eig_values )
228
+ return FCI_Energy
229
+
230
+
231
+
232
+ def Get_Rsl_matrix_as_qubitops (Xsk_op_list ):
233
+
234
+ """
235
+ Function that gives matrix of Rsl from a list of X_sk operators, theta_sks. This is the output from Get_Xsk_op_list function.
236
+ X_sk operators from a given anti_commuting set and S_index
237
+
238
+ Args:
239
+ X_sk_theta_sk_list(list): list of tuples containing X_sk QubitOperator and Theta_sk value
240
+
241
+ returns:
242
+ R_S_q_ops (QubitOperator)
243
+
244
+ """
245
+
246
+ ### old SLOW method (exponentiated matrices)
247
+ # R_sk_list = []
248
+ # for X_sk_Op, theta_sk in Xsk_op_list:
249
+ # pauliword_X_sk_MATRIX = qubit_operator_sparse(QubitOperator(list(X_sk_Op.terms.keys())[0], -1j),
250
+ # n_qubits=N_Qubits)
251
+ # const_X_sk = list(X_sk_Op.terms.values())[0]
252
+
253
+ # R_sk_list.append(expm(pauliword_X_sk_MATRIX * theta_sk / 2 * const_X_sk))
254
+ # Rs_l_matrix = reduce(np.dot, R_sk_list[::-1]) # <- note reverse order!
255
+
256
+ ### new FAST method (symbolic application of rotation operators!)
257
+ R_sk_list = []
258
+ for X_sk_Op , theta_sk in Xsk_op_list :
259
+ op = np .cos (theta_sk / 2 ) * QubitOperator ('' ) - 1j * np .sin (theta_sk / 2 ) * X_sk_Op
260
+ R_sk_list .append (op )
261
+
262
+ R_S_q_ops = reduce (lambda x ,y : x * y , R_sk_list [::- 1 ]) # <- note reverse order!
263
+ return R_S_q_ops
264
+
265
+
266
+ from openfermion .utils import hermitian_conjugated
267
+ def SeqRot_linalg_Energy (anti_commuting_sets , S_key_dict , N_Qubits , atol = 1e-8 , rtol = 1e-05 , check_reduction = False ):
268
+ """
269
+ Function giving ground state energy of Hamiltonian given as a dictionary of anti-commuting sets. Note this uses symbolic operators and only builds sparse matrix once.
270
+
271
+
272
+ Args:
273
+ anti_commuting_sets (dict): dictionary of int keys with list of anti commuting QubitOperators sets
274
+ S_key_dict(dict): dictionary keys match that of anti_commuting_sets. Value gives index of P_s operator
275
+ N_Qubits(int): number of qubits
276
+
277
+ returns:
278
+ FCI_Energy(float): Ground state energy
279
+
280
+ """
281
+ # TODO: could return reduced_H_matrix sparse matrix!
282
+
283
+
284
+ H_single_terms = QubitOperator ()
285
+ gammal_Rdag_P_R_terms = QubitOperator ()
286
+ for key in anti_commuting_sets :
287
+ AC_set = anti_commuting_sets [key ]
288
+
289
+ if len (AC_set ) < 2 :
290
+ H_single_terms += AC_set [0 ]
291
+ else :
292
+ S_index = S_key_dict [key ]
293
+
294
+ X_sk_theta_sk_list , full_normalised_set , Ps , gamma_l = Get_Xsk_op_list (AC_set , S_index , N_Qubits , check_reduction = check_reduction , atol = atol , rtol = rtol )
295
+
296
+
297
+ R_S = Get_Rsl_matrix_as_qubitops (X_sk_theta_sk_list )
298
+ R_dag_P_R = hermitian_conjugated (R_S ) * Ps * R_S
299
+ gammal_Rdag_P_R_terms += gamma_l * R_dag_P_R
300
+
301
+ all_symbolic_ops = H_single_terms + gammal_Rdag_P_R_terms
302
+ reduced_H_matrix = qubit_operator_sparse (all_symbolic_ops , n_qubits = N_Qubits )
303
+ # eig_values, eig_vectors = sparse_eigs(reduced_H_matrix)
304
+ if reduced_H_matrix .shape [0 ]<= 64 :
305
+ eig_values , eig_vectors = eigh (reduced_H_matrix .todense ()) # NOT sparse!
306
+ else :
307
+ eig_values , eig_vectors = eigsh (reduced_H_matrix , k = 1 , which = 'SA' ) # < solves eigenvalue problem for a complex Hermitian matrix.
308
+ FCI_Energy = min (eig_values )
214
309
return FCI_Energy
0 commit comments