Skip to content
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

Make qml.QutritBasisStatePreparation JIT compatible #6308

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

willjmax
Copy link
Contributor

This PR makes qml.QutritBasisStatePreparation JIT compatible. Previously, the template used the qml.TShift operator in the decomposition. However, this approach requires non-jittable control flow on the input. To make the template jittable the decomposition is changed to return a qml.ops.QutritUnitary representing a TShift operator raised to some power. This change requires adding matrix_power and eigh to the multi dispatch. This PR addresses sc-70863.

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.70%. Comparing base (b997e0f) to head (93a140f).
Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6308      +/-   ##
==========================================
- Coverage   99.71%   99.70%   -0.01%     
==========================================
  Files         444      444              
  Lines       42252    42261       +9     
==========================================
+ Hits        42132    42138       +6     
- Misses        120      123       +3     

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

@@ -315,6 +315,44 @@ def matmul(tensor1, tensor2, like=None):
return ar.numpy.matmul(tensor1, tensor2, like=like)


@multi_dispatch(argnum=[0, 1])
Copy link
Contributor

Choose a reason for hiding this comment

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

With each new function added, we should include a test in test/math/test_multi_dispactch

Comment on lines +116 to +121
tshift = qml.math.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]])
for wire, state in zip(wires, basis_state):
for _ in range(0, state):
op_list.append(qml.TShift(wire))
mat = qml.math.matrix_power(tshift, state)
op = qml.ops.QutritUnitary(mat, wires=wire)
op_list.append(op)

Copy link
Contributor

Choose a reason for hiding this comment

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

to avoid QubitUnitary (since it is not hardware compatible), you could use this:

qml.pow(qml.TShift(wires = wire),state)

qml.pow returns an operator, so the idea is the same without access the matrix.

Note that if this works, you may not need to add matrix power in the dispach.py file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that the pow method for qml.TShift is not jittable. It uses the pow method from Operator which has control flow to check if the exponent is 0 or 1.

Co-authored-by: Guillermo Alonso-Linaje <65235481+KetpuntoG@users.noreply.github.com>
Copy link
Contributor

@obliviateandsurrender obliviateandsurrender left a comment

Choose a reason for hiding this comment

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

Thanks @willjmax! Some minor queries, before I dig in deeper.

for wire, state in zip(wires, basis_state):
for _ in range(0, state):
op_list.append(qml.TShift(wire))
mat = qml.math.matrix_power(tshift, state)
Copy link
Contributor

Choose a reason for hiding this comment

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

We cannot use qml.math.linalg.matrix_power? 🤔

@@ -112,7 +113,10 @@ def compute_decomposition(basis_state, wires): # pylint: disable=arguments-diff
"""

op_list = []
tshift = qml.math.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]])
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this scale worse or better than the default case? If worse, then should we limit this only to jit case (and use the default behaviour otherwise)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants