import numpy as np
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler

qc = QuantumCircuit(1)
qc.ry(2 * np.pi / 3, 0)  # ⟨Z⟩ = -0,5
qc.measure_all()

shots = 7_500  # (1 - 0,25) / 0,01² = 7 500
sampler = StatevectorSampler(seed=np.random.default_rng(42))
result = sampler.run([qc] * 200, shots=shots).result()
estimates = []
for r in result:
    counts = r.data.meas.get_counts()
    estimates.append((counts.get("0", 0) - counts.get("1", 0)) / shots)
print("200 estimations de ⟨Z⟩ avec", shots, "shots chacune")
print("moyenne    :", round(float(np.mean(estimates)), 4))
print("écart-type :", round(float(np.std(estimates)), 4))
