Skip to content

πŸš€ Get Started

In this tutorial, we will develop a quantum-powered optimization application using Luna, capable of:

βœ… Solving complex optimization problems through the integration of quantum and classical computing
βœ… Seamlessly accessing quantum hardware via automated backend integration
βœ… Customizing problem configurations to meet specific requirements
βœ… Evaluating and analyzing results effectively

We will begin by addressing a basic optimization problem and gradually introduce advanced features, highlighting key capabilities of Luna throughout the process. Let’s begin! 🌟


Installing using pip:

pip install luna-quantum

Get Your Luna API Key

Sign up on Luna and get your API key. For more details, visit our Main Page.

Solve Your First Optimization Problem

Let's solve a simple optimization problem with the QAOA.

First, authenticate with your LUNA_API_KEY:

from luna_quantum import LunaSolve

LunaSolve.authenticate("<YOUR_LUNA_API_KEY>")

Create Your Model

Create your model that represents the optimization problem. In this example, we create a simple constrained optimization problem:

from luna_quantum import Variable, Model, Vtype

# Beach items
beach_items = ["surfboard", "book", "speaker", "cooler"]
fun_values = {"surfboard":40, "book":15, "speaker":25, "cooler":35}

# Interaction dictionary: positive = synergy, negative = conflict
interactions = {
    ("surfboard", "book"): -25,      # wet board ruins book!
    ("surfboard", "speaker"): 10,    # music while surfing
    ("surfboard", "cooler"): 15,     # cold drink after surfing
    ("book", "speaker"): -30,        # can't read with loud music
    ("book", "cooler"): 20,          # relaxing read with cold drink
    ("speaker", "cooler"): 30,       # perfect beach party combo
}

model = Model("beach_packing_simple")
with model.environment:
    # Create binary variables from the list
    items = {name: Variable(name, vtype=Vtype.Binary) for name in beach_items}

# Invert objective values to match minimization problem
model.objective = (-1) * sum(f_val * items[i] for i, f_val in fun_values.items())

# Invert interaction terms to match minimization problem
model.objective += (-1) * sum(val * items[i] * items[j] for (i,j), val in interactions.items())

print(model)

Tip

Check out the Model docs for more information how to build your model programmatically or from various formats.

Solve the Optimization Problem

Solve the optimization problem with QAOA and analyze the results.

from luna_quantum.solve.parameters.algorithms import QAOA
from luna_quantum.solve.parameters.backends import IBM

# Define the algorithm and backend
algorithm = QAOA(
    backend=IBM(
        backend=IBM.SimulatorBackend(
            backend_type='simulator',
            backend_name='aer'
        )
    ),
    reps=1,
    shots=1024
)

# Run your algorithm
solve_job = algorithm.run(model, name="beach-packing-with-qaoa")
solution = solve_job.result()

Modeling Your Optimization Problem

Understand how to effectively structure and formulate your optimization problem using the Model component.

Select Your Algorithm and Backend

Learn how to choose and implement the most suitable algorithm for your problem, and configure your preferred backend for optimal performance.

Additional Resources

For further guidance, explore the following references:

  • Tutorials – Step-by-step walkthroughs for various use cases
  • Model – Detailed instructions on modeling optimization problems
  • Algorithms – Comprehensive list of available algorithms and their configurations
  • Backends – Comprehensive list of available backends and their configurations
  • LunaSolve – Overview of the LunaSolve platform for optimization