Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split QFI into QFI and QGT #9085

Merged
merged 21 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions qiskit/algorithms/gradients/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022.
# (C) Copyright IBM 2022, 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
Expand Down Expand Up @@ -43,13 +43,14 @@
ParamShiftSamplerGradient
SPSASamplerGradient

QFI
===
Quantum Geometric Tensor
========================
.. autosummary::
a-matsuo marked this conversation as resolved.
Show resolved Hide resolved
:toctree: ../stubs/

BaseQFI
LinCombQFI
BaseQGT
LinCombQGT
QFI

Results
=======
Expand All @@ -59,39 +60,44 @@

EstimatorGradientResult
QFIResult
a-matsuo marked this conversation as resolved.
Show resolved Hide resolved
QGTResult
SamplerGradientResult
"""

from .base_estimator_gradient import BaseEstimatorGradient
from .base_qfi import BaseQFI
from .base_qgt import BaseQGT
from .base_sampler_gradient import BaseSamplerGradient
from .estimator_gradient_result import EstimatorGradientResult
from .finite_diff_estimator_gradient import FiniteDiffEstimatorGradient
from .finite_diff_sampler_gradient import FiniteDiffSamplerGradient
from .lin_comb_estimator_gradient import DerivativeType, LinCombEstimatorGradient
from .lin_comb_qfi import LinCombQFI
from .lin_comb_qgt import LinCombQGT
from .lin_comb_sampler_gradient import LinCombSamplerGradient
from .param_shift_estimator_gradient import ParamShiftEstimatorGradient
from .param_shift_sampler_gradient import ParamShiftSamplerGradient
from .qfi import QFI
from .qfi_result import QFIResult
from .qgt_result import QGTResult
from .sampler_gradient_result import SamplerGradientResult
from .spsa_estimator_gradient import SPSAEstimatorGradient
from .spsa_sampler_gradient import SPSASamplerGradient

__all__ = [
"BaseEstimatorGradient",
"BaseQFI",
"BaseQGT",
"BaseSamplerGradient",
"DerivativeType",
"EstimatorGradientResult",
"FiniteDiffEstimatorGradient",
"FiniteDiffSamplerGradient",
"LinCombEstimatorGradient",
"LinCombQFI",
"LinCombQGT",
"LinCombSamplerGradient",
"ParamShiftEstimatorGradient",
"ParamShiftSamplerGradient",
"QFI",
"QFIResult",
"QGTResult",
"SamplerGradientResult",
"SPSAEstimatorGradient",
"SPSASamplerGradient",
Expand Down
37 changes: 18 additions & 19 deletions qiskit/algorithms/gradients/base_estimator_gradient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022.
# (C) Copyright IBM 2022, 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
Expand Down Expand Up @@ -152,7 +152,7 @@ def _preprocess(
Args:
circuits: The list of quantum circuits to compute the gradients.
parameter_values: The list of parameter values to be bound to the circuit.
parameters: The sequence of parameters to calculate only the gradients of the specified
parameter_sets: The sequence of parameters to calculate only the gradients of the specified
parameters.
supported_gates: The supported gates used to transpile the circuit.

Expand Down Expand Up @@ -182,32 +182,32 @@ def _postprocess(
results: EstimatorGradientResult,
circuits: Sequence[QuantumCircuit],
parameter_values: Sequence[Sequence[float]],
parameter_sets: Sequence[set[Parameter] | None],
parameter_sets: Sequence[set[Parameter]],
) -> EstimatorGradientResult:
"""Postprocess the gradient. This computes the gradient of the original circuit from the
gradient of the gradient circuit by using the chain rule.
"""Postprocess the gradients. This method computes the gradient of the original circuits
by applying the chain rule to the gradient of the circuits with unique parameters.

Args:
results: The results of the gradient of the gradient circuits.
circuits: The list of quantum circuits to compute the gradients.
parameter_values: The list of parameter values to be bound to the circuit.
parameters: The sequence of parameters to calculate only the gradients of the specified
parameters.
results: The computed gradients for the circuits with unique parameters.
circuits: The list of original circuits submitted for gradient computation.
parameter_values: The list of parameter values to be bound to the circuits.
parameter_sets: An optional subset of parameters with respect to which the gradients should
be calculated.

Returns:
The results of the gradient of the original circuits.
The gradients of the original circuits.
"""
gradients, metadata = [], []
for idx, (circuit, parameter_values_, parameter_set) in enumerate(
zip(circuits, parameter_values, parameter_sets)
):
unique_gradient = np.zeros(len(parameter_set))
gradient = np.zeros(len(parameter_set))
if (
"derivative_type" in results.metadata[idx]
and results.metadata[idx]["derivative_type"] == DerivativeType.COMPLEX
):
# If the derivative type is complex, cast the gradient to complex.
unique_gradient = unique_gradient.astype("complex")
gradient = gradient.astype("complex")
gradient_circuit = self._gradient_circuit_cache[_circuit_key(circuit)]
g_parameter_set = _make_gradient_parameter_set(gradient_circuit, parameter_set)
# Make a map from the gradient parameter to the respective index in the gradient.
Expand All @@ -233,11 +233,11 @@ def _postprocess(
bound_coeff = coeff
# The original gradient is a sum of the gradients of the parameters in the
# gradient circuit multiplied by the coefficients.
unique_gradient[i] += (
a-matsuo marked this conversation as resolved.
Show resolved Hide resolved
gradient[i] += (
float(bound_coeff)
* results.gradients[idx][g_parameter_indices[g_parameter]]
)
gradients.append(unique_gradient)
gradients.append(gradient)
metadata.append([{"parameters": parameter_indices}])
return EstimatorGradientResult(
gradients=gradients, metadata=metadata, options=results.options
Expand All @@ -256,10 +256,9 @@ def _validate_arguments(
circuits: The list of quantum circuits to compute the gradients.
observables: The list of observables.
parameter_values: The list of parameter values to be bound to the circuit.
parameters: The Sequence of Sequence of Parameters to calculate only the gradients of
the specified parameters. Each Sequence of Parameters corresponds to a circuit in
``circuits``. Defaults to None, which means that the gradients of all parameters in
each circuit are calculated.
parameter_sets: The Sequence of parameter sets to calculate only the gradients of
the specified parameters. Each set of parameters corresponds to a circuit in
``circuits``.

Raises:
ValueError: Invalid arguments are given.
Expand Down
175 changes: 0 additions & 175 deletions qiskit/algorithms/gradients/base_qfi.py

This file was deleted.

Loading