# Hamiltonian decomposition

**URL:** https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127
**Category:** PennyLane Help
**Created:** [September 5, 2022, 10:50am UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127 "2022-09-05T10:50:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![sassan\_moradi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sassan_moradi/32/422_2.png) [@sassan\_moradi](https://discuss.pennylane.ai/u/sassan_moradi)
#### Post date: [September 5, 2022, 10:50am UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/1 "2022-09-05T10:50:03Z")

</div>

Consider i have a 4x4 matrix

```auto
H = array([[-2., 1., 0., 0.],
       [1., -2., 1., 0.],
       [0., 1., -2., 1.],
       [0., 0., 1., -2.]]

```

i can decomose this matrix to unitaries with PauliX, Identity, PauliY and PauliZ with `qml.utils.decompose_hamiltonian`. I want to decompose M with these matrices

```auto
 C = Matrix([[0, 1],[0, 0]])
 S = Matrix([[0, 0],[1, 0]])

```

where are non-unitary. These matrices are spin rising and spin lowering operators, too. How can i decompose M using Identity, C, and S with `qml.utils.decompose_hamiltonian`?

---

<div class="post-metadata">

### Author: ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)
#### Post date: [September 7, 2022, 9:00pm UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/2 "2022-09-07T21:00:32Z")

</div>

Hi @sassan_moradi, unfortunately `qml.utils.decompose_hamiltonian` doesn’t allow you to specify the operators for the decomposition. I don’t think we have any function that does what you need but if you feel that this is an important feature to have it would be great if you could add it as a feature request on [GitHub](https://github.com/PennyLaneAI/pennylane/issues/new/choose). If you want to explain your use case here or there it can also be useful to understand your need better.

---

<div class="post-metadata">

### Author: ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)
#### Post date: [September 7, 2022, 9:35pm UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/3 "2022-09-07T21:35:36Z")

</div>

Hi @sassan_moradi, looking deeper into your question @josh noticed that it isn’t even possible to decompose your 4x4 matrix into only the two 2x2 matrices you have. You would need 16 basis matrices to decompose into.

---

<div class="post-metadata">

### Author: ![sassan\_moradi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sassan_moradi/32/422_2.png) [@sassan\_moradi](https://discuss.pennylane.ai/u/sassan_moradi)
#### Post date: [September 8, 2022, 6:37am UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/4 "2022-09-08T06:37:09Z")

</div>

It is possible to decompose the tridiagonal matrices with Identity and sigma\_+ and sigma\_-. The number of terms reduce by 2log(N)+1. While with Identity, PauliX, PauliY, and PauliZ, the number of terms are N, if you do the decomposition. Here is the paper: [https://journals.aps.org/pra/abstract/10.1103/PhysRevA.104.022418](https://journals.aps.org/pra/abstract/10.1103/PhysRevA.104.022418)

---

<div class="post-metadata">

### Author: ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)
#### Post date: [September 9, 2022, 8:51pm UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/5 "2022-09-09T20:51:35Z")

</div>

Hi @sassan_moradi, thank you for this link. How important/urgent is this feature for you?

---

<div class="post-metadata">

### Author: ![sassan\_moradi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sassan_moradi/32/422_2.png) [@sassan\_moradi](https://discuss.pennylane.ai/u/sassan_moradi)
#### Post date: [September 10, 2022, 1:50pm UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/6 "2022-09-10T13:50:15Z")

</div>

It is very important. I think i need to change the source code for decompose\_hamiltonian. I think this line paulis = [qml.Identity, qml.PauliX, qml.PauliY, qml.PauliZ] must be changed.

---

<div class="post-metadata">

### Author: ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)
#### Post date: [September 13, 2022, 12:56am UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/7 "2022-09-13T00:56:29Z")

</div>

Hi @sassan_moradi,

Modifying the decompose\_hamiltonian function as you mention would probably cause other errors in different places. It’s probably better to create a new function that tells you whether or not this decomposition is possible (because it’s not possible for any random matrix) and then perform the decomposition if possible. You could try the following:

1. Determine whether you can decompose the matrix in terms of Identity, PauliX, PauliY.
2. If it’s not possible then show an error message and exit the function.
3. If it is possible then decompose it into Identity, PauliX, PauliY, and then further decompose these into C and S.

Is this a feature that you would like to work on and contribute to PennyLane?

---

<div class="post-metadata">

### Author: ![sassan\_moradi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sassan_moradi/32/422_2.png) [@sassan\_moradi](https://discuss.pennylane.ai/u/sassan_moradi)
#### Post date: [September 13, 2022, 7:54am UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/8 "2022-09-13T07:54:45Z")

</div>

Many thanks. I will follow what you mentioned above. First i decompose the matrix to PauliX and PauliY. Since S = (PauliX + 1j_PauliY)/2 and C = (PauliX - 1j_PauliY)/2, i can rewrite the decomposition based on Identity, S, and C.

---

<div class="post-metadata">

### Author: ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)
#### Post date: [September 14, 2022, 12:43am UTC](https://discuss.pennylane.ai/t/hamiltonian-decomposition/2127/9 "2022-09-14T00:43:58Z")

</div>

Awesome! 😃

Let me know if you have any further questions.
