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

[TEMPLATES] Rewrite QAOAEmbedding and BasicEntanglerLayers #1138

Merged
merged 90 commits into from
Mar 23, 2021

Conversation

mariaschuld
Copy link
Contributor

@mariaschuld mariaschuld commented Mar 16, 2021

This is the first in a series of PRs which rewrite the templates as classes that inherit from operations.

The refactor should not change any parts in the code base other than the file containing the template, as well as adding a new file for each template with new tests. For now we keep all old tests, unless they fail for obvious reasons and are clearly replaced by the new tests. When all templates are converted, we can delete the old tests altogether.

Before tackling the remaining templates, we can use this first PR to ion out all problems.

Copy link
Contributor

@agran2018 agran2018 left a comment

Choose a reason for hiding this comment

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

Thank you @mariaschuld!, the new structure 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 @mariaschuld!

pennylane/templates/embeddings/qaoa.py Outdated Show resolved Hide resolved
pennylane/templates/layers/basic_entangler.py Outdated Show resolved Hide resolved
pennylane/templates/layers/basic_entangler.py Outdated Show resolved Hide resolved
Copy link
Contributor

@ixfoduap ixfoduap left a comment

Choose a reason for hiding this comment

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

This looks great! I much prefer the operation version of the templates.

I left several questions and comments regarding possible improvements. My main concern is that if initializing these templates is difficult for the user, besides incorporating built-in initialization methods, we should also think of ways of making them easier to initialize

pennylane/templates/embeddings/qaoa.py Show resolved Hide resolved
pennylane/templates/embeddings/qaoa.py Show resolved Hide resolved
pennylane/templates/embeddings/qaoa.py Outdated Show resolved Hide resolved
pennylane/templates/embeddings/qaoa.py Show resolved Hide resolved
pennylane/templates/layers/basic_entangler.py Show resolved Hide resolved
pennylane/templates/layers/basic_entangler.py Outdated Show resolved Hide resolved
num_wires = AnyWires
par_domain = "A"

def __init__(self, weights, wires=None, rotation=None, do_queue=True):
Copy link
Contributor

Choose a reason for hiding this comment

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

I have a question regarding making it easier for users (and devs) to create their own templates. What's the simplest version of a template? To me it seems like all we really need to do is to define an expand method. Other methods are nice but optional. Would you agree?

I'm also curious regarding the difference between expand() and decomposition used for existing operations. Is there a good reason to use both approaches, or is one generally better than the other and should be used throughout?

Copy link
Contributor

Choose a reason for hiding this comment

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

Another question, although maybe this was discussed in the ADR. Do we have a plan for supporting custom gradients?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

decompose is the old way to do it, and expand the way forward. In fact, in tape mode expand is called by PL, and expand then uses decompose where necessary to support the old version.

And the important methods are expand but also preprocess where - depending on the template - a lot of stuff can happen. The best example is Motonnen where there is tons of classical pre-processing of the features.

But if a template does not need preprocessing, this method could be left out, it is not a required attribute of the class.

In contrast to before, the only difference really is the init method. If we rewrite Operations it may one day become even easier...

Comment on lines +41 to +54
QUEUES = [
(1, (1, 1), ["RX", "RY", "RX"]),
(2, (1, 3), ["RX", "RX", "MultiRZ", "RY", "RY", "RX", "RX"]),
(
2,
(2, 3),
["RX", "RX", "MultiRZ", "RY", "RY", "RX", "RX", "MultiRZ", "RY", "RY", "RX", "RX"],
),
(
3,
(1, 6),
["RX", "RX", "RX", "MultiRZ", "MultiRZ", "MultiRZ", "RY", "RY", "RY", "RX", "RX", "RX"],
),
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Black can be so weird sometimes 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes hey? But I thought it is a good opportunity to black the tests...

Copy link
Contributor

@ixfoduap ixfoduap left a comment

Choose a reason for hiding this comment

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

PR looks great, just a couple of changes I would like to see before merging:

  1. Using :meth:~.QAOAEmbedding.weights_normal and similar to link to methods in the docstrings
  2. Unless someone has objections, a dims method or similar to make it easier for users to provide their own initialization. I'm still not comfortable with "fixing" the difficuly with initialization by providing built-in initialization methods
  3. There is a broadcast() on line 150 of basic_entangler.py that shouldn't be there

I'm happy to approve once these changes are made

@mariaschuld
Copy link
Contributor Author

@ixfoduap I changed the weight initialisation methods to return shapes. Let me know if it is good to go!

Base automatically changed from rip-out-core to master March 22, 2021 07:35
josh146 and others added 3 commits March 22, 2021 16:10
…ub.com:PennyLaneAI/pennylane into rewrite_qaoaembedding_and_basicentanglerlayers
Copy link
Contributor

@ixfoduap ixfoduap left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@ixfoduap ixfoduap left a comment

Choose a reason for hiding this comment

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

Wunderbar! 🚀

from pennylane.init import qaoa_embedding_normal
weights = qaoa_embedding_normal(n_layers=2, n_wires=2, mean=0, std=0.2)

shape = QAOAEmbedding.shape(n_layers=2, n_wires=2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Love it!

@mariaschuld mariaschuld merged commit 6aa1228 into master Mar 23, 2021
@mariaschuld mariaschuld deleted the rewrite_qaoaembedding_and_basicentanglerlayers branch March 23, 2021 09:15
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.

6 participants