Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove printing that's specific to gradient-based VQE #262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a suggestion for future consideration. The decision to print gradient diagnostics should ideally depend on the type of optimizer being used, whether it is gradient-based or gradient-free. Even when numerical gradients are employed, gradient diagnostics should still be printed. However, scipy.optimize.minimize does not directly expose numerical gradients to the callback function. Therefore, the current decision to report gradient diagnostics for numerical gradients is based on self._use_analytic_grad.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at uccnvqe.py, it appears that a user could potentially request optimizer = "Nelder-Mead" and use_analytic_grad = True. More or less the same applies to adaptvqe.py. To ensure accurate printing of diagnostics, I suggest internally setting self._use_analytic_grad to False if a gradient-free optimizer is selected. Additionally, a warning message could be printed to inform the user of this change.

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
Loading