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

Fix decomposition for inverse S and T gates #956

Merged
merged 8 commits into from
Dec 9, 2020
Merged

Conversation

josh146
Copy link
Member

@josh146 josh146 commented Dec 8, 2020

Context:

Two bugs were recently discovered in #955.

  • The tape QNode was decomposing gates based on the contents of device.operations. Instead, it should have been using device.supports_operation(), which has additional logic to deal with inverses.

  • The new Operation.expand() method which is used in tape mode was not inverting the decomposed tape if the original op was inverted.

Description of the Change:

  • The tape QNode expand logic has been changed to use device.supports_operation().

  • The Operation.expand() method now inverts the expanded tape if the operation was inverted.

  • Adds tests to make sure this logic works.

Benefits:

  • Devices that don't natively support qml.S.inv() and qml.T.inv() are now provided with the correct decomposition.

  • In tape mode, devices that do support qml.S.inv() and qml.T.inv() do not have these operations decomposed.

Possible Drawbacks: n/a

Related GitHub Issues: Fixes #995

@josh146 josh146 added the bug 🐛 Something isn't working label Dec 8, 2020
@codecov
Copy link

codecov bot commented Dec 8, 2020

Codecov Report

Merging #956 (29d6be4) into master (967ed1d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #956   +/-   ##
=======================================
  Coverage   97.82%   97.82%           
=======================================
  Files         149      149           
  Lines       10236    10237    +1     
=======================================
+ Hits        10013    10014    +1     
  Misses        223      223           
Impacted Files Coverage Δ
pennylane/tape/operation.py 98.41% <100.00%> (+0.05%) ⬆️
pennylane/tape/qnode.py 98.14% <100.00%> (-0.02%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 967ed1d...29d6be4. Read the comment docs.

Copy link
Contributor

@glassnotes glassnotes left a comment

Choose a reason for hiding this comment

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

Thanks for taking care of this so quickly @josh146 !

pennylane/ops/qubit.py Outdated Show resolved Hide resolved
Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

Hi @josh146, good catch with the decomposition! I'm a bit still hesitant with non-tape mode, however.

Would the inverse of the decomposition not be accounted for in the QNode? 🤔

decomposed_ops = op.decomposition(*op.data, wires=op.wires)

When decomposing the inverse of the S or T gates, by taking the inverse of the decomposition on the operation level, it seems that we end up taking the inverse twice overall (and simply applying S or T eventually):

import pennylane as qml

qml.disable_tape()
dev = qml.device('default.qubit.autograd', wires=1)

if "S" in dev.operations:
    dev.operations.remove("S")

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    qml.S(wires=0).inv()
    return qml.expval(qml.PauliZ(0))

circuit()
dev.state
tensor([7.07106781e-01+0.j        , 4.32978028e-17+0.70710678j], requires_grad=True)

The same example of using the decomposition in tape mode:

tensor([7.07106781e-01+0.j        , 4.32978028e-17-0.70710678j], requires_grad=True)

Perhaps adding such examples (or similar) as higher-level integration tests could help here.

@josh146
Copy link
Member Author

josh146 commented Dec 9, 2020

@antalszava you're entirely right, thank you for noticing that! Considering your comment, I've done the following:

  • Removed the inverse branches in the S and T decompositions,

  • Modified Operation.expand() so that if the original operation is inverted, so is the final decomposition,

  • Added a high-level integration test to check all combinations of tape/non-tape mode, and decompositions/non-decompositions.

Copy link
Contributor

@antalszava antalszava 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! 🥇 Great fix 🙂 Was wondering about a test case for T too, probably the S test case sort of covers the specific case though (as T and S are very similar gates and the operations themselves are tested separately).

Comment on lines +1926 to +1928
patched_operations = dev.operations.copy()
patched_operations.remove("S")
monkeypatch.setattr(dev, "operations", patched_operations)
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

@josh146 josh146 merged commit 35ea58d into master Dec 9, 2020
@josh146 josh146 deleted the fix-inverse-s-t branch December 9, 2020 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants