Repository: PennyLaneAI/pennylane
Pull request #3748 (2023-02-09 22:28:10)

Add openshell error to qchem
**Context:**
The PR modifies the Molecule class to raise an error for unsupported open-shell systems. 

**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**


github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [`doc/releases/changelog-dev.md`](/PennyLaneAI/pennylane/blob/master/doc/releases/changelog-dev.md) with:
* A one-to-two sentence description of the change. You may include a small working example for new features.
* A link back to this PR.
* Your name (or GitHub username) in the contributors section.
codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3748?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> Merging [#3748](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3748?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (10d246c) into [master](https://codecov.io/gh/PennyLaneAI/pennylane/commit/0f48aa6f5ac34a624e5de2b8b09d47a9a486cadd?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (0f48aa6) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master    #3748   +/-   ##
=======================================
  Coverage   99.82%   99.82%           
=======================================
  Files         329      329           
  Lines       28856    28858    +2     
=======================================
+ Hits        28805    28807    +2     
  Misses         51       51           
```


| [Impacted Files](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3748?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Coverage Δ | |
|---|---|---|
| [pennylane/qchem/molecule.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3748?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL3FjaGVtL21vbGVjdWxlLnB5) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)


Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index b2cfcf99f..a9e24b1c5 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -558,6 +558,10 @@
   gates.
   [(#3743)](https://github.com/PennyLaneAI/pennylane/pull/3743)
 
+ * The `qchem.Molecule` class raises an error when the molecule has an odd number of electrons or 
+   when the spin multiplicity is not 1.
+   [(#3748)](https://github.com/PennyLaneAI/pennylane/pull/3748)
+
 <h3>Breaking changes</h3>
 
 * The argument `mode` in execution is replaced by the boolean `grad_on_execution` in the new execution pipeline.
diff --git a/pennylane/qchem/molecule.py b/pennylane/qchem/molecule.py
index af6f10de3..3005e539e 100644
--- a/pennylane/qchem/molecule.py
+++ b/pennylane/qchem/molecule.py
@@ -97,6 +97,16 @@ class Molecule:
 
         self.n_basis, self.basis_data = mol_basis_data(self.basis_name, self.symbols, load_data)
 
+        self.nuclear_charges = [atomic_numbers[s] for s in self.symbols]
+
+        self.n_electrons = sum(np.array(self.nuclear_charges)) - self.charge
+
+        if self.n_electrons % 2 == 1 or self.mult != 1:
+            raise ValueError(
+                "Openshell systems are not supported. Change the charge or spin"
+                " multiplicity of the molecule."
+            )
+
         if l is None:
             l = [i[0] for i in self.basis_data]
 
@@ -127,10 +137,6 @@ class Molecule:
         ]
         self.n_orbitals = len(self.l)
 
-        self.nuclear_charges = [atomic_numbers[s] for s in self.symbols]
-
-        self.n_electrons = sum(np.array(self.nuclear_charges)) - self.charge
-
         self.mo_coefficients = None
 
     def __repr__(self):


Test patch:
diff --git a/tests/qchem/test_molecule.py b/tests/qchem/test_molecule.py
index a2b16426b..353a2b147 100644
--- a/tests/qchem/test_molecule.py
+++ b/tests/qchem/test_molecule.py
@@ -46,6 +46,17 @@ class TestMolecule:
         with pytest.raises(ValueError, match="Currently, the only supported basis sets"):
             qchem.Molecule(symbols, geometry, basis_name="6-3_1_g")
 
+    @pytest.mark.parametrize(
+        ("symbols", "geometry", "charge", "mult"),
+        [
+            (["H", "He"], np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]), 0, 2),
+        ],
+    )
+    def test_openshell_error(self, symbols, geometry, charge, mult):
+        r"""Test that an error is raised if the molecule has unpaired electrons."""
+        with pytest.raises(ValueError, match="Openshell systems are not supported"):
+            qchem.Molecule(symbols, geometry, charge=charge, mult=mult)
+
     @pytest.mark.parametrize(
         ("symbols", "geometry"),
         [
@@ -280,9 +291,9 @@ class TestMolecule:
             ["H", "C", "O"],
             np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 2.0]]),
         )
-        mol = qchem.Molecule(symbols, geometry, 0)
+        mol = qchem.Molecule(symbols, geometry, 1)
         assert (
-            repr(mol) == "<Molecule = CHO, Charge: 0, Basis: STO-3G, Orbitals: 11, Electrons: 15>"
+            repr(mol) == "<Molecule = CHO, Charge: 1, Basis: STO-3G, Orbitals: 11, Electrons: 14>"
         )
 
         symbols, geometry = (["C", "O"], np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]))