Skip to content

Conversation

mtreinish
Copy link
Member

Summary

This commit fixes an issue in the .sympify() method when building the Sympy expression for a ParameterExpression that was constructed by calling ParameterExpression.subs() with a ParameterVectorElement type. The previous implementation of this method was passing the symbols for substitution by string which required sympy to "parse" the input. The square brackets for indexing the vector element in it's string representation was not valid for sympy to parse and this caused the error. To avoid this problem this commit adjusts the usage to pass a sypy expression object instead of a string. This is the intent of the function as string parsing has other issues in sympy and this was an oversight in the implementation that was doing this. By passing a sympy object to sympy it is able to handle the vector's name with square brackets.

Details and comments

Fixes #14640

This commit fixes an issue in the .sympify() method when building the
Sympy expression for a `ParameterExpression` that was constructed by
calling `ParameterExpression.subs()` with a ParameterVectorElement type.
The previous implementation of this method was passing the symbols for
substitution by string which required sympy to "parse" the input. The
square brackets for indexing the vector element in it's string
representation was not valid for sympy to parse and this caused the
error. To avoid this problem this commit adjusts the usage to pass a
sypy expression object instead of a string. This is the intent of the
function as string parsing has other issues in sympy and this was an
oversight in the implementation that was doing this. By passing a sympy
object to sympy it is able to handle the vector's name with square
brackets.

Fixes Qiskit#14640
@mtreinish mtreinish added this to the 2.1.0 milestone Jun 18, 2025
@mtreinish mtreinish added priority: high stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog labels Jun 18, 2025
@mtreinish mtreinish requested a review from a team as a code owner June 18, 2025 16:55
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

I confirmed it works fine in 2.0.3, so no release note needed because the bug isn't released.

@jakelishman jakelishman enabled auto-merge June 18, 2025 17:06
@jakelishman jakelishman added this pull request to the merge queue Jun 18, 2025
@coveralls
Copy link

Pull Request Test Coverage Report for Build 15738926859

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 15 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.01%) to 87.999%

Files with Coverage Reduction New Missed Lines %
crates/transpiler/src/passes/unitary_synthesis.rs 1 94.57%
crates/qasm2/src/lex.rs 2 93.23%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 15737699692: 0.01%
Covered Lines: 83660
Relevant Lines: 95069

💛 - Coveralls

Merged via the queue into Qiskit:main with commit 69d7dbd Jun 18, 2025
28 checks passed
mergify bot pushed a commit that referenced this pull request Jun 18, 2025
This commit fixes an issue in the .sympify() method when building the
Sympy expression for a `ParameterExpression` that was constructed by
calling `ParameterExpression.subs()` with a ParameterVectorElement type.
The previous implementation of this method was passing the symbols for
substitution by string which required sympy to "parse" the input. The
square brackets for indexing the vector element in it's string
representation was not valid for sympy to parse and this caused the
error. To avoid this problem this commit adjusts the usage to pass a
sypy expression object instead of a string. This is the intent of the
function as string parsing has other issues in sympy and this was an
oversight in the implementation that was doing this. By passing a sympy
object to sympy it is able to handle the vector's name with square
brackets.

Fixes #14640

(cherry picked from commit 69d7dbd)
@mtreinish mtreinish deleted the fix-sympify-subs-vector branch June 18, 2025 17:55
github-merge-queue bot pushed a commit that referenced this pull request Jun 18, 2025
…14642)

This commit fixes an issue in the .sympify() method when building the
Sympy expression for a `ParameterExpression` that was constructed by
calling `ParameterExpression.subs()` with a ParameterVectorElement type.
The previous implementation of this method was passing the symbols for
substitution by string which required sympy to "parse" the input. The
square brackets for indexing the vector element in it's string
representation was not valid for sympy to parse and this caused the
error. To avoid this problem this commit adjusts the usage to pass a
sypy expression object instead of a string. This is the intent of the
function as string parsing has other issues in sympy and this was an
oversight in the implementation that was doing this. By passing a sympy
object to sympy it is able to handle the vector's name with square
brackets.

Fixes #14640

(cherry picked from commit 69d7dbd)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
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 priority: high stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ParameterExpression.sympy() is not working properly
5 participants