Skip to main content

SDK Quickstart

info

For a complete documentation of the SDK, refer to the SDK documentation.

info

You have the option to use a managed storage with the SDK, but the total size of all input files must be under 1GB. For larger problems, consider using an alternative supported storage option, which allows up to 42GB.

Installation

info

Requirements: Python >= 3.9

To install the TitanQ SDK, please follow the installation section at the TitanQ PyPI page.

Example with managed storage

The following code is a quick and a simple example using TitanQ SDK. Note at the highlighted line, Model is created without a storage_client.

    import numpy as np
from titanq import Model, Vtype, Target, S3Storage

TITANQ_API_KEY="<insert your TitanQ API key here>"

# Problem construction
edges = {0:[4,5,6,7], 1:[4,5,6,7], 2:[4,5,6,7], 3:[4,5,6,7], 4:[0,1,2,3], 5:[0,1,2,3], 6:[0,1,2,3], 7:[0,1,2,3]}
size = len(edges)

# construct the weight matrix from the edges list
weights = np.zeros((size, size), dtype=np.float32)
for root, connections in edges.items():
for c in connections:
weights[root][c] = 1

# construct the bias vector (Uniform weighting across all nodes)
bias = np.asarray([0]*size, dtype=np.float32)

# TitanQ SDK
model = Model(api_key=TITANQ_API_KEY)
model.add_variable_vector('x', size, Vtype.BINARY)
model.set_objective_matrices(weights, bias, Target.MINIMIZE)
response = model.optimize(timeout_in_secs=1, coupling_mult=0.75, num_engines=2)

print("-" * 15, "+", "-" * 26, sep="")
print("Ising energy | Result vector")
print("-" * 15, "+", "-" * 26, sep="")
for ising_energy, result_vector in response.result_items():
print(f"{ising_energy: <14f} | {result_vector}")

Example using AWS S3 Buckets

The following section is for developers wishing to use the TitanQ with S3 buckets.

info

For detailed instructions on setting up your S3 bucket, refer to the Bucket Management documentation.

    import numpy as np
from titanq import Model, Vtype, Target, S3Storage

TITANQ_API_KEY="<insert your TitanQ API key here>"

AWS_BUCKET_NAME="<insert AWS bucket name here>"
AWS_ACCESS_KEY="<insert AWS bucket access key here>"
AWS_SECRET_ACCESS_KEY="<insert AWS secret access key here>"

# Problem construction
edges = {0:[4,5,6,7], 1:[4,5,6,7], 2:[4,5,6,7], 3:[4,5,6,7], 4:[0,1,2,3], 5:[0,1,2,3], 6:[0,1,2,3], 7:[0,1,2,3]}
size = len(edges)

# construct the weight matrix from the edges list
weights = np.zeros((size, size), dtype=np.float32)
for root, connections in edges.items():
for c in connections:
weights[root][c] = 1

# construct the bias vector (Uniform weighting across all nodes)
bias = np.asarray([0]*size, dtype=np.float32)

# TitanQ SDK
model = Model(
api_key=TITANQ_API_KEY,
storage_client=S3Storage(
bucket_name=AWS_BUCKET_NAME,
access_key=AWS_ACCESS_KEY,
secret_key=AWS_SECRET_ACCESS_KEY
)
)
model.add_variable_vector('x', size, Vtype.BINARY)
model.set_objective_matrices(weights, bias, Target.MINIMIZE)
response = model.optimize(timeout_in_secs=1, coupling_mult=0.75, num_engines=2)

print("-" * 15, "+", "-" * 26, sep="")
print("Ising energy | Result vector")
print("-" * 15, "+", "-" * 26, sep="")
for ising_energy, result_vector in response.result_items():
print(f"{ising_energy: <14f} | {result_vector}")
info

You may need your project ID (shown below) later when using an SDK or gcloud to access buckets