Skip to content

Are there any plans to support symbolic coefficients for QubitOperator? #1053

Description

@andrewprojansky

Hey all,

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.

Activity

  1. added
    a: functionalityArea: the features and operations implemented in OpenFermion
    on Jun 6, 2025
  2. added theissue type on Jun 6, 2025
  3. mhucka commented on Aug 28, 2025

    @mhucka
    Collaborator

    @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.

  4. changed the title [-]Symbolic coefficients for QubitOperator and beyond[/-] [+]Are there any plans to support symbolic coefficients for QubitOperator?[/+] on Aug 30, 2025
  5. moved this to Awaiting response in Follow-up trackeron Sep 24, 2025
  6. mhucka commented on Oct 21, 2025

    @mhucka
    Collaborator

    @andrewprojansky Very gentle ping …

  7. moved this from Waiting to Nudged in Follow-up trackeron Oct 21, 2025
  8. andrewprojansky commented on Nov 5, 2025

    @andrewprojansky
    Author

    Hi! Apologies for only seeing this now, happy to take a serious look into it.

  9. mhucka commented on Jul 27, 2026

    @mhucka
    Collaborator

    @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?

  10. anishbhujbal7 commented on Aug 30, 2026

    @anishbhujbal7
    Contributor

    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.

  11. mhucka commented on Sep 1, 2026

    @mhucka
    Collaborator

    @anishbhujbal7 Thank you for volunteering and your interest. I'll go ahead and assign it to you.

  12. anishbhujbal7 commented on Sep 7, 2026

    @anishbhujbal7
    Contributor

    Hi @mhucka,

    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

    1. Wrap SymPy references in a clean import guard so it remains an optional dependency.

    2. Update term-cleanup logic so symbolic expressions that cancel out to zero are pruned correctly.

    3. 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!

  13. anishbhujbal7 commented on Sep 14, 2026

    @anishbhujbal7
    Contributor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a: functionalityArea: the features and operations implemented in OpenFermions: awaiting-responseStatus: waiting on a response from someone

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions