# H.sparse\_matrix()

**URL:** <https://discuss.pennylane.ai/t/h-sparse-matrix/4291>\
**Category:** PennyLane Help\
**Created:** [March 17, 2024, 7:30am UTC](https://discuss.pennylane.ai/t/h-sparse-matrix/4291 "2024-03-17T07:30:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeet\_sharma](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jeet_sharma/32/2420_2.png) [@jeet\_sharma](https://discuss.pennylane.ai/u/jeet_sharma)\
**Post date:** [March 17, 2024, 7:30am UTC](https://discuss.pennylane.ai/t/h-sparse-matrix/4291/1 "2024-03-17T07:30:21Z")

</div>

```auto
from pennylane import FermiC, FermiA
from pennylane import jordan_wigner
from pennylane import numpy as np

h1 = 0.01 * (FermiC(0) * FermiA(0) + FermiC(1) * FermiA(1))
h2 = -0.02 * (FermiC(0) * FermiA(1) + FermiC(1) * FermiA(0))
h = h1 + h2
print(h)

h = jordan_wigner(h)
val, vec = np.linalg.eigh(h.sparse_matrix().toarray())
print(f"eigenvalues:\n{val}")
print()
print(f"eigenvectors:\n{np.real(vec.T)}")

```

```auto
error: ValueError: Output dtype not compatible with inputs.

```

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [March 18, 2024, 4:38pm UTC](https://discuss.pennylane.ai/t/h-sparse-matrix/4291/2 "2024-03-18T16:38:55Z")

</div>

Hey @jeet_sharma,

This code works fine for me and outputs the following:

```auto
0.01 * a⁺(0) a(0)
+ 0.01 * a⁺(1) a(1)
+ -0.02 * a⁺(0) a(1)
+ -0.02 * a⁺(1) a(0)
eigenvalues:
[-0.01 0. 0.02 0.03]

eigenvectors:
[[-0. -0.70710678 -0.70710678 -0.]
 [1. 0. 0. 0.]
 [0. 0. 0. 1.]
 [0. -0.70710678 0.70710678 0.]]

```

Here is my `qml.about()`:

```auto
Name: PennyLane
Version: 0.35.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/isaac/.virtualenvs/pennylane-stable/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info: macOS-14.4-arm64-arm-64bit
Python version: 3.11.6
Numpy version: 1.26.4
Scipy version: 1.12.0
Installed devices:
- default.clifford (PennyLane-0.35.0)
- default.gaussian (PennyLane-0.35.0)
- default.mixed (PennyLane-0.35.0)
- default.qubit (PennyLane-0.35.0)
- default.qubit.autograd (PennyLane-0.35.0)
- default.qubit.jax (PennyLane-0.35.0)
- default.qubit.legacy (PennyLane-0.35.0)
- default.qubit.tf (PennyLane-0.35.0)
- default.qubit.torch (PennyLane-0.35.0)
- default.qutrit (PennyLane-0.35.0)
- null.qubit (PennyLane-0.35.0)
- lightning.qubit (PennyLane_Lightning-0.35.0)

```

Make sure you’re using the most up-to-date version of PennyLane! You can do so by doing `pip install --upgrade pennylane`. Hope this helps.
