Skip to content

Commit

Permalink
furhter restructuring and bugFixes for all files
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik-SteinFraunh committed Oct 31, 2024
1 parent 70382d6 commit 6f73003
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 49 deletions.
6 changes: 2 additions & 4 deletions src/qrisp/algorithms/qiro/qiro_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(self, problem, replacement_routine , cost_operator, mixer, cl_cost_
self.qiro_cost_operator = cost_operator
self.qiro_mixer = mixer

self.problem = problem
self.problem = copy.deepcopy(problem)
self.replacement_routine = replacement_routine

self.init_function = init_function()
Expand Down Expand Up @@ -172,9 +172,7 @@ def run_qiro(self, qarg, depth, n_recursions, mes_kwargs = {}, max_iter = 50):
for index in range(n_recursions):

new_problem, solutions , sign, exclusions = self.replacement_routine(res, [self.problem, solutions, exclusions])
print("sols+excl")
print(solutions)
print(exclusions)

corr_vals.append(sign)
self.problem = new_problem

Expand Down
8 changes: 4 additions & 4 deletions src/qrisp/algorithms/qiro/qiroproblems/qiroMaxClique.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ def create_max_clique_replacement_routine(res, problem_updated):

# we just directly remove vertices from the graph
if isinstance(max_item, int):
if sign > 0:
if sign < 0:
border = list(graph.adj[max_item].keys())
border.append(max_item)
to_remove = [int(item) for item in graph.nodes() if item not in border]
new_graph.remove_nodes_from( [item for item in graph.nodes() if item not in border])
new_graph.remove_nodes_from( to_remove)
solutions.append(max_item)
exclusions += to_remove

elif sign < 0:
elif sign > 0:
#remove item
new_graph.remove_node(max_item)
exclusions.append(max_item)
Expand All @@ -99,7 +99,7 @@ def create_max_clique_replacement_routine(res, problem_updated):
union.append(max_item[1])
to_remove = [int(item) for item in graph.nodes() if item not in union]
#to_delete = [item for item in graph.nodes() if item not in union]
new_graph.remove_nodes_from([item for item in graph.nodes() if item not in union])
new_graph.remove_nodes_from(to_remove)
exclusions += to_remove

return new_graph, solutions, sign, exclusions
Expand Down
9 changes: 5 additions & 4 deletions src/qrisp/algorithms/qiro/qiroproblems/qiroMaxIndepSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def create_max_indep_replacement_routine(res, problem_updated):
# for single qubit correlations
orig_nodes = list(graph.nodes())


max_item, sign = find_max(orig_nodes, orig_edges , res, solutions)
if max_item == None:
return graph, solutions, 0 ,exclusions
Expand All @@ -71,14 +70,15 @@ def create_max_indep_replacement_routine(res, problem_updated):
# we remove nodes from the graph, as suggested by the replacement rules
# if the item is an int, it is a single node, else it is an edge
if isinstance(max_item, int):
if sign > 0:
# if sign <0 then its mostly appeared as a "1" in the results--> part of solution set
if sign < 0:
# remove all adjacent nodes
to_remove = list(graph.adj[max_item])
new_graph.remove_nodes_from(to_remove)
solutions.append(max_item)
exclusions += to_remove

elif sign < 0:
# if sign >0 then its mostly appeared as a "0" in the results
elif sign > 0:
# remove the node
new_graph.remove_node(max_item)
exclusions.append(max_item)
Expand Down Expand Up @@ -132,6 +132,7 @@ def cost_operator(qv, gamma):
return cost_operator



def create_max_indep_controlled_mixer_reduced(problem_updated):
r"""
Creates the ``controlled_RX_mixer`` for a QIRO instance of the maximal independet set problem for a given graph ``G`` following `Hadfield et al. <https://arxiv.org/abs/1709.03489>`_
Expand Down
31 changes: 6 additions & 25 deletions src/qrisp/algorithms/qiro/qiroproblems/qiroMaxSat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import copy
from qrisp.algorithms.qiro.qiroproblems.qiro_utils import *

from itertools import combinations


def create_maxsat_replacement_routine(res, problem_updated):
"""
Expand Down Expand Up @@ -63,15 +65,9 @@ def create_maxsat_replacement_routine(res, problem_updated):
# FOR SINGLE QUBIT CORRELATIONS
# make -1 here for consistency in the find max
orig_nodes = [i - 1 for i in list(range(1,numVars + 1)) if not i in exclusions]
combinations = []

# add check for solution nodes
for index1 in range(len(orig_nodes)-1):
for index2 in range(index1, len(orig_nodes)):
combinations.append((orig_nodes[index1],orig_nodes[index2]))

combinations_list = combinations(enumerate(orig_nodes), 2)

max_item, sign = find_max(orig_nodes, combinations, res, solutions)
max_item, sign = find_max(orig_nodes, combinations_list, res, solutions)
if max_item == None:
return problem, solutions, 0 ,exclusions

Expand All @@ -81,7 +77,7 @@ def create_maxsat_replacement_routine(res, problem_updated):
max_item += 1

for sgl_clause in clauses:
if sign > 0:
if sign < 0:
#clause evaluates to TRUE, can empty the clause
if max_item in sgl_clause:
sgl_clause.clear()
Expand All @@ -97,7 +93,7 @@ def create_maxsat_replacement_routine(res, problem_updated):
exclusions.append(max_item)

#same as above
elif sign < 0:
elif sign > 0:
if -1* max_item in sgl_clause:
sgl_clause.clear()
elif max_item in sgl_clause:
Expand All @@ -107,21 +103,7 @@ def create_maxsat_replacement_routine(res, problem_updated):
val = max_item
sgl_clause.remove(val)
exclusions.append(max_item)
""" if sign > 0:
for item in clauses:
# if number in item --> remove it --> clause is fulfilled
if max_item in item:
item.clear()
solutions.append(max_item)
exclusions.append(max_item)

elif sign < 0:
for item in clauses:
# if number in item --> remove the number
if -1* max_item in item:
val = -1* max_item
item.remove(val)
exclusions.append(max_item) """

else:
max_item[0] += 1
Expand All @@ -137,7 +119,6 @@ def create_maxsat_replacement_routine(res, problem_updated):
sgl_clause[temp] = -1* max_item[0]
exclusions.append(max_item[1])


elif sign < 0:
for sgl_clause in clauses:
# replace with neg. correlated number if its in an item
Expand Down
8 changes: 4 additions & 4 deletions src/qrisp/algorithms/qiro/qiroproblems/qiro_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def find_max(single_cor, double_cor, res, solutions):
The item with maximal correlation and the sign of the correlation.
"""

max = 0
max_item = None
sign = None
Expand All @@ -51,13 +51,13 @@ def find_max(single_cor, double_cor, res, solutions):

# calc correlation expectation
for key, val in res.items():
summe += pow(val, 2) * pow(-1, int(key[int(abs(item2[0]))])) * pow(-1, int(key[int(abs(item2[1]))]))
summe += val * pow(-1, int(key[int(abs(item2[0]))])) * pow(-1, int(key[int(abs(item2[1]))]))

#find max
if abs(summe) > abs(max):
max, max_item = summe, item2
sign = np.sign(summe)

for node in single_cor:
if node in solutions:
continue
Expand All @@ -69,5 +69,5 @@ def find_max(single_cor, double_cor, res, solutions):
if abs(summe) > abs(max):
max, max_item = summe, node
sign = np.sign(summe)

return max_item, sign
11 changes: 3 additions & 8 deletions src/qrisp/examples/QIROMaxIndepExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@


# First we define a graph via the number of nodes and the QuantumVariable arguments
num_nodes = 13
G = nx.erdos_renyi_graph(num_nodes, 0.4, seed = 107)
num_nodes = 18
G = nx.erdos_renyi_graph(num_nodes, 0.3, seed = 177)
qarg = QuantumVariable(G.number_of_nodes())

# set simulator shots
mes_kwargs = {
#below should be 5k
"shots" : 5000
}

# assign the correct new update functions for qiro from above imports
qiro_instance = QIROProblem(G,
Expand All @@ -25,7 +20,7 @@
)

# 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)
res_qiro = qiro_instance.run_qiro(qarg=qarg, depth = 3, n_recursions = 2)
# and also the final graph, that has been adjusted
final_Graph = qiro_instance.problem

Expand Down

0 comments on commit 6f73003

Please sign in to comment.