Qiskit Aer

repository·main·Indexed 20 days ago

https://github.com/qiskit/qiskit-aer

A high-performance quantum circuit simulator for Qiskit that supports realistic noise models and hardware-like execution via primitives. It provides SamplerV2 and EstimatorV2 for obtaining quasi-probability distributions and expectation values. Aer extends Qiskit's QuantumCircuit with simulation-specific methods for setting states (e.g., set_density_matrix) and saving data (e.g., save_statevector). Supports GPU acceleration via CUDA on Linux x86_64 and MPI for parallelizing statevector, density matrix, and unitary simulations.

Tokens
13.3K
Snippets
38
Records
51
Agent score
71%

What's inside qiskit-aer

  1. Overview of Qiskit Aer

    main
    Qiskit Aer is a high-performance quantum computing simulator designed to run quantum circuits with or without realistic noise models. It offers multiple simulation methods and supports performance optimizations such as MPI (Message Passing Interface) and GPU acceleration to handle complex simulations efficiently.
  2. Locate and understand Aer benchmark types

    main

    Aer benchmarks are located in the test/benchmark directory. They are defined in *_benchmarks.py files. The current suite includes:

    • Quantum Volume: Benchmarks testing different numbers of qubits and various noise models.
    • Simple one-gate circuits: Benchmarks testing simple gate operations (e.g., U3, CX) across different qubit counts and noise models.
  3. Access Aer-specific QuantumCircuit methods

    main

    Aer extends the standard qiskit.circuit.QuantumCircuit class with simulation-specific methods for setting states and saving data. Crucially, these methods are only available after you have imported qiskit_aer. If you attempt to call them before importing the Aer package, they will not be found on the QuantumCircuit object.

    import qiskit
    import qiskit_aer  # Aer must be imported to patch QuantumCircuit
    
    qc = qiskit.QuantumCircuit(2)
    # Now Aer-specific methods like qc.save_statevector() are available
  4. Customize job-level parallel execution with executor and max_job_size

    main

    By default, Qiskit Aer runs simulation jobs on a single-worker Python multiprocessing ThreadPool executor, relying on low-level OpenMP and CUDA for parallelization. To customize how multiple circuits are executed in parallel at the job level, you can provide a custom multiprocessing executor and control how circuits are grouped using the executor and max_job_size backend options.

    • executor: A multiprocessing executor (e.g., concurrent.futures.ThreadPoolExecutor) used to submit chunks of circuits. Defaults to None.
    • max_job_size: Controls the number of circuits in each chunk.
      • If max_job_size=1, every circuit is treated as an individual job. For 60 circuits, Aer submits 60 jobs.
      • If max_job_size=2, circuits are grouped into pairs. For 60 circuits, Aer submits 30 jobs, each containing 2 circuits.
    import qiskit
    from concurrent.futures import ThreadPoolExecutor
    from qiskit_aer import AerSimulator
    from math import pi
    
    # Generate circuits
    circ = qiskit.QuantumCircuit(15, 15)
    circ.h(0)
    circ.cx(0, 1)
    circ.cx(1, 2)
    circ.p(pi/2, 2)
    circ.measure([0, 1, 2], [0, 1 ,2])
    
    circ2 = qiskit.QuantumCircuit(15, 15)
    circ2.h(0)
    circ2.cx(0, 1)
    circ2.cx(1, 2)
    circ2.p(pi/2, 2)
    circ2.measure([0, 1, 2], [0, 1 ,2])
    circ_list = [circ, circ2]
    
    qbackend = AerSimulator()
    
    # Set executor and max_job_size
    exc = ThreadPoolExecutor(max_workers=2)
    qbackend.set_options(executor=exc)
    qbackend.set_options(max_job_size=1)
    result = qbackend.run(circ_list).result()
  5. Install Qiskit Aer from source

    main

    Installing from source allows you to use the latest development version. You should first follow the Qiskit source installation instructions.

    To build from source:

    1. Clone the repository.
    2. Install development dependencies using requirements-dev.txt.
    3. Install the package using pip install . or build a wheel using the build module.

    Building with GPU support To enable GPU acceleration (CUDA) during a source build on Linux, set the AER_THRUST_BACKEND=CUDA flag.

    Building with MPI support To enable MPI support for parallelizing statevector, density_matrix, or unitary simulations, ensure an MPI library (like OpenMPI) is installed on your system and set the AER_MPI=True flag during the build.

    # Clone and install dependencies
    git clone https://github.com/Qiskit/qiskit-aer
    cd qiskit-aer
    pip install -r requirements-dev.txt
    
    # Standard source install
    pip install .
    
    # Build wheel using build module
    pip install build
    python -I -m build --wheel
    
    # Build with GPU support (Linux only)
    python ./setup.py bdist_wheel -- -DAER_THRUST_BACKEND=CUDA
    
    # Build with MPI support
    python ./setup.py bdist_wheel -- -DAER_MPI=True
  6. Run Qiskit Aer with MPI parallelization

    main

    To run Qiskit Aer using MPI parallelization across a cluster, use an MPI executor like mpirun to submit your Python script. You do not need to manually manage MPI processes within your Python code, as MPI_Init is called internally by Qiskit Aer.

    To ensure consistent circuit generation and parameters across all parallel processes, you must set a consistent random seed using qiskit.utils.algorithm_globals.random_seed.

    mpirun -np 4 python example.py
  7. Interpret Aer benchmark output data

    main

    Benchmark results are displayed in tables organized by the number of qubits and the applied noise model.

    Each row starting with Num. qubits: represents a specific configuration. For every qubit count, the framework runs benchmarks against multiple noise models.

    Example Interpretation: If you see the following output:

                  --                                        Noise Model                         
                  ----------------- ------------------------------------------------------------
                    Quantum Volume   No Noise   Mixed Unitary Noise   Reset Noise   Kraus Noise 
                  ================= ========== ===================== ============= =============
                    Num. qubits: 15   3.17±2s          2.38±2s           2.19±2s       9.76±2s   

    This indicates that for a Quantum Volume circuit with 15 qubits, the execution times were:

    • No Noise: 3.17 ± 2 seconds
    • Mixed Unitary Noise: 2.38 ± 2 seconds
    • Reset Noise: 2.19 ± 2 seconds
    • Kraus Noise: 9.76 ± 2 seconds
                    Num. qubits: 15   3.17±2s          2.38±2s           2.19±2s       9.76±2s   
  8. Configure distributed parallelization with multiple GPUs or nodes

    main

    Qiskit Aer parallelizes simulations by distributing quantum states into chunks (sub-states of smaller qubit counts) to manage distributed memory space. To enable this distributed parallelization across multiple GPUs or nodes, you must configure specific backend options. While Aer may attempt to set these automatically if memory is insufficient, it is recommended to set them explicitly.

    Required configuration options:

    • blocking_enable: Set to True to enable distributed parallelization. (Default is False).
    • blocking_qubits: Sets the number of qubits per chunk. This value must be small enough so that the chunk fits within the smallest available memory space (e.g., a single GPU).

    To ensure the chunk fits, satisfy the following inequality in bytes: sizeof(complex) * 2^(blocking_qubits + 4) < size of the smallest memory space

    sim = AerSimulator(method='statevector', device='GPU')
    circ = transpile(QuantumVolume(qubit, 10, seed = 0))
    circ.measure_all()
    result = execute(circ, sim, shots=100, blocking_enable=True, blocking_qubits=23).result()
  9. Install and run Aer benchmarks using Airspeed Velocity

    main

    Aer uses the Airspeed Velocity (ASV) framework to detect performance regressions. Note that benchmarks are only implemented for the Qiskit Addon, not for standalone mode.

    To run benchmarks, you must first ensure all project build prerequisites are installed. Then, follow these steps:

    1. Install asv via pip:
      pip install asv
    2. Navigate to the test directory:
      cd test
    3. Execute the benchmark suite using the configuration file corresponding to your operating system. Currently, only Linux and MacOS are supported.
    # For Linux
    asv run --config asv.linux.conf.json
    
    # For MacOS
    asv run --config asv.macos.conf.json
  10. Install Qiskit Aer locally

    main

    To install the standard version of Qiskit Aer, ensure you have Qiskit installed in your virtual environment first, then use pip to install qiskit-aer.

    Installing GPU support To run GPU-supported simulators (statevector, density matrix, and unitary) on Linux, you must have CUDA® 10.1 or newer installed. Instead of the standard package, install qiskit-aer-gpu, which will overwrite the standard installation and provide the additional GPU capabilities. Note that qiskit-aer-gpu is only available on x86_64 Linux.

    # Standard installation
    pip install qiskit-aer
    
    # GPU supported installation (Linux x86_64 only, requires CUDA 10.1+)
    pip install qiskit-aer-gpu
  11. Install GPU support for Aer

    main

    To run GPU-supported simulators (statevector, density matrix, and unitary) on Linux, you must have CUDA 11.2 or newer installed.

    Depending on your CUDA version, install the corresponding package. Note that installing these will overwrite your existing qiskit-aer installation with the GPU-enabled version.

    Requirements:

    • Linux x86_64 (Other platforms require building from source).
    • NVIDIA GPU drivers and CUDA installed.

    Installation commands:

    • For CUDA 12:
      pip install qiskit-aer-gpu
    - For CUDA 11:
      ```bash
    pip install qiskit-aer-gpu-cu11