Skip to content
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

Feature/ibm sessions #80

Merged
merged 2 commits into from
Apr 17, 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.out
__pycache__/
.idea/
results/
results/
.env
26 changes: 9 additions & 17 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
from src.solver.qubo.DWaveSolver import DWaveSolver
from dotenv import load_dotenv

from src.solver.qubo.CplexSolver import CplexSolver, get_backend_sampler

if __name__ == "__main__":
cvrp = DWaveSolver(
2,
load_dotenv()

cvrp = CplexSolver(
1,
None,
[
(456, 320),
(228, 0),
(912, 0),
(0, 80),
(114, 80),
(570, 160),
(798, 160),
(342, 240),
],
[
(2, 1, 6),
(3, 0, 5),
(4, 5, 8),
(6, 7, 2),
(0, 1, 6),
],
True,
True,
# sampler=DWaveSampler(),
# num_reads=3500,
# time_limit=10,
sampler=get_backend_sampler(),
)
result = cvrp.solve()
result.display()
29 changes: 28 additions & 1 deletion src/solver/qubo/CplexSolver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import os

from docplex.util.status import JobSolveStatus
from numpy import ndarray
from qiskit.primitives import Sampler
from qiskit_algorithms import QAOA
from qiskit_algorithms.optimizers import COBYLA, Optimizer
from qiskit_ibm_runtime import (
QiskitRuntimeService,
Session,
SamplerV1 as CloudSampler,
)
from qiskit_optimization import QuadraticProgram
from qiskit_optimization.algorithms import (
CplexOptimizer,
Expand Down Expand Up @@ -50,7 +57,7 @@ def __init__(
classical_solver=False,
simplify=True,
track_progress=True,
sampler: Sampler = DEFAULT_SAMPLER,
sampler: Sampler | CloudSampler = DEFAULT_SAMPLER,
classic_optimizer: Optimizer = DEFAULT_CLASSIC_OPTIMIZER,
warm_start=False,
pre_solver: OptimizationAlgorithm = DEFAULT_PRE_SOLVER,
Expand All @@ -75,6 +82,7 @@ def _solve_cvrp(self) -> OptimizationResult:
"""
Solve the CVRP using QUBO implemented in Qiskit.
"""

qp = self.adapter.solver_model()

if self.classical_solver:
Expand Down Expand Up @@ -187,3 +195,22 @@ def build_var_dict(self, result: OptimizationResult) -> dict[str, float]:
if self.simplify:
var_dict = self.model.re_add_variables(var_dict)
return var_dict


def get_backend_sampler(
backend_name: str = None, channel: str = "ibm_quantum"
) -> CloudSampler:
"""
Get a Qiskit sampler for the specified backend.
Loads the IBM-Q account using the token from the environment variable.
"""

service = QiskitRuntimeService(token=os.getenv("IBM_TOKEN"), channel=channel)

if backend_name is None:
backend = service.least_busy(operational=True, simulator=False)
else:
backend = service.backend(backend_name)

session = Session(service, backend)
return CloudSampler(session=session)