Skip to main content

Table 4 Pseudo-code for defining the best HQCNN algorithm hyperparameters

From: Medical image diagnosis based on adaptive Hybrid Quantum CNN

# Pseudo-code for Hyperparameter Optimization using Genetic Algorithm

# Step 1: Load DICOM medical images and labels

X, y = load_medical_images_and_labels()

# Step 2: Split the dataset into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)

# Step 3: Define the evaluation function (fitness function)

def evaluate_model(individual):

 num_circuits, circuit_depth, learning_rate = individual

 model = create_model_with_hyperparameters(num_circuits, circuit_depth, learning_rate)

 # Train the model on the training set

 model.fit(X_train, y_train, epochs = 5, batch_size = 32, verbose = 0)

 # Evaluate the model on the test set

 y_pred = model.predict_classes(X_test)

 accuracy = accuracy_score(y_test, y_pred)

 # Get the number of parameters in the model

 num_params = model.count_params()

 return accuracy, -num_params # Maximizing accuracy and minimizing number of parameters

# Step 4: Initialize the Genetic Algorithm

population = initialize_population()

# Step 5: Main evolutionary loop

for gen in range(NUM_GENERATIONS):

 offspring = create_offspring(population)

 fitness_values = evaluate_fitness(offspring)

 assign_fitness_values_to_individuals(offspring, fitness_values)

 population = select_next_generation(offspring)

# Step 6: Get the best individual from the final population

best_individual = select_best_individual(population)

# Step 7: Print the best hyperparameters and the corresponding accuracy

best_num_circuits, best_circuit_depth, best_learning_rate = best_individual

best_accuracy, best_num_params = evaluate_model(best_individual)