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

Update QAOA-Maxcut demo with a few good practices #1251

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dwierichs
Copy link
Contributor

Title:

Summary:

Just updating the MaxCut demo with a few tiny changes that incorporate good practices in Python/PennyLane, which have changed since the creation back in 2019.

Relevant references:

Possible Drawbacks:

Related GitHub Issues:


If you are writing a demonstration, please answer these questions to facilitate the marketing process.

  • GOALS — Why are we working on this now?

    Eg. Promote a new PL feature or show a PL implementation of a recent paper.

  • AUDIENCE — Who is this for?

    Eg. Chemistry researchers, PL educators, beginners in quantum computing.

  • KEYWORDS — What words should be included in the marketing post?

  • Which of the following types of documentation is most similar to your file?
    (more details here)

  • Tutorial
  • Demo
  • How-to

Copy link

👋 Hey, looks like you've updated some demos!

🐘 Don't forget to update the dateOfLastModification in the associated metadata files so your changes are reflected in Glass Onion (search and recommendations).

Please hide this comment once the field(s) are updated. Thanks!

Copy link

github-actions bot commented Oct 30, 2024

Thank you for opening this pull request.

You can find the built site at this link.

Deployment Info:

  • Pull Request ID: 1251
  • Deployment SHA: 4599e6cba84776d9358826f2bd5167bd37cae8e4
    (The Deployment SHA refers to the latest commit hash the docs were built from)

Note: It may take several minutes for updates to this pull request to be reflected on the deployed site.

Comment on lines -6 to -15

.. meta::
:property="og:description": Implementing the quantum approximate optimization algorithm using PennyLane to solve the MaxCut problem.
:property="og:image": https://pennylane.ai/qml/_static/demonstration_assets/qaoa_maxcut_partition.png

.. related::
tutorial_qaoa_intro Intro to QAOA

*Author: Angus Lowe — Posted: 11 October 2019. Last updated: 13 April 2021.*

Copy link
Contributor

Choose a reason for hiding this comment

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

@ikurecic could you confirm that this should be removed? I'm not particularly familiar with how qml metadata is stored. I know about the JSON files, but I'm not aware if any other metadata is used or not.

Comment on lines +161 to +163
qml.CNOT(wires=edge)
qml.RZ(gamma, wires=edge[1])
qml.CNOT(wires=edge)
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this just an IsingZZ?

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, up to a global phase. I was considering to change it but the text explicitly describes this and has a circuit diagram, so I thought it could be fine without change.

But I'm all for reduction if we don't consider that too invasive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So should I change it ? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I won't because I'm not the original author, and Angus put effort into making that figure etc. I'll just add a comment in a note environment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for not responding, I think it's good to leave it as is based on the context you gave. Thanks :)

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.

The graphs with the distribution for the bit-strings looks great! However, the actual numerical values in the demo output are dramatically different:

Published version:

p=1
Objective after step     5:  2.0000000
Objective after step    10:  2.0000000
Objective after step    15:  4.0000000
Objective after step    20:  3.0000000
Objective after step    25:  1.0000000
Objective after step    30:  2.0000000
Optimized (gamma, beta) vectors:
[[-0.82383311]
 [-1.13584295]]
Most frequently sampled bit string is: 0101

p=2
Objective after step     5:  4.0000000
Objective after step    10:  3.0000000
Objective after step    15:  4.0000000
Objective after step    20:  3.0000000
Objective after step    25:  4.0000000
Objective after step    30:  3.0000000
Optimized (gamma, beta) vectors:
[[ 0.87696449  1.31508883]
 [ 0.90138291 -0.34955715]]
Most frequently sampled bit string is: 0101

After this PR:

p=1
Objective after step     5:  0.5000000
Objective after step    10:  0.5000000
Objective after step    15:  0.5000000
Objective after step    20:  2.5000000
Objective after step    25:  0.5000000
Objective after step    30:  2.5000000
Optimized (gamma, beta) vectors:
[[-0.90759251]
 [-1.2352846 ]]
Most frequently sampled bit string is: 1010

p=2
Objective after step     5:  0.5000000
Objective after step    10:  2.5000000
Objective after step    15:  2.5000000
Objective after step    20:  2.5000000
Objective after step    25:  2.5000000
Objective after step    30:  2.5000000
Optimized (gamma, beta) vectors:
[[ 1.54174446 -0.7153231 ]
 [ 0.35679571 -0.72652338]]
Most frequently sampled bit string is: 0101

I think this is happening because the device only has one shot. Before, that would give 4 samples due to 4 executions, but now there's only one sample. It's surprising to see that the distribution still looks as good as it does 😆. I'm guessing changing this line to the following will fix it:

dev = qml.device("lightning.qubit", wires=n_wires, shots=len(graph))

@dwierichs
Copy link
Contributor Author

dwierichs commented Oct 31, 2024

Thanks for the analysis @mudit2812 , that's a good catch!

  • Overall, this demo is stochastic and seeds don't seem to be fixed. So with single-shot expectation values, even the original version varies wildly (The optimized parameters, for example, look almost unrelated between runs 😅 )
  • The variance for the previous approach is $V_1=\sum_{(a,b)\in\mathcal{E}}1-\langle Z_aZ_b\rangle^2$ where $\mathcal{E}$ is the set of edges. For the new approach it is

$$V_2=|\mathcal{E}|+\sum_{(a,b)\in\mathcal{E}'} \langle Z_aZ_b\rangle-\left(\sum_{(a,b)\in\mathcal{E}}\langle Z_aZ_b\rangle\right)^2,$$ $$=V_1+\sum_{(a,b)\in\mathcal{E}'} \langle Z_aZ_b\rangle-\sum_{(a,b)\neq(c,d)\in\mathcal{E}}\langle Z_aZ_b\rangle\langle Z_cZ_d\rangle,$$

where $\mathcal{E}'$ are pairs of next-nearest neighbours. So you are right that the variances differ a lot, and this actually makes me concerned about the "simplification" I'm proposing here. It is more of a switch in measurement strategy, and one that is highly non-trivial to see through (in particular, while allocating len(graph) shots makes sense coming from the current version, I'm not sure whether it can be motivated from the measurement problem itself? 🤔 )

Mhm.

@mudit2812
Copy link
Contributor

Thanks @dwierichs . It sounds like a non-trivial change. Maybe it makes sense to go over the demo body as well just to make sure that the text is not inconsistent with the new implementation. Otherwise, happy to approve.

@dwierichs
Copy link
Contributor Author

@mudit2812 The variance discussion stands, but I actually noticed a small bug I encoded with the change:
I basically changed the objective to $C=\frac{1}{2}(1-\sum_{(j,k)} \langle Z_j Z_k\rangle)$ but the correct thing of course is
$C=\frac{1}{2}\sum_{(j,k)} (1-\langle Z_j Z_k\rangle) = \frac{1}{2}( |\mathcal{E}| - \sum_{(j,k)} \langle Z_j Z_k\rangle)$. I fixed that and everything works as expected as as before now :) In particular, perfect bitstrings are being sampled in the end, as anticipated by the main text.
I also went through the main text and adapted where required for this update.

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.

LGTM!

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.

2 participants