-
Notifications
You must be signed in to change notification settings - Fork 376
Add feasibility checks to optimizers/converters #1199
Add feasibility checks to optimizers/converters #1199
Conversation
Please change the title to be more informative to denote what this PR adds and merge master to your branch. There are some conflicts. |
Done and Done. 😄 |
@t-imamichi I am confused by this PR. It already incorporates the changes of |
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.
Keeping going! All good as an intermediate version!
Please think of adding unit tests. Also, pay attention at the pipeline output, stuff like pylint, code style, spelling, etc.
if(detailed == True): | ||
|
||
for i in range(len(satisfied_variables)): | ||
(print(f'{self.get_variable(i)._name} is within the bounds') if satisfied_variables[i] | ||
else logger.warning(f'{self.get_variable(i)._name} is outside the bounds')) | ||
|
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 initial idea was to return a list of violations, not just printing them.
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 initial idea was to return a list of violations, not just printing them.
This is for debug purposes only. I was not sure about how the return value was required. I will change it into a list.
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.
This is a good question how to return them back. May be we should postpone this feature, I mean returning a list of violations and focus on the boolean result.
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 expect to see a list of something, but what is float here?
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.
So currently I will print the violations and just return the bool result. One way would be to change the return type from ->bool to ->Tuple[bool, List[float]] and return empty list whenever detailed = False.
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 list can contain float values as well. Like x = [1.0, 2.0, 1.0, 1.99999982]
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.
Yeah, but what do they mean, these float values?
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 am sorry I didn't get your question. I would try to explain from what I understood.
So if we define a quadratic problem with variables x, y and z, then we get results.x = [1.0, 2.0, 1.0] would mean x = 1.0, y = 2.0 and z = 1.0.
As I understood from the description of that PR, you don't have to follow that PR. Main purpose of #1196 is to change the |
Yes. You can ignore |
fval=sol.get_objective_value(), | ||
variables=problem.variables, | ||
raw_results=sol) | ||
raw_results=problem.variables, |
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.
Why is raw_results=problem.variables
? originally it was raw_results=sol
.
FAILURE = 1 | ||
"""the optimization algorithm ended in a failure.""" | ||
Raises: | ||
None |
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 no exception is raised, section "Raises" should be omitted.
samples: List[Tuple[str, float, float]], | ||
min_eigen_solver_result: Optional[MinimumEigensolverResult] = None) -> None: | ||
min_eigen_solver_result: Optional[MinimumEigensolverResult] = None, | ||
status: OptimizationResultStatus = OptimizationResultStatus.SUCCESS) -> None: |
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.
Please make status
parameter required.
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 do not think there is something like Required
inside the typing provided by python
https://github.com/python/typing/blob/5d2f3b1992b8b66d41fe001df148693b45d30e31/src/typing.py#L23-L98
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 meant, delete default assignment to status
, this will cause the parameter to be required, you have to pass a value for this parameter.
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.
Correct, but I do need to define a default value for it. Should I go with None
?
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.
No, no default value, that's the thing.
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.
Then it throws an error: default value must be specified here Python(parser-16)
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.
Non default(required) parameters go first. So, move it up on the list of parameters, we still can do this.
replacements: Dict[str, Tuple[str, int]], | ||
history: Tuple[List[MinimumEigenOptimizationResult], OptimizationResult]) -> None: | ||
history: Tuple[List[MinimumEigenOptimizationResult], OptimizationResult], | ||
status: OptimizationResultStatus = OptimizationResultStatus.SUCCESS) -> None: |
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.
Same here, status is required.
fx: Optional[np.ndarray] = None, its: Optional[int] = None, | ||
imode: Optional[int] = None, smode: Optional[str] = None) -> None: | ||
imode: Optional[int] = None, smode: Optional[str] = None, | ||
status: OptimizationResultStatus = OptimizationResultStatus.SUCCESS) -> None: |
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.
Same here, please make status
required.
# check for feasibility | ||
status = OptimizationResultStatus.SUCCESS if problem.is_feasible(result.x) \ | ||
else OptimizationResultStatus.INFEASIBLE |
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.
These lines are not required any more
# create a dict containing only unsatisfied variable(s)/constraint(s) | ||
final_dict = {k: v for k, v in {**satisfied_variables, **satisfied_constraints} | ||
.items() if not v} | ||
|
||
# debug | ||
logger.debug("Violations: %s", final_dict) |
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.
final_dict
is created for debug purposes only. Please create it only when debug is enabled or remove this.
@woodsp-ibm, @t-imamichi please take a look, should be fine now. |
Thank you! It looks good. We need just a reno text to explain the new APIs. |
Co-authored-by: Steve Wood <[email protected]>
Co-authored-by: Steve Wood <[email protected]>
Thank you everyone 😄. I am glad that my first ever PR has been accepted and merged successfully. Thank you @adekusar-drl and @t-imamichi for giving me the opportunity to work on this 🎉. |
Congrats! Enjoy Qiskit hacking. |
* Draft for issue#1134 * resolved issue in quadratic_problem.py * incorporated changes as suggested by @adekusar-drl * fix linting issues * added is_feasible method in all optimizers, added changes suggested by @ woodsp-ibm * removed cast import * made changes as per points 1 to 3 * made suggested changes by @adekusar-drl * some cleaning * added reno * fixed reno * Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml Co-authored-by: Steve Wood <[email protected]> * Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml Co-authored-by: Steve Wood <[email protected]> * code review Co-authored-by: Manoel Marques <[email protected]> Co-authored-by: Takashi Imamichi <[email protected]> Co-authored-by: Anton Dekusar <[email protected]> Co-authored-by: Anton Dekusar <[email protected]> Co-authored-by: Steve Wood <[email protected]>
…kit-aqua#1199) * Draft for issue#1134 * resolved issue in quadratic_problem.py * incorporated changes as suggested by @adekusar-drl * fix linting issues * added is_feasible method in all optimizers, added changes suggested by @ woodsp-ibm * removed cast import * made changes as per points 1 to 3 * made suggested changes by @adekusar-drl * some cleaning * added reno * fixed reno * Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml Co-authored-by: Steve Wood <[email protected]> * Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml Co-authored-by: Steve Wood <[email protected]> * code review Co-authored-by: Manoel Marques <[email protected]> Co-authored-by: Takashi Imamichi <[email protected]> Co-authored-by: Anton Dekusar <[email protected]> Co-authored-by: Anton Dekusar <[email protected]> Co-authored-by: Steve Wood <[email protected]>
Summary
draft of #1134
Added is_feasible() function to check whether the returned result of the optimizers and to check whether they are feasible or not
Details and comments
Please check for the same and provide valuable feedback. Thank you 😃