-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
TrainableFidelityStatevectorKernel
(#639)
* add trainable fidelity statevector kernel * fix copyright * refactor check on trainable parameters * fix copyright * consolidate trainable kernels tests * avoid string flags in tests * fix copyright * test trainer with statevector kernel * add release note * fix style * format release note * lint * style * Update release note based on Steve's comments * add imports to release note snippet * fix typo --------- Co-authored-by: Anton Dekusar <[email protected]>
- Loading branch information
1 parent
0902988
commit b9d0561
Showing
9 changed files
with
260 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
qiskit_machine_learning/kernels/trainable_fidelity_statevector_kernel.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2023. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Trainable Fidelity Statevector Kernel""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Sequence, Type | ||
|
||
import numpy as np | ||
from qiskit import QuantumCircuit | ||
from qiskit.circuit import Parameter, ParameterVector | ||
from qiskit.quantum_info import Statevector | ||
|
||
|
||
from .fidelity_statevector_kernel import FidelityStatevectorKernel, SV | ||
from .trainable_kernel import TrainableKernel | ||
|
||
|
||
class TrainableFidelityStatevectorKernel(TrainableKernel, FidelityStatevectorKernel): | ||
r""" | ||
A trainable version of the | ||
:class:`~qiskit_machine_learning.kernels.FidelityStatevectorKernel`. | ||
Finding good quantum kernels for a specific machine learning task is a big challenge in quantum | ||
machine learning. One way to choose the kernel is to add trainable parameters to the feature | ||
map, which can be used to fine-tune the kernel. | ||
This kernel has trainable parameters :math:`\theta` that can be bound using training algorithms. | ||
The kernel entries are given as | ||
.. math:: | ||
K_{\theta}(x,y) = |\langle \phi_{\theta}(x) | \phi_{\theta}(y) \rangle|^2 | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
feature_map: QuantumCircuit | None = None, | ||
statevector_type: Type[SV] = Statevector, | ||
training_parameters: ParameterVector | Sequence[Parameter] | None = None, | ||
cache_size: int | None = None, | ||
auto_clear_cache: bool = True, | ||
shots: int | None = None, | ||
enforce_psd: bool = True, | ||
) -> None: | ||
""" | ||
Args: | ||
feature_map: Parameterized circuit to be used as the feature map. If ``None`` is given, | ||
:class:`~qiskit.circuit.library.ZZFeatureMap` is used with two qubits. If there's | ||
a mismatch in the number of qubits of the feature map and the number of features | ||
in the dataset, then the kernel will try to adjust the feature map to reflect the | ||
number of features. | ||
statevector_type: The type of Statevector that will be instantiated using the | ||
``feature_map`` quantum circuit and used to compute the fidelity kernel. This type | ||
should inherit from (and defaults to) :class:`~qiskit.quantum_info.Statevector`. | ||
training_parameters: Iterable containing :class:`~qiskit.circuit.Parameter` objects | ||
which correspond to quantum gates on the feature map circuit which may be tuned. | ||
If users intend to tune feature map parameters to find optimal values, this field | ||
should be set. | ||
cache_size: Maximum size of the statevector cache. When ``None`` this is unbounded. | ||
auto_clear_cache: Determines whether the statevector cache is retained when | ||
:meth:`evaluate` is called. The cache is automatically cleared by default. | ||
shots: The number of shots. If ``None``, the exact fidelity is used. Otherwise, the | ||
mean is taken of samples drawn from a binomial distribution with probability equal | ||
to the exact fidelity. | ||
enforce_psd: Project to the closest positive semidefinite matrix if ``x = y``. | ||
Default ``True``. | ||
""" | ||
super().__init__( | ||
feature_map=feature_map, | ||
statevector_type=statevector_type, | ||
training_parameters=training_parameters, | ||
cache_size=cache_size, | ||
auto_clear_cache=auto_clear_cache, | ||
shots=shots, | ||
enforce_psd=enforce_psd, | ||
) | ||
|
||
# Override the number of features defined in the base class. | ||
self._num_features = feature_map.num_parameters - self._num_training_parameters | ||
self._feature_parameters = [ | ||
parameter | ||
for parameter in feature_map.parameters | ||
if parameter not in training_parameters | ||
] | ||
self._parameter_dict = {parameter: None for parameter in self.feature_map.parameters} | ||
|
||
def _evaluate(self, x_vec: np.ndarray, y_vec: np.ndarray, is_symmetric: bool): | ||
new_x_vec = self._parameter_array(x_vec) | ||
new_y_vec = self._parameter_array(y_vec) | ||
return super()._evaluate(new_x_vec, new_y_vec, is_symmetric) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
releasenotes/notes/add-trainable-fidelity-statevector-kernel-6d9c927d16c1cb35.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
features: | ||
- | | ||
A new :class:`~qiskit_machine_learning.kernels.TrainableFidelityStatevectorKernel` class has | ||
been added that provides a trainable version of | ||
:class:`~qiskit_machine_learning.kernels.FidelityStatevectorKernel`. This relationship mirrors | ||
that between the existing | ||
:class:`~qiskit_machine_learning.kernels.TrainableFidelityQuantumKernel`class and | ||
:class:`~qiskit_machine_learning.kernels.FidelityQuantumKernel`. Thus, | ||
:class:`~qiskit_machine_learning.kernels.TrainableFidelityStatevectorKernel` inherits from both | ||
:class:`~qiskit_machine_learning.kernels.FidelityStatevectorKernel` and | ||
:class:`~qiskit_machine_learning.kernels.TrainableKernel`. | ||
This class is used with | ||
:class:`~qiskit_machine_learning.kernels.algorithms.QuantumKernelTrainer` in an identical way to | ||
:class:`~qiskit_machine_learning.kernels.TrainableFidelityQuantumKernel`, except for the | ||
arguments specific to | ||
:class:`~qiskit_machine_learning.kernels.TrainableFidelityStatevectorKernel`. | ||
For an example, see the snippet below: | ||
.. code-block:: python | ||
from qiskit.quantum_info import Statevector | ||
from qiskit_machine_learning.kernels import TrainableFidelityStatevectorKernel | ||
from qiskit_machine_learning.kernels.algorithms import QuantumKernelTrainer | ||
# Instantiate trainable fidelity statevector kernel. | ||
quantum_kernel = TrainableFidelityStatevectorKernel( | ||
feature_map=<your_feature_map>, | ||
statevector_type=Statevector, | ||
training_parameters=<your_training_parameters>, | ||
cache_size=None, | ||
auto_clear_cache=True, | ||
shots=None, | ||
enforce_psd=True, | ||
) | ||
# Instantiate a quantum kernel trainer (QKT). | ||
qkt = QuantumKernelTrainer(quantum_kernel=quantum_kernel) | ||
# Train the kernel using QKT directly. | ||
qkt_results = qkt.fit(<your_X_train>, <your_y_train>) | ||
optimized_kernel = qkt_results.quantum_kernel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.