Skip to content

Commit

Permalink
Merge pull request #28 from flynnbr11/generalise_model_construction
Browse files Browse the repository at this point in the history
Generalise model construction
  • Loading branch information
flynnbr11 committed Aug 24, 2021
2 parents f9e0f92 + efc65e7 commit 9a4f1cb
Show file tree
Hide file tree
Showing 119 changed files with 59,618 additions and 21,547 deletions.
12 changes: 6 additions & 6 deletions launch/local_launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
# QMLA run configuration
##### -------------------------------------------------- #####
num_instances=1 # number of instances in run
run_qhl=0 # perform QHL on known (true) model
run_qhl=1 # perform QHL on known (true) model
run_qhl_multi_model=0 # perform QHL for defined list of models
experiments=5
particles=10
plot_level=2
experiments=25
particles=75
plot_level=6


##### -------------------------------------------------- #####
# Choose an exploration strategy
# This will determine how QMLA proceeds.
##### -------------------------------------------------- #####
exploration_strategy="ExampleBasic"
exploration_strategy="HeisenbergGeneticTest"


##### -------------------------------------------------- #####
Expand Down Expand Up @@ -234,4 +234,4 @@ then
chmod a+x $further_analysis_script
echo "------ Launching analyse further QHL ------"
# sh $further_analysis_script
fi
fi
152 changes: 152 additions & 0 deletions qmla/DEPRECATED_construct_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
from __future__ import print_function

import numpy as np
import copy
import pandas as pd

from qmla.model_building_utilities import (
core_operator_dict,
get_num_qubits,
alph,
get_constituent_names_from_name,
unique_model_pair_identifier,
compute,
)
from qmla.process_string_to_matrix import process_basic_operator
from qmla.string_processing_functions import process_multipauli_term
import qmla.logging

##########
# Section: BaseModel object
##########


class Operator:
r"""
BaseModel objects for Hamiltonian models.
Translates a model name (string) into:
* terms_names: strings specifying constituents
* terms_matrices: whole matrices of constituents
* num_qubits: total dimension of operator [number of qubits it acts on]
* matrix: total matrix operator
* qubits_acted_on: list of qubits which are acted on non-trivially
-- e.g. xTiTTz has list [1,3], since qubit 2 is acted on by identity
* alph_name: rearranged version of name which follows alphabetical convention
-- uniquely identifies equivalent operators for comparison
against previously considered models
:param str name: name of model
"""

def __init__(self, name, fixed_parameters=None, **kwargs):
self.name = alph(name)
self.fixed_parameters = fixed_parameters
print("BASE MODEL fixed_parameters : ", fixed_parameters)

# Modular functionality
self.latex_name_method = (
qmla.shared_functionality.latex_model_names.pauli_set_latex_name
)
self.basic_string_processer = process_multipauli_term

@property
def terms_names(self):
"""
List of constituent operators names.
"""

return get_constituent_names_from_name(self.name)

@property
def parameters_names(self):
# in general may not be the same
return self.terms_names

@property
def num_qubits(self):
"""
Number of qubits this operator acts on.
"""
return get_num_qubits(self.name)

@property
def terms_matrices(self):
"""
List of matrices of constituents.
"""

operators = [
self.model_specific_basic_operator(term) for term in self.terms_names
]
return operators

@property
def num_parameters(self):
return len(self.parameters_names)

@property
def num_terms(self):
"""
Integer, number of constituents (and therefore parameters) in this model.
"""
return len(self.terms_names)

@property
def matrix(self):
"""
Full matrix of operator.
Assumes weight 1 on each constituent matrix.
"""
mtx = None
for i in self.terms_matrices:
if mtx is None:
mtx = i
else:
mtx += i
return mtx

@property
def name_alphabetical(self):
"""
Name of operator rearranged to conform with alphabetical naming convention.
Uniquely identifies equivalent operators.
For use when comparing potential new operators.
"""
return alph(self.name)

@property
def name_latex(self):
return self.latex_name_method(self.name)

@property
def eigenvectors(self):
return get_eigenvectors(self.name)

@property
def fixed_matrix(self):
# TODO does this need to be a property?
if self.fixed_parameters is not None:
return self.construct_matrix(self.fixed_parameters)
else:
return None

def construct_matrix(self, parameters):
r"""
Enables custom logic to compute a matrix based on a list of parameters.
For instance, QInfer-generated particles are passed as an ordered list
for use within the likelihood function.
Default:
sum(p[i] * operators[i])
"""
mtx = np.tensordot(np.array(parameters), np.array(self.terms_matrices), axes=1)
return mtx

def model_specific_basic_operator(self, term):
# process a basic term in the formalism of this model

# return process_basic_operator(term)
return self.basic_string_processer(term)
7 changes: 4 additions & 3 deletions qmla/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Logistics
from qmla.get_exploration_strategy import *
from qmla.construct_models import * # TODO fix __all__
from qmla.model_building_utilities import * # TODO fix __all__
from qmla.controls_qmla import *
from qmla.logging import *
from qmla.redis_settings import *
Expand All @@ -17,10 +17,11 @@
from qmla.model_for_comparison import *
from qmla.model_for_learning import *
from qmla.model_for_storage import *
from qmla.model_building_utilities import *

# Learning/comparisons
from qmla.remote_bayes_factor import *
from qmla.remote_model_learning import *

# Results loader
from qmla.load_results import load_results
# Results loader
from qmla.load_results import load_results
4 changes: 2 additions & 2 deletions qmla/analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# from qmla.analysis.plotting import *
# from qmla.analysis.plotting import *
from qmla.analysis.analysis_and_plot_functions import *
from qmla.analysis.combine_analysis import *
from qmla.analysis.genetic_algorithm_analysis import *
Expand All @@ -9,4 +9,4 @@
from qmla.analysis.performance import *
from qmla.analysis.tree_plot import *
from qmla.analysis.branch_graphs import *
from qmla.analysis.nv_centre_analysis_figure import *
from qmla.analysis.nv_centre_analysis_figure import *

0 comments on commit 9a4f1cb

Please sign in to comment.