-
Notifications
You must be signed in to change notification settings - Fork 436
fix: handle optional SymPy support and zero-pruning in SymbolicOperat… #1452
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
Open
anishbhujbal7
wants to merge
4
commits into
quantumlib:main
Choose a base branch
from
anishbhujbal7:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6258eb6
fix: handle optional SymPy support and zero-pruning in SymbolicOperat…
anishbhujbal7 1e102cc
fix(symbolic_operator): resolve linting errors and import order
anishbhujbal7 65877ef
style: apply black formatting and add mypy type annotation for COEFFI…
anishbhujbal7 7e9d104
style: apply black formatting and add mypy annotation for COEFFICIENT…
anishbhujbal7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,3 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1447,3 +1447,37 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
| def test_tracenorm_zero(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op = MockOperator2() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertFalse(op.induced_norm()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_symbolic_operator_sympy_coefficients(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import sympy | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| x = sympy.Symbol('x') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| y = sympy.Symbol('y') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # 1. Initialize MockOperator1 with a SymPy symbol coefficient | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op1 = MockOperator1(((0, 1), (1, 0)), x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(op1.terms[((0, 1), (1, 0))], x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # 2. Scalar multiplication with a SymPy expression | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op2 = op1 * (2 * y) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(op2.terms[((0, 1), (1, 0))], 2 * x * y) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # 3. Addition of symbolic operators | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op3 = MockOperator1(((0, 1), (1, 0)), y) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op_sum = op1 + op3 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(op_sum.terms[((0, 1), (1, 0))], x + y) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # 4. Symbolic zero cancellation (x - x -> 0 term should be pruned) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op4 = MockOperator1(((0, 1), (1, 0)), -x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op_cancel = op1 + op4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(len(op_cancel.terms), 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_compress_sympy_coefficients(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import sympy | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| x = sympy.Symbol('x') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # Operator with x - x (evaluates to 0 on compress) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op = MockOperator1(((0, 1), (1, 0)), x - x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| op.compress() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(len(op.terms), 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1475
to
+1483
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent future regressions where small imaginary or real parts of symbolic expressions are not pruned during compression, we should add a test case that specifically verifies this behavior.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The refactored
compressmethod introduces a regression where small imaginary and real parts of SymPy expressions (both symbolic and numeric) are no longer pruned. The original implementation usedsympy.simplify(sympy.im(coeff) <= abs_tol) == Trueto discard negligible imaginary/real parts, which is crucial for maintaining hermiticity and numerical stability in downstream calculations.By simplifying the coefficient first and then applying the original pruning logic, we can fix the
x - xzero-pruning issue while preserving the real/imaginary part pruning.References