Quanfluence Developer Kit for Python
Project description
python-quanfluence-sdk
Quanfluence Developer Kit for Python Learn how to solve complex problems with Quanfluence using Python.
Table of Contents
- Introduction
- Installation
- Authentication
- Device Management
- Executing QUBOs
- Complete Workflow Example
- Troubleshooting
- Examples
- Feedback
Introduction
The Quanfluence SDK provides a Python interface for accessing Quanfluence's Coherent Ising Machine. This SDK allows you to create and manage virtual devices, define Quadratic Unconstrained Binary Optimization (QUBO) problems, and execute these problems on Quanfluence's hardware.
Installation
To install the Quanfluence SDK:
pip install quanfluence-sdk
Authentication
Before using the SDK, you need to authenticate with your Quanfluence account credentials:
from quanfluence_sdk import QuanfluenceClient
# Initialize the client
client = QuanfluenceClient()
# Authenticate with your credentials
response = client.signin("your_username", "your_password")
# The client will automatically store your access token for subsequent API calls
Initialising the client and authentication is required everytime you run a script that uses the SDK to make API calls.
Device Management
Creating a Device
Quanfluence allows you to create virtual devices with specific configurations for solving your optimization problems. The following function creates a device. Always set device 'type', 'private_ipv4_address', and 'public_ipv4_address' to the values shown below. You can tweak the paramter values for better results for your problem. Once set, the values remain same until changed using the 'update_device()' function.
# Create a basic local device
device = client.create_device(
title="Device 1",
description="A device for testing QUBO problems",
type="aws", # Always set to this default value
iters=10000, # Number of iterations
runs=5, # Number of runs per problem
alpha=2.0, # Alpha parameter
beta=0.333, # Beta parameter
beta_decay=0.1, # Beta decay rate
noise_stdev=0.0, # Standard deviation of noise
runtime=0, # Default value
trials=1 # Number of trials
'private_ipv4_address': 'http://172.31.12.198:5050'
'public_ipv4_address': ''
)
print(f"Created device with ID: {device['id']}")
Remember the device ID as it is needed for all the API calls to the device. Ideally you will be assigned with a preconfigured device ID from Quanfluence and won't need to create a device.
Retrieving Device Information
To get information about an existing device:
# Replace with your actual device ID
device_id = YOUR_ID
device_info = client.get_device(device_id)
print(device_info)
This will return the existing paramters values and device details for the device ID.
Updating Device Parameters
You can update device parameters to optimize performance for specific problems. Once updated using this function, the parameter values remain the same until updated again.
# Update device parameters
updated_device = client.update_device(device_id, {
"alpha": 2.0,
"beta": 1.5,
"iters": 200,
"runs": 10
})
print(f"Updated device: {updated_device}")
Consider decreasing or increasing the number iterations to optimise between time taken and quality of results.
Executing QUBOs
There are multiple ways to execute a QUBO on a Quanfluence device:
Method 1: Direct QUBO Execution
The QUBO should always be in a python dictionary format with keys indicating spin number using consecutive integers starting from 0 as shown in the example below:
# Execute QUBO directly on the device
Q = [[0, 0, 1], [0, 1, -1], [1, 1, 2]]
result = client.execute_device_qubo_input(device_id, QUBO)
print("Optimization result:", result)
Method 2: Upload and Execute a QUBO File
In case of large problems, a QUBO file can be uploaded to the server and run multiple times without having to send the large problem to the server everytime. The file can be uploaded using the function shown below to obtain the file ID from the server. Use the file ID recieved to execute the file subsequently.
# First, upload a QUBO file to the device
response = client.upload_device_qubo(device_id, "path/to/your/qubo_file.qubo") # Remember the file_name
print("Uploaded filename", response['result'])
# Then execute the uploaded file
execution_result = client.execute_device_qubo_file(device_id, file_name) # Pass the file_name obtained to execute it
print("Execution result:", execution_result)
The QUBO file should be in serialised coordinate (COO) format and have a '.qubo' extension. Refer the Example QUBO file for the format.
Method 3: Jobs - Asynchronous QUBO Execution
For large-scale optimization problems that require significant computational time, Quanfluence provides an asynchronous job execution system. This method allows you to submit QUBO problems as background jobs and monitor their progress without blocking your application.
When to use this method:
- Large QUBO problems that may take several minutes to hours to complete
- When you need to submit multiple problems simultaneously
- Applications requiring non-blocking execution
- Batch processing scenarios
Workflow:
- Upload QUBO File: First, upload your QUBO file to the device (same as Method 2)
- Create Job: Submit the uploaded file for asynchronous execution
- Monitor Progress: Poll the job status to track execution progress
- Retrieve Results: Get the final results once the job completes
# Step 1: Upload a QUBO file to the device (if not already uploaded)
response = client.upload_device_qubo(device_id, "path/to/your/large_problem.qubo")
file_name = response['result']
print(f"Uploaded file: {file_name}")
# Step 2: Create an asynchronous job
job_response = client.create_job(device_id, file_name)
job_id = job_response['id']
print(f"Created job with ID: {job_id}")
# Step 3: Get job progress & result
execution_result = client.get_job(job_id) # Pass the job_id obtained to execute it
print("Job Status & Execution result:", execution_result)
Job Status Values (AWS Batch):
SUBMITTED: Job has been submitted to the queuePENDING: Job is waiting for resources to become availableRUNNABLE: Job is ready to run but waiting for compute resourcesSTARTING: Job is being prepared for executionRUNNING: Job is currently being executed on the compute resourcesSUCCEEDED: Job completed successfully with results availableFAILED: Job encountered an error and failed to completeCANCELLED: Job was cancelled before execution
Complete Workflow Example
Here's a complete example of using the Quanfluence SDK to solve a simple QUBO problem:
import dimod
from quanfluence_sdk import QuanfluenceClient
# Initialize and authenticate
client = QuanfluenceClient()
client.signin("your_username", "your_password")
# Use an existing device
device_id = DEVICE_ID # Replace with your actual device ID
# Define a QUBO problem
# This example minimizes: x₀ - x₀x₁ + 2x₁
Q = [[0, 0, 1], [0, 1, -1], [1, 1, 2]]
# Optimize device parameters for this problem
device = client.update_device(device_id, {
"alpha": 2.0,
"beta": 0.333,
"beta_decay": 0.1,
"iters": 1000
})
# Execute the QUBO
result = client.execute_device_qubo_input(device_id, Q)
print("Optimization result:", result)
# Print the solution
if "solution" in result:
print("Solution variables:", result["solution"])
print("Solution energy:", result["energy"])
Troubleshooting
Limitations
The current supported BQM on the Ising machine has the following limitations:
- The nodes in the BQM (QUBO or Ising models) should always be indexed from 0 to N-1 where N is the number of nodes.
- Disconnected nodes with 0 self-weight are not allowed.
- The values in QUBO passed in dictionary format should always be 32 - bit floating point numbers.
Common Issues
-
Authentication Errors: Make sure your username and password are correct.
-
Missing Access Token: If you see a "Access token is missing" error, ensure you're calling
signin()before making other API calls. -
QUBO Format Issues: Ensure your QUBO is formatted as a dictionary where keys are tuples of indices and values are floating-point numbers.
Examples
- Examples - explore our examples docs and learn more about using sdk.
Feedback
If you get stuck, we’re here to help. The following are the best ways to get assistance working through your issue:
Use our [GitHub Issue Tracker][gh-issues] for reporting bugs or requesting features. Visit the [Quanfluence Community][quanfluence-community] for getting help using Quanfluence Developer Kit for Python or just generally bond with your fellow Quanfluence developers.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file quanfluence_sdk-0.0.7.tar.gz.
File metadata
- Download URL: quanfluence_sdk-0.0.7.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db50399b5ad565e2d5156fd589501ffa7b086916ac84cfdbdae5b8c0d8318959
|
|
| MD5 |
ae0d7d1da174cd6ffe450f5e5d34c9f8
|
|
| BLAKE2b-256 |
bd61af6cccbec2b3fa06e048192f226cf9eeb442b188f91d67c44d53dfd5d124
|
File details
Details for the file quanfluence_sdk-0.0.7-py3-none-any.whl.
File metadata
- Download URL: quanfluence_sdk-0.0.7-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77ce63745edab453449113d1558719a2ef20a2f4374fe1c2edde59790133f6da
|
|
| MD5 |
a6d96be7144689b8f71c3163876b0c60
|
|
| BLAKE2b-256 |
de577e82ec9f855d3ba04f8bd40fc936a7083bc040e0260fc9b565ef9de19597
|