Skip to content

初步修改hubbard为free_fermion - #63

Merged
hzhangxyz merged 1 commit into
mainfrom
dev/add_free_fermion
Sep 16, 2025
Merged

初步修改hubbard为free_fermion#63
hzhangxyz merged 1 commit into
mainfrom
dev/add_free_fermion

Conversation

@windy-pig

@windy-pig windy-pig commented Jul 17, 2025

Copy link
Copy Markdown
Collaborator

Description

Closes: #45

Checklist:

Comment thread qmb/free_fermion.py Outdated
@@ -0,0 +1,222 @@
"""
This file offers an interface for defining Hubbard models on a two-dimensional lattice.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

注释没更新

Comment thread qmb/free_fermion.py Outdated
import dataclasses
import torch
import tyro
from .mlp import WaveFunctionElectronUpDown as MlpWaveFunction

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这两个应该是 WaveFunctionNormal, 适用于没有自旋的系统

Comment thread qmb/free_fermion.py Outdated
electron_number: typing.Annotated[int, tyro.conf.arg(aliases=["-e"])]

# The coefficient of t
t: typing.Annotated[float, tyro.conf.arg(aliases=["-t"])] = 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

需要删掉

Comment thread qmb/free_fermion.py Outdated
if self.m <= 0 or self.n <= 0:
raise ValueError("The dimensions of the Hubbard model must be positive integers.")

if self.electron_number < 0 or self.electron_number > 2 * self.m * self.n:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

应该是小于1倍的m*n

Comment thread qmb/free_fermion.py Outdated
hamiltonian_dict: dict[tuple[tuple[int, int], ...], complex] = self._prepare_hamiltonian(args)
logging.info("Hamiltonian initialization complete")

self.ref_energy: float = self._calc_ref_energy(args)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

我觉得你这函数名,应该全一点。。。。

Comment thread qmb/free_fermion.py Outdated

def show_config(self, config: torch.Tensor) -> str:
string = "".join(f"{i:08b}"[::-1] for i in config.cpu().numpy())
return "[" + ".".join("".join(self._show_config_site(string[(i + j * self.m):(i + j * self.m) + 1]) for i in range(self.m)) for j in range(self.n)) + "]"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这个地方之前独立出一个函数,是因为他是两个bit,四个情况变成一个字符,对于目前这个情况,你可以直接inline地写,参考 ising.py:229

Comment thread qmb/free_fermion.py Outdated
double_sites=model.m * model.n,
physical_dim=2,
is_complex=True,
spin_up=model.electron_number // 2,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

spin_up的参数需要改一下。

@hzhangxyz

Copy link
Copy Markdown
Member

才发现,我网络那边,目前只有同时限制两类粒子的(spin up/down) 和没有任何限制的。在我更新那边之前,你可以使用之前那个up down都限制的网络,然后up设置electron number,down设置0。

目前的版本,从功能上,应该只是粒子数有点问题。

比如

[1801712] 2025-07-21 16:37:47,675 FreeFermion_2x3_e3(main) INFO: Starting script with arguments: ['/home/hzhangxyz/Cloud/Desktop/qmb/env/bin/qmb', 'imag', 'free_fermion', 'mlp', '-P2', '-P3', '-P-e3', '-n1000', '-f1000', '-c0']
[1801712] 2025-07-21 16:37:47,676 FreeFermion_2x3_e3(main) INFO: Model: free_fermion, Network: mlp
[1801712] 2025-07-21 16:37:47,676 FreeFermion_2x3_e3(main) INFO: Log directory: logs, Group name: FreeFermion_2x3_e3, Job name: main
[1801712] 2025-07-21 16:37:47,676 FreeFermion_2x3_e3(main) INFO: Physics arguments: ('2', '3', '-e3')
[1801712] 2025-07-21 16:37:47,676 FreeFermion_2x3_e3(main) INFO: Network arguments: ()
[1801712] 2025-07-21 16:37:47,676 FreeFermion_2x3_e3(main) INFO: Disabling PyTorch's default gradient computation
[1801712] 2025-07-21 16:37:47,676 FreeFermion_2x3_e3(main) INFO: Attempting to load checkpoint
[1801712] 2025-07-21 16:37:47,676 FreeFermion_2x3_e3(main) INFO: Checkpoint not found at: logs/FreeFermion_2x3_e3/main/data.pth
[1801712] 2025-07-21 16:37:47,712 FreeFermion_2x3_e3(main) INFO: Random seed not specified, using current seed: 11918819071607629469
[1801712] 2025-07-21 16:37:47,713 FreeFermion_2x3_e3(main) INFO: Parsing configurations for model: free_fermion with arguments: ('2', '3', '-e3')
[1801712] 2025-07-21 16:37:47,718 FreeFermion_2x3_e3(main) INFO: Loading the model
[1801712] 2025-07-21 16:37:47,718 FreeFermion_2x3_e3(main) INFO: Input arguments successfully parsed
[1801712] 2025-07-21 16:37:47,718 FreeFermion_2x3_e3(main) INFO: Constructing Free fermion model with dimensions: width = 2, height = 3
[1801712] 2025-07-21 16:37:47,718 FreeFermion_2x3_e3(main) INFO: The parameters of the model are: N = 3
[1801712] 2025-07-21 16:37:47,718 FreeFermion_2x3_e3(main) INFO: Initializing Hamiltonian for the lattice
[1801712] 2025-07-21 16:37:47,718 FreeFermion_2x3_e3(main) INFO: Hamiltonian initialization complete
[1801712] 2025-07-21 16:37:48,012 FreeFermion_2x3_e3(main) INFO: The ref energy is -3.8284271247
[1801712] 2025-07-21 16:37:48,012 FreeFermion_2x3_e3(main) INFO: Converting the Hamiltonian to internal Hamiltonian representation
[1801712] 2025-07-21 16:38:07,099 FreeFermion_2x3_e3(main) INFO: Internal Hamiltonian representation for model has been successfully created
[1801712] 2025-07-21 16:38:07,099 FreeFermion_2x3_e3(main) INFO: Physical model loaded successfully
[1801712] 2025-07-21 16:38:07,099 FreeFermion_2x3_e3(main) INFO: Parsing configurations for network: mlp with arguments: ()
[1801712] 2025-07-21 16:38:07,102 FreeFermion_2x3_e3(main) INFO: Initializing the network
[1801712] 2025-07-21 16:38:07,102 FreeFermion_2x3_e3(main) INFO: Hidden layer widths: (512,)
[1801712] 2025-07-21 16:38:07,103 FreeFermion_2x3_e3(main) INFO: Network initialized successfully
[1801712] 2025-07-21 16:38:07,103 FreeFermion_2x3_e3(main) INFO: Skipping loading state dict of the network
[1801712] 2025-07-21 16:38:07,103 FreeFermion_2x3_e3(main) INFO: Moving model to the device: device(type='cuda', index=0)
[1801712] 2025-07-21 16:38:07,363 FreeFermion_2x3_e3(main) INFO: Compiling the network
[1801712] 2025-07-21 16:38:07,564 FreeFermion_2x3_e3(main) INFO: The checkpoints will be saved every 5 steps
[1801712] 2025-07-21 16:38:07,564 FreeFermion_2x3_e3(main) INFO: Arguments Summary: Sampling Count From neural network: 1000, Sampling Count From Last iteration: 1000, Krylov Extend Count: 0, Krylov Extend First: No, Krylov Single Extend: No, krylov Iteration: 32, krylov Threshold: 0.0000000100, Loss Function: sum_filtered_angle_scaled_log, Global Optimizer: No, Use LBFGS: No, Learning Rate: 0.0010000000, Local Steps: 10000, Local Loss Threshold: 0.0000000100, Logging Psi: 30, Local Batch Count For Generation: 1, Local Batch Count For Apply Within: 1, Local Batch Count For Loss Function: 1
[1801712] 2025-07-21 16:38:16,480 FreeFermion_2x3_e3(main) INFO: Starting a new optimization cycle
[1801712] 2025-07-21 16:38:16,480 FreeFermion_2x3_e3(main) INFO: Sampling configurations from neural network
[1801712] 2025-07-21 16:38:20,924 FreeFermion_2x3_e3(main) INFO: Sampling configurations from last iteration
[1801712] 2025-07-21 16:38:20,924 FreeFermion_2x3_e3(main) INFO: Merging configurations from neural network and last iteration
[1801712] 2025-07-21 16:38:20,924 FreeFermion_2x3_e3(main) INFO: Sampling completed, unique configurations count: 64
[1801712] 2025-07-21 16:38:20,924 FreeFermion_2x3_e3(main) INFO: Computing the target for local optimization
[1801712] 2025-07-21 16:39:56,061 FreeFermion_2x3_e3(main) INFO: The current energy is 1.3359544903 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,343 FreeFermion_2x3_e3(main) INFO: The current energy is -0.9260261119 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,347 FreeFermion_2x3_e3(main) INFO: The current energy is -2.4221334596 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,350 FreeFermion_2x3_e3(main) INFO: The current energy is -2.8130367881 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,355 FreeFermion_2x3_e3(main) INFO: The current energy is -2.9442379573 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,358 FreeFermion_2x3_e3(main) INFO: The current energy is -3.1760861080 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,361 FreeFermion_2x3_e3(main) INFO: The current energy is -3.4502139332 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,364 FreeFermion_2x3_e3(main) INFO: The current energy is -3.6119933131 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,368 FreeFermion_2x3_e3(main) INFO: The current energy is -3.6637106187 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,372 FreeFermion_2x3_e3(main) INFO: The current energy is -3.7393865681 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,374 FreeFermion_2x3_e3(main) INFO: The current energy is -3.7925953729 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,377 FreeFermion_2x3_e3(main) INFO: The current energy is -3.8216764985 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,379 FreeFermion_2x3_e3(main) INFO: The current energy is -3.8272965487 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,381 FreeFermion_2x3_e3(main) INFO: The current energy is -3.8282279931 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,382 FreeFermion_2x3_e3(main) INFO: The current energy is -3.8284049583 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,384 FreeFermion_2x3_e3(main) INFO: The current energy is -3.8284201492 where the sampling count is 64
[1801712] 2025-07-21 16:39:56,385 FreeFermion_2x3_e3(main) INFO: The current energy is -3.8284248097 where the sampling count is 64

你如果将粒子数设置为本来就能量最低的那个值,就能算对了。

我看看代码怎么样啊。

@hzhangxyz
hzhangxyz merged commit b0cc46b into main Sep 16, 2025
7 of 8 checks passed
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.

💡 [REQUEST] - Add the free fermion model.

2 participants