Skip to content

[DEMO] QML for Phase Detection #1335

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

Merged
merged 43 commits into from
Apr 28, 2025

Conversation

SaverioMonaco
Copy link
Contributor

@SaverioMonaco SaverioMonaco commented Mar 25, 2025

Title:
Supervised and Unsupervised Quantum Machine Learning models for the phase detection of the ANNNI spin model

Summary:
Showcase of two foundational models in QML for Phase Detection for Spin Systems. One follows a supervised approach, the other follows an unsupervised approach.

Relevant references:

Possible Drawbacks:
QCNN models is considered now outdated.

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.
    Show a working example of the founding models in QML for Phase Detection. Show how MPS and QML can be used together.

  • AUDIENCE — Who is this for?

    Eg. Chemistry researchers, PL educators, beginners in quantum computing.
    Beginners in Quantum Computing, Chemistry researchers

  • KEYWORDS — What words should be included in the marketing post?
    ANNNI, QCNN, QAD, Phase Detection

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

  • [ x] Tutorial
  • Demo
  • How-to

[sc-80710]

@SaverioMonaco
Copy link
Contributor Author

  • The image annni_sketch.png is a rough sketch for the thumbnail. It needs to be updated by replacing $J2$ with $\kappa$ and $J$ with $J_1$ to ensure consistency with the Hamiltonian described in Eq. (1).

  • The other two images, annni_pd.png and annni_pd_analytical.png, were originally generated using matplotlib.pyplot. I imported them as images to reduce code volume. I will add the code to reproduce these images shortly.

@SaverioMonaco
Copy link
Contributor Author

  • annni_pd.png
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm

# Kosterlitz-Thouless transition line
def kt_transition(k):
    return 1.05 * np.sqrt((k - 0.5) * (k - 0.1))

# Ising transition line
def ising_transition(k):
    return np.where(k == 0, 1, (1 - k) * (1 - np.sqrt((1 - 3 * k + 4 * k**2) / (1 - k))) / np.maximum(k, 1e-9))

# Floating Phase transition line
def bkt_transition(k):
    return 1.05 * (k - 0.5)
    
# Get the phase from the DMRG transition lines
def get_phase(k, h):
    if k < .5 and h < ising_transition(k):
        return 0
    elif k > .5 and h < kt_transition(k):
        return 1
    return 2

# Generate the phase diagram data from DMRG transition lines
ks100, hs100 = np.linspace(0, 1, 100), np.linspace(0, 2, 100)
K100, H100 = np.meshgrid(ks100, hs100)
img_dmrg = np.vectorize(get_phase)(K100, H100)

colors = ['#80bfff', '#fff2a8',  '#80f090', '#da8080',]
phase_labels = ["Ferromagnetic", "Antiphase", "Paramagnetic", "Trash Class",]
cmap = ListedColormap(colors)

bounds = [-0.5, 0.5, 1.5, 2.5, 3.5]
norm = BoundaryNorm(bounds, cmap.N)

# Plot the phase diagram
plt.figure(figsize=(4, 4))
plt.imshow(img_dmrg, cmap=cmap, aspect="auto", origin="lower", extent=[0, 1, 0, 2], norm=norm)

# Plot the transition lines.
k_vals1 = np.linspace(0.0, 0.5, 50)
k_vals2 = np.linspace(0.5, 1.0, 50)
plt.plot(k_vals1, ising_transition(k_vals1), 'k', lw=2)
plt.plot(k_vals2, kt_transition(k_vals2), 'k', lw=2)
plt.plot(k_vals2, bkt_transition(k_vals2), 'k', ls = '--', lw=2)

# Create legend entries
for color, phase in zip(colors, phase_labels[:-1]):
    plt.scatter([], [], color=color, label=phase, edgecolors='black')
plt.plot([], [], 'k', label='Transition Lines')
plt.text(0.15, 0.5, s=r'$h_I$', fontsize=12)
plt.text(0.70, 0.5, s=r'$h_C$', fontsize=12)
plt.text(0.8, 0.2, s=r'$h_{BKT}$', fontsize=12)

plt.legend(), plt.xlabel("k"), plt.ylabel("h"), plt.title("Phase diagram"), plt.show();
  • annni_pd_analytical.png
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm

# Kosterlitz-Thouless transition line
def kt_transition(k):
    return 1.05 * np.sqrt((k - 0.5) * (k - 0.1))

# Ising transition line
def ising_transition(k):
    return np.where(k == 0, 1, (1 - k) * (1 - np.sqrt((1 - 3 * k + 4 * k**2) / (1 - k))) / np.maximum(k, 1e-9))

# Floating Phase transition line
def bkt_transition(k):
    return 1.05 * (k - 0.5)
    
# Get the phase from the DMRG transition lines
def get_phase(k, h):
    if k < .5 and h < ising_transition(k):
        return 0
    elif k > .5 and h < kt_transition(k):
        return 1
    return 2

# Generate the phase diagram data from DMRG transition lines
ks100, hs100 = np.linspace(0, 1, 100), np.linspace(0, 2, 100)
K100, H100 = np.meshgrid(ks100, hs100)
img_dmrg = np.vectorize(get_phase)(K100, H100)

colors = ['#80bfff', '#fff2a8',  '#80f090', '#da8080',]
phase_labels = ["Ferromagnetic", "Antiphase", "Paramagnetic", "Trash Class",]
cmap = ListedColormap(colors)

bounds = [-0.5, 0.5, 1.5, 2.5, 3.5]
norm = BoundaryNorm(bounds, cmap.N)

# Plot the phase diagram
plt.figure(figsize=(4, 4))
plt.imshow(img_dmrg, cmap=cmap, aspect="auto", origin="lower", extent=[0, 1, 0, 2], norm=norm)

# Plot the transition lines.
k_vals1 = np.linspace(0.0, 0.5, 50)
k_vals2 = np.linspace(0.5, 1.0, 50)
plt.plot(k_vals1, ising_transition(k_vals1), 'k', lw=2)
plt.plot(k_vals2, kt_transition(k_vals2), 'k', lw=2)
plt.plot(k_vals2, bkt_transition(k_vals2), 'k', ls = '--', lw=2)

# Create legend entries
for color, phase in zip(colors, phase_labels[:-1]):
    plt.scatter([], [], color=color, label=phase, edgecolors='black')
plt.plot([], [], 'k', label='Transition Lines')

# Highlight analytical points (k = 0 and h = 0)
plt.scatter(np.zeros_like(hs100), hs100, color='red', s=20, label="Analytical Points")  # y-axis points
plt.scatter(ks100, np.zeros_like(ks100), color='red', s=20)  # x-axis point

plt.text(0.15, 0.5, s=r'$h_I$', fontsize=12)
plt.text(0.70, 0.5, s=r'$h_C$', fontsize=12)
plt.text(0.8, 0.2, s=r'$h_{BKT}$', fontsize=12)

plt.legend(), plt.xlabel("k"), plt.ylabel("h"), plt.title("Phase diagram"), plt.show();

@CatalinaAlbornoz CatalinaAlbornoz self-requested a review March 25, 2025 17:38
@CatalinaAlbornoz CatalinaAlbornoz self-assigned this Mar 25, 2025
Copy link

github-actions bot commented Mar 27, 2025

Thank you for opening this pull request.

You can find the built site at this link.

Deployment Info:

  • Pull Request ID: 1335
  • Deployment SHA: b9b821b74315284c59892abafce6aece3c892ebf
    (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.

…progress updates in the State Preparation section, assigned the circuit drawings to specific figures, updated the progress updates for the loss curves
, removed the progress bars and tqdm
Small changes in format like colons, comas, periods, and capital letters. Added two missing words.
@CatalinaAlbornoz CatalinaAlbornoz added the ci:build-all-qml-demos Build all the Demos for this Pull Request label Apr 10, 2025
Incorporating changes from Saverio's notebook to py file. The changes come from three different commits.
Copy link
Contributor Author

@SaverioMonaco SaverioMonaco left a comment

Choose a reason for hiding this comment

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

Final touches:

  • Acknowledgement to DESY
  • Training of the QCNN with the full training set instead of sampling each epoch for a smoother loss curve
  • Adapted hyperparameters of the training function of the QCNN

Copy link
Contributor Author

@SaverioMonaco SaverioMonaco left a comment

Choose a reason for hiding this comment

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

Fix of a comment in the QAD training function

Copy link
Contributor

@CatalinaAlbornoz CatalinaAlbornoz left a comment

Choose a reason for hiding this comment

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

Amazing job @SaverioMonaco !!!
I'm excited to see this demo go live.

@CatalinaAlbornoz CatalinaAlbornoz merged commit 3707e93 into PennyLaneAI:master Apr 28, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:build-all-qml-demos Build all the Demos for this Pull Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants