-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:Qiskit/qiskit-terra into feature/to…
…qm-optional
- Loading branch information
Showing
23 changed files
with
292 additions
and
91 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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,63 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2022. | ||
# | ||
# 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. | ||
|
||
"""A custom python-constraint solver used by the :class:`~.CSPLayout` pass""" | ||
from time import time | ||
|
||
from qiskit.utils import optionals as _optionals | ||
|
||
# This isn't ideal usage because we will import constraint at import time | ||
# but to ensure the CustomSolver class is defined we need to do this. | ||
# If constraint is not installed this will not raise a missing library | ||
# exception until CSPLayout is initialized | ||
if _optionals.HAS_CONSTRAINT: | ||
from constraint import RecursiveBacktrackingSolver | ||
|
||
class CustomSolver(RecursiveBacktrackingSolver): | ||
"""A wrap to RecursiveBacktrackingSolver to support ``call_limit``""" | ||
|
||
def __init__(self, call_limit=None, time_limit=None): | ||
self.call_limit = call_limit | ||
self.time_limit = time_limit | ||
self.call_current = None | ||
self.time_start = None | ||
self.time_current = None | ||
super().__init__() | ||
|
||
def limit_reached(self): | ||
"""Checks if a limit is reached.""" | ||
if self.call_current is not None: | ||
self.call_current += 1 | ||
if self.call_current > self.call_limit: | ||
return True | ||
if self.time_start is not None: | ||
self.time_current = time() - self.time_start | ||
if self.time_current > self.time_limit: | ||
return True | ||
return False | ||
|
||
def getSolution(self, domains, constraints, vconstraints): | ||
"""Wrap RecursiveBacktrackingSolver.getSolution to add the limits.""" | ||
if self.call_limit is not None: | ||
self.call_current = 0 | ||
if self.time_limit is not None: | ||
self.time_start = time() | ||
return super().getSolution(domains, constraints, vconstraints) | ||
|
||
def recursiveBacktracking(self, solutions, domains, vconstraints, assignments, single): | ||
"""Like ``constraint.RecursiveBacktrackingSolver.recursiveBacktracking`` but | ||
limited in the amount of calls by ``self.call_limit``""" | ||
if self.limit_reached(): | ||
return None | ||
return super().recursiveBacktracking( | ||
solutions, domains, vconstraints, assignments, single | ||
) |
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
Oops, something went wrong.