Running classical simulations of quantum circuits with qiskit-aer-gpu
This tutorial covers the exact classical simulaiton of a quantum circuit using GPU resources on Hyak through the qiskit-aer-gpu package. IBM’s quantum SDK Qiskit is used to specify the quantum circuit, and the Qiskit Aer package is used to execute the circuits on GPUs. This tutorial assumes python has been installed on the user’s Hyak account using the miniconda distribution. Instructions for this installation may be found in the Getting started with Hyak tutorial page.
Creating the circuit
To begin, the following code specifies a random circuit on two qubits.
from qiskit.circuit.random import random_circuit
num_qubits = 2
depth = 2
circuit = random_circuit(num_qubits, depth, max_operands=2, seed=0)
The random_circuit may be replaced with something that creates a custom circuit of interest. To run the circuit on the GPU backend, the circuit is compiled to gates that are accepted by the backend.
from qiskit_aer import Aer
from qiskit.compiler import transpile
backend = Aer.get_backend('aer_simulator_statevector_gpu')
gates = backend.configuration().basis_gates
circuit = transpile(circuit, basis_gates=gates, optimization_level=0)
print(circuit)
The circuit is run on the GPU using:
circuit.save_statevector()
output_state = backend.run(circuit).result().get_statevector(circuit, decimals=12).data
print(output_state)
Environment setup
We save this code in a python script titled test_gpu_circ.py. To run this script on Hyak, the required packages must be installed into your account using an interactive node. To do this, run the following, after logging into Hyak:
$ srun -A uwqcc -p gpu-l40 --time=1:00:00 --mem=10G --pty /bin/bash
$ conda create --name test_gpu_env
$ conda activate test_gpu_env
$ conda install python=3.12.1
$ pip install qiskit qiskit-aer-gpu
$ exit
Job submission
To run the test_gpu_circ script on a UWQCC GPU on Hyak, create a slurm script named test_gpu_circ.slurm:
#!/bin/bash
#SBATCH --job-name=test_gpu_circ
#SBATCH --mail-type=ALL # when to email about job status updates
#SBATCH --mail-user=<netid>@uw.edu # email for notifications about job status
#SBATCH -p gpu-l40 # partition
#SBATCH -A uwqcc # account
#SBATCH --nodes=1 # number of nodes
#SBATCH --time=1:00:00 # wall time
#SBATCH --ntasks=1 # number of threads
#SBATCH --gpus=l40:1 # number of gpus
#SBATCH --mem=40G # amount of RAM
source /mmfs1/home/<netid>/.bashrc
conda activate test_gpu_env
python test_gpu_circ.py
exit $?
To submit the test_gpu_circ job, run
$ sbatch test_gpu_circ.slurm
Once the job is finished, the output should read:
┌───┐┌───┐┌────┐┌───┐
q_0: ┤ X ├┤ H ├┤ Sx ├┤ H ├
├───┤└───┘└─┬──┘└───┘
q_1: ┤ I ├───────■────────
└───┘
[0.+0.j 1.-0.j 0.+0.j 0.+0.j]
which shows the circuit that was run and the output statevector of the two-qubit system.