# Unable to construct Hamiltonian

**URL:** <https://discuss.pennylane.ai/t/unable-to-construct-hamiltonian/1564>\
**Category:** PennyLane Help\
**Created:** [December 17, 2021, 4:05pm UTC](https://discuss.pennylane.ai/t/unable-to-construct-hamiltonian/1564 "2021-12-17T16:05:58Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![sjahangiri](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sjahangiri/32/1443_2.png) [@sjahangiri](https://discuss.pennylane.ai/u/sjahangiri)\
**Post date:** [January 14, 2022, 10:43pm UTC](https://discuss.pennylane.ai/t/unable-to-construct-hamiltonian/1564/13 "2022-01-14T22:43:08Z")

</div>

Hi @raghavv. The following code creates the molecular Hamiltonian for water step by step. Could you please run the code for both water and _PSPCz_ and let me know at what stage the code breaks? Please modify the molecular info given between the dashed lines for the new molecule. Thanks!

```
import pennylane as qml
import openfermion
from pennylane import numpy as np
from openfermionpyscf import run_pyscf

#------------------------
symbols = ["O", "H", "H"]
x = np.array([0.0000000000, 0.0000000000, 0.1501668459,
              0.0000000000, 0.7580826553, -0.4856334229,
              0.0000000000, -0.7580826553, -0.4856334229])
mult = 1
charge = 0
basis = 'sto-3g'
core = [0, 1, 2, 3]
active = [4, 5]
filename = './water'
#------------------------

geometry = [[s, tuple(x[3 * i : 3 * i + 3])] for i, s in enumerate(symbols)]

molecule = openfermion.MolecularData(geometry, basis, mult, charge, filename=filename)

run_pyscf(molecule, run_scf=1, verbose=0)

molecule_ = openfermion.MolecularData(filename=filename)

ht = molecule_.get_molecular_hamiltonian(occupied_indices=core, active_indices=active)

hf = openfermion.transforms.get_fermion_operator(ht) # fermion hamiltonian

hq = openfermion.transforms.jordan_wigner(hf) # qubit hamiltonian

hp = qml.qchem.convert_observable(hq) # PennyLane hamiltonian

print(hp)

```

The output for water should be

```
  (-74.19370628334303) [I0]
+ (-0.15460393042639708) [Z2]
+ (-0.15460393042639708) [Z3]
+ (0.13038589407848444) [Z0]
+ (0.13038589407848444) [Z1]
+ (0.138284189609364) [Z0 Z2]
+ (0.138284189609364) [Z1 Z3]
+ (0.14767808197773874) [Z0 Z3]
+ (0.14767808197773874) [Z1 Z2]
+ (0.14966950988795605) [Z2 Z3]
+ (0.22003977334376112) [Z0 Z1]
+ (-0.00939389236837476) [Y0 Y1 X2 X3]
+ (-0.00939389236837476) [X0 X1 Y2 Y3]
+ (0.00939389236837476) [Y0 X1 X2 Y3]
+ (0.00939389236837476) [X0 Y1 Y2 X3]
```

---

_[View the full topic](https://discuss.pennylane.ai/t/unable-to-construct-hamiltonian/1564)._
