QuantumBFS / QuantumBFS/SSSS

Possible updates to PyTorch 1.10

Open Beginner friendly
#9 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
180
Forks
55
Avg merge
1m
Merged PRs (30d)
2

Description

Thank you for your lecture, it is very useful.
I know it has already been 5 years since these note originally released. Some functions in PyTorch has been deprecated.
The followings are possible updates to PyTorch 1.10 and Python3.11
schrodinger.py

schrodinger.py

Replaced torch.symeig(H, eigenvectors=True) → ✅ torch.linalg.eigh(H)
#########################################################

import numpy as np 
import torch
torch.set_default_dtype(torch.float64)
import torch.nn as nn
import matplotlib.pyplot as plt

class Schrodinger1D(nn.Module):
    def __init__(self, xmesh):
        super(Schrodinger1D, self).__init__()
        
        self.xmesh = xmesh
        self.potential = nn.Parameter(xmesh**2)

        nmesh = xmesh.shape[0]
        h2 = (xmesh[1] - xmesh[0]) ** 2
        self.K =   torch.diag(1/h2 * torch.ones(nmesh, dtype=xmesh.dtype), diagonal=0) \
                 - torch.diag(0.5/h2 * torch.ones(nmesh-1, dtype=xmesh.dtype), diagonal=1) \
                 - torch.diag(0.5/h2 * torch.ones(nmesh-1, dtype=xmesh.dtype), diagonal=-1)

    def _solve(self):
        H = torch.diag(self.potential) + self.K
        eigvals, eigvecs = torch.linalg.eigh(H)  # Replaced deprecated symeig
        return eigvecs[:, 0]  # Ground state (corresponding to smallest eigenvalue)

    def forward(self, target):
        psi = self._solve()
        return (psi**2 - target).abs().sum()

    def plot(self, target):
        psi = self._solve().detach()

        plt.cla()
        plt.plot(self.xmesh.numpy(), target.numpy(), label='Target Density')
        plt.plot(self.xmesh.numpy(), psi.square().numpy(), label='Current Density')
        plt.plot(self.xmesh.numpy(), self.potential.detach().numpy()/10000, label='Potential (V/10000)')
        plt.legend()
        plt.draw()

if __name__ == '__main__':
    # Prepare mesh and target density
    xmin, xmax, Nmesh = -1, 1, 500
    xmesh = torch.linspace(xmin, xmax, Nmesh)
    
    target = torch.zeros(Nmesh)
    idx = torch.where(torch.abs(xmesh) < 0.5)
    target[idx] = 1. - torch.abs(xmesh[idx])
    target = (target / torch.norm(target))**2
    
    model = Schrodinger1D(xmesh)
    optimizer = torch.optim.LBFGS(
        model.parameters(), 
        max_iter=10, 
        tolerance_change=1E-7, 
        tolerance_grad=1E-7, 
        line_search_fn='strong_wolfe'
    )

    def closure():
        optimizer.zero_grad()
        loss = model(target)  # Density difference 
        loss.backward()
        return loss 

    plt.ion()
    for epoch in range(50):
        loss = optimizer.step(closure)
        print(epoch, loss.item())
        model.plot(target)
        plt.pause(0.01)

    plt.ioff()
    model.plot(target)
    plt.show()

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with 1_deep_learning/schrodinger.py and review the use of deprecated PyTorch APIs, especially torch.symeig. Run the script with the stated Python and PyTorch versions to verify that the update works; done means the example runs without deprecated calls or compatibility errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.