-
Notifications
You must be signed in to change notification settings - Fork 1
feat: support non-unique sampling in VMC and Guide algorithms #213
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ class GuideConfig: | |
| local_step: int = 1000 | ||
| # The number of steps for the distribution optimizer | ||
| dist_step: int = 100 | ||
| # Generate configuration uniquely | ||
| unique: bool = True | ||
|
|
||
| def main( | ||
| self, | ||
|
|
@@ -58,11 +60,12 @@ def main( | |
| ) | ||
|
|
||
| logging.info( | ||
| "Arguments Summary: Sampling Count: %d, Relative Count: %d, Local Steps: %d, Dist Steps: %d", | ||
| "Arguments Summary: Sampling Count: %d, Relative Count: %d, Local Steps: %d, Dist Steps: %d, Unique: %s", | ||
| self.sampling_count, | ||
| self.relative_count, | ||
| self.local_step, | ||
| self.dist_step, | ||
| self.unique, | ||
| ) | ||
|
|
||
| if "guide" not in data: | ||
|
|
@@ -74,8 +77,16 @@ def main( | |
| logging.info("Starting a new optimization cycle") | ||
|
|
||
| logging.info("Sampling configurations") | ||
| configs_src_network, psi_src_network, _, _ = network.generate_unique(self.sampling_count) | ||
| configs_src_sampling, psi_src_sampling, _, _ = sampling.generate_unique(self.sampling_count) | ||
| if self.unique: | ||
| configs_src_network, psi_src_network, _, _ = network.generate_unique(self.sampling_count) | ||
| reweight_network = torch.ones_like(psi_src_network, dtype=torch.float64) | ||
| configs_src_sampling, psi_src_sampling, _, _ = sampling.generate_unique(self.sampling_count) | ||
| reweight_sampling = torch.ones_like(psi_src_sampling, dtype=torch.float64) | ||
| else: | ||
| configs_src_network, psi_src_network, count_network, _ = network.generate(self.sampling_count) | ||
| reweight_network = count_network / psi_src_network.abs() ** 2 | ||
| configs_src_sampling, psi_src_sampling, count_sampling, _ = sampling.generate(self.sampling_count) | ||
| reweight_sampling = count_sampling / psi_src_sampling.abs() ** 2 | ||
|
Comment on lines
79
to
+89
|
||
|
|
||
| logging.info("Calculating relative configurations") | ||
| if self.relative_count <= len(configs_src_network): | ||
|
|
@@ -108,8 +119,9 @@ def energy_sampling() -> torch.Tensor: | |
| with torch.no_grad(): | ||
| psi_dst = network(configs_dst) | ||
| hamiltonian_psi_dst = model.apply_within(configs_dst, psi_dst, configs_src) | ||
| num = psi_src.conj() @ hamiltonian_psi_dst | ||
| den = psi_src.conj() @ psi_src.detach() | ||
| psi_src_reweight = psi_src.conj() * reweight_sampling | ||
| num = psi_src_reweight @ hamiltonian_psi_dst | ||
| den = psi_src_reweight @ psi_src.detach() | ||
| energy = num / den | ||
| energy.real.backward() # type: ignore[no-untyped-call] | ||
| return energy.real | ||
|
|
@@ -121,8 +133,9 @@ def energy_network() -> torch.Tensor: | |
| psi_src = network(configs_src) | ||
| psi_dst = network(configs_dst) | ||
| hamiltonian_psi_dst = model.apply_within(configs_dst, psi_dst, configs_src) | ||
| num = psi_src.conj() @ hamiltonian_psi_dst | ||
| den = psi_src.conj() @ psi_src.detach() | ||
| psi_src_reweight = psi_src.conj() * reweight_network | ||
| num = psi_src_reweight @ hamiltonian_psi_dst | ||
| den = psi_src_reweight @ psi_src.detach() | ||
| energy = num / den | ||
| return energy.real | ||
|
|
||
|
|
@@ -133,8 +146,9 @@ def distribution() -> torch.Tensor: | |
| psi_src = network(configs_src) | ||
| psi_dst = network(configs_dst) | ||
| hamiltonian_psi_dst = model.apply_within(configs_dst, psi_dst, configs_src) | ||
| num = psi_src.conj() @ hamiltonian_psi_dst | ||
| den = psi_src.conj() @ psi_src | ||
| psi_src_reweight = psi_src.conj() * reweight_sampling | ||
| num = psi_src_reweight @ hamiltonian_psi_dst | ||
| den = psi_src_reweight @ psi_src | ||
| energy = num / den | ||
| local_energy = hamiltonian_psi_dst / psi_src | ||
| energy_diff = local_energy - energy | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,8 @@ class VmcConfig: | |||||||||||||||||||||||||||||
| relative_count: int = 40000 | ||||||||||||||||||||||||||||||
| # The number of steps for the local optimizer | ||||||||||||||||||||||||||||||
| local_step: int = 1000 | ||||||||||||||||||||||||||||||
| # Generate configuration uniquely | ||||||||||||||||||||||||||||||
| unique: bool = True | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| unique: bool = True | |
| unique: bool = False |
Copilot
AI
Mar 6, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When unique is false, reweight = count / psi_i.abs() ** 2 is computed from psi_i returned by network.generate(...). If gradients are enabled for sampling (or enabled later via torch.enable_grad), this will keep an autograd graph from the sampling forward pass and can cause repeated-backward errors or unintended gradients through the sampling step. Compute sampling outputs and reweight under torch.no_grad() and/or explicitly detach psi_i before using it to build reweight.
| if self.unique: | |
| configs_i, psi_i, _, _ = network.generate_unique(self.sampling_count) | |
| reweight = torch.ones_like(psi_i, dtype=torch.float64) | |
| else: | |
| configs_i, psi_i, count, _ = network.generate(self.sampling_count) | |
| reweight = count / psi_i.abs() ** 2 | |
| with torch.no_grad(): | |
| if self.unique: | |
| configs_i, psi_i, _, _ = network.generate_unique(self.sampling_count) | |
| reweight = torch.ones_like(psi_i, dtype=torch.float64) | |
| else: | |
| configs_i, psi_i, count, _ = network.generate(self.sampling_count) | |
| psi_i_detached = psi_i.detach() | |
| reweight = count / psi_i_detached.abs() ** 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaulting
unique=Truecan still cause an immediate runtime failure for networks that don't implementgenerate_unique(e.g.,qmp/networks/mps.pyraisesNotImplementedError). Consider defaulting tounique=Falseor implementing a runtime fallback togenerate(...)whengenerate_unique(...)is unavailable.