Cost Functions - IBM Quantum Learning
Cost Functions - IBM Quantum Learning
Cost functions
During this lesson, we'll learn how to evaluate a cost function:
Primitives
If you're wondering how to measure a quantum system and what you can
measure, Qiskit offers two primitives that can help:
pk = ∣⟨k∣ψ⟩∣2
∀k ∈ Zn2 ≡ {0, 1, ⋯ , 2n − 1},
The Qiskit Runtime Sampler runs the circuit multiple times on a quantum
device, performing measurements on each run, and reconstructing the
probability distribution from the recovered bitstrings. The more runs (or
shots) it performs, the more accurate the results will be, but this requires
more time and quantum resources.
However, since the number of possible outputs grows exponentially with the
number of qubits n (i.e. 2n ), the number of shots will need to grow
exponentially as well in order to capture a dense probability distribution.
Therefore, Sampler is only efficient for sparse probability distributions;
where the target state ∣ψ⟩ must be expressible as a linear combination of
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 2/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
the computational basis states, with the number of terms growing at most
polynomially with the number of qubits:
Poly(n)
∣ψ⟩ = ∑ wk ∣k⟩.
^ ⟩ψ := ∑ pλ λ = ⟨ψ∣H
⟨H
^ ∣ψ⟩
∀k ∈ Zn4 ≡ {0, 1, ⋯ , 4n − 1},
such that
4n −1
^ = ∑ wk P^k
H
k=0
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 3/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
diagonalize the Pauli observable in the computational basis and measure it.
We can easily measure Pauli observables because we know Vk ahead of
multiplies by wk , and adds all the results together to obtain the expected
4n −1 2n −1
^ ⟩ψ = ∑ wk ∑ pkj λkj ,
⟨H
k=0 j=0
The reader may notice the implicit assumption that probability sampling
also needs to be efficient as explained for Sampler , which means
Poly(n) Poly(n)
^ ⟩ψ = ∑ wk ∑ pkj λkj .
⟨H
k j
1
Let's assume the single-qubit state ∣+⟩ := H∣0⟩ = (∣0⟩ + ∣1⟩), and
2
observable
^ = (−1 2)
H
2 1
= 2X − Z
^ ⟩+
with the following theoretical expectation value ⟨H
^ ∣+⟩ = 2.
= ⟨+∣H
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 4/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Let see how to compute ⟨X⟩+ and ⟨Z⟩+ directly. Since X and Z do not
commute (i.e. don't share the same eigenbasis), they cannot be measured
simultaneously, therefore we need the auxiliary circuits:
Output:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 5/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Output:
Output:
We can now carry out the computation manually using Sampler and check
the results on Estimator :
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 6/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Output:
Sampler results:
>> Expected value of X: 1.00000
>> Expected value of Z: -0.00300
>> Total expected value: 2.00300
Estimator results:
>> Expected value of X: 1.00000
>> Expected value of Z: 0.00000
>> Total expected value: 2.00000
^ , ∣ψ⟩
Expressing ∣ψ⟩ with respect to the basis of eigenstates of H =
∑λ aλ ∣λ⟩, it follows:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 8/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
λ′ λ
= ∑ ∑ a∗λ′ aλ ⟨λ′ ∣H
^ ∣λ⟩
λ λ′
λ λ′
∑ ∑ a∗λ′ aλ λ
= ⋅ δλ,λ′
λ λ′
= ∑ ∣aλ ∣2 λ
= ∑ pλ λ
^ ∣ψ ⟩ = ⟨ψ∣V † ΛV ∣ψ⟩
⟨ψ ∣ H
2n −1 2n −1
= ⟨ψ∣V † ( ∑ ∣j⟩⟨j∣)Λ( ∑ ∣k⟩⟨k∣)V ∣ψ⟩
j=0 k=0
n n
2 −1 2 −1
= ∑ ∑ ⟨ψ∣V † ∣j⟩⟨j∣Λ∣k⟩⟨k∣V ∣ψ⟩
j=0 k=0
2n −1
= ∑ ⟨ψ∣V † ∣j⟩⟨j∣Λ∣j⟩⟨j∣V ∣ψ⟩
j=0
2n −1
= ∑ ∣⟨j∣V ∣ψ⟩∣2 λj
j=0
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 9/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
2n −1
^ ∣ψ⟩ = ∑ pj λj .
⟨ψ∣H
j=0
It is very important to note that the probabilities are taken from the state
V ∣ψ⟩ instead of ∣ψ⟩. This is why the matrix V is absolutely necessary.
You might be wondering how to obtain the matrix V and the eigenvalues Λ.
If you already had the eigenvalues, then there would be no need to use a
quantum computer since the goal of variational algorithms is to find these
^.
eigenvalues of H
Operator σ V
1 0
I σ0 = ( ) V0 = I
0 1
0 1 1
X σ1 = ( ) V1 = H = 1
(
1 0 2 1 −
0 −i 1 1 1 0
Y σ2 = ( ) V2 = HS † = 1
( )⋅(
i 0 2 1 −1 0 −i
1 0
Z σ3 = ( ) V3 = I
0 −1
3 3 4n −1
^ = ∑ ... ∑ wkn−1 ...k0 σkn−1 ⊗ ... ⊗ σk0 = ∑ wk P^k ,
H
kn−1 =0 k0 = 0
k=0
n−1
where k = ∑l=0 4l kl ≡ kn−1 ...k0 for kn−1 , ..., k0 ∈ {0, 1, 2, 3} (i.e.
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 10/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
n
4 −1 2n −1
^ ∣ψ⟩ = ∑ wk ∑ ∣⟨j∣Vk ∣ψ⟩∣2 ⟨j∣Λk ∣j⟩
⟨ψ∣H
k=0 j=0
n
4 −1 2n −1
= ∑ wk ∑ pkj λkj ,
k=0 j=0
where Vk
:= Vkn−1
⊗ ... ⊗ Vk0 and Λk := Λkn−1 ⊗ ... ⊗ Λk0 , such that:
P^k =
†
V k Λk V k .
Cost functions
In general, cost functions are used to describe the goal of a problem and
how well a trial state is performing with respect to that goal. This definition
can be applied to various examples in chemistry, machine learning, finance,
optimization, and so on.
Let's consider a simple example of finding the ground state of a system. Our
objective is to minimize the expectation value of the observable
^ ):
representing energy (Hamiltonian H
^ ∣ψ(θ)⟩
min⟨ψ(θ)∣H
We can use the Estimator to evaluate the expectation value and pass this
value to an optimizer to minimize. If the optimization is successful, it will
return a set of optimal parameter values θ ∗ , from which we will be able to
construct the proposed solution state ∣ψ(θ ∗ )⟩ and compute the observed
expectation value as C(θ ∗ ).
Notice how we will only be able to minimize the cost function for the limited
set of states that we are considering. This leads us to two separate
possibilities:
Our ansatz does not define the solution state across the search
space: If this is the case, our optimizer will never find the solution,
and we need to experiment with other ansatzes that might be able to
represent our search space more accurately.
Output:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 12/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
We will first carry this out using a simulator: the StatevectorEstimator. This
is usually advisable for debugging, but we will immediately follow the
debugging run with a calculation on real quantum hardware. Increasingly,
problems of interest are no longer classically simulable without state-of-
the-art supercomputing facilities.
1 estimator = StatevectorEstimator()
2 cost = cost_func_vqe(theta_list, ansatz, observable, esti
3 print(cost)
Output:
[-0.58744589]
We will now proceed with running on a real quantum computer. Note the
syntax changes. The steps involving the pass_manager will be discussed
further in the next example. One step of particular importance in variational
algorithms is the use of a Qiskit Runtime session. Starting a session allows
you to run multiple iterations of a variational algorithm without waiting in a
new queue each time parameters are updated. This is important if queue
times are long and/or many iterations are needed. Only partners in the IBM
Quantum Network can use Runtime sessions. If you do not have access to
sessions, you can reduce the number of iterations you submit at a given
time, and save the most recent parameters for use in future runs. If you
submit too many iterations or encounter queue times that are too long, you
may encounter error code 1217, which refers to long delays between job
submissions.
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 13/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Note that the values obtained from the two calculations above are very
similar. Techniques for improving results will be discussed further below.
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 14/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
1 import rustworkx as rx
2 from rustworkx.visualization import mpl_draw
3
4 n = 4
5 G = rx.PyGraph()
6 G.add_nodes_from(range(n))
7 # The edge syntax is (start, end, weight)
8 edges = [(0, 1, 1.0), (0, 2, 1.0), (0, 3, 1.0), (1, 2, 1
9 G.add_edges_from(edges)
10
11 mpl_draw(
12 G, pos=rx.shell_layout(G), with_labels=True, edge_la
13 )
Output:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 15/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
value 1 if node i is one of the groups that we'll label 1 and 0 if it's in the
other group, that we'll label as 0. We will also denote as wij (element (i, j)
of the adjacency matrix w ) the weight of the edge that goes from node i to
node j . Because the graph is undirected, wij = wji . Then we can formulate
i,j=0
n n
= ∑ wij xi − ∑ wij xi xj
i,j=0 i,j=0
n n i
= ∑ wij xi − ∑ ∑ 2wij xi xj
To solve this problem with a quantum computer, we are going to express the
cost function as the expected value of an observable. However, the
observables that Qiskit admits natively consist of Pauli operators, that have
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 16/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
comfortably access the weights of all the edge. This will be used to obtain
our cost function:
1 − zi
zi = 1 − 2xi → xi =
2
xi = 0 → z i = 1
xi = 1 → zi = −1.
2 2
i,j=0
n n
wij wij
=∑ −∑
zi zj
4 4
i,j=0 i,j=0
n i n i
wij wij
= ∑∑ − ∑∑
zi zj
2 2
2 2
Now that we have a cost function to minimize whose variables can have the
values −1 and 1, we can make the following analogy with the Pauli Z :
n−1 i 0
zi ≡ Zi = I ⊗ ... ⊗ Z ⊗ ... ⊗ I
i. Moreover:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 17/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
n i
^ = ∑ ∑ wij Zi Zj
H
2
i=0 j=0
n i
wij
offset = − ∑ ∑
2
i=0 j=0
Output:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 18/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Output:
Offset: -2.5
1 import numpy as np
2
3 x0 = 2 * np.pi * np.random.rand(ansatz.num_parameters)
4
5 estimator = StatevectorEstimator()
6 cost = cost_func_vqe(x0, ansatz, hamiltonian, estimator)
7 print(cost)
Output:
1.473098768180865
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 19/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Output:
1.1120776913677988
We can use Qiskit Runtime Primitive's error suppression and error mitigation
options to address noise and maximize the utility of today's quantum
computers.
Error Suppression
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 21/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Primitives allow for the use of error suppression techniques by setting the
optimization_level option and selecting advanced transpilation options.
In a later course, we will delve into different circuit construction methods to
improve results, but for most cases, we recommend setting
optimization_level=3 .
Output:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 22/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
The circuit above can yield sinusoidal expectation values of the observable
given, provided we insert phases spanning an appropriate interval, such as
[0, 2π].
1 ## Setup phases
2 import numpy as np
3
4 phases = np.linspace(0, 2 * np.pi, 50)
5
6 # phases need to be expressed as a list of lists in order
7 individual_phases = [[phase] for phase in phases]
We can now use a pass manager to transpile the circuit into the "instruction
set architecture" or ISA of the backend. This is a new requirement in Qiskit
Runtime: all circuits submitted to a backend must conform to the
constraints of the backend’s target, meaning they must be written in terms
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 23/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
of the backend's ISA — i.e., the set of instructions the device can understand
and execute. These target constraints are defined by factors like the device’s
native basis gates, its qubit connectivity, and - when relevant - its pulse and
other instruction timing specifications.
Note that in the present case, we will do this twice: once with
optimization_level = 0, and once with it set to 3. Each time we will use the
Estimator primitive to estimate the expectation values of the observable at
different values of phase.
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 24/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Finally, we can plot the results, and we see that the precision of the
calculation was fairly good even without optimization, but it definitely
improved by increasing optimization to level 3. Note that in deeper, more
complicated circuits, the difference between optimization levels of 0 and 3
are likely to be more significant. This is a very simple circuit used as a toy
model.
Output:
Error Mitigation
Error mitigation refers to techniques that allow users to reduce circuit errors
by modeling the device noise at the time of execution. Typically, this results
in quantum pre-processing overhead related to model training and classical
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 25/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
For this course, we will explore these error mitigation models at a high level
to illustrate the error mitigation that Qiskit Runtime primitives can perform
without requiring full implementation details.
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 26/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Overall workflow:
1. Acquire data for the zero state with randomized bit flips (Pauli X before
measurement)
2. Acquire data for the desired (noisy) state with randomized bit flips
(Pauli X before measurement)
3. Compute the special function for each data set, and divide.
Zero noise extrapolation (ZNE) works by first amplifying the noise in the
circuit that is preparing the desired quantum state, obtaining measurements
for several different levels of noise, and using those measurements to infer
the noiseless result.
Overall workflow:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 27/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Each method comes with its own associated overhead: a trade-off between
the number of quantum computations needed (time) and the accuracy of
our results:
Bias 0 O(λNnoise-factors )
We perform this part on real hardware, since error mitigation is not available
on simulators.
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 28/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
As before, we can plot the resulting expectation values as a function of
phase angle for the three levels of error mitigation used. With great
difficulty, one can see that error mitigation improves the results slightly.
Again, this effect is much more pronounced in deeper, more complicated
circuits.
Output:
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 30/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Summary
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 31/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
Our cost function runs during every iteration of the optimization loop. The
next lesson will explore how the classical optimizer uses our cost function
evaluation to select new parameters.
1 import qiskit
2 import qiskit_ibm_runtime
3
4 print(qiskit.version.get_version_info())
5 print(qiskit_ibm_runtime.version.get_version_info())
Output:
1.1.0
0.23.0
Yes No
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 32/33
7/24/25, 10:56 AM Cost functions | IBM Quantum Learning
© IBM Corp., 2017-2025
https://quantum.cloud.ibm.com/learning/en/courses/variational-algorithm-design/cost-functions 33/33