diff --git a/examples/decoding/quantum_surface.py b/examples/decoding/quantum_surface.py index 1134f89..dfec0fd 100644 --- a/examples/decoding/quantum_surface.py +++ b/examples/decoding/quantum_surface.py @@ -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 @@ -46,7 +47,6 @@ try: from examples.decoding.decoding import ( decode_css, - pauli_to_mps, generate_pauli_error_string, ) except ImportError as e: @@ -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, @@ -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): @@ -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, } @@ -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, diff --git a/examples/decoding/quantum_surface.sh b/examples/decoding/quantum_surface.sh index 704be78..c3771f6 100644 --- a/examples/decoding/quantum_surface.sh +++ b/examples/decoding/quantum_surface.sh @@ -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://git@github.com/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) @@ -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 @@ -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" <