Let us a consider a simple scenario to put what we've learnt about QAOA into practice.

Let be the cost Hamiltonian whose ground state we want to obtain and the mixer Hamiltonian that will help us in our task. Then, the QAOA circuit for a number of layers will be:

Complete the code below to implement this QAOA circuit. Note that in this case the initial state is because this is the ground state of

Hint. You might use the following qml.qaoa.cost_layer, qml.qaoa.mixer_layer and qml.layer methods in PennyLane.
n_wires = 1
dev = qml.device("default.qubit", wires=n_wires)
p=3
cost_h=qml.Hamiltonian([1], [qml.PauliZ(0)])
mixer_h=qml.Hamiltonian([1], [qml.PauliX(0)])
params=np.ones((p, 2),requires_grad=True) * 0.5

def qaoa_layer(params,cost_h):
"""Implement one QAOA layer alternating H_C and H_M.

Args:
params (np.array): An array with the trainable parameters of the QAOA ansatz.
cost_h (qml.Hamiltonian): The cost Hamiltonian
"""
##################
# YOUR CODE HERE #
##################
def qaoa_circuit(params,p,cost_h):
"""Implement the initial state and p layers of the QAOA ansatz.

Args:
params (np.array): An array with the trainable parameters of the QAOA ansatz.
p (int): Number of layers of the QAOA ansatz.
cost_h (qml.Hamiltonian): The cost Hamiltonian
"""
##################
# YOUR CODE HERE #
##################
@qml.qnode(dev)
def probability_circuit(params,p,cost_h):
"""QAOA circuit which returns the probabilities.
Args:
params (np.array): An array with the trainable parameters of the QAOA ansatz.
p (int): Number of layers of the QAOA ansatz.
cost_h (qml.Hamiltonian): The cost Hamiltonian
Returns:

or to submit your code

To interact with codercises, please switch to a larger screen size.

Learning Objectives:

  • Describe the QAOA algorithm.
  • Solve simple combinatorial problems using QAOA.