You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Are there any plans to support symbolic coefficients for QubitOperator? #1053
a bit curious as to if anyone has worked on adding support for symbolic coefficients for QubitOperator. I imagine a very simple use case would be in defining coefficient dependant Hamiltonians such as
from sympy import Symbol from openfermion.ops import QubitOperator x = Symbol('x') hamiltonian = x * QubitOperator('X0 X5') + 0.3 * QubitOperator('Z0')
Is there any planned support for this currently? If not, happy to try to work on it.
@andrewprojansky Thank you for your question and offer. If you are still interested and able to work on this, we would be happy to assign it to you. Please let us know.
changed the title [-]Symbolic coefficients for QubitOperator and beyond[/-][+]Are there any plans to support symbolic coefficients for QubitOperator?[/+]on Aug 30, 2025
@andrewprojansky Thank you again for volunteering to take this on. Purely for planning & organization purposes, and without any expectations implied, could you let us know the state of things and whether we should keep you assigned to this issue?
Hi @mhucka, if this issue becomes available, I’d be interested in taking it on.
I’ve been looking for an OpenFermion contribution involving Hamiltonians and operator representations, and I’d be happy to investigate the design and implementation of symbolic coefficients for QubitOperator. Of course, I’ll defer to @andrewprojansky if they are still planning to work on it.
I wanted to check in and share a quick update on what I found when looking into SymbolicOperator and how I plan to approach this.
What I Found
Basic support is already there: SymbolicOperator (and by extension QubitOperator and FermionOperator) can already accept SymPy symbols like Symbol('x') when creating operators or multiplying them by a symbol.
Where it breaks:
Missing import guard: SymPy is imported directly in a few checks. We should wrap this safely so OpenFermion still works fine for users who don't have SymPy installed.
Subtracting / Adding operators: When combining operators (like op_1 + op_2), OpenFermion tries to clean up zero terms using regular number comparisons. For symbolic terms, we need to check if expressions cancel out (like x - x = 0) so zero terms get removed properly without throwing errors.
Plan
Wrap SymPy references in a clean import guard so it remains an optional dependency.
Update term-cleanup logic so symbolic expressions that cancel out to zero are pruned correctly.
Add unit tests for SymbolicOperator and QubitOperator covering:
Creation
Multiplication
Addition
Subtraction
Zero-cancellation with SymPy expressions
I'm currently working on setting up these fixes and tests, but please let me know if this approach sounds good or if there's anything else you'd like me to keep in mind!
Hey @mhucka,
Quick update on this: PR #1452 is now complete. To wrap up what was implemented:
Optional SymPy import guards: Explicitly typed COEFFICIENT_TYPES: Tuple[Type, ...] and added coverage pragmas for non-SymPy fallback branches so environments without SymPy stay clean.
Symbolic zero-pruning: Updated compress() logic and added unit test coverage in symbolic_operator_test.py for unsimplified expressions (e.g., sin(x)^2 + cos(x)^2 - 1) and non-convertible symbolic functions.
Quality gates: Formatted with black, verified pylint (10/10), mypy (0 issues), and hit 100% diff-coverage on all touched lines.
It's ready for review whenever you or the maintainers have a moment to check it out
Activity
mhucka commentedon Aug 28, 2025
@andrewprojansky Thank you for your question and offer. If you are still interested and able to work on this, we would be happy to assign it to you. Please let us know.
[-]Symbolic coefficients for QubitOperator and beyond[/-][+]Are there any plans to support symbolic coefficients for QubitOperator?[/+]mhucka commentedon Oct 21, 2025
@andrewprojansky Very gentle ping …
andrewprojansky commentedon Nov 5, 2025
Hi! Apologies for only seeing this now, happy to take a serious look into it.
mhucka commentedon Jul 27, 2026
@andrewprojansky Thank you again for volunteering to take this on. Purely for planning & organization purposes, and without any expectations implied, could you let us know the state of things and whether we should keep you assigned to this issue?
anishbhujbal7 commentedon Aug 30, 2026
Hi @mhucka, if this issue becomes available, I’d be interested in taking it on.
I’ve been looking for an OpenFermion contribution involving Hamiltonians and operator representations, and I’d be happy to investigate the design and implementation of symbolic coefficients for
QubitOperator. Of course, I’ll defer to @andrewprojansky if they are still planning to work on it.mhucka commentedon Sep 1, 2026
@anishbhujbal7 Thank you for volunteering and your interest. I'll go ahead and assign it to you.
anishbhujbal7 commentedon Sep 7, 2026
Hi @mhucka,
I wanted to check in and share a quick update on what I found when looking into
SymbolicOperatorand how I plan to approach this.What I Found
Basic support is already there:
SymbolicOperator(and by extensionQubitOperatorandFermionOperator) can already accept SymPy symbols likeSymbol('x')when creating operators or multiplying them by a symbol.Where it breaks:
op_1 + op_2), OpenFermion tries to clean up zero terms using regular number comparisons. For symbolic terms, we need to check if expressions cancel out (likex - x = 0) so zero terms get removed properly without throwing errors.Plan
Wrap SymPy references in a clean import guard so it remains an optional dependency.
Update term-cleanup logic so symbolic expressions that cancel out to zero are pruned correctly.
Add unit tests for
SymbolicOperatorandQubitOperatorcovering:I'm currently working on setting up these fixes and tests, but please let me know if this approach sounds good or if there's anything else you'd like me to keep in mind!
fix: handle optional SymPy support and zero-pruning in SymbolicOperat…
anishbhujbal7 commentedon Sep 14, 2026
Hey @mhucka,
Quick update on this: PR #1452 is now complete. To wrap up what was implemented:
Optional SymPy import guards: Explicitly typed COEFFICIENT_TYPES: Tuple[Type, ...] and added coverage pragmas for non-SymPy fallback branches so environments without SymPy stay clean.
Symbolic zero-pruning: Updated compress() logic and added unit test coverage in symbolic_operator_test.py for unsimplified expressions (e.g., sin(x)^2 + cos(x)^2 - 1) and non-convertible symbolic functions.
Quality gates: Formatted with black, verified pylint (10/10), mypy (0 issues), and hit 100% diff-coverage on all touched lines.
It's ready for review whenever you or the maintainers have a moment to check it out
fix(operators): resolve SymPy coefficient compression regression and …
resolve SymPy coefficient compression regression and clean up lint wa…