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.
Solving with a quantum approach
Section titled “Solving with a quantum approach”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.
Available backend types and devices
Section titled “Available backend types and devices”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:
- Local emulators (external) (Qutip, Emu_mps, Emu_sv, ...),
- Remote emulators (external), which can be accessed via
pasqal_cloud(external), - A remote QPU, such as Fresnel (external).
A backend will use device specifications to perform quantum computations. The list of supported devices can be found in the QoolQit devices documentation (external).
Running locally with an emulator
Section titled “Running locally with an emulator”We can perform quantum simulations locally via an emulator (here, we choose the QUTIP emulator).
import torchfrom qubosolver import QUBOInstancefrom qubosolver.config import SolverConfig, LocalEmulatorfrom qubosolver.solver import QuboSolverfrom pulser_simulation import QutipBackendV2from emu_sv import SVBackendfrom emu_mps import MPSBackend
locals_bkds = [ LocalEmulator(backend_type=btype, runs=500) for btype in [ QutipBackendV2, SVBackend, MPSBackend, ]]
# define QUBOQ = torch.tensor([[1.0, 0.0], [0.0, 1.0]])instance = QUBOInstance(coefficients=Q)
# Create a SolverConfig object to use a quantum backendconfig = SolverConfig(use_quantum=True, backend = locals_bkds[0])
# Instantiate the quantum solver.solver = QuboSolver(instance, config)
# Solve the QUBO problem.solution = solver.solve()Running with a remote connection
Section titled “Running with a remote connection”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).
On a real QPU
Section titled “On a real QPU”The code above can be modified to solve the QUBO instance using our real QPU remotely as follows:
import torchfrom qubosolver import QUBOInstancefrom qubosolver.config import SolverConfig, QPUfrom qubosolver.solver import QuboSolver
# define QUBOQ = torch.tensor([[1.0, 0.0], [0.0, 1.0]])instance = QUBOInstance(coefficients=Q)
# define credentialsUSERNAME='#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()On a remote emulators
Section titled “On a remote emulators”Emulators are also available remotely:
import torchfrom qubosolver import QUBOInstancefrom qubosolver.config import SolverConfig, RemoteEmulator, PasqalCloudfrom qubosolver.solver import QuboSolver
# define QUBOQ = torch.tensor([[1.0, 0.0], [0.0, 1.0]])instance = QUBOInstance(coefficients=Q)
# define credentialsUSERNAME='#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()Solving with a classical approach
Section titled “Solving with a classical approach”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 torchfrom qubosolver import QUBOInstancefrom qubosolver.solver import QuboSolverfrom qubosolver.config import ClassicalConfig, SolverConfigfrom qubosolver.solver import QuboSolverClassical, QuboSolverQuantum
# define QUBOQ = 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()