5.3 Using QAOA with PennyLane
As we mentioned in Chapter 2, The Tools of the Trade in Quantum Computing, PennyLane is a quantum programming library focused mainly on quantum machine learning. As such, it doesn’t include as many tools for quantum optimization algorithms — such as QAOA — as Qiskit does. However, it does provide some interesting features such as automatic differentiation — that is, analytical computation of gradients — that may make it an appealing alternative to Qiskit in some circumstances.
Let’s begin by explaining how to declare and work with Hamiltonians in PennyLane. For that, we will use the Hamiltonian
class. It provides a constructor that accepts a list of coefficients and a list of products of Pauli matrices. For instance, if you want to define , you will pass
[2,-1,3.5]
as the first argument and [
PauliZ
(0)
@PauliZ
(1),
PauliZ
(0)
@PauliZ
(2),
PauliZ
(1)]
as the second one. As we know from Chapter 2, The Tools of the Trade in...