qp.qaoa.layers.cost_layer¶
- cost_layer(gamma, hamiltonian)[source]¶
Applies the QAOA cost layer corresponding to a cost Hamiltonian.
For the cost Hamiltonian \(H_C\), this is defined as the following unitary:
\[U_C \ = \ e^{-i \gamma H_C}\]where \(\gamma\) is a variational parameter.
- Parameters:
gamma (int or float) – The variational parameter passed into the cost layer
hamiltonian (.Hamiltonian) – The cost Hamiltonian
- Raises:
ValueError – if the terms of the supplied cost Hamiltonian are not exclusively products of diagonal Pauli gates
Usage Details
We first define a cost Hamiltonian:
from pennylane import qaoa import pennylane as qp cost_h = qp.Hamiltonian([1, 1], [qp.Z(0), qp.Z(0) @ qp.Z(1)])
We can then pass it into
qaoa.cost_layer, within a quantum circuit:dev = qp.device('default.qubit', wires=2) @qp.qnode(dev) def circuit(gamma): for i in range(2): qp.Hadamard(wires=i) qaoa.cost_layer(gamma, cost_h) return [qp.expval(qp.Z(i)) for i in range(2)]
which gives us a circuit of the form:
>>> print(qp.draw(circuit)(0.5)) 0: ──H─╭ApproxTimeEvolution(1.00,1.00,0.50)─┤ <Z> 1: ──H─╰ApproxTimeEvolution(1.00,1.00,0.50)─┤ <Z> >>> print(qp.draw(circuit, level="device")(0.5)) 0: ──H──RZ(1.00)─╭RZZ(1.00)─┤ <Z> 1: ──H───────────╰RZZ(1.00)─┤ <Z>
code/api/pennylane.qaoa.layers.cost_layer
Download Python script
Download Notebook
View on GitHub