6
6
from qibo .optimizers import optimize
7
7
from scipy .optimize import minimize
8
8
9
- from qibochem .ansatz .hf_reference import hf_circuit
10
- from qibochem .ansatz .ucc import ucc_circuit
9
+ from qibochem .ansatz import hf_circuit , ucc_circuit
11
10
from qibochem .driver .molecule import Molecule
12
11
from qibochem .measurement .expectation import expectation
13
12
14
13
# Define molecule and populate
15
14
mol = Molecule (xyz_file = "lih.xyz" )
16
- try :
17
- mol .run_pyscf ()
18
- except ModuleNotFoundError :
19
- mol .run_psi4 ()
15
+ mol .run_pyscf ()
20
16
21
17
22
18
# Apply embedding and boson encoding
23
19
mol .hf_embedding (active = [1 , 2 , 5 ])
24
- hamiltonian = mol .hamiltonian (oei = mol . embed_oei , tei = mol . embed_tei , constant = mol . inactive_energy )
20
+ hamiltonian = mol .hamiltonian ()
25
21
26
22
# Set parameters for the rest of the experiment
27
23
n_qubits = mol .n_active_orbs
56
52
n_unique_excitations = 5
57
53
58
54
# UCCSD: Circuit
59
- all_coeffs = []
60
55
for _ex in excitations :
61
- coeffs = []
62
- circuit += ucc_circuit (n_qubits , _ex , coeffs = coeffs )
63
- all_coeffs .append (coeffs )
56
+ circuit += ucc_circuit (n_qubits , _ex )
64
57
65
58
# Draw the circuit if interested
66
59
print (circuit .draw ())
@@ -71,19 +64,28 @@ def electronic_energy(parameters):
71
64
r"""
72
65
Loss function for the UCCSD ansatz
73
66
"""
74
- all_parameters = []
67
+ coeff_dict = { 1 : ( - 1.0 , 1.0 ), 2 : ( - 0.25 , 0.25 , 0.25 , 0.25 , - 0.25 , - 0.25 , - 0.25 , 0.25 )}
75
68
76
- # UCC parameters
77
- # Expand the parameters to match the total UCC ansatz manually
78
- _ucc = parameters [:n_unique_excitations ]
69
+ # Unique UCC parameters
79
70
# Manually group the related excitations together
80
- ucc_parameters = [_ucc [0 ], _ucc [1 ], _ucc [2 ], _ucc [2 ], _ucc [3 ], _ucc [3 ], _ucc [4 ], _ucc [4 ]]
81
- # Need to iterate through each excitation this time
82
- for _coeffs , _parameter in zip (all_coeffs , ucc_parameters ):
71
+ ucc_parameters = [
72
+ parameters [0 ],
73
+ parameters [1 ],
74
+ parameters [2 ],
75
+ parameters [2 ],
76
+ parameters [3 ],
77
+ parameters [3 ],
78
+ parameters [4 ],
79
+ parameters [4 ],
80
+ ]
81
+ all_parameters = []
82
+ # Iterate through each excitation, with the list of coefficients dependent on whether S/D excitation
83
+ for _ex , _parameter in zip (excitations , ucc_parameters ):
84
+ coeffs = coeff_dict [len (_ex ) // 2 ]
83
85
# Convert a single value to a array with dimension=n_param_gates
84
- ucc_parameter = np .repeat (_parameter , len (_coeffs ))
86
+ ucc_parameter = np .repeat (_parameter , len (coeffs ))
85
87
# Multiply by coeffs
86
- ucc_parameter *= _coeffs
88
+ ucc_parameter *= coeffs
87
89
all_parameters .append (ucc_parameter )
88
90
89
91
# Flatten all_parameters into a single list to set the circuit parameters
@@ -101,7 +103,7 @@ def electronic_energy(parameters):
101
103
102
104
best , params , extra = optimize (electronic_energy , params )
103
105
104
- print ("\n Results using Qibo optimize:" )
106
+ print ("\n Results using Qibo optimize: (With HF embedding) " )
105
107
print (f"FCI energy: { fci_energy :.8f} " )
106
108
print (f" HF energy: { mol .e_hf :.8f} (Classical)" )
107
109
print (f"VQE energy: { best :.8f} (UCCSD ansatz)" )
@@ -115,15 +117,15 @@ def electronic_energy(parameters):
115
117
result = minimize (electronic_energy , params )
116
118
best , params = result .fun , result .x
117
119
118
- print ("\n Results using scipy.optimize:" )
120
+ print ("\n Results using scipy.optimize: (With HF embedding) " )
119
121
print (f"FCI energy: { fci_energy :.8f} " )
120
122
print (f" HF energy: { mol .e_hf :.8f} (Classical)" )
121
123
print (f"VQE energy: { best :.8f} (UCCSD ansatz)" )
122
124
# print()
123
125
# print("Optimized parameters:", params)
124
126
125
127
126
- full_ham = mol .hamiltonian ("f" )
128
+ full_ham = mol .hamiltonian ("f" , oei = mol . oei , tei = mol . tei , constant = 0.0 )
127
129
mol_fci_energy = mol .eigenvalues (full_ham )[0 ]
128
130
129
131
print (f"\n FCI energy: { mol_fci_energy :.8f} (Full Hamiltonian)" )
0 commit comments