qml.qaoa.cost.max_weight_cycle¶
- max_weight_cycle(graph, constrained=True)[source]¶
Returns the QAOA cost Hamiltonian and the recommended mixer corresponding to the maximum-weighted cycle problem, for a given graph.
The maximum-weighted cycle problem is defined in the following way (see here for more details). The product of weights of a subset of edges in a graph is given by
P=∏(i,j)∈E[(cij−1)xij+1]where E are the edges of the graph, xij is a binary number that selects whether to include the edge (i,j) and cij is the corresponding edge weight. Our objective is to maximize P, subject to selecting the xij so that our subset of edges composes a cycle.
- Parameters
graph (nx.Graph or rx.PyGraph or rx.PyDiGraph) – the directed graph on which the Hamiltonians are defined
constrained (bool) – specifies the variant of QAOA that is performed (constrained or unconstrained)
- Returns
The cost and mixer Hamiltonians, as well as a dictionary mapping from wires to the graph’s edges
- Return type
(Hamiltonian, Hamiltonian, dict)
Usage Details
There are two variations of QAOA for this problem, constrained and unconstrained:
Constrained
Note
This method of constrained QAOA was introduced by Hadfield, Wang, Gorman, Rieffel, Venturelli, and Biswas in arXiv:1709.03489.
The maximum weighted cycle cost Hamiltonian for unconstrained QAOA is
HC=Hloss.Here, Hloss is a loss Hamiltonian:
Hloss=∑(i,j)∈EZijlogcijwhere E are the edges of the graph and Zij is a qubit Pauli-Z matrix acting upon the wire specified by the edge (i,j) (see
loss_hamiltonian()
for more details).The returned mixer Hamiltonian is
cycle_mixer()
given byHM=14∑(i,j)∈E(∑k∈V,k≠i,k≠j,(i,k)∈E,(k,j)∈E[XijXikXkj+YijYikXkj+YijXikYkj−XijYikYkj]).This mixer provides transitions between collections of cycles, i.e., any subset of edges in E such that all the graph’s nodes V have zero net flow (see the
net_flow_constraint()
function).Note
- Recommended initialization circuit:
Your circuit must prepare a state that corresponds to a cycle (or a superposition of cycles). Follow the example code below to see how this is done.
Unconstrained
The maximum weighted cycle cost Hamiltonian for constrained QAOA is defined as:
HC =Hloss+3Hnetflow+3Houtflow.The netflow constraint Hamiltonian
net_flow_constraint()
is given byHnetflow=∑i∈V((douti−dini)I−∑j,(i,j)∈EZij+∑j,(j,i)∈EZji)2,where douti and dini are the outdegree and indegree, respectively, of node i. It is minimized whenever a subset of edges in E results in zero net flow from each node in V.
The outflow constraint Hamiltonian
out_flow_constraint()
is given byHoutflow=∑i∈V(douti(douti−2)I−2(douti−1)∑j,(i,j)∈EˆZij+(∑j,(i,j)∈EˆZij)2).It is minimized whenever a subset of edges in E results in an outflow of at most one from each node in V.
The returned mixer Hamiltonian is
x_mixer()
applied to all wires.Note
- Recommended initialization circuit:
Even superposition over all basis states.
Example
First set up a simple graph:
import pennylane as qml import numpy as np import networkx as nx a = np.random.random((4, 4)) np.fill_diagonal(a, 0) g = nx.DiGraph(a)
The cost and mixer Hamiltonian as well as the mapping from wires to edges can be loaded using:
>>> cost, mixer, mapping = qml.qaoa.max_weight_cycle(g, constrained=True)
Since we are using
constrained=True
, we must ensure that the input state to the QAOA algorithm corresponds to a cycle. Consider the mapping:>>> mapping {0: (0, 1), 1: (0, 2), 2: (0, 3), 3: (1, 0), 4: (1, 2), 5: (1, 3), 6: (2, 0), 7: (2, 1), 8: (2, 3), 9: (3, 0), 10: (3, 1), 11: (3, 2)}
A simple cycle is given by the edges
(0, 1)
and(1, 0)
and corresponding wires0
and3
. Hence, the state |100100000000⟩ corresponds to a cycle and can be prepared usingBasisState
or simplePauliX
rotations on the0
and3
wires.