Applications

NV centre characterisation

The model searches presented in [GFK20] have exploration strategies as presented here.

Genetic algorithm for spin bath

class qmla.exploration_strategies.nv_centre_spin_characterisation.nature_physics_2021.NVCentreGenticAlgorithmPrelearnedParameters(exploration_rules, true_model=None, **kwargs)[source]

Exploration strategy for studying large model space of NV centre through a genetic algorithm.

Model generation is through the genetic algorithm exploration strategy, Genetic. This Exploration Strategy sets up the true model as an NV centre spin interacting with a number of nuclei, and makes a wider number of nuceli searchable by the genetic algorithm. The NV centre is approximated by the Gali approximation [SCG13]. Candidate models are assumed to have been learned extremely well by a parameter esimation algorithm, which may be unrealistic in some cases. In the genetic algorithm, to assess candidate models, we use an objective function which computes the average residual between the candidate and the system’s dynamics, against a representative dataset.

_get_secular_approx_true_params(num_qubits=2, total_num_qubits=5)[source]

Using the secular approximation, define true parameters for all present terms.

Parameters
  • num_qubits (int) – number of qubits in the target model

  • total_num_qubits (int) – number of qubits of the search space, i.e. terms will be defined in this dimension, even if the system is not expected to be this large.

Returns dict true_params

frequencies of each term to include in the true model

_set_true_params()[source]

Set up the target model: call a series of subroutines to define the true model, as well as setting the parameters to represent the physics appropriately.

_setup_available_terms_gali_model(n_qubits=2, available_axes=['z'])[source]

Generates the set of terms to include in the genetic algorithm.

Terms are stored as an attribute of the class.

Parameters
  • n_qubits (int) – number of qubits to construct terms up to

  • available_axes (lsit) – axes about which to generate terms, under the Gali approximation

_setup_prior_by_parameters()[source]

Constructs the prior distribution to assign true parameters in the model.

These are set in the gaussian_prior_means_and_widths attribute of this exploration strategy class.

generate_evaluation_data(num_times=100, **kwargs)[source]

Generates sequential, equally spaced times for evaluating the candidate models against.

Parameters

num_times (int) – number of datapoints to generate

Returns dict eval_data

set of experiments for model evaluation

get_evaluation_prior(model_name, estimated_params, cov_mt, **kwargs)[source]

Generate a QInfer distribution representing the trained model’s paramterisation, in order to evaluate that model.

Parameters
  • model_name (str) – string representing the candidate model

  • estimated_params (dict) – average values of the posterior distribution after training, representing the parameter estimates for the model

  • cov_mt (np.array) – covariance matrix, i.e. the relationship between parameters after training

get_prior(model_name, **kwargs)[source]

Given a candidate model, constructs a very thin prior.

This is done to skip the model training stage, and assumes the training has performed extremely well. This method is called by QMLA in constructing candidate models.

Parameters

model_name (str) – string representing the model which is being tested.

Returns prior

QInfer object, used for sampling parameter values when considering the given model

Genetic Algorithms

Genetic algorithms can be used within the Exploration Strategy of QMLA; here we provide a genetic algorithm framework which can be plugged in.

class qmla.shared_functionality.genetic_algorithm.GeneticAlgorithmQMLA(genes, num_sites, true_model=None, base_terms=['x', 'y', 'z'], selection_method='roulette', crossover_method='one_point', mutation_method='element_wise', mutation_probability=0.1, selection_truncation_rate=0.5, num_protected_elite_models=2, unchanged_elite_num_generations_cutoff=5, log_file=None, **kwargs)[source]

Standalone genetic algorithm implementation for integration with qmla.QuantumModelLearningAgent.

This class works with the ExplorationStrategy to construct models according to the genetic strategy.

Parameters
  • genes (list) – individual terms which can be combined to form chromosomes

  • num_sites (int) – maximum dimension permitted in model search

  • true_model (str) – target model. if None, set at random from space of valid models.

  • base_terms (list) – deprecated TODO remove

  • selection_method (str) – mechanism through which to select chromosomes as parents. Currently only ‘roulette’ available, but the framework should facilitate alternatives.

  • crossover_method (str) – mechanism through which parent chromosomes are combined to form offspring. Currently only ‘one_point’ available, but the framework should facilitate alternatives.

  • mutation_method (str) – mechanism through which to perform chromosome mutation Currently only ‘element_wise’ available, but the framework should facilitate alternatives.

  • mutation_probability (float) – rate with which the mutation mechanism incurs mutation.

  • selection_truncation_rate (float) – fraction of models to retain as viable parents to the subsequent generation; the lower-rated other models are discarded.

  • num_protected_elite_models (int) – number of models to automatically admit to the subsequent generation.

  • unchanged_elite_num_generations_cutoff (int) – after this number of generations, if the top model has not changed, the model search is terminated.

  • log_file (str) – path of QMLA instance’s log file.

basic_pair_selection(chromosome_selection_probabilities, **kwargs)[source]

Mechanism for selecting two models from the database of potential parents.

Parameters

chromosome_selection_probabilities (pd.DataFrame) – database indicating the probability that every valid pair of parents should be selected.

Return tuple selected_chromosomes

two models

chromosome_f_score(chromosome)[source]

Get the F score of a candidate model from its chromosome representation.

Parameters

chromosome (np.array) – representation of candidate model

Returns float f_score

F score, between 0 and 1, indicating how many terms overlap between the candidate and target models.

chromosome_string(c)[source]

Map a chromosome array to a string.

consolidate_generation(model_fitnesses, **kwargs)[source]

Following the training of all models on a generation, consolidate that generation.

This involves determining the strongest models from the generation, and constructing the database of parent-pairs and their associated selection probabilities.

Parameters

model_fitnesses (dict) – the fitness of each model in this generation according to the chosen objective function.

crossover(**kwargs)[source]

Wrapper for crossover mechanism.

This method assumes only 2 chromosomes to crossover and passes them to the method set as self.crossover_method, which can be easily replaced to facilitate alternative crossover schemes.

element_wise_mutation(**kwargs)[source]

Probabilistically mutate each gene independently.

elite_ranking_top_n_models(model_fitnesses, **kwargs)[source]

Get the top N models, and store info on the elite models to date.

Parameters

model_fitnesses (dict) – the fitness of each model in this generation according to the chosen objective function.

genetic_algorithm_step(model_fitnesses, **kwargs)[source]

Perform a complete step of the genetic algorithm, assuming all of the required steps have been performed. That is, the database for parent selection must already be available.

Parameters

model_fitnesses (dict) – the fitness of each model in this generation according to the chosen objective function.

Returns list new_models

set of models to place on the next generation.

get_base_chromosome()[source]

Creates basic chromosome, i.e. with all genes set to 0.

get_elite_models(**kwargs)[source]
Wrapper for elite model selection method,

here set to self.elite_ranking_top_n_models.

get_pair_selection_order()[source]

Use the probabilities of parental selection to define the order in which to generate offspring. It is cheaper to perform this once than call the database repeatedly.

Return list pair_selection_order

list of tuples of the order in which to pass the model pairs to the crossover mechanism to generate offspring

get_selection_probabilities(**kwargs)[source]

Wrapper for parent selection function, here set to self.truncate_to_top_half.

log_print(to_print_list)[source]

Wrapper for print_to_log()

map_chromosome_to_model(chromosome)[source]

Given a chromosome, get the corresponding model.

Parameters

chromosome (np.array) – chromosome representing a candidate model

Returns str model_string

name of the corresponding model

map_model_to_chromosome(model)[source]

Given a model, get the corresponding chromosome.

Parameters

model (str) – name of candidate model

Returns np.array chromosome

array of ones and zeros indicating which genes are active in the model

model_f_score(model_name)[source]

Get the F score of a candidate model.

Parameters

model_name (str) – name of candidate model

Returns float f_score

F score, between 0 and 1, indicating how many terms overlap between the candidate and target models.

mutation(**kwargs)[source]

Wrapper for mutation mechanism. All input arguments to the mutation method are passed directly to the nominated mutation function, set as self.mutation_method.

one_point_crossover(**kwargs)[source]

Crossover two chromosomes about a single gene.

Input two chromosomes, and selection (a dict) in kwargs. selection contains chromosome_1 and chromosome_2, as well as a dict called other_data containing cut, which is the position about which to crossover the two chromosomes.

prepare_chromosome_pair_dataframe(chromosome_probabilities, force_mutation=False)[source]

Given a set of individual chromosome fitnesses, generate database of pairs of parent chromosomes, with probability proportional to the fitness of both parents.

rand_model_f()[source]

Generate a random model chromosome and evaluate its F score.

random_initial_models(num_models=5)[source]

Generate random models from the space of valid candidates.

Parameters

num_models (int) – number of candidates to generate

Returns list new_models

the randomly generated model names

random_models_sorted_by_f_score(num_models=14)[source]

Generate a set of random models and sort them by F score.

selection(**kwargs)[source]

Wrapper for user’s selected selection method.

Whatever method is called must return
  • prescribed_chromosomes

  • chromosomes_for_crossover - pairs

truncate_to_top_half(model_fitnesses, **kwargs)[source]

Retain only the top-performing half of models considered at this generation, for consideration as parents to offspring on the subsequent generation.

Parameters

model_fitnesses (dict) – the fitness of each model in this generation according to the chosen objective function.

Genetic Exploration Strategy

A base class for genetic algorithm incorporated into the Exploration Strategy.

class qmla.exploration_strategies.genetic_algorithms.genetic_exploration_strategy.Genetic(exploration_rules, genes, true_model, **kwargs)[source]

Exploration Strategy where the model search is mediated through a genetic algorithm. Genetic algorithm is implemented through qmla.GeneticAlgorithmQMLA. This forms the base class for genetic algorithm applications within QMLA.

Parameters
  • exploration_rules (str) – name of exploration strategy used

  • genes (list) – terms which are permitted in the model search, which become genes in the chromomsomes of the genetic algorithm

  • true_model (str) – name of the target model.

__plot_gene_pool_progression()

Succinct representation of the progression of gene pool with respect to F score.

_plot_correlation_fitness_with_f_score(save_to_file=None)[source]

Show how the fitness of models at each generation progress in terms of F score.

_plot_fitness_v_fscore()[source]

Plot fitness against f score

_plot_fitness_v_fscore_by_generation()[source]

Plot fitness vs f score throughout generations of the genetic algorithm.

_plot_fitness_v_generation(save_to_file=None)[source]

Plot progression of fitness against generations of the genetic algorithm.

_plot_gene_pool()[source]

Show the F scores of all models in all generations

_plot_model_ratings()[source]

Plot ratings of models on all generations, as determined by the RatingSystem

_plot_selection_probabilities()[source]

Plot pie charts of the selection probabilities of prospective parents at each generation. Models are signified by their F score.

analyse_generation(model_points, model_names_ids, **kwargs)[source]

Following a complete generation of the genetic algorithm, perform all necessary processing to enable construction of next set of models.

Parameters
  • model_points (dict) – the number of Bayes factor comparisons for which each candidate within the generation was deemed superior against a contemporary model

  • model_names_ids (dict) – mapping between models’ names and their IDs from the QMLA environment; this enables analaysing further data passed from QMLA within kwargs.

check_tree_completed(spawn_step, **kwargs)[source]

Genetic algorithm specific version of qmla.ExplorationStrategy.check_tree_completed().

check_tree_pruned(**kwargs)[source]

Genetic algorithm specific version of qmla.ExplorationStrategy.check_tree_pruned().

exploration_strategy_finalise()[source]

Genetic algorithm specific version of qmla.ExplorationStrategy.exploration_strategy_finalise().

f_score_from_chromosome_string(chromosome)[source]

F1 score between chromosome and true model

f_score_model_comparison(test_model, target_model=None, beta=1)[source]

Get F score of candidate model, measure of overlap between the terms of the candidate and target model

Parameters
  • test_model (str) – name of candidate model

  • target_model (str) – name of target model, if None, assumed that target is self.true_model

  • beta (float) – relative importance of precision to sensitivity. in general this is F-beta score, usually beta = 1

static gene_pool_progression(gene_pool, ax, f_score_cmap=None, draw_cbar=True, cbar_ax=None)[source]

Method for plotting succinct summary of progression of gene pool with respect to F score.

generate_models(model_list, **kwargs)[source]

Model generation using genetic algorithm.

Follows rules of generate_models().

hamming_distance_model_comparison(test_model, target_model=None)[source]

Compare test_model with target_model by Hamming distance

nominate_champions()[source]

Choose model with highest fitness on final generation

plot_generational_metrics()[source]

Show various metrics across all generations

set_specific_plots(**kwargs)[source]

Genetic algorithm specific version of qmla.ExplorationStrategy.set_specific_plots().