← Back to all posts
December 22, 2024

Fixing Random Seeds in Qiskit for Consistent Sampling Results

This article demonstrates how to achieve consistent quantum circuit sampling results in Qiskit by fixing the random seed (seed_simulator). Using a set seed (e.g., RND_SEED = 1) ensures repeatable outcomes, while setting the seed to 0 leads to varying results due to the lack of seed fixation.

When performing sampling with IBM's quantum computing library Qiskit:

RND_SEED = 1

from qiskit.circuit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit_ibm_runtime import SamplerV2 as Sampler

# Generate a quantum circuit.
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
display(qc.draw())

# Measure the quantum circuit
sim = AerSimulator()
sampler = Sampler(mode=sim, options={
    "simulator": {"seed_simulator": RND_SEED}
})
result = sampler.run([qc], shots=1000).result()
print(result[0].data.meas.get_counts())
# example OUTPUT: {'11': 489, '00': 511}

By fixing the random seed for observation, the same results can be obtained for each run. For instance, when setting RUN_SEED = 1, the output is consistently {'00': 491, '11': 509}, meaning the quantum states '00' and '11' are observed 491 and 509 times, respectively.

However, if the seed is set to 0, as in RND_SEED = 0, the seed is no longer fixed, and the observation results vary with each execution of the run method.

Package Versions

  • Qiskit: 1.3.1
  • Qiskit Aer: 0.15.1
  • Qiskit IBM Runtime: 0.34.0