Qiskit Machine Learning 0.5.0
Changelog
New Features
-
Added support for categorical and ordinal labels to VQC. Now labels can be passed in different formats, they can be plain ordinal labels, a one dimensional array that contains integer labels like 0, 1, 2, …, or an array with categorical string labels. One-hot encoded labels are still supported. Internally, labels are transformed to one hot encoding and the classifier is always trained on one hot labels.
-
Introduced Estimator Quantum Neural Network (EstimatorQNN) based on (runtime) primitives. This implementation leverages the estimator primitive (see BaseEstimator) and the estimator gradients (see BaseEstimatorGradient) to enable runtime access and more efficient computation of forward and backward passes.
The new EstimatorQNN exposes a similar interface to the Opflow QNN, with a few differences. One is the quantum_instance parameter. This parameter does not have a direct replacement, and instead the estimator parameter must be used. The gradient parameter keeps the same name as in the Opflow QNN implementation, but it no longer accepts Opflow gradient classes as inputs; instead, this parameter expects an (optionally custom) primitive gradient.
The existing training algorithms such as VQR, that were based on the Opflow QNN, are updated to accept both implementations. The implementation of NeuralNetworkRegressor has not changed. -
Introduced Quantum Kernels based on (runtime) primitives. This implementation leverages the fidelity primitive (see BaseStateFidelity) and provides more flexibility to end users. The fidelity primitive calculates state fidelities/overlaps for pairs of quantum circuits and requires an instance of Sampler. Thus, users may plug in their own implementations of fidelity calculations.
The new kernels expose the same interface and the same parameters except the quantum_instance parameter. This parameter does not have a direct replacement and instead the fidelity parameter must be used.A new hierarchy is introduced:
- A base and abstract class [BaseKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.BaseKernel.html#qiskit_machine_learning.kernels.BaseKernel) is introduced. All concrete implementation must inherit this class. - A fidelity based quantum kernel [FidelityQuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.FidelityQuantumKernel.html#qiskit_machine_learning.kernels.FidelityQuantumKernel) is added. This is a direct replacement of [QuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.QuantumKernel.html#qiskit_machine_learning.kernels.QuantumKernel). The difference is that the new class takes either a sampler or a fidelity instance to estimate overlaps and construct kernel matrix. - A new abstract class [TrainableKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.TrainableKernel.html#qiskit_machine_learning.kernels.TrainableKernel) is introduced to generalize ability to train quantum kernels. - A fidelity-based trainable quantum kernel [TrainableFidelityQuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.TrainableFidelityQuantumKernel.html#qiskit_machine_learning.kernels.TrainableFidelityQuantumKernel) is introduced. This is a replacement of the existing [QuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.QuantumKernel.html#qiskit_machine_learning.kernels.QuantumKernel) if a trainable kernel is required. The trainer [QuantumKernelTrainer](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.algorithms.QuantumKernelTrainer.html#qiskit_machine_learning.kernels.algorithms.QuantumKernelTrainer) now accepts both quantum kernel implementations, the new one and the existing one.
The existing algorithms such as QSVC, QSVR and other kernel-based algorithms are updated to accept both implementations.
-
Introduced Sampler Quantum Neural Network (SamplerQNN) based on (runtime) primitives. This implementation leverages the sampler primitive (see BaseSampler) and the sampler gradients (see BaseSamplerGradient) to enable runtime access and more efficient computation of forward and backward passes more efficiently.
The new SamplerQNN exposes a similar interface to the CircuitQNN, with a few differences. One is the quantum_instance parameter. This parameter does not have a direct replacement, and instead the sampler parameter must be used. The gradient parameter keeps the same name as in the CircuitQNN implementation, but it no longer accepts Opflow gradient classes as inputs; instead, this parameter expects an (optionally custom) primitive gradient. The sampling option has been removed for the time being, as this information is not currently exposed by the Sampler, and might correspond to future lower-level primitives. -
The existing training algorithms such as VQC, that were based on the CircuitQNN, are updated to accept both implementations. The implementation of NeuralNetworkClassifier has not changed.
-
Expose the callback attribute as public property on TrainableModel. This, for instance, allows setting the callback between optimizations and store the history in separate objects.
-
Gradient operator/circuit initialization in OpflowQNN and CircuitQNN respectively is now delayed until the first call of the backward method. Thus, the networks are created faster and gradient framework objects are not created until they are required.
-
Introduced a new parameter evaluate_duplicates in QuantumKernel. This parameter defines a strategy how kernel matrix elements are evaluated if duplicate samples are found. Possible values are:
- all means that all kernel matrix elements are evaluated, even the diagonal ones when training. This may introduce additional noise in the matrix. - off_diagonal when training the matrix diagonal is set to 1, the rest elements are fully evaluated, e.g., for two identical samples in the dataset. When inferring, all elements are evaluated. This is the default value. - none when training the diagonal is set to 1 and if two identical samples are found in the dataset the corresponding matrix element is set to 1. When inferring, matrix elements for identical samples are set to 1.
-
In the previous releases, in the QGAN class, the gradient penalty could not be enabled to train the discriminator with a penalty function. Thus, a gradient penalty parameter was added during the initialization of the QGAN algorithm. This parameter indicates whether or not penalty function is applied to the loss function of the discriminator during training.
-
Enable the default construction of the ZFeatureMap in the TwoLayerQNN if the number of qubits is 1. Previously, not providing a feature map for the single qubit case raised an error as default construction assumed 2 or more qubits.
-
VQC will now raise an error when training from a warm start when encountering targets with a different number of classes to the previous dataset.
-
VQC will now raise an error when a user attempts multi-label classification, which is not supported.
-
Added two new properties to the TrainableModel class:
- fit_result returns a resulting object from the optimization procedure. Please refers to the Terra’s documentation of the OptimizerResult class. - weights returns an array of trained weights, this is a convenient way to get access to the weights, it is the same as calling model.fit_result.x.
Upgrade Notes
-
The method fit() is not abstract any more. Now, it implements basic checks, calls a new abstract method _fit_internal() to be implemented by sub-classes, and keeps track of fit_result property that is returned by this new abstract method. Thus, any sub-class of TrainableModel must implement this new method. Classes NeuralNetworkClassifier and NeuralNetworkRegressor have been updated correspondingly.
-
Inheriting from sklearn.svm.SVC in PegasosQSVC resulted in errors when calling some inherited methods such as decision_function due to the overridden fit implementation. For that reason, the inheritance has been replaced by a much lighter inheritance from ClassifierMixin providing the score method and a new method decision_function has been implemented. The class is still sklearn compatible due to duck typing. This means that for the user everything that has been working in the previous release still works, except the inheritance. The only methods that are no longer supported (such as predict_proba) were only raising errors in the previous release in practice.
Deprecation Notes
-
The qiskit_machine_learning.algorithms.distribution_learners package is deprecated and will be removed no sooner than 3 months after the release. There’s no direct replacement for the classes from this package. Instead, please refer to the new QGAN tutorial. This tutorial introduces step-by-step how to build a PyTorch-based QGAN using quantum neural networks.
-
Classes qiskit_machine_learning.runtime.TorchRuntimeClient, qiskit_machine_learning.runtime.TorchRuntimeResult, qiskit_machine_learning.runtime.HookBase and functions qiskit_machine_learning.runtime.str_to_obj(), qiskit_machine_learning.runtime.obj_to_str() are being deprecated. You should use QiskitRuntimeService to leverage primitives and runtimes.
-
Class qiskit_machine_learning.neural_networks.CircuitQNN is pending deprecation and is superseded by qiskit_machine_learning.neural_networks.SamplerQNN.
-
Class qiskit_machine_learning.neural_networks.OpflowQNN is pending deprecation and is superseded by qiskit_machine_learning.neural_networks.EstimatorQNN.
-
Class qiskit_machine_learning.neural_networks.TwoLayerQNN is pending deprecation and has no direct replacement. Please make use of qiskit_machine_learning.neural_networks.EstimatorQNN instead.
These classes will be deprecated in a future release and subsequently removed after that.
Class qiskit_machine_learning.kernels.QuantumKernel is pending deprecation and is superseded by qiskit_machine_learning.kernels.FidelityQuantumKernel and qiskit_machine_learning.kernels.TrainableFidelityQuantumKernel.
This class will be deprecated in a future release and subsequently removed after that.
-
For the class QuantumKernel, to improve usability and better describe the usage, user_parameters has been renamed to training_parameters; current behavior is retained. For this change the constructor parameter user_parameters is now deprecated and replaced by training_parameters. The related properties and methods are renamed to match. That is to say:
- argument user_parameters -> training_parameters - property user_parameters -> training_parameters - property user_param_binds -> training_parameter_binds - method assign_user_parameters -> assign_training_parameters - method bind_user_parameters -> bind_training_parameters - method get_unbound_user_parameters -> get_unbound_training_parameters
Bug Fixes
-
Previously in the QuantumGenerator of the QGAN algorithm, if we used a simulator other than the statevector_simulator the result dictionary had not the correct size to compute both the gradient and the loss functions. Now, the values output are stored in a vector of size 2^n and each key is mapped to its value from the result dictionary in the new value array. Also, each key is stored in a vector of size 2^n where each element of the vector keys[i] corresponds to the binary representation of i.
-
Previously in the QuantumGenerator of the QGAN algorithm, the gradients were computed using the statevector backend even if we specified another backend. To solve this issue, the gradient object is converted into a CircuitStateFn instead of its adjoint as in the previous version. The gradients are converted into the backend-dependent structure using CircuitSampler. After the evaluation of the object, the gradient_function is stored in a dense array to fix a dimension incompatibility when computing the loss function.
-
Fixed quantum kernel evaluation when duplicate samples are found in the dataset. Originally, kernel matrix elements were not evaluated for identical samples in the dataset and such elements were set wrongly to zero. Now we introduced a new parameter evaluate_duplicates that ensures that elements of the kernel matrix are evaluated correctly. See the feature section for more details.
-
Previously in the pytorch_discriminator class of the QGAN algorithm, if the gradient penalty parameter was enabled, the latent variable z was not properly initialized : Variable module was used instead of torch.autograd.Variable.
-
Calling PegasosQSVC.decision_function() raises an error. Fixed by writing own method instead of inheriting from SVC. The inheritance from SVC in the PegasosQSVC class is removed. To keep the score method, inheritance to the mixin class ClassifierMixin from scikit-learn is added.