Skip to content

Commit 7bc7743

Browse files
committed
docs: remove references to deleted examples
1 parent b8ac5c8 commit 7bc7743

5 files changed

Lines changed: 138 additions & 56 deletions

File tree

‎docs/api/optimizers/mera.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ optimizer = builder.fermion_parametric_optimizer(
118118
)
119119
```
120120

121-
For a complete native Torch example, see
122-
`examples/FermiHubbardQMera/fermion_qmera_energy.py`.
121+
For a larger native Torch workflow, use the corresponding examples maintained
122+
in the separate `pepsy_examples` repository; the package API is demonstrated
123+
by the builder flow above.
123124

124125
```{eval-rst}
125126
.. automodule:: pepsy.optimizers.mera

‎docs/examples.md‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,12 @@ It demonstrates:
2020
- [How-To: Choose Parameters](howto/choose_parameters.md)
2121
- [How-To: Tune Sweep Solvers](howto/solver_tuning.md)
2222

23-
## Runnable scripts
24-
25-
- `examples/QMeraEnergy/qmera_energy.py` builds a small schedule-first qMERA,
26-
evaluates local ZZ energy from rebuilt and compiled lightcones, and can run a
27-
short optional Torch Adam optimization with `--steps`.
28-
- `examples/FermiHubbardUnified/fermion_workflow.py` uses one
29-
`pepsy.Fermion` object to build site-native Hubbard terms and an MPS gate
30-
stream, then adapts the same model to qMERA's explicit two-mode layout.
31-
- `examples/FermiHubbardQMera/fermion_qmera_energy.py` builds native Symmray
32-
Hubbard `LocalTerm` objects and optionally optimizes their qMERA energy with
33-
Torch Adam using `--steps`.
34-
- `examples/SimpleUpdateGen/long_range_peps.py` demonstrates a long-range PEPS
35-
simple-update term: raw `quimb.SimpleUpdateGen` fails on the non-adjacent
36-
endpoints, while `pepsy.SimpleUpdateGen` routes the gate through SWAPs and
37-
reports the resulting max bond, gauge count, and cluster energy.
38-
- `examples/RelayBP/simple_update_relay_comparison.py` compares exact
39-
contraction with plain D1BP, simple-update-initialized D1BP, and
40-
simple-update-initialized Relay-BP on a small loopy factor network.
41-
- `examples/RelayBP/odd_cycle_stress.py` provides deterministic positive
42-
odd-cycle cases where polarized parallel D1BP stalls and Relay-BP converges.
23+
## Extended examples
24+
25+
The package repository keeps only the lightweight example notebook under
26+
`examples/`. Larger runnable workflows and experiment scripts are maintained
27+
in the separate `pepsy_examples` repository. The tests in this repository keep
28+
the deleted Relay-BP examples' numerical coverage without depending on local
29+
example files.
4330

4431
For a cleaner, docs-first narrative, start from [tutorials](tutorials/index.md).

‎docs/tutorials/bp_simple_update_relay.md‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ Relay-BP adds per-source-node memory over several warm-started legs to make
66
finding a fixed point more robust. It does not, by itself, make a loopy BP
77
contraction exact; use an exact small network to measure approximation error.
88

9-
The runnable example below uses a 3×3 classical Ising factor network. It
10-
compares all three paths against the exact contraction and records the absolute
11-
message convergence and relative contraction error:
12-
13-
```{literalinclude} ../../examples/RelayBP/simple_update_relay_comparison.py
14-
:language: python
15-
```
9+
The comparison is covered directly by the BP regression tests and uses only
10+
the public `gauge_all`, `one_norm_bp`, and
11+
`run_d1bp_from_simple_update_gauges` APIs. The package repository intentionally
12+
does not carry a separate runnable script for this workflow; extended example
13+
scripts belong in the separate `pepsy_examples` repository.
1614

1715
The flow is:
1816

@@ -30,15 +28,12 @@ easy fixed point; the reported residual separately verifies convergence.
3028

3129
## Relay stress cases
3230

33-
The following runnable exact-reference stress cases begin from deliberately
34-
polarized messages on a near-deterministic odd cycle. Parallel D1BP stalls,
35-
whereas Relay's per-source memory reaches a strict residual fixed point. The
36-
reported exact-reference error is intentionally separate: convergence does not
37-
turn a strongly loopy BP approximation into an exact contraction.
38-
39-
```{literalinclude} ../../examples/RelayBP/odd_cycle_stress.py
40-
:language: python
41-
```
31+
The regression suite also covers exact-reference stress cases beginning from
32+
deliberately polarized messages on a near-deterministic odd cycle. Parallel
33+
D1BP stalls, whereas Relay's per-source memory reaches a strict residual fixed
34+
point. The reported exact-reference error is intentionally separate:
35+
convergence does not turn a strongly loopy BP approximation into an exact
36+
contraction.
4237

4338
For the reverse direction, use
4439
`simple_update_core_and_gauges_from_messages(result.bp)`. For strictly

‎tests/test_bp_cluster.py‎

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import numpy as np
1111
import pytest
12-
from pathlib import Path
13-
import runpy
1412

1513
qtn = pytest.importorskip("quimb.tensor")
1614
pytest.importorskip("quimb.tensor.belief_propagation")
@@ -698,13 +696,59 @@ def test_relay_bp_runs_from_real_quimb_su_and_can_return_to_su():
698696

699697
def test_exact_su_and_relay_d1bp_comparison_example():
700698
"""The documented comparison tracks convergence and loopy-BP error."""
701-
example_path = (
702-
Path(__file__).resolve().parents[1]
703-
/ "examples"
704-
/ "RelayBP"
705-
/ "simple_update_relay_comparison.py"
706-
)
707-
comparison = runpy.run_path(str(example_path))["run_comparison"]()
699+
from pepsy.bp import one_norm_bp
700+
701+
tn = qtn.TN2D_classical_ising_partition_function(3, 3, beta=0.2)
702+
exact = float(tn.contract(optimize="auto-hq"))
703+
core = tn.copy()
704+
gauges = {}
705+
core.gauge_all_simple_(gauges=gauges, max_iterations=100, tol=1e-12)
706+
rebuilt = core.copy()
707+
rebuilt.gauge_simple_insert(gauges)
708+
su_representation_error = abs(float(rebuilt.contract()) - exact) / abs(exact)
709+
710+
runs = {
711+
"plain_d1bp": one_norm_bp(
712+
tn,
713+
method="d1bp",
714+
max_iterations=1000,
715+
tol=1e-10,
716+
),
717+
"su_initialized_d1bp": run_d1bp_from_simple_update_gauges(
718+
core,
719+
gauges,
720+
run_opts={"max_iterations": 1000, "tol": 1e-10},
721+
),
722+
"su_initialized_relay_d1bp": run_d1bp_from_simple_update_gauges(
723+
core,
724+
gauges,
725+
use_relay=True,
726+
run_opts={"max_iterations": 1000, "tol": 1e-10},
727+
relay_opts={
728+
"num_relays": 3,
729+
"memory_first_leg": False,
730+
"gamma_range": (0.1, 0.2),
731+
"seed": 0,
732+
},
733+
),
734+
}
735+
records = {}
736+
for name, result in runs.items():
737+
estimate = float(result.contract())
738+
records[name] = {
739+
"converged": result.converged,
740+
"quimb_converged": result.quimb_converged,
741+
"iterations": result.iterations,
742+
"num_legs": result.num_legs_run,
743+
"max_mdiff": result.max_mdiff,
744+
"estimate": estimate,
745+
"relative_error": abs(estimate - exact) / abs(exact),
746+
}
747+
comparison = {
748+
"exact": exact,
749+
"simple_update_representation_error": su_representation_error,
750+
"runs": records,
751+
}
708752

709753
assert comparison["simple_update_representation_error"] < 1e-12
710754
runs = comparison["runs"]

‎tests/test_bp_relay.py‎

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import numpy as np
66
import pepsy as py
77
import pytest
8-
from pathlib import Path
9-
import runpy
8+
import warnings
109

1110
qtn = pytest.importorskip("quimb.tensor")
11+
from quimb.tensor.belief_propagation import D1BP # noqa: E402
1212

1313
from pepsy.bp import RelayBPResult, one_norm_bp, relay_bp # noqa: E402
1414
from pepsy.bp.relay import _relay_message_sources # noqa: E402
@@ -43,6 +43,26 @@ def _peps_2x2():
4343
return qtn.PEPS.rand(Lx=2, Ly=2, bond_dim=2, seed=10)
4444

4545

46+
def _odd_antiferromagnetic_cycle(epsilon):
47+
"""Return a positive triangle whose edge factors nearly flip a bit."""
48+
factor = np.array([[epsilon, 1.0], [1.0, epsilon]])
49+
return qtn.TensorNetwork(
50+
[
51+
qtn.Tensor(factor, inds=("ab", "ca")),
52+
qtn.Tensor(factor, inds=("ab", "bc")),
53+
qtn.Tensor(factor, inds=("bc", "ca")),
54+
]
55+
)
56+
57+
58+
def _polarized_messages(tn):
59+
"""Choose a deterministic non-fixed initial message for every edge end."""
60+
return {
61+
key: np.array([1.0, 0.0])
62+
for key in D1BP(tn, update="parallel").messages
63+
}
64+
65+
4666
def test_one_norm_bp_close_to_exact_on_small_grid():
4767
tn = _ising_tn(3, 0.2)
4868
exact = float(tn.contract())
@@ -232,13 +252,48 @@ def test_parallel_update_runs():
232252

233253
def test_relay_d1bp_odd_cycle_stress_cases_converge_strictly():
234254
"""Relay damps deterministic parallel D1BP stalls on odd parity cycles."""
235-
example_path = (
236-
Path(__file__).resolve().parents[1]
237-
/ "examples"
238-
/ "RelayBP"
239-
/ "odd_cycle_stress.py"
240-
)
241-
records = runpy.run_path(str(example_path))["run_stress_cases"]()
255+
records = []
256+
for epsilon in (1e-3, 1e-2):
257+
tn = _odd_antiferromagnetic_cycle(epsilon)
258+
exact = float(tn.contract())
259+
initial = _polarized_messages(tn)
260+
common = {
261+
"method": "d1bp",
262+
"init_messages": initial,
263+
"update": "parallel",
264+
"diis": False,
265+
"max_iterations": 100,
266+
"tol": 1e-10,
267+
}
268+
with warnings.catch_warnings():
269+
warnings.filterwarnings(
270+
"ignore",
271+
message="Belief propagation did not converge.*",
272+
category=UserWarning,
273+
)
274+
plain = one_norm_bp(tn, **common)
275+
relay = relay_bp(
276+
tn,
277+
**common,
278+
num_relays=5,
279+
memory_first_leg=True,
280+
gamma_range=(0.2, 0.8),
281+
seed=0,
282+
)
283+
relay_estimate = float(relay.contract())
284+
records.append(
285+
{
286+
"epsilon": epsilon,
287+
"exact": exact,
288+
"plain_converged": plain.converged,
289+
"plain_max_mdiff": plain.max_mdiff,
290+
"relay_converged": relay.converged,
291+
"relay_iterations": relay.iterations,
292+
"relay_num_legs": relay.num_legs_run,
293+
"relay_max_mdiff": relay.max_mdiff,
294+
"relay_relative_error": abs(relay_estimate - exact) / abs(exact),
295+
}
296+
)
242297

243298
assert len(records) == 2
244299
for record in records:

0 commit comments

Comments
 (0)