Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
86 changes: 86 additions & 0 deletions demonstrations_v2/tutorial_qaoa_maxcut/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,89 @@ def qaoa_maxcut(n_layers=1):
plt.tight_layout()
plt.show()

##############################################################################
Comment thread
soranjh marked this conversation as resolved.
# Resource Estimation
# -------------------
# In this section we use the :mod:`estimator <pennylane.estimator>` module to estimate the resources for QAOA.
# We begin by defining the unitary operators :math:`U_B` and :math:`U_C`. For the purpose of
# resource estimation, we don't need to know the concrete value of the input parameters
# (:math:`\beta`, :math:`\gamma`); instead we need to specify the ``precision``, which informs how
# accurately the single qubit rotation operators would get further compiled to hardware native gates.

import pennylane.estimator as qre # the estimator module

def U_B_res(n_wires, wires=None, epsilon=1e-5):
qre.Prod([(qre.RX(precision=epsilon), n_wires)], wires)

def U_C_res(n_edges, wires=None, epsilon=1e-5):
qre.Prod(
[
(qre.CNOT(), 2 * n_edges),
(qre.RZ(precision=epsilon), n_edges),
],
wires,
)

def circuit_res(n_wires, n_edges, n_layers, wires=None, epsilon=None):
qre.Prod([(qre.Hadamard(), n_wires)], wires)
U_B_res(n_layers * n_wires, wires, epsilon)
U_C_res(n_layers * n_edges, wires, epsilon)


##############################################################################
# The :class:`~.pennylane.estimator.ops.op_math.symbolic.Prod` class represents a product of
# operators, we can leverage it to account for the number of times the gate is applied.
# We can now estimate the resources required for a single execution (shot) of the QAOA circuit.

# Set base parameters:
wires = [0, 1, 2, 3]
n_wires = len(wires)
n_edges = len(graph)

# Set hyper parameters:
n_layers = 2
epsilon = 1e-6

cost_per_circuit = qre.estimate(circuit_res)(
n_wires, n_edges, n_layers, wires, epsilon,
)

print(cost_per_circuit)

##############################################################################
# Awesome! We were able to estimate the cost for a single circuit execution. Now we need to take
# sampling into account. Recall that we sample each circuit 20 times per optimization loop. We
# also repeated the optimization loop 30 times to ensure we converged on the optimal parameters.
# This means we sampled the circuit 600 times! We can easily scale up our resource estimates
# with one line of code.

total_shots = 20 * 30
total_cost = cost_per_circuit.multiply_series(total_shots)
print(total_cost)

##############################################################################
# Constructing the circuit this way makes it tailored for resource estimation, allowing us to scale
# up our workflows without worrying about that exponential performance bottlenecks that are common
Comment thread
Jaybsoni marked this conversation as resolved.
Outdated
# with circuit simulation. Here we push the capabilities for a large scale example:

# Set base parameters:
n_wires = 500 # 500 qubits
n_edges = 10_000 # 10,000 graph edges
wires = list(range(n_wires))

# Set hyper parameters:
n_layers = 100 # 100 layers in the model
epsilon = 1e-12 # 10^-12 accuracy threshold

cost_per_circuit = qre.estimate(circuit_res)(
n_wires, n_edges, n_layers, wires, epsilon,
)

total_shots = 100 * 500 # 100 shots/loop * 500 loops
total_cost = cost_per_circuit.multiply_series(total_shots)

print(total_cost)
Comment thread
soranjh marked this conversation as resolved.

##############################################################################
# PennyLane's resource estimation functionality makes it easy to cost out large scale workflows
# with *hundreds* of qubits and *trillions* of gates!
8 changes: 6 additions & 2 deletions demonstrations_v2/tutorial_qaoa_maxcut/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
"authors": [
{
"username": "alowe"
},
{
"username": "Jay"
}
],
Comment thread
AntonNI8 marked this conversation as resolved.
"executable_stable": true,
"executable_latest": true,
"dateOfPublication": "2019-10-11T00:00:00+00:00",
"dateOfLastModification": "2026-04-17T00:00:00+00:00",
"dateOfLastModification": "2026-04-24T00:00:00+00:00",
"categories": [
"Optimization"
"Optimization",
"Resource Estimation"
],
"tags": [],
"previewImages": [
Expand Down
Loading