Skip to content

Commit

Permalink
Remove printing that's specific to gradient-based VQE (#262)
Browse files Browse the repository at this point in the history
* Remove printing that's specific to gradient-based VQE
  • Loading branch information
JonathonMisiewicz authored May 23, 2024
1 parent 49aad21 commit 2035e07
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/qforte/abc/uccvqeabc.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,34 +312,29 @@ def report_iteration(self, x):
self._k_counter += 1

if self._k_counter == 1:
print(
"\n k iteration Energy dE Ngvec ev Ngm ev* ||g||"
)
print(
"--------------------------------------------------------------------------------------------------"
)
header = "\n k iteration Energy dE"
if self._use_analytic_grad:
header += " Ngvec ev Ngm ev* ||g||"
header += "\n------------------------------------------------------"
if self._use_analytic_grad:
header += "--------------------------------------------"
print(header)
if self._print_summary_file:
f = open("summary.dat", "w+", buffering=1)
f.write(
"\n# k iteration Energy dE Ngvec ev Ngm ev* ||g||"
)
f.write(
"\n#--------------------------------------------------------------------------------------------------"
)
f.close()
header.replace("\n ", "\n# ").replace("\n-", "\n#--")
with open("summary.dat", "w+", buffering=1) as f:
f.write(header)

# else:
dE = self._curr_energy - self._prev_energy
print(
f" {self._k_counter:7} {self._curr_energy:+12.10f} {dE:+12.10f} {self._res_vec_evals:4} {self._res_m_evals:6} {self._curr_grad_norm:+12.10f}"
)
update = f" {self._k_counter:7} {self._curr_energy:+12.10f} {dE:+12.10f}"
if self._use_analytic_grad:
update += f" {self._res_vec_evals:4} {self._res_m_evals:6} {self._curr_grad_norm:+12.10f}"
print(update)

if self._print_summary_file:
f = open("summary.dat", "a", buffering=1)
f.write(
f"\n {self._k_counter:7} {self._curr_energy:+12.12f} {dE:+12.12f} {self._res_vec_evals:4} {self._res_m_evals:6} {self._curr_grad_norm:+12.12f}"
)
f.close()
with open("summary.dat", "a", buffering=1) as f:
update = "\n " + update
f.write(header)

self._prev_energy = self._curr_energy

Expand Down
7 changes: 7 additions & 0 deletions src/qforte/ucc/adaptvqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def run(
self._opt_maxiter = opt_maxiter
self._use_analytic_grad = use_analytic_grad
self._optimizer = optimizer
if self._use_analytic_grad and self._optimizer in {
"nelder-mead",
"powell",
"cobyla",
}:
print(f"{self._optimizer} optimizer doesn't support analytic grads.")
self._use_analytic_grad = False
self._pool_type = pool_type
self._use_cumulative_thresh = use_cumulative_thresh
self._add_equiv_ops = add_equiv_ops
Expand Down
7 changes: 7 additions & 0 deletions src/qforte/ucc/uccnvqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def run(
self._opt_maxiter = opt_maxiter
self._use_analytic_grad = use_analytic_grad
self._optimizer = optimizer
if self._use_analytic_grad and self._optimizer in {
"nelder-mead",
"powell",
"cobyla",
}:
print(f"{self._optimizer} optimizer doesn't support analytic grads.")
self._use_analytic_grad = False
self._pool_type = pool_type
self._noise_factor = noise_factor

Expand Down

0 comments on commit 2035e07

Please sign in to comment.