← Quantum Store · Guides · Lessons · Glossary · Packages · About · Türkçe

IBM Quantum and the Qiskit community

Noisy quantum simulation with Qiskit Aer

SoftwareSimulationLicence: Apache-2.0Version: 0.17.29.0 KB

Official source: Qiskit/qiskit-aer

Download ZIP (free)

Real quantum computers are noisy. A tested example that models gate and measurement errors with Qiskit Aer and compares the result with the ideal Bell circuit.

What is Qiskit Aer?

Qiskit Aer is the high-performance simulator in IBM's Qiskit ecosystem. As well as running ideal circuits quickly, it lets you add noise models that imitate the errors of real devices.

Why noise?

On today's quantum computers every gate and every measurement goes wrong with a small probability. Trying your algorithm in a noisy simulation before sending it to real hardware shows you in advance how much the results will suffer.

The example in this package

gurultulu_bell.py runs the two-qubit Bell circuit twice:

Next steps

  1. Lower the CNOT error rate from 0.05 to 0.01; see how the wrong results decrease.
  2. Add 10 more CNOTs to the circuit (apply each twice, so logically nothing changes); watch how the noise builds up with the gate count.
  3. Download the measured noise model of a real IBM device with qiskit-ibm-runtime and try the same circuit with it.

Installation and running

pip install qiskit qiskit-aer
python gurultulu_bell.py

Tested with: qiskit 2.5.2, qiskit-aer 0.17.2, Python 3.12.

Official source

Links

Package contents

Licence

The example code and the guide in this package were written by the Quantum Store editors and are MIT licensed. The project it uses is subject to its own licence (Apache-2.0).


Quantum Store — https://hakanerdogan.com/en

Example code

The example shipped in the package, run and checked. Comments and printed messages are in Turkish; the code is the same in any language. Everything is in the ZIP.

gurultulu_bell.py

"""Qiskit Aer ile gürültülü simülasyon: ideal ve gürültülü Bell durumunu karşılaştır.

Çalıştırma:
    pip install qiskit qiskit-aer
    python gurultulu_bell.py
"""

from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit_aer.noise import NoiseModel, ReadoutError, depolarizing_error

SHOTS = 4000

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

# Basit bir gürültü modeli: tek kübitlik kapılarda %1, CNOT'ta %5 depolarizasyon,
# her kübitin ölçümünde %3 okuma hatası. Gerçek cihazların değerleri bu mertebededir.
gurultu = NoiseModel()
gurultu.add_all_qubit_quantum_error(depolarizing_error(0.01, 1), ["h", "x", "sx", "rz"])
gurultu.add_all_qubit_quantum_error(depolarizing_error(0.05, 2), ["cx"])
gurultu.add_all_qubit_readout_error(ReadoutError([[0.97, 0.03], [0.03, 0.97]]))


def calistir(sim: AerSimulator) -> dict[str, int]:
    sonuc = sim.run(transpile(qc, sim), shots=SHOTS, seed_simulator=11).result()
    return dict(sorted(sonuc.get_counts().items()))


def hata_orani(sayim: dict[str, int]) -> float:
    # Bell durumunda 01 ve 10 asla çıkmamalı; çıkan her biri gürültünün izidir.
    return (sayim.get("01", 0) + sayim.get("10", 0)) / SHOTS


ideal = calistir(AerSimulator())
gurultulu = calistir(AerSimulator(noise_model=gurultu))

print(qc.draw(output="text"))
print(f"İdeal      : {ideal}  hatalı sonuç oranı {hata_orani(ideal):.1%}")
print(f"Gürültülü  : {gurultulu}  hatalı sonuç oranı {hata_orani(gurultulu):.1%}")

assert hata_orani(ideal) == 0, ideal
assert 0.03 < hata_orani(gurultulu) < 0.2, gurultulu
print("Tamam: gürültü, ideal devrede hiç görülmeyen 01 ve 10 sonuçlarını üretti.")

Related concepts

← All packages

Share: X · LinkedIn