qiskit-documentation/docs/migration-guides/local-simulators.mdx

208 lines
10 KiB
Plaintext

---
title: Migrate to local simulators
description: Migrate from using the IBM Quantum cloud simulators to using Qiskit Runtime local testing mode.
---
# Migrate from cloud simulators to local simulators
In quantum computing, the choice between using simulators and quantum hardware is crucial for making progress in the field. While simulators are useful for testing and debugging, in this era of quantum utility, quantum development and industry advancement requires actual hardware. As part of the move to quantum utility, IBM Quantum™ cloud simulators were retired on 15 May 2024. This guide explains the retirement in more detail, and how to migrate from cloud-based simulators, such as `ibmq_qasm_simulator`, to local simulators.
## Why are the cloud simulators being retired?
The cloud simulators are being retired for several reasons:
<details>
<summary><a>Simulators have limitations</a></summary>
Simulators can be useful, but they are too limited to use for research or experimentation:
- Simulators are valuable for understanding small-scale QPUs (quantum processing units), but their usefulness maxes out at around 50 qubits, even with access to high-performance supercomputers. This ceiling comes from the exponential growth in computational resources required to simulate larger quantum computers (review [Massively parallel quantum computer simulator, eleven years later](https://arxiv.org/abs/1805.04708) for the full explanation). Exploring quantum computers of 100 qubits and more requires hardware.
- While some simulators offer noise models, it is a very hard problem to capture the entire dynamics of a real QPU. Quantum hardware offers the potential for researchers to confront the challenges inherent in quantum computers, such as noise, errors, and decoherence in a realistic testing environment.
</details>
<details>
<summary><a>Using quantum hardware builds unique skills</a></summary>
Interacting with quantum hardware grows skills and experience unattainable by only using simulators:
- Direct interaction with quantum hardware builds skills because you must implement or use error mitigation or suppression techniques, for reliable computation.
- Hands-on experience with quantum hardware develops a deeper understanding of quantum phenomena and how to tailor algorithms to the characteristics of quantum processors.
- Engaging with quantum hardware results in practical insights into the challenges and opportunities of quantum computing, enhancing developers' ability to drive innovation in the field.
</details>
<details>
<summary><a>Algorithms should be adapted for quantum hardware</a></summary>
Successful quantum algorithms must be adapted to exploit the capabilities of quantum hardware, optimizing performance and efficiency.
- Quantum hardware provides a more accurate representation of real-world QPUs than simulators.
- Fine-tuning algorithms for quantum hardware involves adjusting ansatz, circuit implementations, parameters, and configuration to maximize performance. This process is best achieved through direct experimentation with quantum hardware.
</details>
## When should simulators be used?
Quantum simulators should be used to help develop and test programs before fine-tuning them and sending them to quantum hardware. Local simulators can do this with good performance and efficiency. Clifford circuits can be simulated very efficiently, and results can be verified, which is a useful way to gain confidence in an experiment.
<Admonition type="note">
Local testing mode does not have built-in error suppression or mitigation. Instead, you must specify those options explicitly. See [Configure error mitigation for Qiskit Runtime](/guides/configure-error-mitigation) for details.
</Admonition>
## Migrate to local simulators
With `qiskit-ibm-runtime` 0.22.0 or later, you can use local testing mode to replace cloud simulators. Depending on your needs, there are several ways to use local testing mode. To begin, specify one of the fake backends in ``qiskit_ibm_runtime.fake_provider`` or specify a Qiskit Aer backend when instantiating a primitive or a session.
### Guidance for choosing a simulator
Use the following table to help choose a simulator.
| Simulator | Fake Backends | AerSimulator | Clifford Simulation |
|---------------------|-------------------------------------------------|---------------------------------------------------|----------------------------------------------------|
| **Purpose** | Mimics specific IBM&reg; QPUs by using snapshots | General-purpose, high-performance simulation | Efficient simulation for Clifford circuits |
| **Noise model** | Automatically applies noise model from QPU snapshots | Custom or based on real QPU calibration data | Ideal for noise-free simulations |
| **Circuit size** | Limited to the capabilities of the mimicked QPU | Can handle larger circuits | Suitable for very large circuits (hundreds of qubits) |
| **Results** | Moderate runtime for QPU-specific tests | Shorter runtime for a wide range of simulations | Extremely fast, suitable for stabilizer circuits |
| **Use case** | Testing transpiler and QPU-specific behavior | General development, custom noise models | Large stabilizer circuits, error correction |
<Admonition type="note">
For most users, `AerSimulator` is a good choice, due to its flexibility and performance. However, if your work targets a specific QPU, a fake backend might be a better choice.
</Admonition>
### Fake backends
The [fake backends](/api/qiskit-ibm-runtime/fake_provider) mimic the behaviors of IBM QPUs by using snapshots. The snapshots contain important information about the QPU, such as the coupling map, basis gates, and qubit properties, which are useful for testing the transpiler and performing noisy simulations of the QPU. The noise model from the snapshot is automatically applied during simulation.
Example:
```python
from qiskit.circuit.library import RealAmplitudes
from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
from qiskit_ibm_runtime import SamplerV2 as Sampler, QiskitRuntimeService
service = QiskitRuntimeService()
# Bell Circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
# Run the sampler job locally using FakeManilaV2
fake_manila = FakeManilaV2()
pm = generate_preset_pass_manager(backend=fake_manila, optimization_level=1)
isa_qc = pm.run(qc)
# You can use a fixed seed to get fixed results.
options = {"simulator": {"seed_simulator": 42}}
sampler = Sampler(mode=fake_manila, options=options)
result = sampler.run([isa_qc]).result()
```
### AerSimulator
You can use local testing mode with simulators from [Qiskit Aer](/guides/simulate-with-qiskit-aer), which provides higher-performance simulation that can handle larger circuits and [custom noise models](/guides/build-noise-models). It also supports Clifford simulation mode, which can efficiently simulate Clifford circuits with a large number of qubits.
Example with sessions, without noise:
```python
from qiskit_aer import AerSimulator
from qiskit.circuit.library import RealAmplitudes
from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import Session, SamplerV2 as Sampler, QiskitRuntimeService
service = QiskitRuntimeService()
# Bell Circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
# Run the sampler job locally using AerSimulator.
# Session syntax is supported but ignored because local mode doesn't support sessions.
aer_sim = AerSimulator()
pm = generate_preset_pass_manager(backend=aer_sim, optimization_level=1)
isa_qc = pm.run(qc)
with Session(backend=aer_sim) as session:
sampler = Sampler()
result = sampler.run([isa_qc]).result()
```
To simulate with noise, specify a QPU (quantum hardware) and submit it to Aer. Aer builds a noise model based on the calibration data from that QPU and instantiates an Aer backend with that model. If you prefer, you can [build a noise model](/guides/build-noise-models).
Example with noise:
```python
from qiskit_aer import AerSimulator
from qiskit.circuit.library import RealAmplitudes
from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler
service = QiskitRuntimeService()
# Bell Circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
# Specify a QPU to use for the noise model
real_backend = service.backend("ibm_brisbane")
aer = AerSimulator.from_backend(real_backend)
# Run the sampler job locally using AerSimulator.
pm = generate_preset_pass_manager(backend=aer, optimization_level=1)
isa_qc = pm.run(qc)
sampler = Sampler(mode=aer)
result = sampler.run([isa_qc]).result()
```
<span id="clifford-sim"></span>
### Clifford simulation
Because Clifford circuits can be simulated efficiently with verifiable results, Clifford simulation is a very useful tool. For an in-depth example, see [Efficient simulation of stabilizer circuits with Qiskit Aer primitives](/guides/simulate-stabilizer-circuits).
Example:
```python
import numpy as np
from qiskit.circuit.library import EfficientSU2
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler
service = QiskitRuntimeService()
n_qubits = 500 # <---- note this uses 500 qubits!
circuit = EfficientSU2(n_qubits)
circuit.measure_all()
rng = np.random.default_rng(1234)
params = rng.choice(
[0, np.pi / 2, np.pi, 3 * np.pi / 2],
size=circuit.num_parameters,
)
# Tell Aer to use the stabilizer (clifford) simulation method
aer_sim = AerSimulator(method="stabilizer")
pm = generate_preset_pass_manager(backend=aer_sim, optimization_level=1)
isa_circuit = pm.run(circuit)
sampler = Sampler(mode=aer_sim)
result = sampler.run([(isa_circuit, params)]).result()
```