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

TemplateOptimization creates ParameterExpressions with Sympy types when Symengine is available #11106

Closed
jakelishman opened this issue Oct 25, 2023 · 0 comments · Fixed by #11107
Assignees
Labels
bug Something isn't working

Comments

@jakelishman
Copy link
Member

Environment

  • Qiskit Terra version: 0.45.0rc1
  • Python version: 3.10
  • Operating system: macOS

What is happening?

The TemplateOptimization pass can return circuits that contain Sympy-backed ParameterExpressions even when Symengine is installed and available. This doesn't immediately seem to pose any troubles, mostly because Symengine can work with Sympy objects and converts them internally, Symengine is supposed to be used if available and ParameterExpression assumes that its types are all Symengine types if it is available. This can lead to problems when it wants to call Symengine functions that do not have the same arguments / names as their Sympy equivalents.

How can we reproduce the issue?

# Not used, but this example doesn't make sense if you don't have symengine installed.
import symengine

from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.transpiler.passes import TemplateOptimization

a, b = Parameter("a"), Parameter("b")

template = QuantumCircuit(1)
template.p(a, 0)
template.p(-a, 0)
template.rz(a, 0)
template.rz(-a, 0)

circuit = QuantumCircuit(1)
circuit.p(-b, 0)
circuit.p(b, 0)

pass_ = TemplateOptimization(template_list=[template], user_cost_dict={"p": 100, "rz": 1})
out = pass_(circuit)
type(out.data[1].operation.params[0]._symbol_expr)

The output type is some Sympy type, but it should be a Symengine one.

What should happen?

Template optimisation uses sympy internally in order to use sympy.solve, but does not necessarily convert its objects back to the native form before creating ParameterExpression objects. It should arrange for these objects to be cast back to Symengine ones, if it's playing with ParameterExpression at a low level.

Any suggestions?

Tbh, the template optimisation code probably doesn't actually need sympy here at all - if we instead restricted the templates to say that parameters can only be used in linearly (templates are just simple), we'd be able to roll our own basic linear simultaneous-equation solver and skip the heavy sympy import and use.

This code isn't used much though, and it could do with a fairly serious refactor, so that's a very low priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant