TNO Quantum
TNO Quantum provides generic software components aimed at facilitating the development of quantum applications.
VC
VC is part of TNO Quantum and provides a Python implementation of a Variational Quantum Classifier.
The implementation (see the VariationalClassifier
class) has been done in accordance with
the scikit-learn estimator API,
which means that the classifier can be used as any other (binary and multiclass)
scikit-learn classifier and
combined with transforms through Pipelines.
In addition, the VariationalClassifier
makes use of PyTorch
tensors, optimizers, and loss functions.
Quick Install
VC can be installed using pip as follows:
pip install tno.quantum.ml.classifiers.vc
Optionally (though required for the examples below), you can install the test datasets:
pip install tno.quantum.ml.datasets
Example
Here’s an example of how the VariationalClassifier
class can be used for
classification based on the
Iris dataset:
from tno.quantum.ml.classifiers.vc import VariationalClassifier
from tno.quantum.ml.datasets import get_iris_dataset
X_training, y_training, X_validation, y_validation = get_iris_dataset()
vc = VariationalClassifier()
vc = vc.fit(X_training, y_training)
predictions_validation = vc.predict(X_validation)
More elaborate examples can be found in the examples repository.
Backends
Internally, the VariationalClassifier
class makes use of hybrid classical/quantum
models to represent a function to be learned. These quantum models give rise to circuits
which can be run on different backends, such as quantum hardware and quantum simulators.
Backends are selected by closely following the PennyLane interface for specifying devices. As a result, PennyLane plugins are also supported. Here’s an example showcasing how Quantum Inspire hardware can be used through the PennyLane-QuantumInspire plugin (note that you will need to install the plugin as described here):
from tno.quantum.ml.classifiers.vc import VariationalClassifier
from tno.quantum.ml.datasets import get_wine_dataset
X_training, y_training, X_validation, y_validation = get_wine_dataset(
n_features=5, n_classes=3, random_seed=0
)
vc = VariationalClassifier(
backend={
"name": "quantuminspire.qi",
"options": {"backend": "Starmon-5", "project": "my project"},
},
)
vc = vc.fit(X_training, y_training)
predictions_validation = vc.predict(X_validation)
As an alternative example, here’s how the default.qubit simulator provided
by PennyLane can be specified by the backend
keyword:
backend={"name": "default.qubit", "options": {}}
Quantum Models
A quantum model defines a quantum circuit and a strategy for classical post-processing of measurements. Currently, three models are available:
The
"expected_values_model"
, as implemented by theExpectedValuesModel
class (seeget_qfunc()
for details on the post-processing strategy).The
"modulo_model"
, as implemented by theModuloModel
class.The
"parity_model"
, as implemented by theParityModel
class.
For example, the "modulo_model"
can be specified with the
model
keyword as follows:
from tno.quantum.ml.classifiers.vc import VariationalClassifier
from tno.quantum.ml.datasets import get_iris_dataset
X_training, y_training, X_validation, y_validation = get_iris_dataset(
n_features=4, n_classes=3, random_seed=0
)
vc = VariationalClassifier(
model={
"name": "modulo_model",
"options": {"n_layers": 2, "n_trainable_sublayers": 2, "scaling": 0.5},
},
)
vc = vc.fit(X_training, y_training)
predictions_validation = vc.predict(X_validation)
Developing New Quantum Models
Quantum models are developed using PennyLane and can be found in the
models
module.
New models can be implemented by extending from QModel
and adding them to get_model()
.
Support for PyTorch Optimizers
The currently supported optimizers can be found in the optimizers
module.
Other PyTorch optimizers can be added in get_optimizer()
.
(End)use Limitations
The content of this software may solely be used for applications that comply with international export control laws.