Skip to content

Conversation

doichanj
Copy link
Collaborator

@doichanj doichanj commented May 30, 2025

Summary

This pr fixes #14467 and some issues for compatibility

Details and comments

For #14467, SymbolExpr now accepts back slash when parsing string for ParameterExpression.init.
Previously SymbolExpr accepts _, $\, [, ] for a name of Parameter, now accepting _, $, \, [, ]

Implement Value::Fraction to optimize division by integer instead of converting to Value::Real before, for example a/2 was converted to 0.5*a. So we can now optimize equation with integer fraction generally.
For example, a/4 + 3*a/4 is optimized as a or a/2 - 0.5*a is optimized as 0.

Fix displaying conjugate op as conjugate as similar to sympy instead of conj output before.

@doichanj doichanj requested a review from a team as a code owner May 30, 2025 08:56
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @mtreinish
  • @nkanazawa1989

doichanj added a commit to doichanj/qiskit that referenced this pull request May 30, 2025
@coveralls
Copy link

coveralls commented May 30, 2025

Pull Request Test Coverage Report for Build 15468871640

Details

  • 129 of 291 (44.33%) changed or added relevant lines in 2 files are covered.
  • 12 unchanged lines in 4 files lost coverage.
  • Overall coverage decreased (-0.07%) to 87.971%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/symbol_parser.rs 3 5 60.0%
crates/circuit/src/symbol_expr.rs 126 286 44.06%
Files with Coverage Reduction New Missed Lines %
crates/circuit/src/symbol_parser.rs 1 84.38%
crates/transpiler/src/passes/unitary_synthesis.rs 1 94.57%
crates/circuit/src/symbol_expr.rs 4 73.85%
crates/qasm2/src/lex.rs 6 91.48%
Totals Coverage Status
Change from base Build 15464547777: -0.07%
Covered Lines: 83004
Relevant Lines: 94354

💛 - Coveralls

@@ -0,0 +1,9 @@
---
fixes:
Copy link
Member

Choose a reason for hiding this comment

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

We don't actually need a release note for this. We haven't released 2.1.0 yet so there hasn't been a release with SymbolExpr yet.

alpha = Parameter(r"\alpha")
beta = Parameter(r"\beta")
qc.rz(alpha + beta, 0)
self.assert_roundtrip_equal(qc)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add tests to validate the other fixes you made?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added 2 test cases to test 2 integer fraction add/sub optimization

@mtreinish mtreinish added this to the 2.1.0 milestone May 30, 2025
@mtreinish mtreinish added Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository labels May 30, 2025
@kevinhartman kevinhartman requested a review from mtreinish June 2, 2025 13:49
@mtreinish mtreinish self-assigned this Jun 2, 2025
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

I'm not sure I understand the need for Value::Fraction here. We already have the Div op, how does fraction differ from that. I'm tempted to say we should maybe revert 08e830e from this PR and discuss that in isolation since it does seem unrelated to the fixes for the issues reported in #14467.

I am also still worried the test coverage here is a bit low. We're adding a new value type internally so we should try to ensure this is fully exercised. We should try to ensure we have testing coverage for this path in some of the other paths. Specifically I'm thinking the string formatting and ParameterExpression.sympify

@@ -106,6 +102,9 @@ fn parse_unary(s: &str) -> IResult<&str, SymbolExpr, VerboseError<&str>> {
"log" => UnaryOp::Log,
"exp" => UnaryOp::Exp,
"sign" => UnaryOp::Sign,
"conj" => UnaryOp::Conj, // this is for backward compatibility
Copy link
Member

Choose a reason for hiding this comment

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

Backwards compatibility with what? Between Qiskit and sympy or something else. If it's just with earlier versions of this PR I don't think we need this because it's never been released.

Copy link
Collaborator Author

@doichanj doichanj Jun 3, 2025

Choose a reason for hiding this comment

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

Without Value::Fraction I was feeling it is difficult to optimize only by BinaryOp::Div. For example, we could not calculate a/3 + 2*a/3 as a without Value::Fractional because 2*a/3 is represented by 2 Binary ops, Binary(BinaryOp:Div, Binary(BinaryOp::Mul, 2, a), 3).
By using Value::Fraction it is easy to understand we can add 2 binaries; Binary(BinaryOp::Mul, 1/3, a) + Binary(BinaryOp::Mul, 2/3, a)

I think we can separate this issue into other PR, but this issue is also urgent should be fixed before Qiskit 2.1

conj is used in previous SymbolExpr PR, but is not released yet so can we just replace as conjugate ?

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

I removed the Fraction piece from this, the equality operator matching division expressions that are mathematically equivalent isn't super critical for 2.1, in general __eq__ doesn't handle equivalent expressions anyway so I'm fine with that as a known limitation. I worried more about adding complexity to the symbol expr code at this point in the release. We can look at adding that in 2.2. Without that I think this lgtm I added a small test to test backslashes with $ but otherwise it's just a rebase to merge.

@mtreinish mtreinish enabled auto-merge June 5, 2025 14:03
@mtreinish mtreinish added this pull request to the merge queue Jun 5, 2025
Merged via the queue into Qiskit:main with commit 0b1fcef Jun 5, 2025
26 checks passed
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
* Add fixes to SymbolExpr

* format

* format

* add comment on test

* Add test cases for optimization of integer fraction

* implement Value::Fraction to optimize integer expressions

* format

* Revert "format"

This reverts commit 9abb5ec.

* Revert "implement Value::Fraction to optimize integer expressions"

This reverts commit 08e830e.

* Remove conj we don't have backwards compatibility with earlier name

* Add dedicated test for math mode with latex style parameter names

---------

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: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QPY load error with backslash in parameter expression
4 participants