Skip to content

Conversation

@albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Aug 7, 2025

Context:

The following code caused recursion errors:

H = 0.25 * qml.Z(5) + 0.25* qml.Z(6) + 0.5 * qml.X(5) @ qml.X(6)

dev = qml.device("default.qubit", wires = range(7))
walk_powers_1 = [qml.pow(qml.Qubitization(H, control = [3,4]), i) for i in range(0,3)]
walk_powers_2 = [qml.pow(qml.adjoint(qml.Qubitization(H, control = [3,4])), i) for i in reversed(range(1,3))]
lcu_ops = walk_powers_2 + walk_powers_1
bessel_1 = [0.7651976865579666, 0.44005058574493355, 0.1149034849319005]
all_coeffs = [(-1)**(i+1)*bessel_1[-i] for i in range(1,3)] + bessel_1
lcu_coeffs = np.array([all_coeffs[i]*1j**(i-2) for i in range(len(all_coeffs))])
lcu = qml.dot(lcu_coeffs, lcu_ops)

@qml.qnode(dev)
def prep_sel_prep_walk():
    """
    Quantum circuit that applies PrepSelPrep to the Walk operator
    to generate the Jacobi Anger expansion to order 2.
    Returns:
    - (np.ndarray): The quantum state 
    """

    qml.PrepSelPrep(lcu, control = [0,1,2])

    return qml.state()

encoded_matrix = qml.matrix(prep_sel_prep_walk, wire_order=range(7))()[:4, :4]
print("The encoded matrix is: \n", encoded_matrix)

With a minimal non-working example of:

class DummyOp(qml.operation.Operator):
    pass

simplified = (DummyOp(0) ** 2).simplify()

The pow was simplifying to a prod, and then the prod was simplifying to a power.

Description of the Change:

Simplify the entrants to prod instead of the prod itself.

Benefits:

Possible Drawbacks:

Related GitHub Issues:

Fixes #8045 [sc-97284]

Copy link
Contributor

@andrijapau andrijapau left a comment

Choose a reason for hiding this comment

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

Should we run benchmarks against this PR?

@albi3ro
Copy link
Contributor Author

albi3ro commented Aug 7, 2025

Should we run benchmarks against this PR?

This didn't show up for so long because it doesn't effect any of the normal core operators that define a pow method as a single output. If it effected any benchmarks, then benchmarks probably would have been failing.

@codecov
Copy link

codecov bot commented Aug 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.68%. Comparing base (e03512c) to head (17c3836).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8044      +/-   ##
==========================================
- Coverage   99.68%   99.68%   -0.01%     
==========================================
  Files         543      543              
  Lines       56215    56216       +1     
==========================================
  Hits        56040    56040              
- Misses        175      176       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@albi3ro albi3ro requested a review from andrijapau August 7, 2025 18:54
Copy link
Contributor

@andrijapau andrijapau left a comment

Choose a reason for hiding this comment

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

Looks good to me

@mudit2812 mudit2812 self-requested a review August 8, 2025 16:44
@mudit2812
Copy link
Contributor

Have we validated that this fixes the codercise? @alvaro-at-xanadu are you able to run the github action for the codercises using this branch?

@Alex-Preciado Alex-Preciado added the urgent Mark a pull request as high priority label Aug 9, 2025
@andrijapau andrijapau added this pull request to the merge queue Aug 11, 2025
@andrijapau
Copy link
Contributor

Adding to merge queue w/ approval from @albi3ro 😄

Merged via the queue into master with commit d625107 Aug 11, 2025
55 checks passed
@andrijapau andrijapau deleted the fix-pow-simplify branch August 11, 2025 18:18
@mlxd mlxd changed the title fix recursion error from power simpliciation fix recursion error from power simplification Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

urgent Mark a pull request as high priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] qml.pow and qml.prod simplify into each other creating excessive arithmetic depth

6 participants