Skip to content

Commit 94443a0

Browse files
committed
updated SeqRot functions to involve symbolic operations more and less matrix methods. Should run faster.
1 parent 98a41c4 commit 94443a0

File tree

6 files changed

+2386
-1637
lines changed

6 files changed

+2386
-1637
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@
1414
.DS_Store
1515

1616
Projects/CS_VQE/data/fullHamiltonians/fullHamiltonians/*
17+
Projects/CS_VQE/Analysis/Be1_STO-3G_singlet
18+
Projects/CS_VQE/Analysis/C1-O1_STO-3G_singlet
19+
Projects/CS_VQE/Analysis/F2_STO-3G_singlet
20+
Projects/CS_VQE/Analysis/H1-Cl1_STO-3G_singlet
21+
Projects/CS_VQE/Analysis/H1-F1_3-21G_singlet
22+
Projects/CS_VQE/Analysis/H1-F1_STO-3G_singlet
23+
Projects/CS_VQE/Analysis/H1-He1_3-21G_singlet_1+
24+
Projects/CS_VQE/Analysis/H1-Li1-O1_STO-3G_singlet
25+
Projects/CS_VQE/Analysis/H1-Li1_3-21G_singlet
26+
Projects/CS_VQE/Analysis/H1-Li1_STO-3G_singlet
27+
Projects/CS_VQE/Analysis/H1-Na1_STO-3G_singlet
28+
Projects/CS_VQE/Analysis/H1-O1_STO-3G_singlet
29+
Projects/CS_VQE/Analysis/H2-Be1_STO-3G_singlet
30+
Projects/CS_VQE/Analysis/H2-Mg1_STO-3G_singlet
31+
Projects/CS_VQE/Analysis/H2-S1_STO-3G_singlet
32+
Projects/CS_VQE/Analysis/H2_3-21G_singlet
33+
Projects/CS_VQE/Analysis/H2_6-31G_singlet
34+
Projects/CS_VQE/Analysis/H3-N1_STO-3G_singlet
35+
Projects/CS_VQE/Analysis/H3_3-21G_singlet_1+
36+
Projects/CS_VQE/Analysis/H3_STO-3G_singlet_1+
37+
Projects/CS_VQE/Analysis/H4-C1_STO-3G_singlet
38+
Projects/CS_VQE/Analysis/H4-N1_STO-3G_singlet_1+
39+
Projects/CS_VQE/Analysis/Mg1_STO-3G_singlet
40+
Projects/CS_VQE/Analysis/N2_STO-3G_singlet
41+
Projects/CS_VQE/Analysis/O2_STO-3G_singlet
1742

1843
quchem_examples/tmp*
1944
Projects/CS_VQE/Pickle_out/*.pickle

Projects/CS_VQE/Analysis/Full_Results_CSVQE_and_UP_LCU_and_SeqRot_on_script_A_with_pruning.ipynb

Lines changed: 281 additions & 469 deletions
Large diffs are not rendered by default.
-394 Bytes
Binary file not shown.

quchem/Unitary_Partitioning/Unitary_partitioning_Seq_Rot.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,21 @@ def Get_Rsl_matrix(Xsk_op_list, N_Qubits):
179179
return Rs_l_matrix
180180

181181

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+
"""
183197
# TODO: could return reduced_H_matrix sparse matrix!
184198

185199
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
211225
else:
212226
eig_values, eig_vectors = eigsh(reduced_H_matrix, k=1, which='SA') # < solves eigenvalue problem for a complex Hermitian matrix.
213227
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)
214309
return FCI_Energy

0 commit comments

Comments
 (0)