diff --git a/src/qrisp/algorithms/qaoa/problems/portfolio_test_run.py b/src/qrisp/algorithms/qaoa/problems/portfolio_test_run.py deleted file mode 100644 index 13bba3b8..00000000 --- a/src/qrisp/algorithms/qaoa/problems/portfolio_test_run.py +++ /dev/null @@ -1,115 +0,0 @@ -import numpy as np - -lots = 2 - -A = [45, 37, 42, 35, 39] -B = [38, 31, 26, 28, 33] -C = [10, 15, 17, 21, 12] -D = [11,14,14,20,33] -data = np.array([A, B, C,D]) - -old_pos = [1, 1, 0, 0] -risk_return = 0.5 -covar_matrix = np.cov(data, bias=True) -asset_return = [1.2, 0.8, 1.3, 1.1] -tc = 0.1 -problem = [old_pos,risk_return,covar_matrix,asset_return, tc] - -from qrisp.qaoa.build_dicke_mixer import * -from qrisp.qaoa.buildportfolio_costop import * -from qrisp.qaoa.buildportfolio_init import * -from qrisp.qaoa import QAOAProblem -cost_op = portfolio_cost_operator(problem=problem) - -cl_cost = portfolio_cl_cost_function(problem=problem) -init_fun = portfolio_init(lots=lots) -mixer_op = portfolio_mixer() - -qv = QuantumVariable(4) -q_array = QuantumArray(qtype=qv, shape=(2)) - - -theproblem = QAOAProblem(cost_operator=cost_op, mixer=mixer_op, cl_cost_function=cl_cost) -theproblem.set_init_function(init_fun) - -theNiceQAOA = theproblem.run(q_array,depth = 3) - -print(theNiceQAOA) -#print the ideal solutions -print("5 most likely Solutions") - - -def a_cost_fct(key): - half = len(key[0]) - new_key = [int(key[0][i])-int(key[1][i]) for i in range(half)] - rr1 = sum([risk_return*covar_matrix[i][j] *new_key[i]*new_key[j] for i in range(half) for j in range(half)]) - rr2 = sum([(1-risk_return)*asset_return[j] *new_key[j] for j in range(half)]) - c_tc= sum([tc for i in range(half) if new_key[i] != old_pos[i]]) - energy = -(rr1+ rr2+ c_tc) - - return energy - -maxfive = sorted(theNiceQAOA, key=theNiceQAOA.get, reverse=True)[:5] -for name, age in theNiceQAOA.items(): # for name, age in dictionary.iteritems(): (for Python 2.x) - if name in maxfive: - - print((name, age)) - print(a_cost_fct(name)) - - -print(q_array.qs) - -""" - -init_fun(q_array) -#print(qv.qs) -inital_state = q_array.duplicate(init = True) -#print(q_array) -#print(inital_state) -half = int(len(q_array[0])/2) -# the code below does what its supposed to -#dicke_state(q_array[0], half) -#dicke_state(q_array[1], half) - -# this one doesnt -mixer_op(q_array,np.pi/4) -#print(q_array) - -#initial_vs_mixed = multi_measurement([inital_state, q_array]) -#print(initial_vs_mixed) -""" - - -""" - -print(len(initial_vs_mixed)) -count = 0 -for k,v in initial_vs_mixed.items(): - if v > 0.00001: - count +=1 - -print(count) - """ - -""" cost_op(q_array,np.pi/4) -res = q_array.get_measurement() - - -old_dict = dict() -for k,v in res.items(): - key_old = str(k[0] + k[1]) - old_dict.setdefault(key_old, v) - -old_cost= portfolio_cl_cost_function_for_qv(problem=problem) - -res_old, key_list_old = old_cost(old_dict) -res, key_list = cl_cost(res) -print(key_list_old) -print("new") -print(key_list) -print(len(key_list_old)) - -print(len(key_list)) -print(res) -print(res_old) """ - diff --git a/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxClique.py b/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxClique.py index 4fc16bf7..af5b4e55 100644 --- a/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxClique.py +++ b/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxClique.py @@ -23,7 +23,7 @@ from qrisp.algorithms.qiro.qiroproblems.qiro_utils import * -def create_max_clique_replacement_routine(res, graph, solutions, exclusions): +def create_max_clique_replacement_routine(res, graph, solutions=[], exclusions=[]): """ Creates a replacement routine for the problem structure, i.e., defines the replacement rules. See the `original paper `_ for a description of the update rules. @@ -56,7 +56,10 @@ def create_max_clique_replacement_routine(res, graph, solutions, exclusions): orig_nodes = list(graph.nodes()) #get the max_edge and eval the sum and sign + max_item, sign = find_max(orig_nodes, orig_edges , res, solutions) + if max_item == None: + return graph, solutions, 0 ,exclusions new_graph = copy.deepcopy(graph) diff --git a/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxIndepSet.py b/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxIndepSet.py index 858809ba..2ad33978 100644 --- a/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxIndepSet.py +++ b/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxIndepSet.py @@ -57,8 +57,10 @@ def create_max_indep_replacement_routine(res, graph, solutions=[], exclusions=[] # for single qubit correlations orig_nodes = list(graph.nodes()) - max_item = [] + max_item, sign = find_max(orig_nodes, orig_edges , res, solutions) + if max_item == None: + return graph, solutions, 0 ,exclusions # create a copy of the graph new_graph = copy.deepcopy(graph) @@ -125,56 +127,4 @@ def cost_operator(qv, gamma): -""" def create_maxIndep_mixer_reduced(graph, solutions): - - def RX_mixer(qv, beta): - - from qrisp import rx - for i in graph.nodes(): - if not i in solutions: - rx(2 * beta, qv[i]) - return RX_mixer - -def init_function_reduced(graph, solutions): - - def init_state(qv): - from qrisp import h - for i in graph.nodes(): - if not i in solutions: - h(qv[i]) - for i in solutions: - x(qv[i]) - return init_state - - - -#TODO: -def create_maxIndep_cl_cost_function_reduced(graph): - - #btw alternative formulation: for edge: check if string[edge[0]] != string[edge[1]] - def aClcostFct(res_dic): - tot_energy = 0.001 - tot_counts = 0 - for state in res_dic.keys(): - # we assume solution is right - temp = True - energy = 0 - for edge in graph.edges(): - if not state[edge[0]] != state[edge[1]]: - temp = False - - # else we just add the number of marked as |1> nodes - if temp: - intlist = [s for s in range(len(list(state))) if list(state)[s] == "1"] - energy = -len(intlist) - - tot_energy += energy * res_dic[state] - tot_counts += res_dic[state] - - #print(tot_energy/tot_counts) - - return tot_energy/tot_counts - - return aClcostFct -""" \ No newline at end of file diff --git a/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxSat.py b/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxSat.py index 64a5f74c..fa5e58b3 100644 --- a/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxSat.py +++ b/src/qrisp/algorithms/qiro/qiroproblems/qiroMaxSat.py @@ -25,7 +25,7 @@ from qrisp.algorithms.qiro.qiroproblems.qiro_utils import * -def create_maxsat_replacement_routine(res, problem, solutions, exclusions): +def create_maxsat_replacement_routine(res, problem, solutions=[], exclusions=[]): """ Creates a replacement routine for the problem structure, i.e., defines the replacement rules. See the `original paper `_ for a description of the update rules. @@ -69,6 +69,8 @@ def create_maxsat_replacement_routine(res, problem, solutions, exclusions): max_item, sign = find_max(orig_nodes, combinations, res, solutions) + if max_item == None: + return problem, solutions, 0 ,exclusions # we just directly remove clauses from clauses if isinstance(max_item, int): diff --git a/src/qrisp/algorithms/qiro/qiroproblems/qiro_utils.py b/src/qrisp/algorithms/qiro/qiroproblems/qiro_utils.py index 2ab02370..f3eb6b06 100644 --- a/src/qrisp/algorithms/qiro/qiroproblems/qiro_utils.py +++ b/src/qrisp/algorithms/qiro/qiroproblems/qiro_utils.py @@ -41,6 +41,8 @@ def find_max(single_cor, double_cor, res, solutions): """ max = 0 + max_item = None + sign = None for item2 in double_cor: if abs(item2[0]) == abs(item2[1]): diff --git a/src/qrisp/examples/QIROMaxCliqueExample.py b/src/qrisp/examples/QIROMaxCliqueExample.py index 3ddc6b3c..9975ee4a 100644 --- a/src/qrisp/examples/QIROMaxCliqueExample.py +++ b/src/qrisp/examples/QIROMaxCliqueExample.py @@ -2,9 +2,7 @@ from qrisp import QuantumVariable import matplotlib.pyplot as plt import networkx as nx -from qrisp.qiro import QIROProblem, qiro_init_function, qiro_RXMixer, create_maxClique_cost_operator_reduced, create_maxClique_replacement_routine -from qrisp.qaoa import QAOAProblem, RX_mixer, maxCliqueCostfct, maxCliqueCostOp - +from qrisp.algorithms.qiro import * # First we define a graph via the number of nodes and the QuantumVariable arguments num_nodes = 15 @@ -20,15 +18,15 @@ } #assign cost_function and maxclique_instance, normal QAOA -testCostFun = maxCliqueCostfct(Gtwo) -maxclique_instance = QAOAProblem(maxCliqueCostOp(G), RX_mixer, maxCliqueCostfct(G)) +testCostFun = create_max_clique_cl_cost_function(Gtwo) +maxclique_instance = QAOAProblem(create_max_indep_set_mixer(G), RX_mixer, create_max_clique_cl_cost_function(G)) # assign the correct new update functions for qiro from above imports qiro_instance = QIROProblem(problem = Gtwo, - replacement_routine = create_maxClique_replacement_routine, - cost_operator = create_maxClique_cost_operator_reduced, + replacement_routine = create_max_clique_replacement_routine, + cost_operator = create_max_clique_cost_operator_reduced, mixer = qiro_RXMixer, - cl_cost_function = maxCliqueCostfct, + cl_cost_function = create_max_clique_cl_cost_function, init_function = qiro_init_function ) diff --git a/src/qrisp/examples/QIROMaxIndepExample.py b/src/qrisp/examples/QIROMaxIndepExample.py index acac239b..5b4ef93b 100644 --- a/src/qrisp/examples/QIROMaxIndepExample.py +++ b/src/qrisp/examples/QIROMaxIndepExample.py @@ -1,6 +1,5 @@ # imports -from qrisp.qaoa.problems.maxIndepSetInfrastr import maxIndepSetclCostfct, maxIndepSetCostOp -from qrisp.qiro import QIROProblem, qiro_init_function, qiro_RXMixer, create_maxIndep_replacement_routine, create_maxIndep_cost_operator_reduced +from qrisp.algorithms.qiro import * from qrisp import QuantumVariable import networkx as nx @@ -17,12 +16,12 @@ } # assign the correct new update functions for qiro from above imports -qiro_instance = QIROProblem(G, - replacement_routine=create_maxIndep_replacement_routine, - cost_operator= create_maxIndep_cost_operator_reduced, - mixer= qiro_RXMixer, - cl_cost_function= maxIndepSetclCostfct, - init_function= qiro_init_function +qiro_instance = QIROProblem(G, + replacement_routine=create_max_indep_replacement_routine, + cost_operator=create_max_indep_cost_operator_reduced, + mixer=qiro_RXMixer, + cl_cost_function=create_max_indep_set_cl_cost_function, + init_function=qiro_init_function ) # We run the qiro instance and get the results! @@ -33,7 +32,7 @@ # Lets see what the 5 best results are print("QIRO 5 best results") maxfive = sorted(res_qiro, key=res_qiro.get, reverse=True)[:5] -costFunc = maxIndepSetclCostfct(G) +costFunc = create_max_indep_set_cl_cost_function(G) for key, val in res_qiro.items(): if key in maxfive: diff --git a/src/qrisp/examples/QIROMaxSatExample.py b/src/qrisp/examples/QIROMaxSatExample.py deleted file mode 100644 index 70217bf8..00000000 --- a/src/qrisp/examples/QIROMaxSatExample.py +++ /dev/null @@ -1,53 +0,0 @@ -# imports -from qrisp.qaoa.problems.maxSatInfrastr import maxSatclCostfct, clausesdecoder -from qrisp.qiro import QIROProblem, qiro_init_function, qiro_RXMixer, create_maxSat_replacement_routine, create_maxSat_cost_operator_reduced -from qrisp import QuantumVariable -import matplotlib.pyplot as plt -import networkx as nx - -# define the problem according to maxSat encoding -problem = [8 , [[1,2,-3],[1,4,-6], [4,5,6],[1,3,-4],[2,4,5],[1,3,5],[-2,-3,6], [1,7,8], [3,-7,-8], [3,4,8],[4,5,8], [1,2,7]]] -decodedClauses = clausesdecoder( problem) - -qarg = QuantumVariable(problem[0]) - -# set simulator shots -mes_kwargs = { - #below should be 5k - "shots" : 5000 - } - - -# assign cost_function and maxclique_instance, normal QAOA -testCostFun = maxSatclCostfct(problem) - -# assign the correct new update functions for qiro from above imports -qiro_instance = QIROProblem(problem = problem, - replacement_routine = create_maxSat_replacement_routine, - cost_operator = create_maxSat_cost_operator_reduced, - mixer = qiro_RXMixer, - cl_cost_function = maxSatclCostfct, - init_function = qiro_init_function - ) - - -# We run the qiro instance and get the results! -res_qiro = qiro_instance.run_qiro(qarg=qarg, depth = 3, n_recursions = 2, - #mes_kwargs = mes_kwargs - ) - - -print("QIRO 5 best results") -maxfive = sorted(res_qiro, key=res_qiro.get, reverse=True)[:5] -for key, val in res_qiro.items(): - if key in maxfive: - - print(key) - print(testCostFun({key:1})) - -# or compare it with the networkx result of the max_clique algorithm... -# or a write brute force solution, this is up to you - -# and finally, we print the final clauses! -final_clauses = qiro_instance.problem -print(final_clauses) diff --git a/src/qrisp/examples/QIROMaxSetPackTrafoToMIS.py b/src/qrisp/examples/QIROMaxSetPackTrafoToMIS.py index 576b4176..1a55b011 100644 --- a/src/qrisp/examples/QIROMaxSetPackTrafoToMIS.py +++ b/src/qrisp/examples/QIROMaxSetPackTrafoToMIS.py @@ -1,6 +1,4 @@ - -from qrisp.qaoa.problems.maxIndepSetInfrastr import maxIndepSetclCostfct, maxIndepSetCostOp -from qrisp.qiro import QIROProblem, trafo_maxPackToMIS, qiro_init_function, qiro_RXMixer, create_maxIndep_replacement_routine, create_maxIndep_cost_operator_reduced +from qrisp.algorithms.qiro import * from qrisp import QuantumVariable import networkx as nx @@ -27,7 +25,7 @@ problem = [sol, sets] print(sets) -G = trafo_maxPackToMIS(problem=problem) +G = transform_max_set_pack_to_mis(problem=problem) qarg = QuantumVariable(G.number_of_nodes()) # set simulator shots @@ -37,12 +35,12 @@ } # assign the correct new update functions for qiro from above imports -qiro_instance = QIROProblem(G, - replacement_routine=create_maxIndep_replacement_routine, - cost_operator= create_maxIndep_cost_operator_reduced, - mixer= qiro_RXMixer, - cl_cost_function= maxIndepSetclCostfct, - init_function= qiro_init_function +qiro_instance = QIROProblem(G, + replacement_routine=create_max_indep_replacement_routine, + cost_operator=create_max_indep_cost_operator_reduced, + mixer=qiro_RXMixer, + cl_cost_function=create_max_indep_set_cl_cost_function, + init_function=qiro_init_function ) # We run the qiro instance and get the results! @@ -53,7 +51,7 @@ # Lets see what the 5 best results are print("QIRO 5 best results") maxfive = sorted(res_qiro, key=res_qiro.get, reverse=True)[:5] -costFunc = maxIndepSetclCostfct(G) +costFunc = create_max_indep_set_cl_cost_function(G) for key, val in res_qiro.items(): if key in maxfive: diff --git a/src/qrisp/examples/QIROTestMaxSat.py b/src/qrisp/examples/QIROTestMaxSat.py deleted file mode 100644 index 4c37fd6b..00000000 --- a/src/qrisp/examples/QIROTestMaxSat.py +++ /dev/null @@ -1,58 +0,0 @@ -# imports -from qrisp.qiro.qiro_problem import QIROProblem -from qrisp.qaoa.problems.maxSatInfrastr import maxSatclCostfct, clausesdecoder -from qrisp.qiro.qiroproblems.qiroMaxSatInfrastr import * -from qrisp.qiro.qiro_mixers import qiro_init_function, qiro_RXMixer -from qrisp import QuantumVariable -import matplotlib.pyplot as plt -import networkx as nx - - -problem = [6,[[1,2,-3],[1,4,-6], [4,5,6],[1,3,-4],[2,4,5],[1,3,5],[-2,-3,6]]] - - -decodedClauses = clausesdecoder( problem) -qarg = QuantumVariable(problem[0]) - -# set simulator shots -mes_kwargs = { - #below should be 5k - "shots" : 5000 - } - - -#assign cost_function and maxclique_instance, normal QAOA -testCostFun = maxSatclCostfct(problem) - -# assign the correct new update functions for qiro from above imports -qiro_instance = QIROProblem(problem = problem, - replacement_routine = create_maxSat_replacement_routine, - cost_operator = create_maxSat_cost_operator_reduced, - mixer = qiro_RXMixer, - cl_cost_function = maxSatclCostfct, - init_function = qiro_init_function - ) - - -# We run the qiro instance and get the results! -res_qiro = qiro_instance.run_qiro(qarg=qarg, depth = 3, n_recursions = 2, - #mes_kwargs = mes_kwargs - ) - -# and also the final graph, that has been adjusted -final_clauses = qiro_instance.problem - - -print("QIRO 5 best results") -maxfive = sorted(res_qiro, key=res_qiro.get, reverse=True)[:5] -for key, val in res_qiro.items(): - if key in maxfive: - - print(key) - print(testCostFun({key:1})) - -# or compare it with the networkx result of the max_clique algorithm... -# write brute force solution - -# and finally, we draw the final graph and the original graphs to compare them! -print(final_clauses) \ No newline at end of file diff --git a/tests/test_QAOAMkCS.py b/tests/test_QAOAMkCS.py index 4703cb3f..7d9658ab 100644 --- a/tests/test_QAOAMkCS.py +++ b/tests/test_QAOAMkCS.py @@ -24,6 +24,7 @@ from qrisp.default_backend import def_backend + qrisp_sim = def_backend qaoa_backend = qrisp_sim @@ -107,7 +108,7 @@ def G1e2c_bin(): def test_mkcs_5nodes(): G = nx.Graph() - G.add_edges_from([[0,1],[0,4],[1,2],[1,3],[1,4],[2,3],[3,4]]) + G.add_edges_from([[0,1],[0,4],[1,2],[1,4],[2,3]]) num_nodes = len(G.nodes) @@ -158,11 +159,11 @@ def G_bin(): if all(res_str_bin[pair[0]] != res_str_bin[pair[1]] for pair in list(G.edges())): return True - for _ in range(5): + for _ in range(8): if G_bin() == True: break - else: - assert False + else: + assert False # bin = G_bin @@ -171,10 +172,6 @@ def G_bin(): - - - - # ONE HOT