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

Fixed Depth Hamiltonian Simulation via Cartan Decomposition demo #1261

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

Conversation

Qottmann
Copy link
Collaborator

Before submitting

Please complete the following checklist when submitting a PR:

  • Ensure that your tutorial executes correctly, and conforms to the
    guidelines specified in the README.

  • Remember to do a grammar check of the content you include.

  • All tutorials conform to
    PEP8 standards.
    To auto format files, simply pip install black, and then
    run black -l 100 path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Title:

Summary:

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 Nov 11, 2024

Thank you for opening this pull request.

You can find the built site at this link.

Deployment Info:

  • Pull Request ID: 1261
  • Deployment SHA: 8045f44dc9e08cacb6cbf247b78007606a45e6c0
    (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.

@dwierichs dwierichs mentioned this pull request Nov 21, 2024
3 tasks
@Qottmann Qottmann changed the base branch from dev to master November 22, 2024 10:17
Comment on lines +310 to +312
h_0_m = Kc_m @ H_m @ Kc_m.conj().T
h_0 = qml.pauli_decompose(h_0_m)
print(len(h_0))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the resulting h_0 is actually not in $\mathfrak{h}$ at all, do you see any obvious mistake in the construction? @dwierichs

Copy link
Contributor

Choose a reason for hiding this comment

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

I got it to work.
I did the following tweaks (took some time 😅 )

  • gammas = [np.pi**i % 2 for i in range(1, len(h)+1)] instead of gammas = [np.pi**i for i in range(len(h))]. The % 2 leads to more uniform gammas, which helps optimization, apparently. This makes sense because we care about numerical stability here and with the previous gammas, some directions had entirely different scale than others. The shift by 1 in the i might be unnecessary.
  • Switch from Adam to L-BFGS. This is more expensive, but seems to optimize significantly better.
  • Optimize for 2k epochs rather than 500. Together with the optimizer change the optimization then takes 60s instead of 8s.
  • Switch h_0_m = Kc_m @ H_m @ Kc_m.conj().T to h_0_m = Kc_m.conj().T @ H_m @ Kc_m (line 314). As we transform the v_m in loss by K_m @ v_m @ K_m.conj().T, we need to do the opposite to H_m when applying the optimized parameters. I'd suggest to transform H_m in loss as well, to be more consistent (I'm aware that this breaks with how the paper writes it down, but transition from paper to code might be nicer than within code?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me know whether I should push these changes.

Comment on lines +373 to +388
Us_kak = jax.vmap(Us_kak)(ts)
Us_trotter50 = jax.vmap(lambda t: qml.matrix(qml.TrotterProduct(H, time=t, n=50, order=4), wire_order=range(n_wires)))(ts)
Us_trotter500 = jax.vmap(lambda t: qml.matrix(qml.TrotterProduct(H, time=t, n=500, order=4), wire_order=range(n_wires)))(ts)

res_kak = 1 - jnp.abs(jnp.einsum("bij,bji->b", Us_exact.conj(), Us_kak)) / 2**n_wires
res_trotter50 = 1 - jnp.abs(jnp.einsum("bij,bji->b", Us_exact.conj(), Us_trotter50)) / 2**n_wires
res_trotter500 = 1 - jnp.abs(jnp.einsum("bij,bji->b", Us_exact.conj(), Us_trotter500)) / 2**n_wires

plt.plot(ts, res_kak, label="KAK")
plt.plot(ts, res_trotter50, "x--", label="50 Trotter steps")
plt.plot(ts, res_trotter500, ".-", label="500 Trotter steps")
plt.ylabel("empirical error")
plt.xlabel("t")
# plt.yscale("log")
plt.legend()
plt.show()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

seems wild to me that both 50 and 500 steps seem to do equally bad 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we want to do time=-t in TrotterProduct because it implements $e^{iHt}$ instead of $e^{-iHt}$ 😬

Copy link
Contributor

Choose a reason for hiding this comment

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

Turns out 50 steps is waaay to precise already :D :)

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.

3 participants