Post-processing
Load a QUBO from the dataset¶
In [ ]:
Copied!
import os
import re
from pathlib import Path
from qubosolver.saveload import load_qubo_dataset
output_directory = Path(str(os.path.abspath("01-dataset-generation-and-loading")).replace("docs/tutorial", "qubosolver_logs/tutorial"))
def load_datasets_by_size(directory: str):
"""
Loads datasets from a directory by extracting the size from filenames.
Args:
directory (str): Path to the directory containing dataset files.
Returns:
dict[int, torch.Tensor]: A dictionary where keys are sizes and values are loaded datasets.
"""
# Regular expression to match filenames like "raw_qubo_dataset_size_{size}.pt"
pattern = r"raw_qubo_dataset_size_(\d+)\.pt"
datasets_by_size = {}
os.makedirs(directory, exist_ok=True)
for filename in os.listdir(directory):
match = re.match(pattern, filename)
if match:
size = int(match.group(1))
file_path = os.path.join(directory, filename)
dataset = load_qubo_dataset(file_path)
datasets_by_size[size] = dataset
print(f"Loaded dataset with size {size} from {file_path}")
return datasets_by_size
datasets = load_datasets_by_size(output_directory)
size = 20
data_size_5 = datasets[size]
qubo_cofficents, first_qubo_solution = data_size_5[9]
print(f"Coefficients : {qubo_cofficents}")
import os
import re
from pathlib import Path
from qubosolver.saveload import load_qubo_dataset
output_directory = Path(str(os.path.abspath("01-dataset-generation-and-loading")).replace("docs/tutorial", "qubosolver_logs/tutorial"))
def load_datasets_by_size(directory: str):
"""
Loads datasets from a directory by extracting the size from filenames.
Args:
directory (str): Path to the directory containing dataset files.
Returns:
dict[int, torch.Tensor]: A dictionary where keys are sizes and values are loaded datasets.
"""
# Regular expression to match filenames like "raw_qubo_dataset_size_{size}.pt"
pattern = r"raw_qubo_dataset_size_(\d+)\.pt"
datasets_by_size = {}
os.makedirs(directory, exist_ok=True)
for filename in os.listdir(directory):
match = re.match(pattern, filename)
if match:
size = int(match.group(1))
file_path = os.path.join(directory, filename)
dataset = load_qubo_dataset(file_path)
datasets_by_size[size] = dataset
print(f"Loaded dataset with size {size} from {file_path}")
return datasets_by_size
datasets = load_datasets_by_size(output_directory)
size = 20
data_size_5 = datasets[size]
qubo_cofficents, first_qubo_solution = data_size_5[9]
print(f"Coefficients : {qubo_cofficents}")
Postprocessing after a tabu search heuristic¶
In [ ]:
Copied!
from qubosolver import QUBOInstance
from qubosolver.config import SolverConfig
from qubosolver.solver import QuboSolver
instance = QUBOInstance(qubo_cofficents)
# Create a SolverConfig object with classical solver options.
config = SolverConfig.from_kwargs(
use_quantum=False,
classical_solver_type="dwave_tabu",
do_postprocessing=True
)
# Instantiate the classical solver using the dispatcher.
classical_solver = QuboSolver(instance, config)
# Solve the QUBO problem.
solution = classical_solver.solve()
print("Solution result:", solution)
from qubosolver import QUBOInstance
from qubosolver.config import SolverConfig
from qubosolver.solver import QuboSolver
instance = QUBOInstance(qubo_cofficents)
# Create a SolverConfig object with classical solver options.
config = SolverConfig.from_kwargs(
use_quantum=False,
classical_solver_type="dwave_tabu",
do_postprocessing=True
)
# Instantiate the classical solver using the dispatcher.
classical_solver = QuboSolver(instance, config)
# Solve the QUBO problem.
solution = classical_solver.solve()
print("Solution result:", solution)