Skip to content

Qiro bug fix #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 0 additions & 115 deletions src/qrisp/algorithms/qaoa/problems/portfolio_test_run.py

This file was deleted.

5 changes: 4 additions & 1 deletion src/qrisp/algorithms/qiro/qiroproblems/qiroMaxClique.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://journals.aps.org/prxquantum/abstract/10.1103/PRXQuantum.5.020327>`_ for a description of the update rules.
Expand Down Expand Up @@ -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)

Expand Down
56 changes: 3 additions & 53 deletions src/qrisp/algorithms/qiro/qiroproblems/qiroMaxIndepSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
"""
4 changes: 3 additions & 1 deletion src/qrisp/algorithms/qiro/qiroproblems/qiroMaxSat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://journals.aps.org/prxquantum/abstract/10.1103/PRXQuantum.5.020327>`_ for a description of the update rules.
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions src/qrisp/algorithms/qiro/qiroproblems/qiro_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
14 changes: 6 additions & 8 deletions src/qrisp/examples/QIROMaxCliqueExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

Expand Down
17 changes: 8 additions & 9 deletions src/qrisp/examples/QIROMaxIndepExample.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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!
Expand All @@ -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:

Expand Down
53 changes: 0 additions & 53 deletions src/qrisp/examples/QIROMaxSatExample.py

This file was deleted.

20 changes: 9 additions & 11 deletions src/qrisp/examples/QIROMaxSetPackTrafoToMIS.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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!
Expand All @@ -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:

Expand Down
Loading
Loading