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
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
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.Hamiltonian.html
Looking forward to hearing more from you.
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!
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.
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!
Sure!
I defined a simple function that takes a PauliSumOp as an input and gives a Pennylane Hamiltonian as output
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!
Hi! @sbarison
Thanks for the help! It works perfectly!