Skip to content
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

Ensure TemplateOptimization returns native-symbolic objects #11107

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

jakelishman
Copy link
Member

Summary

TemplateOptimization currently uses Sympy internally to get access to solvers that have no equivalent in Symengine. Previously, it then did not convert the solutions to its equations back into Symengine format (if appropriate) before passing them on to ParameterExpression, which could lead to different assumptions about the type of the contained objects and bugs if ParameterExpression attempted to use Symengine-specific forms of methods on Sympy objects.

Details and comments

Fix #11106.

Believe it or not, I ended up here while trying to make a sensible implementation of #7107. It turns out there was a long tail of little fixes.

`TemplateOptimization` currently uses Sympy internally to get access to
solvers that have no equivalent in Symengine.  Previously, it then did
not convert the solutions to its equations back into Symengine format
(if appropriate) before passing them on to `ParameterExpression`, which
could lead to different assumptions about the type of the contained
objects and bugs if `ParameterExpression` attempted to use
Symengine-specific forms of methods on Sympy objects.
@jakelishman jakelishman added Changelog: Bugfix Include in the "Fixed" section of the changelog mod: transpiler Issues and PRs related to Transpiler labels Oct 25, 2023
@jakelishman jakelishman added this to the 1.0.0pre1 milestone Oct 25, 2023
@jakelishman jakelishman requested a review from a team as a code owner October 25, 2023 11:09
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

Comment on lines -580 to +601
# Check compatibility by solving the resulting equation
sym_sol = sym.solve(equations, set(temp_symbols.values()))
for key in sym_sol:
try:
sol[str(key)] = ParameterExpression(circ_dict, sym_sol[key])
except TypeError:
return None

if not sol:
# Check compatibility by solving the resulting equation. `dict=True` (surprisingly) forces
# the output to always be a list, even if there's exactly one solution.
sym_sol = sym.solve(equations, set(temp_symbols.values()), dict=True)
if not sym_sol:
# No solutions.
return None

for key in temp_symbols:
fake_bind[key] = sol[str(key)]
# If there's multiple solutions, arbitrarily pick the first one.
sol = {
param.name: ParameterExpression(circ_dict, to_native_symbolic(expr))
for param, expr in sym_sol[0].items()
}
fake_bind = {key: sol[key.name] for key in temp_symbols}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is a bit of a larger change than completely required, but I was having a hard time parsing what was going on with several objects having multiple possible types, being created long before being initialised, and try/except early returns.

Strictly there's a minor (positive) behavioural change here: previously, the try/except handling would cause matches that had multiple possible solutions to fail binding, whereas now it'll pick one of the solutions. This makes the pass strictly more powerful, since templates are isolated from each other based on maximal matching, but in practice, multiple solutions require non-linear parametrised circuits, and there aren't any of those in the template library.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to doc the potential behavioural change in an upgrade release note?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer not adding a release note, because I'd prefer to not commit the API to supporting arbitrarily complex symbolic solutions; we may very well want to replace this handling with simple in-house symbolic linear solvers in the future if we want to extend this to supporting runtime classical parameters.

Let me mark this for merge now to shorten the dependency chain, and if we decide differently, we can revisit.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 6639515070

  • 9 of 10 (90.0%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.002%) to 86.918%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passes/optimization/template_matching/template_substitution.py 9 10 90.0%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 93.76%
crates/qasm2/src/lex.rs 4 90.91%
Totals Coverage Status
Change from base Build 6631999916: -0.002%
Covered Lines: 73973
Relevant Lines: 85107

💛 - Coveralls

Copy link
Member

@1ucian0 1ucian0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks Jake!

Comment on lines -580 to +601
# Check compatibility by solving the resulting equation
sym_sol = sym.solve(equations, set(temp_symbols.values()))
for key in sym_sol:
try:
sol[str(key)] = ParameterExpression(circ_dict, sym_sol[key])
except TypeError:
return None

if not sol:
# Check compatibility by solving the resulting equation. `dict=True` (surprisingly) forces
# the output to always be a list, even if there's exactly one solution.
sym_sol = sym.solve(equations, set(temp_symbols.values()), dict=True)
if not sym_sol:
# No solutions.
return None

for key in temp_symbols:
fake_bind[key] = sol[str(key)]
# If there's multiple solutions, arbitrarily pick the first one.
sol = {
param.name: ParameterExpression(circ_dict, to_native_symbolic(expr))
for param, expr in sym_sol[0].items()
}
fake_bind = {key: sol[key.name] for key in temp_symbols}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to doc the potential behavioural change in an upgrade release note?

@jakelishman jakelishman added this pull request to the merge queue Dec 18, 2023
Merged via the queue into Qiskit:main with commit a29d73f Dec 18, 2023
14 checks passed
@jakelishman jakelishman deleted the template-match-symbols branch December 18, 2023 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog mod: transpiler Issues and PRs related to Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TemplateOptimization creates ParameterExpressions with Sympy types when Symengine is available
4 participants