smartyfy.mrindigi.com

Quantum Computing Master Course — 144. Statevector Simulation in Qiskit

Lesson 144: Statevector Simulation in Qiskit

Smartyfy Quantum Computing Master Course

Objectives and prerequisites

Interpret an SDK statevector, cross-check complex amplitudes against direct calculation, and distinguish state equivalence from matching measurement probabilities. Prerequisites are Lessons 141–143, Pauli observables, and complex inner products. We use the Qiskit 2.5.2 official-documentation reference checked on 10 October 2026.

Execution status: The Qiskit snippet was documentation-checked but not executed because Qiskit was not installed. The separate standard-library calculation below was executed and verified its intended numerical results. Neither example invokes a hardware service or models device noise.

Build a phase-sensitive example

Apply H to q0, then S to q0, then CX with q0 as control and q1 as target, starting from |00⟩. State strings are ordered q1q0 and q0 is the least-significant array bit. The first gate gives (|00⟩ + |01⟩)/√2. The phase gate S = diag(1,i) changes this to (|00⟩ + i|01⟩)/√2. CNOT then gives (|00⟩ + i|11⟩)/√2.

The expected amplitude list is [1/√2, 0, 0, i/√2]. Its nonzero probabilities are one half at 00 and one half at 11. This example deliberately includes a relative imaginary phase, so a simulator that preserves only real amplitudes or computational probabilities will not pass a full state check.

Documentation-checked SDK calculation

from math import sqrt
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector, Pauli

circuit = QuantumCircuit(2)
circuit.h(0)
circuit.s(0)
circuit.cx(0, 1)
state = Statevector.from_instruction(circuit)
expected = Statevector([1/sqrt(2), 0, 0, 1j/sqrt(2)])
assert state.equiv(expected)
print('Exact model probabilities:', state.probabilities_dict())
print('ZZ expectation:', state.expectation_value(Pauli('ZZ')))
print('XY expectation:', state.expectation_value(Pauli('XY')))

Statevector.from_instruction starts from the all-zero input appropriate to the circuit and evolves the instructions. The example has no measurements, reset, or classical conditions. Instructions that do not fit the chosen pure-state evolution method need a different simulation treatment rather than being assumed to work automatically.

The data attribute represents amplitudes, whereas probabilities_dict returns computational-basis probabilities labelled by strings. The equiv method compares states up to global phase with numerical tolerances. These operations answer different questions: matching probability dictionaries is weaker than equivalent pure states.

Expected quantities for (|00⟩ + i|11⟩)/√2
Quantity Value Meaning
P(00), P(11) 1/2, 1/2 Computational probabilities
Expectation of ZZ 1 The two Z outcomes agree
Expectation of XY 1 A phase-sensitive correlation
Squared overlap with ordinary plus Bell state 1/2 Different physical pure states

Interpret Pauli labels consistently

For a two-qubit Pauli string, the left character acts on q1 and the right character on q0. Thus XY means X on q1 and Y on q0. This matches the q1q0 basis labels, while a circuit’s top wire is usually q0. Write the operator assignment explicitly when moving between a diagram, a string, and a numerical calculation.

Using Y|0⟩ = i|1⟩ and Y|1⟩ = −i|0⟩, the operator X⊗Y sends |00⟩ to i|11⟩ and |11⟩ to −i|00⟩. Applied to our state, the extra coefficient i on |11⟩ cancels the minus i from that action, returning the same state. Therefore the XY expectation is +1.

ZZ also returns the same state because each occupied basis label has even parity. But the ordinary plus Bell state has the same Z-basis probabilities and a different XY expectation. This is a concrete reason to test a phase-sensitive observable or the full amplitudes rather than relying only on a histogram.

Executed independent cross-check

Save Lesson 141’s first code block as local_statevector141.py beside this script before running it.

from math import sqrt
from local_statevector141 import apply_one, apply_cx, probabilities, H, X, Z
S = [[1,0],[0,1j]]
Y = [[0,-1j],[1j,0]]
state = apply_cx(apply_one(apply_one([1,0,0,0],H,0),S,0),0,1)
expected = [1/sqrt(2),0,0,1j/sqrt(2)]
assert all(abs(a-b)<1e-12 for a,b in zip(state,expected))

def inner(a,b):
    return sum(x.conjugate()*y for x,y in zip(a,b))
zz_state = apply_one(apply_one(state,Z,0),Z,1)
xy_state = apply_one(apply_one(state,Y,0),X,1)
assert abs(inner(state,zz_state)-1)<1e-12
assert abs(inner(state,xy_state)-1)<1e-12
bell_plus = [1/sqrt(2),0,0,1/sqrt(2)]
assert abs(abs(inner(state,bell_plus))**2-.5)<1e-12
print('Independent amplitudes:', state)
print('Independent probabilities:', [round(p,12) for p in probabilities(state)])
print('ZZ and XY expectations:', round(inner(state,zz_state).real,12),
      round(inner(state,xy_state).real,12))

The executed output matches the expected complex amplitudes and probabilities, and both declared expectations equal one within floating-point tolerance. The squared overlap with the ordinary plus Bell state is one half. This verifies the mathematical target independently; it does not turn the unexecuted SDK snippet into an SDK runtime test.

Global phase and normalization

Multiplying every amplitude by exp(iγ) changes no pure-state measurement prediction. An equivalent-state check should accept that common phase. Changing only the |11⟩ coefficient is a relative-phase change and generally changes interference. Comparing absolute values of amplitudes would incorrectly treat both changes as harmless.

Normalization remains essential. A statevector with the right amplitude ratios but twice the norm is not a valid normalized pure state until an explicit normalization step is applied. A verification routine should inspect normalization and the intended construction instead of silently correcting every unexpected result.

When a circuit is used as a controlled subroutine, a phase that was global for the standalone operation can become relative between the control branches. That is a separate operator-level situation. The phase-insensitive comparison here is appropriate for the specified final state preparation, not a license to drop phases from every controlled construction.

Know what the simulator can reveal

An ideal statevector gives direct access to every amplitude in the classical model. A physical experiment does not reveal that full list from one run; reconstructing a state requires many suitably chosen measurements and assumptions. The simulator’s internal access is a debugging resource, not a new physical measurement capability.

Statevector storage grows exponentially with qubit count. Small circuits are excellent for validating algebra, ordering, and expected observables, while larger simulations may require different representations or restrictions. Noise that creates mixed states cannot generally be represented by one deterministic pure-state vector without an explicitly stated trajectory or other model.

For reproducibility, keep the unmeasured circuit, exact expected vector, ordering convention, SDK reference and actual installed version if run, tolerance, and observed output. If future compilation changes the layout, account for that permutation before comparing vectors in different coordinate conventions.

Four exercises with worked solutions

  1. What are the nonzero amplitude indices after H(0), S(0), CX(0,1)?

    Solution. Indices zero and three, representing 00 and 11. Their amplitudes are 1/√2 and i/√2 respectively.

  2. Calculate squared overlap with (|00⟩ + |11⟩)/√2.

    Solution. The inner product is (1+i)/2 or its conjugate depending on argument order. Its squared magnitude is (1²+1²)/4 = 1/2.

  3. Does multiplying the entire state by −i change the XY expectation?

    Solution. No. The bra contributes the conjugate phase and the ket contributes the phase, so they cancel in every expectation value.

  4. Why are exact statevector probabilities not a hardware histogram?

    Solution. They are calculated from modeled amplitudes without finite-shot sampling or device noise. A hardware histogram is a sample from an implemented measurement process.

Five explained multiple-choice questions

  1. The amplitude of |11⟩ in this example is: A. 1/2. B. 1/√2. C. i/√2. D. i/2.

    Answer: C. S contributes a relative i before CNOT moves that amplitude to index three.

  2. Matching computational probabilities alone implies: A. Equal density matrices in every basis. B. Agreement for that measurement basis, with phase differences still possible. C. Equal relative phases. D. Equal expectation of every Pauli operator.

    Answer: B. Probabilities in one basis do not determine all state coherences.

  3. The label XY means: A. X on q1 and Y on q0. B. X on q0 and Y on q1. C. X followed by Y on one qubit. D. A classical XOR of two measurement strings.

    Answer: A. Pauli-string ordering follows the displayed q1q0 tensor convention.

  4. The independent calculation establishes: A. The exact installed Qiskit package ran successfully. B. A physical Bell-state preparation was performed. C. The device’s readout distribution has no error. D. The intended mathematical amplitudes and observables passed a separate local check.

    Answer: D. Mathematical verification and SDK execution are explicitly different evidence here.

  5. A statevector’s full amplitude list is available because: A. The classical simulation stores a mathematical representation of the whole pure state. B. One physical measurement reveals all amplitudes. C. Relative phase has become a classical probability. D. Sampling has removed the exponential storage requirement.

    Answer: A. Internal simulator access is not a measurement capability of the physical system.

Pitfalls and summary

Cross-check complex amplitudes, operator ordering, and phase-sensitive expectations. Keep ideal probabilities separate from samples, and distinguish executed mathematics from documentation-checked SDK code and from physical experiments.

Primary sources and further study

These sources support the definitions and context. The numerical examples, exercises, and explanations here were prepared for this lesson. Research papers are optional advanced reading.