3. Job Execution Method
Log in to the compute nodes from the login server by Interactive job and build the environment
Return to the login server and create a shell script
Submit a job into the Batch queue from the login server (running a shell script)
Check the execution results
3.1. Environmental Construction
3.1.1. Installing Python packages
Note
Since the number of nodes is limited, please exit when you are finished.
[username@loginvm-XXX ~]$ salloc -N 1 -p Interactive --time=1:00:00
You create an execution environment on the compute node. The following example creates a Python virtual environment in the example directory and installs the packages needed to use mpiQulacs.
[username@fx-XX-XX-XX ~]$ mkdir example
[username@fx-XX-XX-XX ~]$ cd example
[username@fx-XX-XX-XX ~]$ python3.8 -m venv qenv
[username@fx-XX-XX-XX ~]$ source ./qenv/bin/activate
(qenv) [username@fx-XX-XX-XX ~]$ pip install --upgrade pip wheel
(qenv) [username@fx-XX-XX-XX ~]$ pip install mpi4py # Required to use mpiQulacs
(qenv) [username@fx-XX-XX-XX ~]$ pip install mpiQulacs
(qenv) [username@fx-XX-XX-XX ~]$ exit
3.2. Creating scripts
3.2.1. Creating a job script file for MPI execution
~/example/job.sh .#!/usr/bin/env bash
# Usage:
# mpirun -n <num_ranks> -npernode 1 job.sh <path/to/venv> <command> <command arguments>
#
# Example:
# (python): mpirun -n 2 -npernode 1 job.sh ~/example/venv python ./sample.py
# (pytest): mpirun -n 2 -npernode 1 job.sh ~/example/venv pytest
# Setting the Environment Variables required to use MPI in the quantum simulator system
export UCX_IB_MLX5_DEVX=no
export OMP_PROC_BIND=TRUE
# Number of threads used for OpenMP parallelization
# Due to the OpenMP multi-threading behavior, some libraries (such as scipy) can introduce small calculation errors, which can lead to inconsistent calculation results between processes during MPI execution. When using mpiQulacs, set it to 1.
export OMP_NUM_THREADS=1
# Number of threads used by mpiQulacs (if not set, the value of OMP_NUM_THREADS is used)
export QULACS_NUM_THREADS=48
#
source $1/bin/activate
shift
### Workaround for the glibc bug (https://bugzilla.redhat.com/show_bug.cgi?id=1722181)).
if [ -z "${LD_PRELOAD}" ]; then
export LD_PRELOAD=/lib64/libgomp.so.1
else
export LD_PRELOAD=/lib64/libgomp.so.1:$LD_PRELOAD
fi
#
LSIZE=${OMPI_COMM_WORLD_LOCAL_SIZE}
LRANK=${OMPI_COMM_WORLD_LOCAL_RANK}
COM=$1
shift
if [ $LSIZE -eq 1 ]; then
numactl -m 0-3 -N 0-3 ${COM} "$@"
elif [ $LSIZE -eq 4 ]; then
numactl -N ${LRANK} -m ${LRANK} ${COM} "$@"
else
${COM} "$@"
fi
After writing the code, execute the following command to grant execute permission.
$ chmod u+x ~/example/job.sh
3.2.2. Python script example of quantum program
from qulacs import QuantumCircuit, QuantumState
from mpi4py import MPI # import is required for MPI execution even if MPI classes are not used
seed=1234
# Get process rank
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
# creation of quantum states
num_qubits = 10
state = QuantumState(num_qubits, use_multi_cpu=True)
# Define quantum circuits and update quantum states
circuit = QuantumCircuit(num_qubits)
circuit.add_H_gate(0)
for i in range(num_qubits - 1):
circuit.add_CNOT_gate(i, i + 1)
circuit.update_quantum_state(state)
# Sampling values from quantum states
sampled_values = state.sampling(20, seed)
# Show results only for a process with rank 0 (node01)
if rank == 0:
print(sampled_values)
Note
To run MPI, you must write an import statement “from mpi4py import MPI” before running the quantum circuit.
Note
The program (example.py) runs simultaneously on multiple compute nodes. Simply calling the print method will print the message to standard output multiple times for as many compute nodes. Therefore, the print statement is limited to node01. See Overview of MPI Parallel Computing in Python for details.
3.2.3. Creating a batch file
#!/bin/bash
#SBATCH -p Batch # Specify Batch queue
#SBATCH -o test-%j # Output file name
#SBATCH -N 64 # Number of nodes allocated
#SBATCH -t 06:00:00 # Limit on the job execution time
mpirun -npernode 4 job.sh ~/example/qenv python example.py # The -n option is not required when executing with the number of nodes allocated above.
# mpirun -n 1 -npernode 1 job.sh ~/example/qenv python example.py # To run on more than the allocated number of nodes, give the -n option; in this example, one node.
python followed by the command argument example.py .python example.py to run in parallel on 64 compute nodes via job.sh.Note
The optimal number of parallel circuits depends on the size and configuration of the circuit. Please consult with Fujitsu Researcher and adjust the parallel number accordingly.
Warning
3.3. Job Submission
You run the batch file you created.
[username@loginvm-XXX example]$ sbatch sim.job
3.4. Check Output Results
When the job runs, the test-<JOBID> file is created and the results are output. If the JOBID is 10000, the results are output to the file test-10000 .
[username@loginvm-XXX example]$ cat test-10000
[1023, 0, 1023, 1023, 0, 1023, 1023, 1023, 0, 1023, 1023, 0, 0, 1023, 0, 1023, 0, 0, 0, 1023]