Skip to content
Pasqal Documentation

Solving a Quadratic Unconstrained Binary Optimization instance

Solving a QUBO instance is straightforward with qubo-solver. We can directly use the QuboSolver class by feeding instances of the QUBOInstance and SolverConfig classes. We can also specify whether to use a classical approach or a quantum one.

To use a quantum approach, several choices have to be made regarging the configuration, explained in more details in the SolverConfig section. One main decision is about the backend, that is how we choose to perform quantum runs. We can decide to either perform our on emulators (locally, or remotely) or using a real quantum processing unit (QPU). Our QPU, based on the Rydberg Analog Model, is accessible remotely.

The supported backends are available via Qooqit (external), a Python package designed for algorithm development in the Rydberg Analog Model.

The backends can be divided into 3 main categories:

A backend will use device specifications to perform quantum computations. The list of supported devices can be found in the QoolQit devices documentation (external).

We can perform quantum simulations locally via an emulator (here, we choose the QUTIP emulator).

import torch
from qubosolver import QUBOInstance
from qubosolver.config import SolverConfig, LocalEmulator
from qubosolver.solver import QuboSolver
from pulser_simulation import QutipBackendV2
from emu_sv import SVBackend
from emu_mps import MPSBackend
locals_bkds = [
LocalEmulator(backend_type=btype, runs=500)
for btype in [
QutipBackendV2,
SVBackend,
MPSBackend,
]
]
# define QUBO
Q = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
instance = QUBOInstance(coefficients=Q)
# Create a SolverConfig object to use a quantum backend
config = SolverConfig(use_quantum=True, backend = locals_bkds[0])
# Instantiate the quantum solver.
solver = QuboSolver(instance, config)
# Solve the QUBO problem.
solution = solver.solve()

We can decide to perform our runs remotely via pasqal_cloud (external). To do so, we have to provide several information after setting up an account (external).

The code above can be modified to solve the QUBO instance using our real QPU remotely as follows:

import torch
from qubosolver import QUBOInstance
from qubosolver.config import SolverConfig, QPU
from qubosolver.solver import QuboSolver
# define QUBO
Q = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
instance = QUBOInstance(coefficients=Q)
# define credentials
USERNAME='#TO_PROVIDE'
PROJECT_ID='#TO_PROVIDE'
PASSWORD= None
if PASSWORD is not None:
connection = PasqalCloud(
username=USERNAME,
password=PASSWORD,
project_id=PROJECT_ID,
)
# define a remote backend
backendconf = QPU(connection=connection)
# Instantiate the quantum solver.
solver = QuboSolver(instance, backend=backendconf)
# Solve the QUBO problem.
solution = solver.solve()

Emulators are also available remotely:

import torch
from qubosolver import QUBOInstance
from qubosolver.config import SolverConfig, RemoteEmulator, PasqalCloud
from qubosolver.solver import QuboSolver
# define QUBO
Q = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
instance = QUBOInstance(coefficients=Q)
# define credentials
USERNAME='#TO_PROVIDE'
PROJECT_ID='#TO_PROVIDE'
PASSWORD= None
if PASSWORD is not None:
connection = PasqalCloud(
username=USERNAME,
password=PASSWORD,
project_id=PROJECT_ID,
)
# define a remote backend
backendconf = RemoteEmulator(connection=connection)
# Instantiate the quantum solver.
solver = QuboSolver(instance, backend=backendconf)
# Solve the QUBO problem.
solution = solver.solve()

We show below an example of solving a QUBO using CPLEX. More information on classical approaches can be found in the Classical solvers section of the Contents documentation.

import torch
from qubosolver import QUBOInstance
from qubosolver.solver import QuboSolver
from qubosolver.config import ClassicalConfig, SolverConfig
from qubosolver.solver import QuboSolverClassical, QuboSolverQuantum
# define QUBO
Q = torch.tensor([[1.0, 0.0], [0.0, 1.0]])
instance = QUBOInstance(coefficients=Q)
# Create a SolverConfig object with classical solver options.
classical_config = ClassicalConfig(
classical_solver_type="cplex",
cplex_maxtime=10.0,
cplex_log_path="test_solver.log",
)
config = SolverConfig(use_quantum=False, classical=classical_config)
# Instantiate the classical solver via the pipeline's classical solver dispatcher.
classical_solver = QuboSolver(instance, config)
# Solve the QUBO problem.
solution = classical_solver.solve()