-
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.
Make python-constraint optional (#7733)
* Make python-constraint optional Since #7213 we no longer have been using the CSPLayout pass by default in the preset passmanagers or transpile(). This is because it has been superseded by the VF2Layout pass which is now used everywhere. While we will keep the CSPLayout pass around for the forseeable future there is no need to install python-constraint by default anymore since it's only user is the CSPLayout pass, which isn't going to be commonly used anymore now that it's not used in the default compilation path anymore. This commit removes the python-constraint library from the requirements list and makes it an optional dependency. Fixes #7726 * Add docstring for HAS_CONSTRAINT * Add private module to avoid module level optional import This commit splits the custom solver class definition out into a separate private module that is not imported until runtime. This enables us to avoid a module level import for python-constraint meaning we only try to import if something is actually using CSPLayout. * Fix rebase issue * Fix lint * Fix typo in release note Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> * Alphabetize optionals list Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
- Loading branch information
1 parent
e76af20
commit e508729
Showing
7 changed files
with
94 additions
and
48 deletions.
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
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
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
12 changes: 12 additions & 0 deletions
12
releasenotes/notes/constraint-optional-b6a2b2ee21211ccd.yaml
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,12 @@ | ||
--- | ||
upgrade: | ||
- | | ||
The ``python-constraint`` dependency, which is used solely by the | ||
:class:`~.CSPLayout` transpiler pass, is no longer in the requirements list | ||
for the Qiskit Terra package. This is because the :class:`~.CSPLayout` pass | ||
is no longer used by default in any of the preset pass managers for | ||
:func:`~.transpile`. While the pass is still available, if you're using it | ||
you will need to manually install ``python-contraint`` or when you | ||
install ``qiskit-terra`` you can use the ``csp-layout`` extra, for example:: | ||
pip install "qiskit-terra[csp-layout]" |
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