-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import copy | ||
import sys | ||
|
||
from qiskit.algorithms.minimum_eigensolvers import QAOA | ||
from qiskit.algorithms.optimizers import COBYLA | ||
from qiskit.primitives import Sampler | ||
from qiskit_optimization import QuadraticProgram | ||
from qiskit_optimization.algorithms import MinimumEigenOptimizer | ||
from qiskit_optimization.problems import VarType | ||
|
||
if len(sys.argv) != 3: | ||
raise TypeError('This script expects exactly 2 arguments. Input file (argument 1) and output file (argument 2).') | ||
|
||
input_path = sys.argv[1] | ||
output_path = sys.argv[2] | ||
|
||
qp = QuadraticProgram() | ||
qp.read_from_lp_file(input_path) | ||
|
||
|
||
def relax_problem(problem): | ||
"""Change all variables to continuous.""" | ||
relaxed_problem = copy.deepcopy(problem) | ||
for variable in relaxed_problem.variables: | ||
variable.vartype = VarType.CONTINUOUS | ||
|
||
return relaxed_problem | ||
|
||
qubo = qp | ||
# qubo = relax_problem(QuadraticProgramToQubo().convert(qp)) | ||
# print(qp.prettyprint()) | ||
|
||
qaoa_mes = QAOA(Sampler(), optimizer=COBYLA(), initial_point=[0.0, 1.0]) | ||
|
||
qaoa = MinimumEigenOptimizer(qaoa_mes) | ||
|
||
qaoa_result = qaoa.solve(qubo) | ||
print(qaoa_result.prettyprint()) | ||
|
||
f = open(output_path, 'w') | ||
f.write(qaoa_result.prettyprint()) | ||
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,5 @@ | |
numpy | ||
pygmlparser | ||
qiskit == 0.44.* | ||
qiskit_optimization | ||
qiskit_optimization[cplex] | ||
qiskit-aer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/edu/kit/provideq/toolbox/qubo/QuboMetaSolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package edu.kit.provideq.toolbox.qubo; | ||
|
||
import edu.kit.provideq.toolbox.meta.MetaSolver; | ||
import edu.kit.provideq.toolbox.meta.Problem; | ||
import edu.kit.provideq.toolbox.meta.ProblemType; | ||
import edu.kit.provideq.toolbox.meta.setting.MetaSolverSetting; | ||
import edu.kit.provideq.toolbox.qubo.solvers.QiskitQuboSolver; | ||
import edu.kit.provideq.toolbox.qubo.solvers.QuboSolver; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Simple {@link MetaSolver} for MaxCut problems. | ||
*/ | ||
@Component | ||
public class QuboMetaSolver extends MetaSolver<String, String, QuboSolver> { | ||
|
||
@Autowired | ||
public QuboMetaSolver(QiskitQuboSolver qiskitQuboSolver) { | ||
super(ProblemType.QUBO, qiskitQuboSolver); | ||
} | ||
|
||
@Override | ||
public QuboSolver findSolver( | ||
Problem<String> problem, | ||
List<MetaSolverSetting> metaSolverSettings) { | ||
return (new ArrayList<>(this.solvers)).get((new Random()).nextInt(this.solvers.size())); | ||
} | ||
|
||
@Override | ||
public List<String> getExampleProblems() { | ||
return List.of(""" | ||
Maximize | ||
3x + y | ||
Subject To | ||
Binary | ||
x y | ||
End | ||
"""); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/edu/kit/provideq/toolbox/qubo/SolveQuboRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package edu.kit.provideq.toolbox.qubo; | ||
|
||
import edu.kit.provideq.toolbox.SolveRequest; | ||
|
||
/** | ||
* POST Requests to /solve/qubo should have a response body of this form. | ||
* The needed formula the qubo formula to solve in the | ||
* <a href="https://www.gurobi.com/documentation/current/refman/lp_format.html">LP format</a>. | ||
*/ | ||
public class SolveQuboRequest extends SolveRequest<String> { | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/edu/kit/provideq/toolbox/qubo/solvers/QiskitQuboSolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package edu.kit.provideq.toolbox.qubo.solvers; | ||
|
||
import edu.kit.provideq.toolbox.PythonProcessRunner; | ||
import edu.kit.provideq.toolbox.Solution; | ||
import edu.kit.provideq.toolbox.SubRoutinePool; | ||
import edu.kit.provideq.toolbox.meta.Problem; | ||
import edu.kit.provideq.toolbox.meta.ProblemType; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class QiskitQuboSolver extends QuboSolver { | ||
private final String quboPath; | ||
private final ApplicationContext context; | ||
|
||
@Autowired | ||
public QiskitQuboSolver( | ||
@Value("${qiskit.directory.qubo}") String quboPath, | ||
ApplicationContext context) { | ||
this.quboPath = quboPath; | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Qiskit Qubo"; | ||
} | ||
|
||
@Override | ||
public boolean canSolve(Problem<String> problem) { | ||
return problem.type() == ProblemType.QUBO; | ||
} | ||
|
||
@Override | ||
public void solve(Problem<String> problem, Solution<String> solution, | ||
SubRoutinePool subRoutinePool) { | ||
// Run Qiskit solver via console | ||
var processResult = context | ||
.getBean( | ||
PythonProcessRunner.class, | ||
quboPath, | ||
"qubo_qiskit.py") | ||
.addProblemFilePathToProcessCommand() | ||
.addSolutionFilePathToProcessCommand() | ||
.problemFileName("problem.lp") | ||
.run(problem.type(), solution.getId(), problem.problemData()); | ||
|
||
// Return if process failed | ||
if (!processResult.success()) { | ||
solution.setDebugData(processResult.output()); | ||
solution.abort(); | ||
return; | ||
} | ||
|
||
solution.setSolutionData(processResult.output()); | ||
solution.complete(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/edu/kit/provideq/toolbox/qubo/solvers/QuboSolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package edu.kit.provideq.toolbox.qubo.solvers; | ||
|
||
import edu.kit.provideq.toolbox.meta.ProblemSolver; | ||
|
||
public abstract class QuboSolver implements ProblemSolver<String, String> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters