Skip to content

Commit

Permalink
Recomputing MF when working with atom indices in DMET (#130)
Browse files Browse the repository at this point in the history
* Recomputing and testing MF when working with atom indexes. Change Localization import level.
  • Loading branch information
alexfleury-sb authored Feb 24, 2022
1 parent b5e453b commit 5b4b8d0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
20 changes: 6 additions & 14 deletions examples/dmet.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@
"from tangelo import SecondQuantizedMolecule\n",
"\n",
"# The minimal import for DMET.\n",
"from tangelo.problem_decomposition.dmet.dmet_problem_decomposition import DMETProblemDecomposition\n",
"from tangelo.problem_decomposition import DMETProblemDecomposition\n",
"# Ability to change localization method.\n",
"from tangelo.problem_decomposition.dmet.dmet_problem_decomposition import Localization\n",
"from tangelo.problem_decomposition.dmet import Localization\n",
"# Use for VQE ressources estimation vs DMET.\n",
"from tangelo.algorithms import VQESolver\n",
"# Use for comparison.\n",
Expand All @@ -103,9 +103,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [],
"source": [
"butane = \"\"\"\n",
Expand Down Expand Up @@ -169,9 +167,7 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -378,9 +374,7 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -502,9 +496,7 @@
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down
9 changes: 5 additions & 4 deletions examples/overview_endtoend.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@
}
],
"source": [
"from tangelo.problem_decomposition.dmet.dmet_problem_decomposition import DMETProblemDecomposition, Localization\n",
"from tangelo.problem_decomposition import DMETProblemDecomposition\n",
"from tangelo.problem_decomposition.dmet import Localization\n",
"\n",
"# DMET-VQE, 5 fragments of size 2 atoms each\n",
"dmet_options = {\"molecule\": mol, \"verbose\": False,\n",
Expand Down Expand Up @@ -1257,9 +1258,9 @@
"hash": "89fcd33a61ce1c8ae91bdb4eff5eb93c6c13a9486366ee1fefd22c672d14276a"
},
"kernelspec": {
"display_name": "tangelo_oct21",
"display_name": "qsdk",
"language": "python",
"name": "tangelo_oct21"
"name": "qsdk"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -1271,7 +1272,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.9.9"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions tangelo/problem_decomposition/dmet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .dmet_problem_decomposition import Localization
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from enum import Enum
from functools import reduce
import numpy as np
from pyscf import gto
from pyscf import gto, scf
import scipy
import warnings

Expand Down Expand Up @@ -108,7 +108,7 @@ def __init__(self, opt_dict):
if max(fragment_atoms_flatten) >= self.molecule.natm:
raise RuntimeError("An atom id is higher than the number of atom (indices start at 0).")
elif len(fragment_atoms_flatten) != len(set(fragment_atoms_flatten)):
raise RuntimeError("Atom indexes must only appear once.")
raise RuntimeError("Atom indices must only appear once.")

# Converting fragment_atoms to an expected list of number of atoms (not atom ids).
new_fragment_atoms = [len(frag) for frag in self.fragment_atoms]
Expand All @@ -130,8 +130,10 @@ def __init__(self, opt_dict):
self.fragment_atoms = new_fragment_atoms

# Force recomputing the mean field if the atom ordering has been changed.
self.mean_field = None
warnings.warn("The mean field will be recomputed even if one has been provided by the user.", RuntimeWarning)
self.mean_field = scf.RHF(self.molecule)
self.mean_field.verbose = 0
self.mean_field.scf()

# Check if the number of fragment sites is equal to the number of atoms in the molecule
if self.molecule.natm != sum(self.fragment_atoms):
Expand Down
16 changes: 15 additions & 1 deletion tangelo/problem_decomposition/tests/dmet/test_dmet.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import numpy as np

from tangelo.molecule_library import mol_H4_doublecation_minao, mol_H4_doublecation_321g, mol_H10_321g, mol_H10_minao
from tangelo.problem_decomposition.dmet.dmet_problem_decomposition import Localization, DMETProblemDecomposition
from tangelo.problem_decomposition import DMETProblemDecomposition
from tangelo.problem_decomposition.dmet import Localization
from tangelo.algorithms.variational import VQESolver
from tangelo.toolboxes.molecular_computation.rdms import matricize_2rdm

Expand Down Expand Up @@ -159,6 +160,19 @@ def test_fragment_ids(self):

self.assertEqual(solver.fragment_atoms, [1, 1, 1, 1])

def test_build_with_atom_indices(self):
"""Tests if a mean field is recomputed when providing atom indices."""

opt_dmet = {"molecule": mol_H4_doublecation_321g,
"fragment_atoms": [[0], [1], [2], [3]],
"fragment_solvers": "ccsd",
"electron_localization": Localization.iao,
"verbose": False
}

solver = DMETProblemDecomposition(opt_dmet)
solver.build()

def test_fragment_ids_exceptions(self):
"""Tests exceptions if a bad nested list of atom ids is provided. Two
cases: if an atom id is higher than the number of atoms and if an id is
Expand Down

0 comments on commit 5b4b8d0

Please sign in to comment.