Skip to content

Commit

Permalink
parallelization
Browse files Browse the repository at this point in the history
  • Loading branch information
meandmytram committed Jan 21, 2025
1 parent 9940772 commit 324472c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 71 deletions.
120 changes: 67 additions & 53 deletions examples/decoding/quantum_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import pickle
import logging
import argparse
from multiprocessing import Pool

import numpy as np
from tqdm import tqdm
import qecstruct as qec

# Setup logging
Expand Down Expand Up @@ -46,7 +47,6 @@
try:
from examples.decoding.decoding import (
decode_css,
pauli_to_mps,
generate_pauli_error_string,
)
except ImportError as e:
Expand Down Expand Up @@ -76,6 +76,9 @@ def parse_arguments():
parser.add_argument(
"--error_rate", type=float, required=True, help="The error rate."
)
parser.add_argument(
"--bias_prob", type=float, required=True, help="The decoder bias probability."
)
parser.add_argument(
"--num_experiments",
type=int,
Expand All @@ -94,32 +97,13 @@ def parse_arguments():
required=True,
help="The seed for the random number generator.",
)
return parser.parse_args()


def run_single_experiment(lattice_size, chi_max, error, bias_prob, error_model):
"""Run a single experiment."""
rep_code = qec.repetition_code(lattice_size)
surface_code = qec.hypergraph_product(rep_code, rep_code)

_, success = decode_css(
code=surface_code,
error=error,
chi_max=chi_max,
multiply_by_stabiliser=True,
bias_type=error_model,
bias_prob=bias_prob,
renormalise=True,
silent=True,
contraction_strategy="Optimised",
parser.add_argument(
"--num_processes",
type=int,
required=True,
help="The number of processes to use in parallelisation.",
)

if success == 1:
logging.info("Decoding successful.")
return 0
else:
logging.info("Decoding failed.")
return 1
return parser.parse_args()


def generate_errors(lattice_size, error_rate, num_experiments, error_model, seed):
Expand All @@ -144,42 +128,71 @@ def generate_errors(lattice_size, error_rate, num_experiments, error_model, seed
return errors


def run_single_experiment(lattice_size, chi_max, error, bias_prob, error_model):
"""Run a single experiment."""
rep_code = qec.repetition_code(lattice_size)
surface_code = qec.hypergraph_product(rep_code, rep_code)

logicals_distribution, success = decode_css(
code=surface_code,
error=error,
chi_max=chi_max,
multiply_by_stabiliser=False,
bias_type=error_model,
bias_prob=bias_prob,
renormalise=True,
silent=False,
contraction_strategy="Optimised",
)

if success == 1:
logging.info("Decoding successful.")
return logicals_distribution, 0
else:
logging.info("Decoding failed.")
return logicals_distribution, 1


def run_experiment(
lattice_size, chi_max, error_rate, num_experiments, error_model, seed, errors
lattice_size,
chi_max,
error_rate,
bias_prob,
num_experiments,
error_model,
seed,
errors,
num_processes=1,
):
"""Run the experiment consisting of multiple single experiments."""
"""Run the experiment consisting of multiple single experiments in parallel."""
logging.info(
f"Starting {num_experiments} experiments for LATTICE_SIZE={lattice_size},"
f"CHI_MAX={chi_max}, ERROR_RATE={error_rate}, ERROR_MODEL={error_model}, SEED={seed}"
)

failures = []

for l in tqdm(range(num_experiments)):
try:
failure = run_single_experiment(
lattice_size=lattice_size,
chi_max=chi_max,
error=errors[l],
bias_prob=0.05,
error_model=error_model,
)
failures.append(failure)
except Exception as e:
logging.error(f"Experiment {l} failed with error: {str(e)}", exc_info=True)
failures.append(1)

logging.info(
f"Finished experiment {l} for LATTICE_SIZE={lattice_size}, CHI_MAX={chi_max},"
f"ERROR_RATE={error_rate}, ERROR_MODEL={error_model}, SEED={seed}"
)
f"CHI_MAX={chi_max}, ERROR_RATE={error_rate}, BIAS_PROB={bias_prob},"
f"ERROR_MODEL={error_model}, SEED={seed}"
)

args = [
(lattice_size, chi_max, errors[i], bias_prob, error_model)
for i in range(num_experiments)
]

with Pool(num_processes) as pool:
logicals_distribution, results = pool.starmap(run_single_experiment, args)

logging.info(
f"Finished {num_experiments} experiments for LATTICE_SIZE={lattice_size},"
f"CHI_MAX={chi_max}, ERROR_RATE={error_rate}, BIAS_PROB={bias_prob},"
f"ERROR_MODEL={error_model}, SEED={seed}"
)

return {
"failures": failures,
"logicals_distributions": logicals_distribution,
"failures": results,
"errors": errors,
"lattice_size": lattice_size,
"chi_max": chi_max,
"error_rate": error_rate,
"bias_prob": bias_prob,
"error_model": error_model,
"seed": seed,
}
Expand Down Expand Up @@ -211,6 +224,7 @@ def main():
args.lattice_size,
args.bond_dim,
args.error_rate,
args.bias_prob,
args.num_experiments,
args.error_model,
args.seed,
Expand Down
32 changes: 14 additions & 18 deletions examples/decoding/quantum_surface.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pip install --no-index --upgrade pip
pip install --no-index numpy scipy opt_einsum tqdm qecstruct more_itertools networkx
pip install git+ssh://[email protected]/quicophy/matrex.git

num_processes=4 # Number of processes to use in parallel

# Define arrays of lattice sizes, bond dimensions, error rates, and seeds
lattice_sizes=(13)
bond_dims=(256)
Expand All @@ -23,14 +25,10 @@ seeds=(
143 144 145 146 147 148 149 150 151 152
153 154 155 156 157 158 159 160 161 162
163 164 165 166 167 168 169 170 171 172
173 174 175 176 177 178 179 180 181 182
183 184 185 186 187 188 189 190 191 192
193 194 195 196 197 198 199 200 201 202
203 204 205 206 207 208 209 210 211 212
213 214 215 216 217 218 219 220 221 222
) # 100 random seeds
num_experiments=10 # Per each random seed
error_model="Bit Flip" # Error model used in the experiments
) # 50 random seeds
num_experiments=20 # Per each random seed
error_model="Bitflip" # Error model used in the experiments
bias_prob=0.001 # The decoder bias probability

error_rates=()
start=0.105
Expand All @@ -48,26 +46,24 @@ for seed in "${seeds[@]}"; do
for lattice_size in "${lattice_sizes[@]}"; do
for bond_dim in "${bond_dims[@]}"; do
for error_rate in "${error_rates[@]}"; do
# Sanitize the error model name by replacing spaces with underscores
sanitized_error_model=$(echo "${error_model}" | sed 's/ /_/g')
# Define job script filename
job_script="submit-job-${lattice_size}-${bond_dim}-${error_rate}-${sanitized_error_model}-${seed}.sh"
job_script="submit-job-${lattice_size}-${bond_dim}-${error_rate}-${error_model}-${seed}.sh"
# Create the job submission script
cat > "$job_script" <<EOS
#!/bin/bash
#SBATCH --time=48:00:00 # Time limit (hh:mm:ss)
#SBATCH --cpus-per-task=1 # Number of CPU cores per task
#SBATCH --mem=64000 # Memory per node
#SBATCH --job-name=decoding-${lattice_size}-${bond_dim}-${error_rate}-${sanitized_error_model}-${seed} # Descriptive job name
#SBATCH --output=decoding-${lattice_size}-${bond_dim}-${error_rate}-${sanitized_error_model}-${seed}-%j.out # Standard output and error log
#SBATCH --time=96:00:00 # Time limit (hh:mm:ss)
#SBATCH --cpus-per-task=${num_processes} # Number of CPU cores per task
#SBATCH --mem=16000 # Memory per node
#SBATCH --job-name=decoding-${lattice_size}-${bond_dim}-${error_rate}-${error_model}-${seed} # Descriptive job name
#SBATCH --output=decoding-${lattice_size}-${bond_dim}-${error_rate}-${error_model}-${seed}-%j.out # Standard output and error log
module load python/3.11.5
source "$HOME/envs/myenv/bin/activate"
# Run the Python script with the specified arguments
python examples/decoding/quantum_surface.py --lattice_size ${lattice_size} --bond_dim ${bond_dim} --error_rate ${error_rate} --num_experiments ${num_experiments} --error_model "${error_model}" --seed ${seed}
python examples/decoding/quantum_surface.py --lattice_size ${lattice_size} --bond_dim ${bond_dim} --error_rate ${error_rate} --bias_prob ${bias_prob} --num_experiments ${num_experiments} --error_model "${error_model}" --seed ${seed} --num_processes ${num_processes}
EOS
echo "Submitting the job for lattice size ${lattice_size}, bond dimension ${bond_dim}, error rate ${error_rate}, error model ${error_model}, and seed ${seed}."
echo "Submitting the job for lattice size ${lattice_size}, bond dimension ${bond_dim}, error rate ${error_rate}, error model ${error_model}, bias probability ${bias_prob} and seed ${seed}."
sbatch "$job_script"
rm "$job_script"
done
Expand Down

0 comments on commit 324472c

Please sign in to comment.