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

Deprecate init module #1689

Merged
merged 19 commits into from
Sep 28, 2021
Merged

Deprecate init module #1689

merged 19 commits into from
Sep 28, 2021

Conversation

mariaschuld
Copy link
Contributor

@mariaschuld mariaschuld commented Sep 27, 2021

The init module contains simple functions that generate random arrays of the correct dimension for use in templates. This approach was superseded by having the templates define these functions as a class method. But the module is still in the API reference, and may be used in the docs and demos.

This PR adds deprecation warnings to the functions in the init module and replaces its usage in the docs. It also updates the templates docs here and there, because some sections were outdated.

Drawbacks

A separate PR should remove usage in the demos.

@github-actions
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.

:class:`Operation <pennylane.operation.Operation>` class. Unless a device knows how to implement this class on a
quantum computer, it queries the operation's ``expand()`` method, which expresses the operation as a
decomposition of other operations. More precisely, this method returns a :class:`tape <pennylane.tape.QuantumTape>`
instance that represents the queue of the decomposition.
Copy link
Contributor Author

@mariaschuld mariaschuld Sep 27, 2021

Choose a reason for hiding this comment

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

I sneaked in a little overhaul of this and the following file...

Copy link
Member

Choose a reason for hiding this comment

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

Conceptually, there is no difference in PennyLane between a template or ansatz and a gate - they are

This is great to have! I have a received a lot of questions regarding the difference lately.

@@ -37,12 +37,13 @@ def apply(func, qnode_collection):
As we are using the ``'torch'`` interface, we now apply ``torch.sum``
to the QNodeCollection:

>>> cost = qml.apply(torch.sum, qnodes)
>>> cost = qml.collections.apply(torch.sum, qnodes)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was a bug...qml.apply is now another function.

Copy link
Member

Choose a reason for hiding this comment

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

nice catch 😬

>>>
>>> with OperationRecorder() as rec:
>>> qml.templates.layers.StronglyEntanglingLayers(weights, wires=[0, 1])
>>> qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cosmetic change...

@@ -485,7 +485,7 @@ def circuit(weights):
qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1])
return qml.expval(qml.PauliZ(0))

weights = jnp.array(qml.init.strong_ent_layers_normal(n_wires=2, n_layers=2))
weights = jnp.array(np.random.random(qml.templates.StronglyEntanglingLayers.shape(n_layers=2, n_wires=2)))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A bit convoluted, but random number generation with jax is complicated, so I decided to parse here...

@@ -1430,7 +1430,7 @@ def test_qnode_collection_integration(self):

assert qnodes.interface == "tf"

weights = tf.Variable(qml.init.strong_ent_layers_normal(n_wires=2, n_layers=2))
weights = tf.Variable(np.random.random(qml.templates.StronglyEntanglingLayers.shape(n_layers=2, n_wires=2)))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could use the tf.random.uniform function but would have to parse to Variable anyways...

weights = jnp.array(qml.init.strong_ent_layers_normal(n_wires=2, n_layers=2))
weights = jnp.array(
np.random.random(qml.templates.StronglyEntanglingLayers.shape(n_layers=2, n_wires=2))
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here and in the following I am creating numpy arrays and parsing them...the reason to not use native jax.random functions is that some tests compare to data produced with a seed, and they fail in such a case.

@codecov
Copy link

codecov bot commented Sep 27, 2021

Codecov Report

Merging #1689 (4bb020a) into master (9fe7256) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1689   +/-   ##
=======================================
  Coverage   99.20%   99.20%           
=======================================
  Files         201      201           
  Lines       15080    15121   +41     
=======================================
+ Hits        14960    15001   +41     
  Misses        120      120           
Impacted Files Coverage Δ
pennylane/collections/apply.py 100.00% <ø> (ø)
pennylane/collections/dot.py 100.00% <ø> (ø)
pennylane/collections/qnode_collection.py 100.00% <ø> (ø)
pennylane/collections/sum.py 100.00% <ø> (ø)
pennylane/math/quantum.py 100.00% <ø> (ø)
pennylane/optimize/shot_adaptive.py 100.00% <ø> (ø)
pennylane/tape/operation_recorder.py 100.00% <ø> (ø)
...nnylane/templates/layers/particle_conserving_u1.py 100.00% <ø> (ø)
...nnylane/templates/layers/particle_conserving_u2.py 100.00% <ø> (ø)
pennylane/vqe/vqe.py 100.00% <ø> (ø)
... and 1 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 9fe7256...4bb020a. Read the comment docs.

Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

@mariaschuld thanks for getting to this so quickly 🚅 looks great from my end!

Only thing to do before merging is to add this deprecation to the changelog

:class:`Operation <pennylane.operation.Operation>` class. Unless a device knows how to implement this class on a
quantum computer, it queries the operation's ``expand()`` method, which expresses the operation as a
decomposition of other operations. More precisely, this method returns a :class:`tape <pennylane.tape.QuantumTape>`
instance that represents the queue of the decomposition.
Copy link
Member

Choose a reason for hiding this comment

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

Conceptually, there is no difference in PennyLane between a template or ansatz and a gate - they are

This is great to have! I have a received a lot of questions regarding the difference lately.

Comment on lines 293 to 294
shape = BasicEntanglerLayers.shape(n_layers=2, n_wires=n_wires)
weights = np.random.random(size=shape)
Copy link
Member

Choose a reason for hiding this comment

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

really nice!

doc/introduction/templates.rst Outdated Show resolved Hide resolved
qml.inv(bell_state_preparation(wires=[0, 1]))
return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
def circuit(a, b):
MyTemplate(a, b, wires=range(n_wires))
Copy link
Member

Choose a reason for hiding this comment

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

nice catch


.. code-block:: python3

@qml.template
Copy link
Member

Choose a reason for hiding this comment

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

👍 Probably we should also remove this from the codebase lol (@antalszava)

doc/development/adding_templates.rst Outdated Show resolved Hide resolved
@@ -8,21 +8,10 @@ Templates

Copy link
Member

Choose a reason for hiding this comment

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

I haven't visited the template gallery in a while, it's looking really awesome

doc/introduction/templates.rst Outdated Show resolved Hide resolved
@@ -37,12 +37,13 @@ def apply(func, qnode_collection):
As we are using the ``'torch'`` interface, we now apply ``torch.sum``
to the QNodeCollection:

>>> cost = qml.apply(torch.sum, qnodes)
>>> cost = qml.collections.apply(torch.sum, qnodes)
Copy link
Member

Choose a reason for hiding this comment

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

nice catch 😬

@mariaschuld mariaschuld merged commit 4df8dc5 into master Sep 28, 2021
@mariaschuld mariaschuld deleted the deprecate_init_module branch September 28, 2021 12:35
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.

None yet

3 participants