Accessing LunaSolve's Algorithms

In this tutorial, we'll explore how to access LunaSolve's optimization algorithms from LunaBench, providing access to a broad range of pre-implemented algorithms not directly available in the LunaBench SDK.

Before we begin, ensure you have installed the LunaBench Python SDK.

Upon completing this tutorial, you will be able to:

  1. Connect to LunaSolve from within LunaBench.
  2. Select algorithms from LunaSolve to be used in your LunaBench pipeline.
  3. Run a benchmark pipeline with algorithms from various sources.

1. Creating the Dataset

To start, we'll begin with assembling a straightforward dataset. For the purposes of this tutorial, our focus will be on crafting a basic dataset comprised of QUBOs.

# Define the problem type as qubo
problem_name = "qubo"

# Define the dataset
dataset = {
    "qubo_00": {
        problem_name: [
            [2, 1],
            [1, 2]
        ]
    },
    "qubo_01": {
        problem_name: [
            [-2, 3],
            [3, -2]
        ]
    },
    "qubo_02": {
        problem_name: [
            [2, 1, 0],
            [1, 2, 1],
            [0, 1, 2]
        ]
    },
}

2. Connecting to LunaSolve

LunaBench allows you to use the algorithms from LunaSolve by accessing the platform as a backend and running the algorithms on its servers. To access LunaSolve, you will need an API key, which can be obtained by logging into the platform through LunaBench.

from lunabench.utils.luna.authentication import get_bearer_token

luna_token = get_bearer_token(
    email="<EMAIL>",
    password="<PASSWORD>"
)

3. Adding LunaSolve's Algorithms to the Pipeline

After logging into LunaSolve you will have access to all of the available solvers. LunaBench will propagate all parameters you provide, allowing you to configure the algorithms in exactly the same way as you do in LunaSolve, including how you manage QPU tokens. LunaSolve offers the flexibility to either store tokens on our platform or send them inline. If you store tokens, you can access them in the same manner here. For detailed instructions on using solvers and quantum hardware from LunaSolve, you can find a specific tutorial here.

In this example, we will access the version o Simulated Annealing hosted on our platform as well as D-Wave's Quantum Annealer.

from lunabench import solve_dataset

# Specify the algorithms from LunaSolve
algorithms = {
   "SA": {
        "location": "cloud",
        "n_iter": 1,
        "provider": "dwave"
    },
    "QA": {
        "location": "cloud",
        "provider": "dwave",
        "qpu_tokens": {
            "dwave": {
                "source": "inline",
                "token": "<TOKEN>"
            }
        }
    }
}

# Define the number of runs for each algorithm
n_runs = 2

# Solve the complete dataset
solved, result_id = solve_dataset(
    problem_name=problem_name,
    dataset=dataset,
    solve_algorithms=algorithms,
    n_runs=n_runs,
    luna_token=luna_token
)

Was this page helpful?