Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions demonstrations_v2/tutorial_qaoa_maxcut/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
version of NumPy.
"""

import pennylane as qml
import pennylane as qp
from pennylane import numpy as np

np.random.seed(42)
Expand All @@ -153,15 +153,15 @@
# unitary operator U_B with parameter beta
def U_B(beta):
for wire in range(n_wires):
qml.RX(2 * beta, wires=wire)
qp.RX(2 * beta, wires=wire)


# unitary operator U_C with parameter gamma
def U_C(gamma):
for edge in graph:
qml.CNOT(wires=edge)
qml.RZ(gamma, wires=edge[1])
qml.CNOT(wires=edge)
qp.CNOT(wires=edge)
qp.RZ(gamma, wires=edge[1])
qp.CNOT(wires=edge)
# Could also do
# IsingZZ(gamma, wires=edge)

Expand All @@ -180,7 +180,7 @@ def bitstring_to_int(bit_string_sample):
# ~~~~~~~
# Next, we create a quantum device with 4 qubits.

dev = qml.device("lightning.qubit", wires=n_wires)
dev = qp.device("lightning.qubit", wires=n_wires)

##############################################################################
# We also require a quantum node which will apply the operators according to the angle parameters,
Expand All @@ -194,23 +194,23 @@ def bitstring_to_int(bit_string_sample):
# by setting ``return_samples=True``.


@qml.set_shots(20)
@qml.qnode(dev)
@qp.set_shots(20)
@qp.qnode(dev)
def circuit(gammas, betas, return_samples=False):
# apply Hadamards to get the n qubit |+> state
for wire in range(n_wires):
qml.Hadamard(wires=wire)
qp.Hadamard(wires=wire)
# p instances of unitary operators
for gamma, beta in zip(gammas, betas):
U_C(gamma)
U_B(beta)

if return_samples:
# sample bitstrings to obtain cuts
return qml.sample()
return qp.sample()
# during the optimization phase we are evaluating the objective using expval
C = qml.sum(*(qml.Z(w1) @ qml.Z(w2) for w1, w2 in graph))
return qml.expval(C)
C = qp.sum(*(qp.Z(w1) @ qp.Z(w2) for w1, w2 in graph))
return qp.expval(C)


def objective(params):
Expand All @@ -237,7 +237,7 @@ def qaoa_maxcut(n_layers=1):
init_params = 0.01 * np.random.rand(2, n_layers, requires_grad=True)

# initialize optimizer: Adagrad works well empirically
opt = qml.AdagradOptimizer(stepsize=0.5)
opt = qp.AdagradOptimizer(stepsize=0.5)

# optimize parameters in objective
params = init_params.copy()
Expand All @@ -248,7 +248,7 @@ def qaoa_maxcut(n_layers=1):
print(f"Objective after step {i+1:3d}: {-objective(params): .7f}")

# sample 100 bitstrings by setting return_samples=True and the QNode shot count to 100
bitstrings = qml.set_shots(circuit, shots=100)(*params, return_samples=True)
bitstrings = qp.set_shots(circuit, shots=100)(*params, return_samples=True)
# convert the samples bitstrings to integers
sampled_ints = [bitstring_to_int(string) for string in bitstrings]

Expand Down
4 changes: 2 additions & 2 deletions demonstrations_v2/tutorial_qaoa_maxcut/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"executable_stable": true,
"executable_latest": true,
"dateOfPublication": "2019-10-11T00:00:00+00:00",
"dateOfLastModification": "2025-10-15T00:00:00+00:00",
"dateOfLastModification": "2026-04-06T00:00:00+00:00",
"categories": [
"Optimization"
],
Expand All @@ -34,4 +34,4 @@
}
],
"discussionForumUrl": "https://discuss.pennylane.ai/t/qaoa-and-optimization/5188"
}
}
Loading