Skip to content

Commit

Permalink
Minor updates QIRO implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
renezander90 committed Oct 17, 2024
1 parent c893cb8 commit d77873d
Showing 13 changed files with 43 additions and 222 deletions.
6 changes: 3 additions & 3 deletions documentation/source/general/tutorial/QIROtutorial.rst
Original file line number Diff line number Diff line change
@@ -193,7 +193,7 @@ We start off by defining the graph of our problem, as well as the :ref:`QuantumV
::

from qrisp import QuantumVariable
from qrisp.qiro import QIROProblem, create_maxIndep_replacement_routine, create_maxIndep_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qiro import QIROProblem, create_max_indep_replacement_routine, create_max_indep_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qaoa import create_max_indep_set_cl_cost_function
import matplotlib.pyplot as plt
import networkx as nx
@@ -210,8 +210,8 @@ With this, we can directly throw everything that is relevant at the :ref:`QIROPr

# assign the correct new update functions for qiro
qiro_instance = QIROProblem(G,
replacement_routine=create_maxIndep_replacement_routine,
cost_operator=create_maxIndep_cost_operator_reduced,
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
Original file line number Diff line number Diff line change
@@ -27,13 +27,13 @@ Based on the **maximum absolute entry** of the correlation matrix M and its sign
* If :math:`\text{M}_{ij} < 0, (i, j) ∈ E` was selected, we remove all vertices that **do not** share an edge with either vertex :math:`i` or :math:`j`. Since one of the vertices :math:`i` and :math:`j` will be part of the final solution (but not both), any vertex that is **not** connected to either :math:`i` or :math:`j` (or both) is guaranteed to violate the problem constraints, and can be removed from the graph.


.. autofunction:: create_maxClique_replacement_routine
.. autofunction:: create_max_clique_replacement_routine


QIRO Cost operator
------------------

.. autofunction:: create_maxClique_cost_operator_reduced
.. autofunction:: create_max_clique_cost_operator_reduced


Example implementation
@@ -42,7 +42,7 @@ Example implementation
::

from qrisp import QuantumVariable
from qrisp.qiro import QIROProblem, create_maxClique_replacement_routine, create_maxClique_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qiro import QIROProblem, create_max_clique_replacement_routine, create_max_clique_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qaoa import max_clique_problem, create_max_clique_cl_cost_function
import matplotlib.pyplot as plt
import networkx as nx
@@ -54,8 +54,8 @@ Example implementation

# QIRO
qiro_instance = QIROProblem(problem=G,
replacement_routine= reate_maxClique_replacement_routine,
cost_operator=create_maxClique_cost_operator_reduced,
replacement_routine= reate_max_clique_replacement_routine,
cost_operator=create_max_clique_cost_operator_reduced,
mixer=qiro_RXMixer,
cl_cost_function=create_max_clique_cl_cost_function,
init_function=qiro_init_function
@@ -65,7 +65,7 @@ Example implementation
# The final graph that has been adjusted
final_graph = qiro_instance.problem

That’s it! In the following, we print the 5 most likely solutions together with their cost values, and compare to the NetworkX solution
That’s it! In the following, we print the 5 most likely solutions together with their cost values, and compare to the NetworkX solution.

::

Original file line number Diff line number Diff line change
@@ -26,21 +26,21 @@ Based on the **maximum absolute entry** of the correlation matrix M and its sign
* If :math:`\text{M}_{ij} < 0, (i, j) ∈ E` was selected, we remove all vertices that share an edge with both vertices :math:`i` and :math:`j`. Since one of the vertices :math:`i` and :math:`j` will be part of the final solution (but not both), any vertex that is connected to both :math:`i` and :math:`j` is guaranteed to violate the problem constraints, and can be removed from the graph. In this case, it may be possible that no vertex is found to be a canditate for being removed. We will then simply choose the second biggest absolute value of **M** for the replacement routine.


.. autofunction:: create_maxIndep_replacement_routine
.. autofunction:: create_max_indep_replacement_routine


QIRO Cost operator
------------------

.. autofunction:: create_maxIndep_cost_operator_reduced
.. autofunction:: create_max_indep_cost_operator_reduced


Example implementation
----------------------
::

from qrisp import QuantumVariable
from qrisp.qiro import QIROProblem, create_maxIndep_replacement_routine, create_maxIndep_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qiro import QIROProblem, create_max_indep_replacement_routine, create_max_indep_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qaoa import create_max_indep_set_cl_cost_function
import matplotlib.pyplot as plt
import networkx as nx
@@ -51,8 +51,8 @@ Example implementation
qarg = QuantumVariable(G.number_of_nodes())

qiro_instance = QIROProblem(G,
replacement_routine=create_maxIndep_replacement_routine,
cost_operator=create_maxIndep_cost_operator_reduced,
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
Original file line number Diff line number Diff line change
@@ -51,13 +51,13 @@ Based on the **maximum absolute entry** of the correlation matrix M and its sign



.. autofunction:: create_maxSat_replacement_routine
.. autofunction:: create_maxsat_replacement_routine


QIRO Cost operator
------------------

.. autofunction:: create_maxSat_cost_operator_reduced
.. autofunction:: create_maxsat_cost_operator_reduced


Example implementation
@@ -67,16 +67,16 @@ Example implementation

from qrisp import QuantumVariable
from qrisp.qaoa import create_maxsat_cl_cost_function
from qrisp.qiro import QIROProblem, qiro_init_function, qiro_RXMixer, create_maxSat_replacement_routine, create_maxSat_cost_operator_reduced
from qrisp.qiro import QIROProblem, qiro_init_function, qiro_RXMixer, create_maxsat_replacement_routine, create_maxsat_cost_operator_reduced

clauses = [[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]]
num_vars = 8
problem = (num_vars, clauses)
qarg = QuantumVariable(num_vars)

qiro_instance = QIROProblem(problem = problem,
replacement_routine = create_maxSat_replacement_routine,
cost_operator = create_maxSat_cost_operator_reduced,
replacement_routine = create_maxsat_replacement_routine,
cost_operator = create_maxsat_cost_operator_reduced,
mixer = qiro_RXMixer,
cl_cost_function = create_maxsat_cl_cost_function,
init_function = qiro_init_function
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ Example implementation
::

from qrisp import QuantumVariable
from qrisp.qiro import QIROProblem, create_maxIndep_replacement_routine, create_maxIndep_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qiro import QIROProblem, create_max_indep_replacement_routine, create_max_indep_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qaoa import create_max_indep_set_cl_cost_function
import matplotlib.pyplot as plt
import networkx as nx
@@ -39,8 +39,8 @@ Example implementation
qarg = QuantumVariable(G.number_of_nodes())

qiro_instance = QIROProblem(G,
replacement_routine=create_maxIndep_replacement_routine,
cost_operator=create_maxIndep_cost_operator_reduced,
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
1 change: 0 additions & 1 deletion src/qrisp/algorithms/qiro/__init__.py
Original file line number Diff line number Diff line change
@@ -23,4 +23,3 @@
from qrisp.algorithms.qaoa.problems import *
from qrisp.algorithms.qiro.qiroproblems import *
from qrisp.algorithms.qiro.qiro_mixers import *
from qrisp.algorithms.qiro.qiro_problem import *
13 changes: 8 additions & 5 deletions src/qrisp/algorithms/qiro/qiro_problem.py
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ class QIROProblem(QAOAProblem):
::
from qrisp import QuantumVariable
from qrisp.qiro import QIROProblem, create_maxClique_replacement_routine, create_maxClique_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qiro import QIROProblem, create_max_clique_replacement_routine, create_max_clique_cost_operator_reduced, qiro_RXMixer, qiro_init_function
from qrisp.qaoa import max_clique_problem, create_max_clique_cl_cost_function
import matplotlib.pyplot as plt
import networkx as nx
@@ -53,8 +53,8 @@ class QIROProblem(QAOAProblem):
# QIRO
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 = create_max_clique_cl_cost_function,
init_function = qiro_init_function
@@ -87,8 +87,11 @@ class QIROProblem(QAOAProblem):
plt.title('final QIRO graph')
plt.figure(2)
nx.draw(G, with_labels = True, node_color='#ADD8E6', edge_color='#D3D3D3')
plt.title('Original graph')
most_likely = [index for index, value in enumerate(max_five_qiro[0]) if value == '1']
nx.draw(G, with_labels=True,
node_color=['#FFCCCB' if node in most_likely else '#ADD8E6' for node in G.nodes()],
edge_color=['#FFCCCB' if edge[0] in most_likely and edge[1] in most_likely else '#D3D3D3' for edge in G.edges()])
plt.title('Original graph with most likely QIRO solution')
plt.show()
20 changes: 2 additions & 18 deletions src/qrisp/algorithms/qiro/qiroproblems/qiroMaxClique.py
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@
from qrisp.algorithms.qiro.qiroproblems.qiro_utils import *


def create_maxClique_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.
@@ -102,7 +102,7 @@ def create_maxClique_replacement_routine(res, graph, solutions, exclusions):
return new_graph, solutions, sign, exclusions


def create_maxClique_cost_operator_reduced(graph, solutions=[]):
def create_max_clique_cost_operator_reduced(graph, solutions=[]):
r"""
Creates the ``cost_operator`` for the problem instance.
This operator is adjusted to consider qubits that were found to be a part of the problem solution.
@@ -132,19 +132,3 @@ def cost_operator(qv, gamma):

return cost_operator









"""
BM: 18 nodes, seed 99
0.5933101570400762
normal
0.08909071112167886
"""
4 changes: 2 additions & 2 deletions src/qrisp/algorithms/qiro/qiroproblems/qiroMaxIndepSet.py
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
from qrisp.algorithms.qiro.qiroproblems.qiro_utils import *


def create_maxIndep_replacement_routine(res, graph, solutions=[], exclusions=[]):
def create_max_indep_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.
@@ -93,7 +93,7 @@ def create_maxIndep_replacement_routine(res, graph, solutions=[], exclusions=[])
return new_graph, solutions, sign, exclusions


def create_maxIndep_cost_operator_reduced(graph, solutions=[]):
def create_max_indep_cost_operator_reduced(graph, solutions=[]):
r"""
Creates the ``cost_operator`` for the problem instance.
This operator is adjusted to consider qubits that were found to be a part of the problem solution.
4 changes: 2 additions & 2 deletions src/qrisp/algorithms/qiro/qiroproblems/qiroMaxSat.py
Original file line number Diff line number Diff line change
@@ -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.
@@ -120,7 +120,7 @@ def create_maxSat_replacement_routine(res, problem, solutions, exclusions):
return [numVars, clauses], solutions, sign, exclusions


def create_maxSat_cost_operator_reduced(problem, solutions=[]):
def create_maxsat_cost_operator_reduced(problem, solutions=[]):

"""
Creates the ``cost_operator`` for the problem instance.
Loading

0 comments on commit d77873d

Please sign in to comment.