Skip to content

Commit

Permalink
Reorganised algorithms.gradients (#9969)
Browse files Browse the repository at this point in the history
* Reorganised algorithms.gradients

Fixes issue #9695 by reorganising algorithms.gradients into seperate folders.

* Moved 'lin_comb_qgt.py' to the 'qgt' folder

Moved 'lin_comb_qgt.py' to the 'qgt' folder to ensure all qgt related files are grouped together.

* Formatted files with linter.

Ran linter and reformatted files accordingly.

* Updated '__init__.py'

Updated '__init__.py' to reflect new folder structure.

* Removed 'qgt' folder

Assimilated QGT classes into related folders within 'algorithms.gradients'. Also, 'qgt_result.py' was placed into the 'base' folder since it constitutes a base class.

* Update qiskit/algorithms/gradients/__init__.py

Co-authored-by: Julien Gacon <[email protected]>

* Update qiskit/algorithms/gradients/__init__.py

Co-authored-by: Julien Gacon <[email protected]>

* Removed 'qfi' folder

Removed 'qfi' folder and moved remaining contents into parent folder 'gradients'.

* Update qiskit/algorithms/gradients/__init__.py

Co-authored-by: Julien Gacon <[email protected]>

* Attempted to resolve merge conflicts.

Altered files to match new versions causing the conflict.

* Ran linter

Ran 'tox -eblack' on the repository to resolve linter issues.

* Fixed base module hierarchy issue

Altered contents of 'qfi.py' to refer to 'BaseQGT' according to the new hierarchy given in the PR.

* Fixed more hierarchy issues.

Changed more references to reflect new hierarchy.

---------

Co-authored-by: Julien Gacon <[email protected]>
  • Loading branch information
ica574 and Cryoris authored Jun 28, 2023
1 parent d976352 commit 2ca4698
Show file tree
Hide file tree
Showing 28 changed files with 160 additions and 85 deletions.
100 changes: 60 additions & 40 deletions qiskit/algorithms/gradients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,94 @@
.. currentmodule:: qiskit.algorithms.gradients
Estimator Gradients
===================
Base Classes
============
.. autosummary::
:toctree: ../stubs/
BaseEstimatorGradient
DerivativeType
FiniteDiffEstimatorGradient
LinCombEstimatorGradient
ParamShiftEstimatorGradient
SPSAEstimatorGradient
ReverseEstimatorGradient
BaseQGT
BaseSamplerGradient
EstimatorGradientResult
SamplerGradientResult
QGTResult
Sampler Gradients
=================
Finite Differences
==================
.. autosummary::
:toctree: ../stubs/
BaseSamplerGradient
FiniteDiffEstimatorGradient
FiniteDiffSamplerGradient
Linear Combination of Unitaries
===============================
.. autosummary::
:toctree: ../stubs/
LinCombEstimatorGradient
LinCombSamplerGradient
LinCombQGT
Parameter Shift Rules
=====================
.. autosummary::
:toctree: ../stubs/
ParamShiftEstimatorGradient
ParamShiftSamplerGradient
SPSASamplerGradient
Quantum Geometric Tensor
========================
Quantum Fisher Information
==========================
.. autosummary::
:toctree: ../stubs/
BaseQGT
LinCombQGT
QFIResult
QFI
Classical Methods
=================
.. autosummary::
:toctree: ../stubs/
ReverseEstimatorGradient
ReverseQGT
Results
=======
Simultaneous Perturbation Stochastic Approximation
==================================================
.. autosummary::
:toctree: ../stubs/
EstimatorGradientResult
QFIResult
QGTResult
SamplerGradientResult
SPSAEstimatorGradient
SPSASamplerGradient
"""

from .base_estimator_gradient import BaseEstimatorGradient
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_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 .base.base_estimator_gradient import BaseEstimatorGradient
from .base.base_qgt import BaseQGT
from .base.base_sampler_gradient import BaseSamplerGradient
from .base.estimator_gradient_result import EstimatorGradientResult
from .finite_diff.finite_diff_estimator_gradient import FiniteDiffEstimatorGradient
from .finite_diff.finite_diff_sampler_gradient import FiniteDiffSamplerGradient
from .lin_comb.lin_comb_estimator_gradient import DerivativeType, LinCombEstimatorGradient
from .lin_comb.lin_comb_qgt import LinCombQGT
from .lin_comb.lin_comb_sampler_gradient import LinCombSamplerGradient
from .param_shift.param_shift_estimator_gradient import ParamShiftEstimatorGradient
from .param_shift.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
from .reverse_gradient.reverse_gradient import ReverseEstimatorGradient
from .reverse_gradient.reverse_qgt import ReverseQGT
from .base.qgt_result import QGTResult
from .base.sampler_gradient_result import SamplerGradientResult
from .spsa.spsa_estimator_gradient import SPSAEstimatorGradient
from .spsa.spsa_sampler_gradient import SPSASamplerGradient
from .reverse.reverse_gradient import ReverseEstimatorGradient
from .reverse.reverse_qgt import ReverseQGT

__all__ = [
"BaseEstimatorGradient",
Expand Down
11 changes: 11 additions & 0 deletions qiskit/algorithms/gradients/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (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
# 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.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
from qiskit.transpiler.passes import TranslateParameterizedGates

from .estimator_gradient_result import EstimatorGradientResult
from .utils import (
from ..utils import (
DerivativeType,
GradientCircuit,
_assign_unique_parameters,
_make_gradient_parameters,
_make_gradient_parameter_values,
)

from ..algorithm_job import AlgorithmJob
from ...algorithm_job import AlgorithmJob


class BaseEstimatorGradient(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
from qiskit.transpiler.passes import TranslateParameterizedGates

from .qgt_result import QGTResult
from .utils import (
from ..utils import (
DerivativeType,
GradientCircuit,
_assign_unique_parameters,
_make_gradient_parameters,
_make_gradient_parameter_values,
)

from ..algorithm_job import AlgorithmJob
from ...algorithm_job import AlgorithmJob


class BaseQGT(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
from qiskit.transpiler.passes import TranslateParameterizedGates

from .sampler_gradient_result import SamplerGradientResult
from .utils import (
from ..utils import (
GradientCircuit,
_assign_unique_parameters,
_make_gradient_parameters,
_make_gradient_parameter_values,
)

from ..algorithm_job import AlgorithmJob
from ...algorithm_job import AlgorithmJob


class BaseSamplerGradient(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from qiskit.providers import Options

from .utils import DerivativeType
from ..utils import DerivativeType


@dataclass(frozen=True)
Expand Down
11 changes: 11 additions & 0 deletions qiskit/algorithms/gradients/finite_diff/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (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
# 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.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
from qiskit.providers import Options
from qiskit.quantum_info.operators.base_operator import BaseOperator

from .base_estimator_gradient import BaseEstimatorGradient
from .estimator_gradient_result import EstimatorGradientResult
from ..base.base_estimator_gradient import BaseEstimatorGradient
from ..base.estimator_gradient_result import EstimatorGradientResult

from ..exceptions import AlgorithmError
from ...exceptions import AlgorithmError


class FiniteDiffEstimatorGradient(BaseEstimatorGradient):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from qiskit.primitives import BaseSampler
from qiskit.providers import Options

from .base_sampler_gradient import BaseSamplerGradient
from .sampler_gradient_result import SamplerGradientResult
from ..base.base_sampler_gradient import BaseSamplerGradient
from ..base.sampler_gradient_result import SamplerGradientResult

from ..exceptions import AlgorithmError
from ...exceptions import AlgorithmError


class FiniteDiffSamplerGradient(BaseSamplerGradient):
Expand Down
11 changes: 11 additions & 0 deletions qiskit/algorithms/gradients/lin_comb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (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
# 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.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
from qiskit.providers import Options
from qiskit.quantum_info.operators.base_operator import BaseOperator

from .base_estimator_gradient import BaseEstimatorGradient
from .estimator_gradient_result import EstimatorGradientResult
from .utils import DerivativeType, _make_lin_comb_gradient_circuit, _make_lin_comb_observables
from ..base.base_estimator_gradient import BaseEstimatorGradient
from ..base.estimator_gradient_result import EstimatorGradientResult
from ..utils import DerivativeType, _make_lin_comb_gradient_circuit, _make_lin_comb_observables

from ..exceptions import AlgorithmError
from ...exceptions import AlgorithmError


class LinCombEstimatorGradient(BaseEstimatorGradient):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
from qiskit.providers import Options
from qiskit.quantum_info import SparsePauliOp

from .base_qgt import BaseQGT
from ..base.base_qgt import BaseQGT
from .lin_comb_estimator_gradient import LinCombEstimatorGradient
from .qgt_result import QGTResult
from .utils import DerivativeType, _make_lin_comb_qgt_circuit, _make_lin_comb_observables
from ..base.qgt_result import QGTResult
from ..utils import DerivativeType, _make_lin_comb_qgt_circuit, _make_lin_comb_observables

from ..exceptions import AlgorithmError
from ...exceptions import AlgorithmError


class LinCombQGT(BaseQGT):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
from qiskit.primitives.utils import _circuit_key
from qiskit.providers import Options

from .base_sampler_gradient import BaseSamplerGradient
from .sampler_gradient_result import SamplerGradientResult
from .utils import _make_lin_comb_gradient_circuit
from ..base.base_sampler_gradient import BaseSamplerGradient
from ..base.sampler_gradient_result import SamplerGradientResult
from ..utils import _make_lin_comb_gradient_circuit

from ..exceptions import AlgorithmError
from ...exceptions import AlgorithmError


class LinCombSamplerGradient(BaseSamplerGradient):
Expand Down
11 changes: 11 additions & 0 deletions qiskit/algorithms/gradients/param_shift/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (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
# 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.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
from qiskit.opflow import PauliSumOp
from qiskit.quantum_info.operators.base_operator import BaseOperator

from .base_estimator_gradient import BaseEstimatorGradient
from .estimator_gradient_result import EstimatorGradientResult
from .utils import _make_param_shift_parameter_values
from ..base.base_estimator_gradient import BaseEstimatorGradient
from ..base.estimator_gradient_result import EstimatorGradientResult
from ..utils import _make_param_shift_parameter_values

from ..exceptions import AlgorithmError
from ...exceptions import AlgorithmError


class ParamShiftEstimatorGradient(BaseEstimatorGradient):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

from qiskit.circuit import Parameter, QuantumCircuit

from .base_sampler_gradient import BaseSamplerGradient
from .sampler_gradient_result import SamplerGradientResult
from .utils import _make_param_shift_parameter_values
from ..base.base_sampler_gradient import BaseSamplerGradient
from ..base.sampler_gradient_result import SamplerGradientResult
from ..utils import _make_param_shift_parameter_values

from ..exceptions import AlgorithmError
from ...exceptions import AlgorithmError


class ParamShiftSamplerGradient(BaseSamplerGradient):
Expand Down
4 changes: 2 additions & 2 deletions qiskit/algorithms/gradients/qfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.providers import Options

from .base_qgt import BaseQGT
from .lin_comb_estimator_gradient import DerivativeType
from .base.base_qgt import BaseQGT
from .lin_comb.lin_comb_estimator_gradient import DerivativeType
from .qfi_result import QFIResult

from ..algorithm_job import AlgorithmJob
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from .derive_circuit import derive_circuit
from .split_circuits import split

from ..base_estimator_gradient import BaseEstimatorGradient
from ..estimator_gradient_result import EstimatorGradientResult
from ..base.base_estimator_gradient import BaseEstimatorGradient
from ..base.estimator_gradient_result import EstimatorGradientResult
from ..utils import DerivativeType

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from qiskit.providers import Options
from qiskit.primitives import Estimator

from ..base_qgt import BaseQGT
from ..qgt_result import QGTResult
from ..base.base_qgt import BaseQGT
from ..base.qgt_result import QGTResult
from ..utils import DerivativeType

from .split_circuits import split
Expand Down
11 changes: 11 additions & 0 deletions qiskit/algorithms/gradients/spsa/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This code is part of Qiskit.
#
# (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
# 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.
Loading

0 comments on commit 2ca4698

Please sign in to comment.