Skip to content

Commit 50382dc

Browse files
committed
npdm index masks
1 parent 4dd280e commit 50382dc

File tree

12 files changed

+780
-225
lines changed

12 files changed

+780
-225
lines changed

docs/source/theory/spin-orbital.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ First consider the second and third terms
169169
+ \frac{1}{2} \sum_{ij\in L, kl\in R} v_{klij} a_i^\dagger a_j a_k^\dagger a_l \\
170170
=&\ \sum_{ij\in L, kl\in R} v_{ijkl} a_i^\dagger a_j a_k^\dagger a_l
171171
= \sum_{ij\in L} a_i^\dagger a_j \sum_{kl\in R} v_{ijkl} a_k^\dagger a_l
172-
= \sum_{ij\in L} \hat{B}_{ij} \hat{Q}_{ij\prime}^{R}
172+
= \sum_{ij\in L} \hat{B}_{ij} \hat{Q}_{ij}^{R\prime}
173173
174174
For the other two terms,
175175

@@ -182,12 +182,12 @@ For the other two terms,
182182
-\frac{1}{2} \sum_{il\in L, jk\in R} v_{klij} a_i^\dagger a_l a_k^\dagger a_j \\
183183
=&\ -\sum_{il\in L, jk\in R} v_{ijkl} a_i^\dagger a_l a_k^\dagger a_j \\
184184
=&\ -\sum_{il\in L} a_i^\dagger a_l \sum_{jk\in R} v_{ijkl} a_k^\dagger a_j
185-
= \sum_{il\in L} \hat{B}_{il} \hat{Q}_{il\prime\prime}^{R}
185+
= \sum_{il\in L} \hat{B}_{il} \hat{Q}_{il}^{R\prime\prime}
186186
187187
Then
188188

189189
.. math::
190-
\hat{Q}_{ij}^{R} = \hat{Q}_{ij\prime}^{R} + \hat{Q}_{ij\prime\prime}^{R}
190+
\hat{Q}_{ij}^{R} = \hat{Q}_{ij}^{R\prime} + \hat{Q}_{ij}^{R\prime\prime}
191191
= \sum_{kl\in R} \big( v_{ijkl} - v_{ilkj} \big) a_k^\dagger a_l
192192
193193
Normal/Complementary Partitioning

docs/source/theory/su2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ we have
315315
+ a_{p\beta}^\dagger a_{q\beta}^\dagger a_{r\beta} a_{s\beta}
316316
+ a_{p\alpha}^\dagger a_{q\beta}^\dagger a_{r\alpha} a_{s\beta}
317317
+ a_{p\beta}^\dagger a_{q\alpha}^\dagger a_{r\beta} a_{s\alpha} \\
318-
=&\ \sum_{\sigma\sigma'} a_{p\sigma}^\dagger a_{q\sigma'}^\dagger a_{r\sigma} s_{s\sigma'}
318+
=&\ \sum_{\sigma\sigma'} a_{p\sigma}^\dagger a_{q\sigma'}^\dagger a_{r\sigma} a_{s\sigma'}
319319
320320
Another case
321321

docs/source/tutorial/hubbard.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@
897897
"$$\n",
898898
"\n",
899899
"$$\n",
900-
" -t \\sum_{\\langle i,j \\rangle, \\sigma} \\left( c_{i,\\sigma}^\\dagger c_{j,\\sigma} + \\mathrm{H.c.} \\right)\n",
900+
" -t \\sum_{\\langle i,j \\rangle, \\sigma} \\left( a_{i,\\sigma}^\\dagger a_{j,\\sigma} + \\mathrm{H.c.} \\right)\n",
901901
" = -\\sqrt{2} t \\sum_{\\langle i,j \\rangle} \\left[\n",
902902
" \\big( E_i \\big)^{[1/2]} \\otimes_{[0]} \\big( F_{j} \\big)^{[1/2]} + \\big( E_{j} \\big)^{[1/2]} \\otimes_{[0]} \\big( F_i \\big)^{[1/2]}\n",
903903
" \\right]\n",

pyblock2/driver/core.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5498,6 +5498,7 @@ def get_npdm(
54985498
algo_type=None,
54995499
npdm_expr=None,
55005500
mask=None,
5501+
index_masks=None,
55015502
simulated_parallel=0,
55025503
fused_contraction_rotation=True,
55035504
cutoff=1e-24,
@@ -5537,6 +5538,9 @@ def get_npdm(
55375538
mask : None or list[int] or list[list[int]]
55385539
The mask for setting repeated indices for the operator expression.
55395540
Default is None, meaning that all indices can be different.
5541+
index_masks : None or list[list[int]] or list[list[list[int]]]
5542+
The list of allowed site indices for each operator in the operator expression.
5543+
Default is None, meaning that all site indices are allowed.
55405544
simulated_parallel : int
55415545
Number of processors for simulating parallel algorithm serially.
55425546
Default is zero, meaning that the serial algorithm is used if
@@ -5629,7 +5633,7 @@ def get_npdm(
56295633
for _ in range(pdm_type - 1):
56305634
op_str = su2_coupling % op_str
56315635
perm = bw.b.SpinPermScheme.initialize_su2(
5632-
pdm_type * 2, op_str, True,
5636+
int(pdm_type * 2), op_str, True,
56335637
mask=bw.b.VectorUInt16() if mask is None else bw.b.VectorUInt16(mask),
56345638
max_n_sites=ket.n_sites,
56355639
)
@@ -5646,41 +5650,33 @@ def get_npdm(
56465650
if mask is None:
56475651
perms = bw.b.VectorSpinPermScheme(
56485652
[
5649-
bw.b.SpinPermScheme.initialize_sz(pdm_type * 2, cd, True,
5653+
bw.b.SpinPermScheme.initialize_sz(len(cd), cd, True,
56505654
mask=bw.b.VectorUInt16(), max_n_sites=ket.n_sites) if fermionic_ops is None else
5651-
bw.b.SpinPermScheme.initialize_sany(pdm_type * 2, cd, fermionic_ops,
5655+
bw.b.SpinPermScheme.initialize_sany(len(cd), cd, fermionic_ops,
56525656
mask=bw.b.VectorUInt16(), max_n_sites=ket.n_sites) for cd in op_str
56535657
]
56545658
)
56555659
elif len(mask) != 0 and not isinstance(mask[0], int):
56565660
assert len(mask) == len(op_str)
5657-
pts = (
5658-
[pdm_type] * len(op_str) if isinstance(pdm_type, int) else pdm_type
5659-
)
56605661
perms = bw.b.VectorSpinPermScheme(
56615662
[
56625663
bw.b.SpinPermScheme.initialize_sz(
5663-
pt * 2, cd, True, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
5664+
len(cd), cd, True, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
56645665
) if fermionic_ops is None else
56655666
bw.b.SpinPermScheme.initialize_sany(
5666-
pt * 2, cd, fermionic_ops, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
5667-
)
5668-
for cd, xm, pt in zip(op_str, mask, pts)
5667+
len(cd), cd, fermionic_ops, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
5668+
) for cd, xm in zip(op_str, mask)
56695669
]
56705670
)
56715671
else:
5672-
pts = (
5673-
[pdm_type] * len(op_str) if isinstance(pdm_type, int) else pdm_type
5674-
)
56755672
perms = bw.b.VectorSpinPermScheme(
56765673
[
56775674
bw.b.SpinPermScheme.initialize_sz(
5678-
pt * 2, cd, True, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
5675+
len(cd), cd, True, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
56795676
) if fermionic_ops is None else
56805677
bw.b.SpinPermScheme.initialize_sany(
5681-
pt * 2, cd, fermionic_ops, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
5682-
)
5683-
for cd, pt in zip(op_str, pts)
5678+
len(cd), cd, fermionic_ops, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
5679+
) for cd in op_str
56845680
]
56855681
)
56865682
elif SymmetryTypes.SGF in bw.symm_type:
@@ -5693,41 +5689,34 @@ def get_npdm(
56935689
if mask is None:
56945690
perms = bw.b.VectorSpinPermScheme(
56955691
[
5696-
bw.b.SpinPermScheme.initialize_sz(pdm_type * 2, cd, True,
5692+
bw.b.SpinPermScheme.initialize_sz(len(cd), cd, True,
56975693
mask=bw.b.VectorUInt16(), max_n_sites=ket.n_sites) if fermionic_ops is None else
5698-
bw.b.SpinPermScheme.initialize_sany(pdm_type * 2, cd, fermionic_ops,
5694+
bw.b.SpinPermScheme.initialize_sany(len(cd), cd, fermionic_ops,
56995695
mask=bw.b.VectorUInt16(), max_n_sites=ket.n_sites) for cd in op_str
57005696
]
57015697
)
57025698
elif len(mask) != 0 and not isinstance(mask[0], int):
57035699
assert len(mask) == len(op_str)
5704-
pts = (
5705-
[pdm_type] * len(op_str) if isinstance(pdm_type, int) else pdm_type
5706-
)
57075700
perms = bw.b.VectorSpinPermScheme(
57085701
[
57095702
bw.b.SpinPermScheme.initialize_sz(
5710-
pt * 2, cd, True, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
5703+
len(cd), cd, True, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
57115704
) if fermionic_ops is None else
57125705
bw.b.SpinPermScheme.initialize_sany(
5713-
pt * 2, cd, fermionic_ops, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
5706+
len(cd), cd, fermionic_ops, mask=bw.b.VectorUInt16(xm), max_n_sites=ket.n_sites
57145707
)
5715-
for cd, xm, pt in zip(op_str, mask, pts)
5708+
for cd, xm in zip(op_str, mask)
57165709
]
57175710
)
57185711
else:
5719-
pts = (
5720-
[pdm_type] * len(op_str) if isinstance(pdm_type, int) else pdm_type
5721-
)
57225712
perms = bw.b.VectorSpinPermScheme(
57235713
[
57245714
bw.b.SpinPermScheme.initialize_sz(
5725-
pt * 2, cd, True, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
5715+
len(cd), cd, True, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
57265716
) if fermionic_ops is None else
57275717
bw.b.SpinPermScheme.initialize_sany(
5728-
pt * 2, cd, fermionic_ops, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
5729-
)
5730-
for cd, pt in zip(op_str, pts)
5718+
len(cd), cd, fermionic_ops, mask=bw.b.VectorUInt16(mask), max_n_sites=ket.n_sites
5719+
) for cd in op_str
57315720
]
57325721
)
57335722
elif SymmetryTypes.SAny in bw.symm_type:
@@ -5761,6 +5750,19 @@ def get_npdm(
57615750
]
57625751
)
57635752

5753+
if index_masks is not None:
5754+
if any(any(isinstance(im, (int, np.int64)) for im in ims) for ims in index_masks if len(ims) != 0):
5755+
index_masks = [index_masks]
5756+
if len(index_masks) == 1:
5757+
index_masks = index_masks * len(perms)
5758+
assert len(perms) == len(index_masks)
5759+
for perm, ims in zip(perms, index_masks):
5760+
f = lambda x: [int(px) for px in x]
5761+
if self.reorder_idx is not None:
5762+
rev_idx = np.argsort(self.reorder_idx)
5763+
f = lambda x: [rev_idx[int(px)] for px in x]
5764+
perm.index_mask = bw.b.VectorVectorUInt16([bw.b.VectorUInt16(f(x)) for x in ims])
5765+
57645766
if iprint >= 1:
57655767
print("npdm string =", op_str)
57665768

@@ -5949,7 +5951,7 @@ def get_npdm(
59495951
for ip in range(len(npdms)):
59505952
npdms[ip] = np.asarray(npdms[ip])
59515953

5952-
if self.reorder_idx is not None:
5954+
if self.reorder_idx is not None and index_masks is None:
59535955
rev_idx = np.argsort(self.reorder_idx)
59545956
for ip in range(len(npdms)):
59555957
for i in range(npdms[ip].ndim):

0 commit comments

Comments
 (0)