-
Notifications
You must be signed in to change notification settings - Fork 26
Add stabilized SCF #520
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
Open
nabbelbabbel
wants to merge
9
commits into
main
Choose a base branch
from
jpu/stabilized-scf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add stabilized SCF #520
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
205753f
add stabilized scf impl
e65ec9a
add pyscf version
5b40b4d
Merge remote-tracking branch 'origin/main' into jpu/stabilized-scf
816d699
resolve comments
eec6de1
resolve comments fix bugs
b82bfe8
fix docs?
8e7f124
Merge remote-tracking branch 'origin/main' into jpu/stabilized-scf
4acb4d8
resolve comment
e1bc451
Merge branch 'main' into jpu/stabilized-scf
nabbelbabbel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
cpp/src/qdk/chemistry/algorithms/microsoft/stabilized_scf.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE.txt in the project root for | ||
| // license information. | ||
|
|
||
| #include "stabilized_scf.hpp" | ||
|
|
||
| #include <qdk/chemistry/algorithms/stability.hpp> | ||
| #include <qdk/chemistry/data/stability_result.hpp> | ||
| #include <qdk/chemistry/utils/orbital_rotation.hpp> | ||
| #include <string> | ||
|
|
||
| namespace qdk::chemistry::algorithms::microsoft { | ||
|
|
||
| namespace detail { | ||
| void copy_common_scf_settings(const data::Settings& source, | ||
| data::Settings& destination) { | ||
| for (const auto& [key, value] : source.get_all_settings()) { | ||
| if (destination.has(key)) { | ||
| destination.update(key, value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool can_check_external_stability( | ||
| const std::shared_ptr<data::Wavefunction>& wavefunction) { | ||
| const auto& symmetries = wavefunction->get_orbitals()->symmetries(); | ||
| if (!symmetries || !symmetries->has_axis(data::AxisName::Spin)) { | ||
| return false; | ||
| } | ||
| if (!symmetries->axis(data::AxisName::Spin).equivalent()) { | ||
| return false; | ||
| } | ||
| const auto counts = wavefunction->total_num_particles(); | ||
| const auto num_alpha = counts->value(data::axes::alpha()); | ||
| const auto num_beta = counts->value(data::axes::beta()); | ||
| return num_alpha == num_beta; | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| StabilizedScfSettings::StabilizedScfSettings() | ||
| : qdk::chemistry::algorithms::ElectronicStructureSettings() { | ||
| set_default("scf_solver", data::AlgorithmRef("scf_solver", "qdk"), | ||
| "Nested SCF solver used for each SCF optimization."); | ||
| set_default("stability_checker", | ||
| data::AlgorithmRef("stability_checker", "qdk"), | ||
| "Nested stability checker used after each SCF optimization."); | ||
| set_default( | ||
| "max_stability_iterations", static_cast<int64_t>(5), | ||
| "Maximum number of stability-check/rerun cycles.", | ||
| data::BoundConstraint<int64_t>{0, std::numeric_limits<int64_t>::max()}); | ||
| set_default("check_internal", true, | ||
| "Check internal orbital-rotation stability."); | ||
| set_default("check_external", true, | ||
| "Check external orbital-rotation stability when applicable."); | ||
| set_default("fail_on_unstable", true, | ||
| "Throw if the final wavefunction remains unstable after the " | ||
| "configured stability cycles."); | ||
| set_default("external_instability_action", std::string("unrestricted"), | ||
| "Action for external instabilities: 'unrestricted' switches to " | ||
| "unrestricted SCF, 'rotate_only' only rotates orbitals.", | ||
| data::ListConstraint<std::string>{ | ||
| {std::vector<std::string>{"unrestricted", "rotate_only"}}}); | ||
| } | ||
|
|
||
| StabilizedScfSolver::StabilizedScfSolver() { | ||
| _settings = std::make_unique<StabilizedScfSettings>(); | ||
| } | ||
|
|
||
| std::pair<double, std::shared_ptr<data::Wavefunction>> | ||
| StabilizedScfSolver::_run_impl(std::shared_ptr<data::Structure> structure, | ||
| int charge, int spin_multiplicity, | ||
| BasisOrGuessType basis_or_guess) const { | ||
| const int64_t max_stability_iterations = | ||
| _settings->get<int64_t>("max_stability_iterations"); | ||
| const bool check_internal = _settings->get<bool>("check_internal"); | ||
| const bool check_external_setting = _settings->get<bool>("check_external"); | ||
| const bool fail_on_unstable = _settings->get<bool>("fail_on_unstable"); | ||
| const auto external_instability_action = | ||
| _settings->get<std::string>("external_instability_action"); | ||
|
|
||
| auto create_scf_solver = [&](const std::string& scf_type_override = "") { | ||
| auto solver = _create_nested<ScfSolverFactory>("scf_solver"); | ||
| detail::copy_common_scf_settings(*_settings, solver->settings()); | ||
| if (!scf_type_override.empty()) { | ||
| solver->settings().set("scf_type", scf_type_override); | ||
| } | ||
| return solver; | ||
| }; | ||
|
|
||
| auto create_stability_checker = [&](bool check_external) { | ||
| auto checker = _create_nested<StabilityCheckerFactory>("stability_checker"); | ||
| if (checker->settings().has("internal")) { | ||
| checker->settings().set("internal", check_internal); | ||
| } | ||
| if (checker->settings().has("external")) { | ||
| checker->settings().set("external", check_external); | ||
| } | ||
| if (checker->settings().has("method") && _settings->has("method")) { | ||
| checker->settings().set("method", _settings->get<std::string>("method")); | ||
| } | ||
| return checker; | ||
| }; | ||
|
|
||
| std::string scf_type_override; | ||
| auto scf_solver = create_scf_solver(); | ||
| auto [energy, wavefunction] = | ||
| scf_solver->run(structure, charge, spin_multiplicity, basis_or_guess); | ||
| bool is_stable = true; | ||
|
|
||
| for (int64_t iteration = 0; iteration < max_stability_iterations; | ||
| ++iteration) { | ||
| const bool check_external = | ||
| check_external_setting && | ||
| detail::can_check_external_stability(wavefunction); | ||
| auto stability_checker = create_stability_checker(check_external); | ||
|
|
||
| std::shared_ptr<data::StabilityResult> result; | ||
| std::tie(is_stable, result) = stability_checker->run(wavefunction); | ||
| if (is_stable) { | ||
| return {energy, wavefunction}; | ||
| } | ||
|
|
||
| bool do_external = false; | ||
| Eigen::VectorXd rotation_vector; | ||
| if (!result->is_external_stable() && result->has_external_result()) { | ||
| rotation_vector = | ||
| result->get_smallest_external_eigenvalue_and_vector().second; | ||
| do_external = true; | ||
|
nabbelbabbel marked this conversation as resolved.
|
||
| } else if (!result->is_internal_stable()) { | ||
| rotation_vector = | ||
| result->get_smallest_internal_eigenvalue_and_vector().second; | ||
| } else { | ||
| throw std::runtime_error( | ||
| "Stability checker reported an unstable wavefunction without an " | ||
| "internal or external instability result"); | ||
| } | ||
|
|
||
| auto [num_alpha, num_beta] = wavefunction->get_total_num_electrons(); | ||
| auto rotated_orbitals = qdk::chemistry::utils::rotate_orbitals( | ||
| wavefunction->get_orbitals(), rotation_vector, num_alpha, num_beta, | ||
| do_external); | ||
|
|
||
| if (do_external && external_instability_action == "unrestricted") { | ||
| scf_type_override = "unrestricted"; | ||
| } | ||
|
|
||
| scf_solver = create_scf_solver(scf_type_override); | ||
| std::tie(energy, wavefunction) = | ||
| scf_solver->run(structure, charge, spin_multiplicity, rotated_orbitals); | ||
| } | ||
|
|
||
| if (max_stability_iterations > 0) { | ||
| auto stability_checker = create_stability_checker( | ||
| check_external_setting && | ||
| detail::can_check_external_stability(wavefunction)); | ||
| std::tie(is_stable, std::ignore) = stability_checker->run(wavefunction); | ||
| } | ||
|
|
||
| if (!is_stable && fail_on_unstable) { | ||
| throw std::runtime_error( | ||
| "Stabilized SCF did not find a stable wavefunction within " + | ||
| std::to_string(max_stability_iterations) + " stability cycles"); | ||
| } | ||
|
|
||
| return {energy, wavefunction}; | ||
| } | ||
|
|
||
| } // namespace qdk::chemistry::algorithms::microsoft | ||
36 changes: 36 additions & 0 deletions
36
cpp/src/qdk/chemistry/algorithms/microsoft/stabilized_scf.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE.txt in the project root for | ||
| // license information. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <qdk/chemistry/algorithms/scf.hpp> | ||
| #include <qdk/chemistry/data/settings.hpp> | ||
|
|
||
| namespace qdk::chemistry::algorithms::microsoft { | ||
|
|
||
| class StabilizedScfSettings | ||
| : public qdk::chemistry::algorithms::ElectronicStructureSettings { | ||
| public: | ||
| StabilizedScfSettings(); | ||
| }; | ||
|
|
||
| class StabilizedScfSolver : public qdk::chemistry::algorithms::ScfSolver { | ||
| public: | ||
| StabilizedScfSolver(); | ||
|
|
||
| ~StabilizedScfSolver() = default; | ||
|
|
||
| std::string name() const final { return "qdk_stabilized"; } | ||
|
|
||
| std::vector<std::string> aliases() const final { | ||
| return {"qdk_stabilized", "stabilized", "stabilized_scf"}; | ||
| } | ||
|
Comment on lines
+26
to
+28
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a thing we're supporting now?!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you mean aliases? Then yes, I think we always have. May just not have used it often |
||
|
|
||
| protected: | ||
| std::pair<double, std::shared_ptr<data::Wavefunction>> _run_impl( | ||
| std::shared_ptr<data::Structure> structure, int charge, | ||
| int spin_multiplicity, BasisOrGuessType basis_or_guess) const override; | ||
| }; | ||
|
|
||
| } // namespace qdk::chemistry::algorithms::microsoft | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.