Skip to main content

A client python API for accessing LightSolver's capabilities

Project description

LightSolver Platform Client

The LightSolver Platform Client is a Python package designed to interface with the LightSolver Cloud to facilitate solving Quadratic Unconstrained Binary Optimization (QUBO) problems.

This package is designated for internal access to features during the development process, as well as serves as a prototype for future versions of the production LightSolver Platform Client.

Features

  • QUBO Problem Solving: The solve_qubo function accepts a QUBO problem, represented either as a 2D array (matrix) or an adjacency list, and returns the solution.
  • Synchronous and Asynchronous Operation: Users can choose between blocking (synchronous) and non-blocking (asynchronous) modes for QUBO problem solving.
  • Flexible Installation: Compatible with both Windows and MacOS systems.

Solve QUBO

The solve_qubo function handles the computation of QUBO problems, either represented by a 2D array (matrix) or by an adjacency list. For code samples, see the Usage section.

Input Matrix Validity

  • The matrix must be square.
  • The matrix supports int or float cell values.

Return Value

A dictionary with the following fields:

- 'id': Unique identifier of the solution.
- 'solution': The solution as a Python list() of 1s and 0s.
- 'objval: The objective value of the solution.
- 'solverRunningTime': Time spent by the solver to calculate the problem.
- 'receivedTime': Timestamp when the request was received by the server.

Synchronous and Asynchronous Usage

  • Synchronous Mode (Default): The waitForSolution flag is set to True by default. The function blocks operations until a result is received.
  • Asynchronous Mode: Set waitForSolution to False. The function returns immediately with a token object, allowing the script to continue while the server processes the QUBO problem.

Setting Up

Prerequisites

  • Operating System: MacOS or Windows 11.
  • Valid token for connecting to the LightSolver Cloud (provided separately).
  • Python 3.10 or higher (Download Here).
    • Select the appropriate MacOS/Windows version at the bottom.
    • Note: for Windows installation, switch on the "Add to Path" option in the wizard.
  • Highly Recommended: Use a virtual environment before installing laser-mind-client (Please see detailed action further below under the relevant OS).

Installation

Complete the installation on Windows or MacOS as described below. For further assistance with setup or connection issues, contact support@lightsolver.com.

Windows

  1. Press the windows key, type "cmd", and select "Command Prompt".

  2. Navigate to the root folder of the project where you plan to use the LightSolver Client:

    cd <your project folder>
  1. (Recommended) Create and activate the virtual environment:
    python -m venv .venv
    .venv\Scripts\activate
  1. Install the laser-mind-client package:
    pip install laser-mind-client
  1. (Recommended) Test using one of the provided test examples. Unzip "lightsolver_onboarding.zip."
    cd lightsolver_onboarding
    python ./tests/test_solve_qubo_matrix.py

MacOS

  1. Open new terminal window.

  2. Navigate to the root folder of the project where you plan to use the LightSolver Client:

    cd <your project folder>
  1. (Recommended) Create and activate the virtual environment:
    python3 -m venv .venv
    chmod 755  .venv/bin/activate
    source .venv/bin/activate
  1. Install the laser-mind-client package.
    pip install laser-mind-client
  1. (Recommended) Test using one of the provided test examples. Unzip "lightsolver_onboarding.zip."
    cd lightsolver_onboarding
    python3 ./tests/test_solve_qubo_matrix.py

Authentication

Initialization of the LaserMind class automatically forms a secure and authenticated connection with the LightSolver Cloud. Subsequent calls by the same user are similarly secure and authenticated.

Usage

To begin solving any QUBO problem:

  1. Create an instance of the LaserMind class. This class represents the client that requests solutions from the LightSolver Cloud.
  2. Call the solve_qubo function using either a matrix or an adjacency list. Note: You may either provide a value for matrixData or for edgeList, but not both.

Solve QUBO Matrix Example

This example creates a matrix representing a QUBO problem and solves it using the LightSolver Platform Client. The solve_qubo function is used with the following parameters:

  • matrixData: A 2D array representing the QUBO problem.
  • timeout: The required time limit for the calculation in seconds.
import numpy
from laser_mind_client_meta import MessageKeys
from laser_mind_client import LaserMind

# Enter your TOKEN here
userToken = "<my_token>"

# Create a mock QUBO problem
quboProblemData = numpy.random.randint(-1, 2, (10,10))

# Symmetrize the matrix
quboProblemData = (quboProblemData + quboProblemData.T) // 2

# Connect to the LightSolver Cloud
lsClient = LaserMind(userToken=userToken)

res = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1)

assert MessageKeys.SOLUTION in res, "Test FAILED, response is not in expected format"

print(f"Test PASSED, response is: \n{res}")

Solve QUBO Adjacency List Example

This example describes a QUBO problem using an adjacency list. This is useful for sparse matrices. The solve_qubo function is used with the following parameters:

  • edgeList: The adjacency list representing the QUBO problem.
  • timeout: The required time limit for the calculation in seconds.
from laser_mind_client_meta import MessageKeys
from laser_mind_client import LaserMind

# Enter your TOKEN here
userToken = "<my_token>"

# Create a mock QUBO problem
quboListData = [
    [1,1,5],
    [1,2,-6],
    [2,2,3],
    [2,3,-1],
    [3,10,1]]

# Connect to the LightSolver Cloud
lsClient = LaserMind(userToken=userToken)

res = lsClient.solve_qubo(edgeList=quboListData, timeout=1)

assert MessageKeys.SOLUTION in res, "Test FAILED, response is not in expected format"

print(f"Test PASSED, response is: \n{res}")

Solve QUBO Matrix using Asynchronous Flow

This example demonstrates how to solve a QUBO problem asynchronously using the LightSolver Platform Client. Begin by creating a matrix to represent your QUBO problem. The solve_qubo function is used with the following parameters:

  • matrixData: A 2D array representing the QUBO problem.
  • timeout: The desired time limit for the calculation in seconds.
  • waitForSolution: A boolean flag set to False to indicate non-blocking mode.
import numpy
from laser_mind_client_meta import MessageKeys
from laser_mind_client import LaserMind

# Enter your TOKEN here
userToken = "<my_token>"

# Create a mock QUBO problem
quboProblemData = numpy.random.randint(-1, 2, (10,10))

# Symmetrize our matrix
quboProblemData = (quboProblemData + quboProblemData.T) // 2

# Connect to the LightSolver Cloud
lsClient = LaserMind(userToken=userToken)

# Request a solution to the QUBO problem and get the request token for future retrieval.
# This call does not block operations until the problem is solved.
requestToken = lsClient.solve_qubo(matrixData = quboProblemData, timeout=1, waitForSolution=False)

# You can run other code here that is not dependant on the request, while the server processes your request.

# Retrieve the solution using the get_solution_sync method.
# This blocks operations until the solution is acquired.
res = lsClient.get_solution_sync(requestToken)

assert MessageKeys.SOLUTION in res, "Test FAILED, response is not in expected format"

print(f"Test PASSED, response is: \n{res}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

laser_mind_client-0.26.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

laser_mind_client-0.26.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file laser_mind_client-0.26.0.tar.gz.

File metadata

  • Download URL: laser_mind_client-0.26.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for laser_mind_client-0.26.0.tar.gz
Algorithm Hash digest
SHA256 baa717362069f2767d05327f2e5abb199c28638cb3483ffabd497b323085ebe1
MD5 4790eeb0426da512d88f83e698b51267
BLAKE2b-256 5e59f956a60ad2a97882143e8e2a7af5a9121701979af2cd490a8236c4d24977

See more details on using hashes here.

File details

Details for the file laser_mind_client-0.26.0-py3-none-any.whl.

File metadata

File hashes

Hashes for laser_mind_client-0.26.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63fb85c6203ef4d791f9f0b9cb8c40c25d4a6f49b9630378f367ccd48fe17d64
MD5 76eb7346cf3f0dd397b75d46b9550784
BLAKE2b-256 5ae39733fa97edd50899c8a54ca1916438824a81e58a04b301991b8d9671aa44

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page