# Qiskit Operators into Pennylane

**URL:** <https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577>\
**Category:** PennyLane Help\
**Created:** [December 23, 2021, 8:41am UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577 "2021-12-23T08:41:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbarison](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sbarison](https://discuss.pennylane.ai/u/sbarison)\
**Post date:** [December 23, 2021, 8:41am UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577/1 "2021-12-23T08:41:27Z")

</div>

Good morning,

I have a Qiskit Hamiltonian (created as a PauliSumOp) and I would like to use it also in Pennylane without recreating it from scratch, is it possible?  
The idea is to use this Hamiltonian in a VQE.

Thank you very much

---

<div class="post-metadata">

**Author:** ![maliasadi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/maliasadi/32/3939_2.png) [@maliasadi](https://discuss.pennylane.ai/u/maliasadi)\
**Post date:** [December 23, 2021, 9:34pm UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577/2 "2021-12-23T21:34:27Z")

</div>

Hi @sbarison , thank you for your question!

You might extract the terms and construct the PL Hamiltonian with them. If you send us a code example of your Qiskit Hamiltonian, we can help you with constructing the PL one from it.

Also, this is worth looking at the following PL methods that helps you to extract the terms and build what you want:

- [https://pennylane.readthedocs.io/en/stable/code/api/pennylane.grouping.string\_to\_pauli\_word.html](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.grouping.string_to_pauli_word.html)

- [https://pennylane.readthedocs.io/en/stable/code/api/pennylane.Hamiltonian.html](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.Hamiltonian.html)

Looking forward to hearing more from you.

---

<div class="post-metadata">

**Author:** ![sbarison](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sbarison](https://discuss.pennylane.ai/u/sbarison)\
**Post date:** [December 27, 2021, 8:13am UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577/3 "2021-12-27T08:13:07Z")

</div>

Dear @maliasadi,  
it’s not really important the specific Hamiltonian, given a generic PauliSumOp I was wondering if there was a method to translate it into a Pennylane observable, as the function `from_qiskit` for the circuits.

The first of the two links seems what I was looking for.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![maliasadi](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/maliasadi/32/3939_2.png) [@maliasadi](https://discuss.pennylane.ai/u/maliasadi)\
**Post date:** [December 30, 2021, 6:39pm UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577/4 "2021-12-30T18:39:42Z")

</div>

Hi @sbarison, I am glad you find the link helpful. Also, in Pennylane, there is not any method to construct a PL H from a Qiskit H right now; and to the best of my knowledge, the best way around is by extracting the terms and using them to construct a PL H.

---

<div class="post-metadata">

**Author:** ![Pranav\_Chandarana](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/pranav_chandarana/32/885_2.png) [@Pranav\_Chandarana](https://discuss.pennylane.ai/u/Pranav_Chandarana)\
**Post date:** [January 18, 2022, 7:26am UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577/5 "2022-01-18T07:26:17Z")

</div>

Hello!  
Could you please show an example of how to convert it?  
Actually, I was trying to convert the following Qiskit Hamiltonian into Pennylane,  
`3.75 * III- 3.75 * ZII+ 3.75 * IZZ- 3.75 * ZZZ`

Thanks for the help!  
P.S: If you can help on how to convert a generic Qiskit Hamiltonian into Pennylane Hamiltonian, It would be great!

---

<div class="post-metadata">

**Author:** ![sbarison](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sbarison](https://discuss.pennylane.ai/u/sbarison)\
**Post date:** [January 18, 2022, 7:57am UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577/6 "2022-01-18T07:57:19Z")

</div>

Sure!  
I defined a simple function that takes a PauliSumOp as an input and gives a Pennylane Hamiltonian as output

```auto
import pennylane as qml

def hamiltonian_from_qiskit(sum_op):

	'''
	This function takes a qiskit.PauliSumOp as input and
	gives back a Pennylane Hamiltonian as output

	sum_op = qiskit.PauliSumOp

	'''
	op = sum_op.primitive.to_list()
	n_terms = len(op)
	coeffs = []
	paulis = []

	for i in range(n_terms):
		paulis.append(qml.grouping.string_to_pauli_word(op[i][0]))
		coeffs.append(op[i][1].real)

	p_op = qml.Hamiltonian(coeffs,paulis)

	return p_op

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Pranav\_Chandarana](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/pranav_chandarana/32/885_2.png) [@Pranav\_Chandarana](https://discuss.pennylane.ai/u/Pranav_Chandarana)\
**Post date:** [January 18, 2022, 8:18am UTC](https://discuss.pennylane.ai/t/qiskit-operators-into-pennylane/1577/7 "2022-01-18T08:18:38Z")

</div>

Hi! @sbarison

Thanks for the help! It works perfectly!
