diff --git a/examples/dmet.ipynb b/examples/dmet.ipynb index b4692a49e..bb10c0541 100755 --- a/examples/dmet.ipynb +++ b/examples/dmet.ipynb @@ -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", @@ -103,9 +103,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "scrolled": true - }, + "metadata": {}, "outputs": [], "source": [ "butane = \"\"\"\n", @@ -169,9 +167,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "scrolled": true - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -378,9 +374,7 @@ { "cell_type": "code", "execution_count": 8, - "metadata": { - "scrolled": true - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -502,9 +496,7 @@ { "cell_type": "code", "execution_count": 12, - "metadata": { - "scrolled": true - }, + "metadata": {}, "outputs": [ { "name": "stdout", diff --git a/examples/overview_endtoend.ipynb b/examples/overview_endtoend.ipynb index 5bfbf6ca1..faeecfccd 100644 --- a/examples/overview_endtoend.ipynb +++ b/examples/overview_endtoend.ipynb @@ -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", @@ -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": { @@ -1271,7 +1272,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.9.9" } }, "nbformat": 4, diff --git a/tangelo/problem_decomposition/dmet/__init__.py b/tangelo/problem_decomposition/dmet/__init__.py index 532746351..b3c32a5e5 100644 --- a/tangelo/problem_decomposition/dmet/__init__.py +++ b/tangelo/problem_decomposition/dmet/__init__.py @@ -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 diff --git a/tangelo/problem_decomposition/dmet/dmet_problem_decomposition.py b/tangelo/problem_decomposition/dmet/dmet_problem_decomposition.py index ffd07d93e..0d2d2f33a 100644 --- a/tangelo/problem_decomposition/dmet/dmet_problem_decomposition.py +++ b/tangelo/problem_decomposition/dmet/dmet_problem_decomposition.py @@ -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 @@ -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] @@ -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): diff --git a/tangelo/problem_decomposition/tests/dmet/test_dmet.py b/tangelo/problem_decomposition/tests/dmet/test_dmet.py index 24f23152f..7e8cc9ec3 100644 --- a/tangelo/problem_decomposition/tests/dmet/test_dmet.py +++ b/tangelo/problem_decomposition/tests/dmet/test_dmet.py @@ -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 @@ -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