Skip to main content

Optimization and ML modeling package targeting EQC devices

Project description

EQC Models

Quick Reference

Installation

Install from the Python Package Index (PyPI) using

pip install eqc-models

For pre-release and source versions, install eqc-models by extracting the source package and running

pip install -e .

from the folder with the pyproject.toml file. This will create an editable installation that is local to your user environment. Certain optimizations require extension modules, in particular eqc_models.base.polyeval. This is a C extension produced from a Cython module which evaluates polynomial expressions. Since there could be thousands of terms, the C extension can greatly improve performance of some algorithms. Installing from source with pip will build the module.

Modeling

Implement a model by selecting from the most appropriate model class from the available models and pass the required data. The model classes are (in depth first order by hierarchy)

  • eqc_models.base.EQCModel: a base class for all models
  • eqc_models.ml.classifierbase.ClassifierBase: a base model for machine learning classifier models
  • eqc_models.ml.classifierqboost.QBoostClassifier: a machine learning classifier model using QBoost
  • eqc_models.ml.classifierqsvm.QSVMClassifier: a machine learning classifier model using QSVM
  • eqc_models.ml.communitydetection.CommunityDetectionModel: a model for modeling community detection problems
  • eqc_models.base.QuadraticModel: a base class for all models with up to quadratic terms
  • eqc_models.base.ConstrainedQuadraticModel: a base class for all models with up to quadratic objective functions and linear equality constraints
  • eqc_models.base.PolynomialModel: a base class for all models that utilize a polynomial formulation (up to fifth order)
  • eqc_models.allocation.AllocationModel: an allocation model with equality resource constraints
  • eqc_models.allocation.AllocationModelX: an allocation model with equality and inequality resource constraints
  • eqc_models.allocation.portmomentum.PortMomentum: a portfolio allocation momentum model
  • eqc_models.portbase.PortBase: a portfolio allocation base model
  • eqc_models.graph.MaxCutModel: a maximum cut model
  • eqc_models.assignment.qap.QAPModel: a quadratic assignment problem model
  • eqc_models.sequence.tsp.TSPModel: a base class for TSP models
  • eqc_models.sequence.tsp.MTZTSPModel: a traveling salesman model using the MTZ formulation
  • eqc_models.utilities.qplib.QGLModel: a model based on ConstrainedQuadraticModel to solve problems from qplib

Solvers

The hierarchy of solvers uses QciClient from the qci-client package. Both Dirac-1 and Dirac-3 can be accessed using these solvers. Specific classes exist for the devices.

  • eqc_models.qciclientsolver.Dirac1CloudSolver: A solver class that accesses Dirac-1 via the Qatalyst cloud service
  • eqc_models.qciclientsolver.Dirac3ContinuousCloudSolver: A solver class for quasi-continuous models that accesses Dirac-3 via the Qatalyst cloud service
  • eqc_models.qciclientsolver.Dirac3IntegerCloudSolver: A solver class for integer models that accesses Dirac-3 via the Qatalyst cloud service

Using Classifier Models

Use of classification models is based on the well known pattern of fitting a model, then using the fitted model to predict classifications. Here is a snippet from the qsvm_iris_dirac3.py script:

# Get QSVM model
obj = QSVMClassifier(
    relaxation_schedule=2,
    num_samples=1,
    upper_limit=1.0,
    gamma=1.0,
    eta=1.0,
    zeta=1.0,
)

# Train
obj.fit(X_train, y_train)

y_train_prd = obj.predict(X_train)
y_test_prd = obj.predict(X_test)

Using Decision Optimization Models

Decision optimization models can be utilized by building a model, then passing the model to a solver's solve method. The following snippet of code is taken from the qplib_runner.py script.

# C, J are the linear and quadratic components of the expression
# A and b are left hand and right hand side values for linear constraints
# R is the summation constraint value for the Dirac-3 requirement
model = QGLModel(C, J, A, b)
domains = [R if types[j] == "REAL" else 1 for j in range(num_variables)]
model.domains = np.array(domains)
model.penalty_multiplier = alpha
# set the machine slacks to 1 to add a dummy variable to capture slack value from the summation constraint
model.machine_slacks = 1
print(f"Constructed model with R={R} alpha={alpha}")
solver = Dirac3CloudSolver()
print(f"Running model with relaxation_schedule={schedule}")
response = solver.solve(model, basename, sum_constraint=R, relaxation_schedule=schedule)
print(f"Energy {response['results']['energies'][0]}")

Design Overview

This package is designed around two central base classes. The first, EqcModel, includes the most basic common elements of an optimization model. The second, ModelSolver, includes the most basic elements of a solver class. While not all classes in this package inherit from one of these two classes, the core functionality exists in thie hierarchy. Additional things, such as algorithms and supporting methods take advantage of these classes. Another design choice in the package is to utilize multiple inheritance and class mixins for construction of functional classes. This design is not intended to impact the end user, who can simply instantiate a certain model or solver class for usage. If a new model class is desired, the mixin design may be utilized to construct this new class. Additionally, a hybrid solver could be implemented using this same design.

Troubleshooting Common Errors

No module named '_bz2' This error occurs because NetworkX, a dependency of eqc-models, requires the bzip 2 library and it has not been found. On some linux environments, the OS package libbzip2-dev can be installed with apt or yum, then the python environment must be installed again.

Further Information

Most of the classes in this package have doctests, see these docstrings for specific concrete examples. Also, the scripts and test directories have examples on modeling and solving with eqc-models.

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

eqc_models-0.14.5.tar.gz (4.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eqc_models-0.14.5-py3-none-any.whl (217.7 kB view details)

Uploaded Python 3

File details

Details for the file eqc_models-0.14.5.tar.gz.

File metadata

  • Download URL: eqc_models-0.14.5.tar.gz
  • Upload date:
  • Size: 4.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for eqc_models-0.14.5.tar.gz
Algorithm Hash digest
SHA256 0911692a4a8c26be7cc7eac8b48df74e2fc238fc02572affc75768101851a8d0
MD5 68da5cb2f2b61b1eb64a6b2267660ad0
BLAKE2b-256 f2bd2442f3c8d61b3dba472dc2485a8a15fe6d82086b4c4f02ad8cfefeb51eb2

See more details on using hashes here.

File details

Details for the file eqc_models-0.14.5-py3-none-any.whl.

File metadata

  • Download URL: eqc_models-0.14.5-py3-none-any.whl
  • Upload date:
  • Size: 217.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for eqc_models-0.14.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b7c40b8a6c39405b0a2f1477aee1c53473c1f2dc5de8242aae2f56a3be8550ed
MD5 b11a1eafeb5eb4bed696be0c31aff4d3
BLAKE2b-256 e2e2d6e53903dbcf4f5e876d9e6aa1b1f3bc072a323482f44759f5c49fc650b2

See more details on using hashes here.

Supported by

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