Counts
Seeds, reproducibility, and what a partial measurement returns.
py
"""Counts: seeds, reproducibility and partial measurement."""
from qiskit import QuantumCircuit
from menos.qiskit import MENOSBackend
backend = MENOSBackend()
qc = QuantumCircuit(3, 3)
qc.h(0)
qc.h(1)
qc.h(2)
qc.measure([0, 1, 2], [0, 1, 2])
# The same circuit, options and seed always give the same counts.
first = backend.run(qc, shots=500, seed_simulator=42).result().get_counts()
again = backend.run(qc, shots=500, seed_simulator=42).result().get_counts()
other = backend.run(qc, shots=500, seed_simulator=43).result().get_counts()
assert first == again
assert first != other
print("seed 42:", dict(sorted(first.items())))
print("seed 43:", dict(sorted(other.items())))
# Measuring a subset of the qubits: only the measured classical bits appear.
partial = QuantumCircuit(3, 1)
partial.x(2)
partial.measure(2, 0)
only = backend.run(partial, shots=100, seed_simulator=1).result().get_counts()
print("one measured qubit:", only)
assert only == {"1": 100}Output
text
seed 42: {'000': 64, '001': 58, '010': 65, '011': 58, '100': 68, '101': 64, '110': 68, '111': 55}
seed 43: {'000': 75, '001': 68, '010': 76, '011': 50, '100': 65, '101': 60, '110': 58, '111': 48}
one measured qubit: {'1': 100}The same seed always gives the same counts (the script asserts it), and a different seed gives a different sample of the same uniform distribution. With a one-bit classical register the keys are one character wide.
