-
Notifications
You must be signed in to change notification settings - Fork 575
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
Adds the quantum fourier transform and controlled phase operations #1064
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1064 +/- ##
==========================================
- Coverage 97.75% 97.74% -0.01%
==========================================
Files 153 153
Lines 11517 11579 +62
==========================================
+ Hits 11258 11318 +60
- Misses 259 261 +2
Continue to review full report at Codecov.
|
pennylane/ops/qubit.py
Outdated
from pennylane.operation import (AnyWires, DiagonalOperation, Observable, | ||
Operation) | ||
from pennylane.templates import template | ||
from pennylane.templates.state_preparations import (BasisStatePreparation, | ||
MottonenStatePreparation) | ||
from pennylane.utils import expand, pauli_eigs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I ran isort
)
def decomposition(wires): | ||
num_wires = len(wires) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When running the QFT, is there a way to make it print out the full decomposition when using draw
? Currently it draws this:
0: ──╭QFT──╭┤ State
1: ──├QFT──├┤ State
2: ──╰QFT──╰┤ State
but for the sake of testing it'd be convenient to see the innards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be the moment that our new qml.draw()
shines!
Since it is not dependent on the QNode state (unlike qnode.draw()
), we can add some logic to qml.draw()
to always decompose the QFT operation (or maybe any operation that has a draw_decomp
flag or something).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be great to have it at the level of the draw function itself -
qml.draw(circuit, draw_decomp=True)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even
qml.draw(qnode, decompose=[“QFT”, “Rot”])(params)
Co-authored-by: Olivia Di Matteo <2068515+glassnotes@users.noreply.github.com>
|
||
* Number of wires: 2 | ||
* Number of parameters: 1 | ||
* Gradient recipe: :math:`\frac{d}{d\phi}f(CR_\phi(\phi)) = \frac{1}{2}\left[f(CR_\phi(\phi+\pi/2)) - f(CR_\phi(\phi-\pi/2))\right]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did check this and can confirm that it's unlike the other CRX, CRY, CRZ gates because the 2-parameter shift works:
import pennylane as qml
from pennylane import numpy as np
wires = 2
dev = qml.device("default.qubit", wires=range(wires))
w1 = np.random.random((4, 2, 3))
w2 = np.random.random((4, 2, 3))
@qml.qnode(dev)
def f(x):
qml.templates.StronglyEntanglingLayers(w1, wires=range(wires))
# qml.CRX(x, wires=[0, 1])
qml.ControlledPhaseShift(x, wires=[0, 1])
qml.templates.StronglyEntanglingLayers(w2, wires=range(wires))
return qml.expval(qml.PauliZ(0))
x = 0.3
g1 = qml.grad(f)(x)
g2 = (f(x + np.pi / 2) - f(x - np.pi / 2)) / 2
np.allclose(g1, g2)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for remembering to update the JAX, Autograd, and TF devices!
With qml.math
, we should be able to merge jax_ops, autograd_ops, tf_ops, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @trbromley, really excited to have QFT available 😁
This should be good to merge, I did a local coverage run and coverage looks good - the only bit I'm unsure about is incomplete coverage on |
Context:
Adds two new gates:
QFT
ControlledPhaseShift
(which is required in the decomposition of QFT)Description of the Change:
These two gates have been added as operations in
pennylane/ops/qubit.py
. Both have a defined decomposition and matrix representation. Also added these gates to the supported operations indefault.*
devices.Possible Drawbacks:
We use the
ControlledPhaseShift
which looks similar to the CV gateControlledPhase
. However, the first sentence of theControlledPhaseShift
docstring reads "A qubit controlled phase shift." to hopefully remove some confusion.