Skip to content

Commit

Permalink
Merge pull request #239 from GiacomoFrn/brute_force_solver
Browse files Browse the repository at this point in the history
Partial hotfix of issue #234: Brute force solver as OpenQAOA workflow class method
  • Loading branch information
Q-lds authored Jun 5, 2023
2 parents d82f2a7 + 3fb58b0 commit 28d8030
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/openqaoa-core/algorithms/qaoa/qaoa_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ...qaoa_components.variational_parameters.variational_baseparams import (
QAOAVariationalBaseParams,
)
from ...utilities import get_mixer_hamiltonian, generate_timestamp
from ...utilities import get_mixer_hamiltonian, generate_timestamp, ground_state_hamiltonian
from ...optimizers.qaoa_optimizer import get_optimizer


Expand Down Expand Up @@ -301,6 +301,38 @@ def compile(
)

return None

def solve_brute_force(self, bounded=True, verbose=False):
"""
A method to solve the QAOA problem using brute force i.e. by
evaluating the cost function at all possible bitstrings
Parameters
----------
bounded: `bool`, optional
If set to True, the function will not perform computations for qubit
numbers above 25. If False, the user can specify any number. Defaults
to True.
verbose: `bool`, optional
If set to True, the function will print the results of the computation.
Defaults to False.
"""

if self.compiled is False:
raise ValueError("Please compile the QAOA before running the brute force solver!")

# compute the exact ground state and ground state energy of the cost hamiltonian
energy, configuration = ground_state_hamiltonian(self.cost_hamil, bounded=bounded)

if verbose:
print(f"Ground State energy: {energy}, Solution: {configuration}")

self.brute_force_results = {
"energy": energy,
"configuration": configuration,
}

return None

def optimize(self, verbose=False):
"""
Expand Down Expand Up @@ -475,3 +507,4 @@ def _serializable_dict(
]["circuit_properties"]["q"]

return serializable_dict

0 comments on commit 28d8030

Please sign in to comment.