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

[TAPE] Incorporate the tape refactor into core #823

Merged
merged 48 commits into from
Sep 28, 2020
Merged

[TAPE] Incorporate the tape refactor into core #823

merged 48 commits into from
Sep 28, 2020

Conversation

josh146
Copy link
Member

@josh146 josh146 commented Sep 25, 2020

Context:

  • This is the final tape refactor PR. In this PR, the many tape components, including interfaces, measurements, operations, QNodes, and the tapes, are moved out of the pennylane.beta, and better organized into a new pennylane.tape subpackage. We also provide helper functions here to make it easier to use the new tape core if wanted.

Description of the Change:

  • All tape components were moved into the new pennylane.tape directory. The tests were similarly restructured.

  • Documentation has been written, see it here: https://pennylane--823.org.readthedocs.build/en/823/code/qml_tape.html

  • Three new mocks were added to the QuantumTape() context:

    # create mock measurement functions
    mocks += [mock.patch.object(qml, "expval", qml.tape.measure.expval)]
    mocks += [mock.patch.object(qml, "var", qml.tape.measure.var)]
    mocks += [mock.patch.object(qml, "sample", qml.tape.measure.sample)]
    mocks += [mock.patch.object(qml, "probs", qml.tape.measure.probs)]

    That is, within a tape context, calling qml.expval et al. will instead result in a call to the functions within the new qml.tape.measure module. After the context closes, these locations will resume pointing to the original core components.

  • It is now possible to create a tape QNode by simply changing the qnode decorator to qml.tape.qnode:

    import pennylane as qml
    from pennylane import numpy as np
    dev = qml.device("default.qubit", wires=1)
    
    @qml.tape.qnode(dev)
    def circuit(x):
        qml.RX(x, wires=0)
        return qml.expval(qml.PauliZ(0))
    
    >>> circuit(0.5)
    0.8775825618903726

    This allows a single Python file to contain a mixture of old and new QNodes.

  • Finally, two new convenience functions have been added; qml.enable_tape() and qml.disable_tape():

    dev = qml.device("default.qubit", wires=1)
    
    qml.enable_tape()
    
    @qml.qnode(dev)
    def circuit(x):
        qml.RX(x, wires=0)
        return qml.expval(qml.PauliZ(0))
    
    >>> print(circuit(0.5))
    0.8775825618903726
    >>> print(type(circuit))
    <class 'pennylane.tape.qnode.QNode'>
    >>> print(circuit.qtape)
    <AutogradQuantumTape: wires=[0], params=1>
    
    qml.disable_tape()
    
    @qml.qnode(dev)
    def circuit(x):
        qml.RX(x, wires=0)
        return qml.expval(qml.PauliZ(0))
    
    >>> print(circuit(0.5))
    0.8775825618903726
    >>> print(type(circuit))
    <class 'pennylane.interfaces.autograd.to_autograd.<locals>.AutogradQNode'>

    This allows entire scripts/files to be globally switched to using the new tape core, by simply adding qml.enable_tape() near the top. This will be useful with testing compatibility, and benchmarking.

  • In tape mode, we allow qml.state() to be used. Outside of tape mode, an error will be raised.

Benefits: n/a

Possible Drawbacks:

In tape-mode, the QNode does not yet have feature-parity with the standard PennyLane QNode. Features currently not available in tape mode include:

  • Circuit drawing and visualization

  • Metric tensor computation

  • The ability to automatically extract the layer structure of variational circuits

  • Hashing and serialization.

  • The qml.qnn module

These can all be done in subsequent PRs.

Related GitHub Issues: n/a

@josh146 josh146 changed the base branch from master to tape-pr-9 September 25, 2020 07:07
@codecov
Copy link

codecov bot commented Sep 25, 2020

Codecov Report

Merging #823 into master will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #823      +/-   ##
==========================================
+ Coverage   91.05%   91.09%   +0.03%     
==========================================
  Files         129      130       +1     
  Lines        8678     8701      +23     
==========================================
+ Hits         7902     7926      +24     
+ Misses        776      775       -1     
Impacted Files Coverage Δ
pennylane/tape/queuing.py 97.05% <ø> (ø)
pennylane/tape/tapes/reversible.py 100.00% <ø> (ø)
pennylane/__init__.py 63.52% <100.00%> (+0.87%) ⬆️
pennylane/tape/__init__.py 100.00% <100.00%> (ø)
pennylane/tape/circuit_graph.py 100.00% <100.00%> (ø)
pennylane/tape/interfaces/__init__.py 100.00% <100.00%> (ø)
pennylane/tape/interfaces/autograd.py 100.00% <100.00%> (ø)
pennylane/tape/interfaces/tf.py 96.22% <100.00%> (ø)
pennylane/tape/interfaces/torch.py 100.00% <100.00%> (ø)
pennylane/tape/measure.py 96.87% <100.00%> (ø)
... and 7 more

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 9ac47c8...8b40221. Read the comment docs.

Co-authored-by: antalszava <antalszava@gmail.com>
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 this looks good overall! Left some questions/suggestions, would be keen to hear your thoughts, but overall would be good from my side. Also, I think there might be a couple of places where a search and replace might be needed to cover the qml.variable, qml.various occurrences in the tests.

A thing I was wondering about is if we'd like to move tape out of beta though it would stay an experimental feature. 🤔

@josh146
Copy link
Member Author

josh146 commented Sep 25, 2020

Also, I think there might be a couple of places where a search and replace might be needed to cover the qml.variable, qml.various occurrences in the tests.

Thanks for catching this Antal, I did use a lot of find and replace here 😬

A thing I was wondering about is if we'd like to move tape out of beta though it would stay an experimental feature. 🤔

It is definitely still an experimental feature. Perhaps we could raise a warning when the tape is enabled? Or would that be too annoying?

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.

Perhaps we could raise a warning when the tape is enabled? Or would that be too annoying?

Hmm, it could indeed be a bit repetitive each time. I'm sort of on the fence. If users get to know about the features brought with tape, they might already know that it's experimental, so it should be fine. Perhaps other reviewers would have further suggestions.

Thanks for the answers! Overall this looks good to me 😊

Copy link
Contributor

@trbromley trbromley left a comment

Choose a reason for hiding this comment

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

Thanks @josh146, looks awesome!

I noticed a few spots in test docstrings where qml. had been added (my favourite was qml.various). I left (a few too many) suggestions correcting them - sorry for the spam! (Just noticed @antalszava pointed it out too)

doc/code/qml_tape.rst Show resolved Hide resolved
doc/code/qml_tape.rst Outdated Show resolved Hide resolved
Comment on lines +48 to +49
>>> with tf.GradientTape() as tape:
... res = circuit(params)
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor thing: could a novice reader get confused at seeing another "tape"-like object and be mixed up between our tape and the TF tape? A remedy to this could be to use the autograd interface for this example.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I agree!

doc/code/qml_tape.rst Outdated Show resolved Hide resolved
doc/code/qml_tape.rst Outdated Show resolved Hide resolved
tests/tape/tapes/test_qubit_param_shift.py Outdated Show resolved Hide resolved
tests/tape/tapes/test_qubit_param_shift.py Outdated Show resolved Hide resolved
tests/tape/tapes/test_tape.py Outdated Show resolved Hide resolved
tests/tape/tapes/test_tape.py Outdated Show resolved Hide resolved
tests/tape/tapes/test_tape.py Outdated Show resolved Hide resolved
@josh146
Copy link
Member Author

josh146 commented Sep 27, 2020

Thanks @trbromley and @antalszava! I've incorporated all your changes.

Apologies for the qml.variable and qml.various --- my text editors refactoring mode was... more zealous than I expected. I've gone through and undone any find-and-replaces inside docstrings and comments.

doc/code/qml_tape.rst Outdated Show resolved Hide resolved
Co-authored-by: Maria Schuld <mariaschuld@gmail.com>
import modified measurement functions within the quantum tape:

>>> from pennylane.beta.queuing import expval, var, sample, probs
As the quantum tape is a *beta* feature. See :mod:`pennylane.tape`
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe check the sentence above again, is the . supposed to be a comma?

sample(qml.PauliZ(0))
sample(qml.PauliX(1))
qml.sample(qml.PauliZ(0))
qml.sample(qml.PauliX(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Hope you had a good search & replace function there :)

Copy link
Contributor

@mariaschuld mariaschuld left a comment

Choose a reason for hiding this comment

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

Really a great idea to have tape mode! We can now extend the tape with new feature, staying in the experimental mode.

I checked out the branch and played around a bit, works like a charm.

@josh146 josh146 merged commit 379c45a into master Sep 28, 2020
@josh146 josh146 deleted the tape-pr-10 branch September 28, 2020 08:16
@co9olguy
Copy link
Member

🍾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-ready 👌 PRs which are ready for review by someone from the core team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants