Solving a Quadratic Unconstrained Binary Optimization instance
Solving a QUBO instance is straightforward with qubo-solver. We can directly use the QuboSolver class by providing a QUBOInstance with a given SolverConfig configuration.
SolverConfig specifies whether to use a classical approach or a quantum one. Note that SolverConfig comes with many options but the default ones can be used straightforwardly.
We have however more advanced tutorials on the quantum-related components to dive deeper into these advanced concepts.
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 of the documentation (external).
One main decision is about the backend (external), 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), 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 by default).
import torchfrom qubosolver import QUBOInstancefrom qubosolver.config import SolverConfig, LocalEmulatorfrom qubosolver.solver import QuboSolver
# define QUBOQ = torch.tensor([[-0.2, 0, 1.0], [0, 0,1.5], [1.0, 1.5, 0]])instance = QUBOInstance(coefficients=Q)
# Create a SolverConfig object to use a quantum backendconfig = SolverConfig(use_quantum=True, backend = LocalEmulator())
# Instantiate the quantum solver.solver = QuboSolver(instance, config)
# Solve the QUBO problem.solution = solver.solve()
# Display resultsprint(solution)QUBOSolution(bitstrings=tensor([[1., 0., 0.],
[0., 0., 1.],
[0., 1., 0.],
[0., 0., 0.]]), costs=tensor([-0.2000, 0.0000, 0.0000, 0.0000]), counts=tensor([30, 29, 31, 10]), probabilities=tensor([0.3000, 0.2900, 0.3100, 0.1000]), solution_status=<SolutionStatusType.UNPROCESSED: 'unprocessed'>)
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 (run only with your pasqal_cloud information):
import torchfrom qubosolver import QUBOInstancefrom qubosolver.config import SolverConfigfrom qubosolver.solver import QuboSolverfrom qubosolver.config import QPU, PasqalCloud
# Replace with your username, project id and password on the Pasqal Cloud.USERNAME="#TO_PROVIDE"PROJECT_ID="#TO_PROVIDE"PASSWORD=None
if PASSWORD is not None:
# define QUBO Q = torch.tensor([[-0.2, 0, 1.0], [0, 0,1.5], [1.0, 1.5, 0]]) instance = QUBOInstance(coefficients=Q)
connection = PasqalCloud( username=USERNAME, password=PASSWORD, project_id=PROJECT_ID, ) config = SolverConfig(use_quantum=True, backend=QPU(connection=connection)) # Run the solver solver = QuboSolver(instance, config) solutions = solver.solve()
# Display results print(solutions)On a remote emulators
Section titled “On a remote emulators”Emulators are also available remotely via pasqal_cloud:
import torchfrom qubosolver import QUBOInstancefrom qubosolver.config import SolverConfigfrom qubosolver.solver import QuboSolverfrom qubosolver.config import RemoteEmulator, PasqalCloud
# Replace with your username, project id and password on the Pasqal Cloud.USERNAME="#TO_PROVIDE"PROJECT_ID="#TO_PROVIDE"PASSWORD=None
if PASSWORD is not None:
# define QUBO Q = torch.tensor([[-0.2, 0, 1.0], [0, 0,1.5], [1.0, 1.5, 0]]) instance = QUBOInstance(coefficients=Q)
connection = PasqalCloud( username=USERNAME, password=PASSWORD, project_id=PROJECT_ID, ) config = SolverConfig(use_quantum=True, backend=RemoteEmulator(connection=connection)) # Run the solver solver = QuboSolver(instance, config) solutions = solver.solve()
# Display results print(solutions)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([[-0.2, 0, 1.0], [0, 0,1.5], [1.0, 1.5, 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()
# Display resultsprint(solution)QUBOSolution(bitstrings=tensor([[1., 0., 0.]]), costs=tensor([-0.2000]), counts=None, probabilities=None, solution_status=<SolutionStatusType.UNPROCESSED: 'unprocessed'>)