← Quantum Store · Guides · Lessons · Glossary · About · Türkçe
Qiskit course: real quantum programming · Lesson 4 / 6 · 10 min
The statevector and expectation values
See the state before measurement with Statevector, write observables with SparsePauliOp and compute their expectation values with the Estimator.
Statevector: the state before measurement
The Sampler only gives measurement results. In simulation, however, we can also see every amplitude before measurement. Statevector computes the circuit ideally and returns all 2ⁿ amplitudes. Do not add measurements: a measurement collapses the state to a random outcome, so Statevector needs a circuit without them.
On a real quantum computer the statevector cannot be seen directly; only measurement results can. Each extra qubit also doubles the memory needed, so an ordinary computer gives up at around 30 qubits. Statevector is ideal for understanding and testing small circuits.
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1) # no measurement
sv = Statevector(qc)
print(sv) # 0.707 for 00 and 11, 0 for the others
print(sv.probabilities_dict()) # about {'00': 0.5, '11': 0.5}
Observables and expectation values
We can give measurement outcomes numerical values and average them. For the Z observable an outcome of 0 counts as +1 and an outcome of 1 as −1. If the qubit always gives 0 the expectation value is +1, if it always gives 1 it is −1, and if it is 50–50 it is 0.
On two qubits ZZ counts +1 when both bits agree and −1 when they differ. In the Bell state the bits always agree, so the expectation value of ZZ is +1. In Qiskit observables are written as Pauli strings such as SparsePauliOp("ZZ"), and these strings are also read from right to left: "ZI" applies Z to q1 and nothing (I) to q0.
Computing with the Estimator
StatevectorEstimator takes a circuit and a list of observables and computes the expectation value of each. For the Bell state ZZ gives +1, ZI gives 0 and XX again +1. In chemistry the energy of a molecule is written as a sum of many Pauli terms and computed with the Estimator; this is the basis of algorithms such as VQE.
from qiskit.primitives import StatevectorEstimator
from qiskit.quantum_info import SparsePauliOp
observables = [SparsePauliOp('ZZ'), SparsePauliOp('ZI'), SparsePauliOp('XX')]
estimator = StatevectorEstimator()
result = estimator.run([(qc, observables)]).result()
print(result[0].data.evs) # about [1. 0. 1.]
Try it yourself
In the Bell example 00 and 11 always appear together: the bits always agree, so the expectation value of ZZ is +1. In the coin flip example 0 and 1 are 50–50: the expectation value of Z is (+1 − 1) / 2 = 0.
Open the circuit in your browserThe circuit playground is in Turkish. Button guide: “1000 kez ölç” = measure 1000 times, “Durum vektörünü göster” = show the state vector, “Bu devrenin Qiskit kodu” = this circuit’s Qiskit code, “Geri al” = undo, “Temizle” = clear. Click a gate in the circuit to remove it.
Key points
- Statevector returns every amplitude of a circuit without measurements; this is only possible in simulation.
- For the Z observable an outcome of 0 counts as +1 and 1 as −1; the expectation value is their average.
- Pauli strings such as SparsePauliOp("ZZ") are also read from right to left: the rightmost letter belongs to q0.
- The Estimator computes expectation values; it underpins applications such as chemistry and VQE.
Test yourself
What is the expectation value of the ZZ observable in the Bell state?
- 0
- +1
- −1
Show the answer
+1. The Bell state only gives 00 and 11; the two bits always agree, so ZZ counts +1 in every measurement and the average is +1.
← Bit order: reading Qiskit results correctly · Rotation gates and parameterised circuits →