-
Notifications
You must be signed in to change notification settings - Fork 36
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
Constrained minimizer #258
Open
mtweiden
wants to merge
4
commits into
main
Choose a base branch
from
cstr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""This module implements the ConstrainedMinimizer 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 ConstrainedMinimizer class. | ||
|
||
A minimizer that finds parameters for a circuit template that minimize a | ||
'cost' CostFunction while also satisfying some 'constraint' 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. 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): An initial point in parameter space. | ||
|
||
Returns: | ||
(np.ndarray): The inputs that minimize the cost while obeying the | ||
constraints. | ||
|
||
Notes: | ||
This function should be side-effect free. This is because many | ||
calls may be running in parallel. | ||
""" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between the constraint to the cost? Should we minimize both of them? Maybe the constraint should be a Boolean function meaning that it returns true or false?
Should add more comments about what type of constraints can be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
cstr
cost function will almost always be a unitary distance/fidelity based cost, while thecost
cost function quantifies some desirable quality of the output circuit to optimize (i.e. it will not be distance based).The choice to minimize them both comes from the general form of constrained optimization problems. It still makes sense to use a scalar valued cost for the constraint because it's likely we would want to know how far off from being constrained a solution is (or if we want to shift some success threshold).
I've added some details in the docstring about what the constraint should look like. The cost function is left more open ended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I'm still confused.
This abstract function tries to minimize the cost, the constraint, or both?
I assume both, but then why would one want two different cost functions to minimize? do they have different weights? why not combine them into a single cost function?
When you say "satisfying some 'constraint' CostFunction" it means that it is a boolean function (you can have a distance with a threshold, that will create a boolean value...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to preface by saying that these design choices are based on how off the shelf constrained optimizers work. The wikipedia page on the KKT conditions and the documentation for scipy are good reads for this.
The cost is minimized given that some constraint inequality is satisfied (i.e.
(cstr(x) - epsilon) <= 0
). The cost and constraint functions are likely to check for very different things, so from an interpretability standpoint it does not make sense to combine them at this level. Also, they are combined into one cost function under the hood but this is the responsibility of the actual optimizer used, not theConstrainedMinimizer
class. Combining them here means that off the shelf constrained optimizers cannot be used.While satisfying constraints does mean passing some boolean condition, treating them as discrete tests means we can't apply optimization tools that expect continuous and differentiable functions (which is an expectation of most constrained optimizers).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so how does the user specifies the constraint inequality ? in the scipy documentation there is an upper and a lower bound. Do you think there should one here as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. It's not passed here because it fits better with how the
Minimizer.minimize
function call looks. In the current implementation, actual instances of theConstrainedMinimizer
have the success threshold as a parameter in__init__
. There are different ways to pass the success threshold given the optimizer, so I think handling this internally still makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it makes a difference, I think keeping the instantiation API open for now is valuable. So if you think there is a modification to Minimizer API that makes better sense, we should discuss it.