From ac84aa5a5c121ba4bdc6f212dc1ad4cc514eef6d Mon Sep 17 00:00:00 2001 From: Mathias Weiden Date: Sun, 14 Jul 2024 19:36:53 -0400 Subject: [PATCH 1/4] Abstract constrained minimizer added --- bqskit/ir/opt/constrained.py | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 bqskit/ir/opt/constrained.py diff --git a/bqskit/ir/opt/constrained.py b/bqskit/ir/opt/constrained.py new file mode 100644 index 000000000..8c0a02369 --- /dev/null +++ b/bqskit/ir/opt/constrained.py @@ -0,0 +1,48 @@ +"""This module implements the Minimizer base class.""" +from __future__ import annotations + +import abc +from typing import TYPE_CHECKING + +import numpy as np +import numpy.typing as npt + +from bqskit.qis.unitary.unitary import RealVector + +if TYPE_CHECKING: + from bqskit.ir.opt.cost.function import CostFunction + + +class ConstrainedMinimizer(abc.ABC): + """ + The Minimizer class. + + An minimizer finds the parameters for a circuit template that minimizes some + CostFunction. + """ + + @abc.abstractmethod + def constrained_minimize( + self, + cost: CostFunction, + cstr: CostFunction, + x0: RealVector, + ) -> npt.NDArray[np.float64]: + """ + Minimize `cost` starting from initial point `x0` while obeying `cstr`. + + Args: + cost (CostFunction): The CostFunction to minimize. + + cstr (CostFunction): The CostFunction used to constrain solutions. + + x0 (np.ndarray): The initial point. + + Returns: + (np.ndarray): The inputs that best minimizes the cost while obeying + the constraints. + + Notes: + This function should be side-effect free. This is because many + calls may be running in parallel. + """ From 8bb6216d621200667b34a6b7b8308102f50d3477 Mon Sep 17 00:00:00 2001 From: Mathias Weiden Date: Tue, 16 Jul 2024 07:44:38 -0700 Subject: [PATCH 2/4] Updated class documentation --- bqskit/ir/opt/constrained.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bqskit/ir/opt/constrained.py b/bqskit/ir/opt/constrained.py index 8c0a02369..7f1f86b83 100644 --- a/bqskit/ir/opt/constrained.py +++ b/bqskit/ir/opt/constrained.py @@ -15,10 +15,10 @@ class ConstrainedMinimizer(abc.ABC): """ - The Minimizer class. + The ConstrainedMinimizer class. An minimizer finds the parameters for a circuit template that minimizes some - CostFunction. + CostFunction while also satisfying some constraint CostFunction. """ @abc.abstractmethod From b575738471283d15087fc53ea40cb7ae22b0f407 Mon Sep 17 00:00:00 2001 From: Mathias Weiden Date: Tue, 16 Jul 2024 07:55:48 -0700 Subject: [PATCH 3/4] Updated cost and cstr documentation --- bqskit/ir/opt/constrained.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bqskit/ir/opt/constrained.py b/bqskit/ir/opt/constrained.py index 7f1f86b83..fedb4f436 100644 --- a/bqskit/ir/opt/constrained.py +++ b/bqskit/ir/opt/constrained.py @@ -1,4 +1,4 @@ -"""This module implements the Minimizer base class.""" +"""This module implements the ConstrainedMinimizer base class.""" from __future__ import annotations import abc @@ -17,8 +17,8 @@ class ConstrainedMinimizer(abc.ABC): """ The ConstrainedMinimizer class. - An minimizer finds the parameters for a circuit template that minimizes some - CostFunction while also satisfying some constraint CostFunction. + A minimizer that finds parameters for a circuit template that minimize a + 'cost' CostFunction while also satisfying some 'constraint' CostFunction. """ @abc.abstractmethod @@ -32,15 +32,18 @@ def constrained_minimize( Minimize `cost` starting from initial point `x0` while obeying `cstr`. Args: - cost (CostFunction): The CostFunction to minimize. + cost (CostFunction): The CostFunction to minimize. This function + should capture the objective of the optimization. cstr (CostFunction): The CostFunction used to constrain solutions. + In most cases, this will be based on the Hilbert-Schmidt dist- + ance or some related fidelity metric. - x0 (np.ndarray): The initial point. + x0 (np.ndarray): An initial point in parameter space. Returns: - (np.ndarray): The inputs that best minimizes the cost while obeying - the constraints. + (np.ndarray): The inputs that minimize the cost while obeying the + constraints. Notes: This function should be side-effect free. This is because many From 90d10dbc10882ad61339162c8712cd8add89a9ef Mon Sep 17 00:00:00 2001 From: Mathias Weiden Date: Wed, 17 Jul 2024 09:36:59 -0700 Subject: [PATCH 4/4] Added ConstrainedMinimizer to __init__.py --- bqskit/ir/opt/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bqskit/ir/opt/__init__.py b/bqskit/ir/opt/__init__.py index ad915823f..8b11dc1b6 100644 --- a/bqskit/ir/opt/__init__.py +++ b/bqskit/ir/opt/__init__.py @@ -2,6 +2,7 @@ instantiation and cost minimization.""" from __future__ import annotations +from bqskit.ir.opt.constrained import ConstrainedMinimizer from bqskit.ir.opt.cost.function import CostFunction from bqskit.ir.opt.cost.functions import HilbertSchmidtCost from bqskit.ir.opt.cost.functions import HilbertSchmidtCostGenerator @@ -24,6 +25,7 @@ 'QFactor', 'Minimization', 'Minimizer', + 'ConstrainedMinimizer', 'LBFGSMinimizer', 'CostFunction', 'CostFunctionGenerator',