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

Docstrings construct QuantumTape via initialization instead of queuing #4243

Merged
merged 10 commits into from
Jun 15, 2023

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Jun 13, 2023

This PR updates the documentation for our circuit representation QuantumTape to reflect the recommended way of constructing the circuit object.

This change has a range of benefits:

Showing external power users how to construct QuantumTape directly upon initialization

Direct construction avoids the black magic and confusion that occurs with the queuing system. WIth direct construction, what the programmer provides upon initialization is exactly what is in the circuit. The programmer doesn't have to worry about removing a given operation. For example, the circuit could contain both op and adjoint(op).

Direct construction allows the same operation to be used multiple times in a circuit:

op = qml.PauliX(0)
QuantumTape([op, op, op])

It also allows the same operations or measurements to be used in multiple different circuits:

ops = [qml.PauliX(0), qml.S(1)]
tape1 = QuantumTape(ops, [qml.expval(qml.PauliZ(0))])
tape2 = QuantumTape(ops, [qml.expval(qml.PauliX(0))])

Unifying documentation and source code practices

Right now, our documentation demonstrates a different way of doing things than implement in our source code. Our source code and tests never keep around an AnnotatedQueue object after program capture. We always capture the program in an AnnotatedQueue, and then construct a circuit object (QuantumScript) via initialization. This allows us to directly control what is going into the circuit, not carry around information we don't need anymore, and leave the circuit (mostly) untouched after initialization.

Since external power users, external developers, and new developers do not know that we do something completely different in source code, this leads to confusion and new code that goes against our desired best practices.

By having the documentation reflect our recommend best practices, it will help new developers understand how we want to actually write code.

Freeing up required future changes to PennyLane

Since this documentation change gets power users familiar with the "construction upon initialization" syntax, it frees us up to remove AnnotatedQueue from QuantumTape in the future. This change will be required to make QuantumTape immutable.

Immutability will forbid certain classes of bugs, allow QuantumTape to better function in a distributed system, improve integration with jax and other machine learning libraries, and open up performance improvements with caching.

By making our circuit representation independent of queuing, we also make anything that relies on QuantumTape robust to changes to how we perform circuit capture. When we change our user interface and how we capture user circuits, these docstrings should still be valid.

@albi3ro albi3ro requested a review from trbromley June 13, 2023 16:29
@codecov
Copy link

codecov bot commented Jun 14, 2023

Codecov Report

Merging #4243 (474f343) into master (8a75abd) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #4243   +/-   ##
=======================================
  Coverage   99.78%   99.78%           
=======================================
  Files         352      352           
  Lines       31733    31733           
=======================================
  Hits        31665    31665           
  Misses         68       68           
Impacted Files Coverage Δ
pennylane/drawer/tape_mpl.py 100.00% <ø> (ø)
pennylane/drawer/tape_text.py 100.00% <ø> (ø)
pennylane/gradients/__init__.py 100.00% <ø> (ø)
pennylane/gradients/finite_difference.py 99.45% <ø> (ø)
pennylane/gradients/hadamard_gradient.py 100.00% <ø> (ø)
pennylane/gradients/jvp.py 100.00% <ø> (ø)
pennylane/gradients/parameter_shift.py 100.00% <ø> (ø)
pennylane/gradients/parameter_shift_cv.py 99.61% <ø> (ø)
pennylane/gradients/spsa_gradient.py 100.00% <ø> (ø)
pennylane/gradients/vjp.py 100.00% <ø> (ø)
... and 19 more

@albi3ro albi3ro marked this pull request as ready for review June 14, 2023 17:31
@albi3ro albi3ro changed the title [Draft] Docstrings construct QuantumTape via initialization instead of queuing Docstrings construct QuantumTape via initialization instead of queuing Jun 14, 2023
@albi3ro albi3ro requested a review from timmysilv June 14, 2023 17:33
Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

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

exciting stuff!

pennylane/tape/tape.py Outdated Show resolved Hide resolved
pennylane/drawer/tape_text.py Outdated Show resolved Hide resolved
pennylane/drawer/tape_text.py Outdated Show resolved Hide resolved
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
@albi3ro albi3ro requested a review from a team June 15, 2023 13:03
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.

Overall sounds good! 👍

pennylane/drawer/tape_mpl.py Outdated Show resolved Hide resolved
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Formatting suggestions, but otherwise looks good to go 😄

pennylane/drawer/tape_text.py Outdated Show resolved Hide resolved
pennylane/drawer/tape_mpl.py Outdated Show resolved Hide resolved
pennylane/drawer/tape_text.py Outdated Show resolved Hide resolved
pennylane/gradients/__init__.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_transform.py Show resolved Hide resolved
pennylane/transforms/metric_tensor.py Outdated Show resolved Hide resolved
pennylane/transforms/qcut/cutcircuit.py Outdated Show resolved Hide resolved
pennylane/transforms/qcut/cutcircuit.py Outdated Show resolved Hide resolved
pennylane/transforms/qcut/cutcircuit.py Outdated Show resolved Hide resolved
albi3ro and others added 2 commits June 15, 2023 12:05
@albi3ro albi3ro enabled auto-merge (squash) June 15, 2023 16:08
@albi3ro albi3ro merged commit d28563d into master Jun 15, 2023
@albi3ro albi3ro deleted the tape-docstrings branch June 15, 2023 16:36
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.

4 participants