jVMC 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. jVMC-1.2.0/LICENSE +21 -0
  2. jVMC-1.2.0/PKG-INFO +88 -0
  3. jVMC-1.2.0/README.md +74 -0
  4. jVMC-1.2.0/jVMC/__init__.py +14 -0
  5. jVMC-1.2.0/jVMC/global_defs.py +55 -0
  6. jVMC-1.2.0/jVMC/mpi_wrapper.py +299 -0
  7. jVMC-1.2.0/jVMC/nets/__init__.py +8 -0
  8. jVMC-1.2.0/jVMC/nets/activation_functions.py +32 -0
  9. jVMC-1.2.0/jVMC/nets/cnn.py +195 -0
  10. jVMC-1.2.0/jVMC/nets/ffn.py +47 -0
  11. jVMC-1.2.0/jVMC/nets/initializers.py +41 -0
  12. jVMC-1.2.0/jVMC/nets/rbm.py +91 -0
  13. jVMC-1.2.0/jVMC/nets/rnn1d_general.py +226 -0
  14. jVMC-1.2.0/jVMC/nets/rnn2d_general.py +311 -0
  15. jVMC-1.2.0/jVMC/nets/sym_wrapper.py +68 -0
  16. jVMC-1.2.0/jVMC/nets/two_nets_wrapper.py +31 -0
  17. jVMC-1.2.0/jVMC/operator/__init__.py +3 -0
  18. jVMC-1.2.0/jVMC/operator/base.py +280 -0
  19. jVMC-1.2.0/jVMC/operator/branch_free.py +284 -0
  20. jVMC-1.2.0/jVMC/operator/povm.py +432 -0
  21. jVMC-1.2.0/jVMC/sampler.py +507 -0
  22. jVMC-1.2.0/jVMC/stats.py +174 -0
  23. jVMC-1.2.0/jVMC/util/__init__.py +6 -0
  24. jVMC-1.2.0/jVMC/util/minsr.py +158 -0
  25. jVMC-1.2.0/jVMC/util/output_manager.py +222 -0
  26. jVMC-1.2.0/jVMC/util/stepper.py +182 -0
  27. jVMC-1.2.0/jVMC/util/symmetries.py +234 -0
  28. jVMC-1.2.0/jVMC/util/tdvp.py +340 -0
  29. jVMC-1.2.0/jVMC/util/util.py +213 -0
  30. jVMC-1.2.0/jVMC/version.py +2 -0
  31. jVMC-1.2.0/jVMC/vqs.py +482 -0
  32. jVMC-1.2.0/jVMC.egg-info/PKG-INFO +88 -0
  33. jVMC-1.2.0/jVMC.egg-info/SOURCES.txt +48 -0
  34. jVMC-1.2.0/jVMC.egg-info/dependency_links.txt +1 -0
  35. jVMC-1.2.0/jVMC.egg-info/requires.txt +25 -0
  36. jVMC-1.2.0/jVMC.egg-info/top_level.txt +2 -0
  37. jVMC-1.2.0/setup.cfg +4 -0
  38. jVMC-1.2.0/setup.py +35 -0
  39. jVMC-1.2.0/tests/__init__.py +0 -0
  40. jVMC-1.2.0/tests/minsr_test.py +48 -0
  41. jVMC-1.2.0/tests/mpi_wrapper_test.py +49 -0
  42. jVMC-1.2.0/tests/nets_test.py +87 -0
  43. jVMC-1.2.0/tests/operator_test.py +137 -0
  44. jVMC-1.2.0/tests/povm_test.py +289 -0
  45. jVMC-1.2.0/tests/sampler_test.py +515 -0
  46. jVMC-1.2.0/tests/stats_test.py +35 -0
  47. jVMC-1.2.0/tests/stepper_test.py +43 -0
  48. jVMC-1.2.0/tests/symmetries_test.py +128 -0
  49. jVMC-1.2.0/tests/tdvp_test.py +266 -0
  50. jVMC-1.2.0/tests/vqs_test.py +317 -0
jVMC-1.2.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Markus Schmitt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
jVMC-1.2.0/PKG-INFO ADDED
@@ -0,0 +1,88 @@
1
+ Metadata-Version: 2.1
2
+ Name: jVMC
3
+ Version: 1.2.0
4
+ Summary: jVMC: Versatile and performant variational Monte Carlo
5
+ Home-page: https://jvmc.readthedocs.io/en/latest/#
6
+ Author: Markus Schmitt, Moritz Reh
7
+ Author-email: markus.schmitt@uni-koeln.de
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Description-Content-Type: text/markdown
12
+ Provides-Extra: dev
13
+ License-File: LICENSE
14
+
15
+ [![Documentation Status](https://readthedocs.org/projects/jvmc/badge/?version=latest)](https://jvmc.readthedocs.io/en/latest/?badge=latest)
16
+ [![PyPi version](https://badgen.net/pypi/v/jVMC/)](https://pypi.org/project/jVMC/)
17
+ [![arXiv](https://img.shields.io/badge/arXiv-2108.03409-b31b1b.svg)](https://arxiv.org/abs/2108.03409)
18
+
19
+ # jVMC
20
+ This is an impementation of Variational Monte Carlo (VMC) for quantum many-body dynamics using the [JAX library](https://jax.readthedocs.io "JAX library") (and [Flax](https://flax.readthedocs.io "FLAX library") on top) to exploit the blessings of automatic differentiation for easy model composition and just-in-time compilation for execution on accelerators.
21
+
22
+ 1. [Documentation](#documentation)
23
+ 2. [Installation](#installation)
24
+ 3. [Online example](#online-example)
25
+ 4. [Important gotchas](#important-gotchas)
26
+ 5. [Citing jVMC](#citing-jvmc)
27
+
28
+ Please report bugs as well as other issues or suggestions on our [issues page](https://github.com/markusschmitt/vmc_jax/issues).
29
+
30
+ ## Documentation
31
+
32
+ Documentation is available [here](https://jvmc.readthedocs.io/en/latest/ "Documentation").
33
+
34
+ ## Installation
35
+
36
+ ### Option 1: ``pip``-install
37
+
38
+ 1. We recommend you create a new conda environment to work with jVMC:
39
+
40
+ conda create -n jvmc python=3.8
41
+ conda activate jvmc
42
+
43
+ 2. ``pip``-install the package
44
+
45
+ pip install jVMC
46
+
47
+
48
+ ### Option 2: Clone and ``pip``-install
49
+
50
+ 1. Clone the jVMC repository and check out the development branch:
51
+
52
+ git clone https://github.com/markusschmitt/vmc_jax.git
53
+ cd vmc_jax
54
+
55
+ 2. We recommend you create a new conda environment to work with jVMC:
56
+
57
+ conda create -n jvmc python=3.8
58
+ conda activate jvmc
59
+
60
+
61
+ 3. ``pip``-install the package
62
+
63
+ pip install .
64
+
65
+ Alternatively, for development:
66
+
67
+ pip install -e .[dev]
68
+
69
+ Test that everything worked, e.g. run 'python -c "import jVMC"' from a place different than ``vmc_jax``.
70
+
71
+ ### Compiling JAX
72
+
73
+ [How to compile JAX on a supercomputing cluster](documentation/readme/compile_jax_on_cluster.md)
74
+
75
+
76
+ ## Online example
77
+
78
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/markusschmitt/vmc_jax/blob/master/examples/ex0_ground_state_search.ipynb)
79
+
80
+ Click on the badge above to open a notebook that implements an exemplary ground state search in Google Colab.
81
+
82
+ ## Important gotchas
83
+ ### Out-of-memory issues and batching
84
+ Memory requirements grow with increasing network sizes. To avoid out-of-memory issues, the ``batchSize`` parameter of the ``NQS`` class has to be adjusted. The ``batchSize`` indicates on how many input configurations the network is evaluated concurrently. Out-of-memory issues are usually resolved by reducing this number. The ``numChains`` parameter of the ``Sampler`` class for Markov Chain Monte Carlo sampling plays a similar role, but its optimal values in terms of computational speed are typically not memory critical.
85
+
86
+ ## Citing jVMC
87
+
88
+ If you use the jVMC package for your research, please cite our reference paper [SciPost Phys. Codebases 2 (2022)](https://scipost.org/10.21468/SciPostPhysCodeb.2).
jVMC-1.2.0/README.md ADDED
@@ -0,0 +1,74 @@
1
+ [![Documentation Status](https://readthedocs.org/projects/jvmc/badge/?version=latest)](https://jvmc.readthedocs.io/en/latest/?badge=latest)
2
+ [![PyPi version](https://badgen.net/pypi/v/jVMC/)](https://pypi.org/project/jVMC/)
3
+ [![arXiv](https://img.shields.io/badge/arXiv-2108.03409-b31b1b.svg)](https://arxiv.org/abs/2108.03409)
4
+
5
+ # jVMC
6
+ This is an impementation of Variational Monte Carlo (VMC) for quantum many-body dynamics using the [JAX library](https://jax.readthedocs.io "JAX library") (and [Flax](https://flax.readthedocs.io "FLAX library") on top) to exploit the blessings of automatic differentiation for easy model composition and just-in-time compilation for execution on accelerators.
7
+
8
+ 1. [Documentation](#documentation)
9
+ 2. [Installation](#installation)
10
+ 3. [Online example](#online-example)
11
+ 4. [Important gotchas](#important-gotchas)
12
+ 5. [Citing jVMC](#citing-jvmc)
13
+
14
+ Please report bugs as well as other issues or suggestions on our [issues page](https://github.com/markusschmitt/vmc_jax/issues).
15
+
16
+ ## Documentation
17
+
18
+ Documentation is available [here](https://jvmc.readthedocs.io/en/latest/ "Documentation").
19
+
20
+ ## Installation
21
+
22
+ ### Option 1: ``pip``-install
23
+
24
+ 1. We recommend you create a new conda environment to work with jVMC:
25
+
26
+ conda create -n jvmc python=3.8
27
+ conda activate jvmc
28
+
29
+ 2. ``pip``-install the package
30
+
31
+ pip install jVMC
32
+
33
+
34
+ ### Option 2: Clone and ``pip``-install
35
+
36
+ 1. Clone the jVMC repository and check out the development branch:
37
+
38
+ git clone https://github.com/markusschmitt/vmc_jax.git
39
+ cd vmc_jax
40
+
41
+ 2. We recommend you create a new conda environment to work with jVMC:
42
+
43
+ conda create -n jvmc python=3.8
44
+ conda activate jvmc
45
+
46
+
47
+ 3. ``pip``-install the package
48
+
49
+ pip install .
50
+
51
+ Alternatively, for development:
52
+
53
+ pip install -e .[dev]
54
+
55
+ Test that everything worked, e.g. run 'python -c "import jVMC"' from a place different than ``vmc_jax``.
56
+
57
+ ### Compiling JAX
58
+
59
+ [How to compile JAX on a supercomputing cluster](documentation/readme/compile_jax_on_cluster.md)
60
+
61
+
62
+ ## Online example
63
+
64
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/markusschmitt/vmc_jax/blob/master/examples/ex0_ground_state_search.ipynb)
65
+
66
+ Click on the badge above to open a notebook that implements an exemplary ground state search in Google Colab.
67
+
68
+ ## Important gotchas
69
+ ### Out-of-memory issues and batching
70
+ Memory requirements grow with increasing network sizes. To avoid out-of-memory issues, the ``batchSize`` parameter of the ``NQS`` class has to be adjusted. The ``batchSize`` indicates on how many input configurations the network is evaluated concurrently. Out-of-memory issues are usually resolved by reducing this number. The ``numChains`` parameter of the ``Sampler`` class for Markov Chain Monte Carlo sampling plays a similar role, but its optimal values in terms of computational speed are typically not memory critical.
71
+
72
+ ## Citing jVMC
73
+
74
+ If you use the jVMC package for your research, please cite our reference paper [SciPost Phys. Codebases 2 (2022)](https://scipost.org/10.21468/SciPostPhysCodeb.2).
@@ -0,0 +1,14 @@
1
+ from . import global_defs
2
+ from . import nets
3
+ from . import operator
4
+ from . import util
5
+ from . import mpi_wrapper
6
+ from . import vqs
7
+ from . import sampler
8
+ from . import stats
9
+
10
+ from .version import __version__
11
+ from .global_defs import set_pmap_devices
12
+
13
+ from jax.config import config
14
+ config.update("jax_enable_x64", True)
@@ -0,0 +1,55 @@
1
+ import numpy as np
2
+
3
+ # Complex floating point
4
+ tCpx = np.complex128
5
+ # Real floating point
6
+ tReal = np.float64
7
+
8
+ from mpi4py import MPI
9
+
10
+ import jax
11
+
12
+ from functools import partial
13
+ import collections
14
+
15
+ try:
16
+ myDevice = jax.devices()[MPI.COMM_WORLD.Get_rank() % len(jax.devices())]
17
+ except:
18
+ myDevice = jax.devices()[0]
19
+ print("WARNING: Could not assign devices based on MPI ranks. Assigning default device ", myDevice)
20
+
21
+ myPmapDevices = jax.devices() # [myDevice]
22
+ myDeviceCount = len(myPmapDevices)
23
+ pmap_for_my_devices = partial(jax.pmap, devices=myPmapDevices)
24
+
25
+ pmapDevices = None
26
+ def pmap_devices_updated():
27
+ if collections.Counter(pmapDevices) == collections.Counter(myPmapDevices):
28
+ return False
29
+ return True
30
+
31
+
32
+ def get_iterable(x):
33
+ if isinstance(x, collections.abc.Iterable):
34
+ return x
35
+ else:
36
+ return (x,)
37
+
38
+
39
+ def set_pmap_devices(devices):
40
+ devices = list(get_iterable(devices))
41
+ global myPmapDevices
42
+ global myDeviceCount
43
+ global pmap_for_my_devices
44
+ myPmapDevices = devices
45
+ myDeviceCount = len(myPmapDevices)
46
+ pmap_for_my_devices = partial(jax.pmap, devices=myPmapDevices)
47
+ myDevice = myPmapDevices[0]
48
+
49
+
50
+ def device_count():
51
+ return len(myPmapDevices)
52
+
53
+
54
+ def devices():
55
+ return myPmapDevices
@@ -0,0 +1,299 @@
1
+ from mpi4py import MPI
2
+ import jax
3
+ import jax.numpy as jnp
4
+ import numpy as np
5
+
6
+ import jVMC.global_defs as global_defs
7
+
8
+ comm = MPI.COMM_WORLD
9
+ rank = comm.Get_rank()
10
+ commSize = comm.Get_size()
11
+
12
+ globNumSamples = 0
13
+ myNumSamples = 0
14
+
15
+ from functools import partial
16
+ import time
17
+ communicationTime = 0.
18
+
19
+
20
+ def _cov_helper(data, p):
21
+ return jnp.expand_dims(
22
+ jnp.matmul(jnp.conj(jnp.transpose(data)), jnp.multiply(p[:, None], data)),
23
+ axis=0
24
+ )
25
+
26
+
27
+ _sum_up_pmapd = None
28
+ _sum_sq_pmapd = None
29
+ mean_helper = None
30
+ cov_helper = None
31
+
32
+
33
+ def jit_my_stuff():
34
+ # This is a helper function to make sure that pmap'd functions work with the actual choice of devices
35
+ # at all times.
36
+
37
+ global _sum_up_pmapd
38
+ global _sum_sq_pmapd
39
+ global mean_helper
40
+ global cov_helper
41
+ global pmapDevices
42
+
43
+ if global_defs.pmap_devices_updated():
44
+ _sum_up_pmapd = global_defs.pmap_for_my_devices(lambda x: jax.lax.psum(jnp.sum(x, axis=0), 'i'), axis_name='i')
45
+ # _sum_sq_pmapd = global_defs.pmap_for_my_devices(lambda data, mean, p: jax.lax.psum(jnp.conj(data - mean).dot(p[..., None] * (data - mean)), 'i'), axis_name='i', in_axes=(0, None, 0))
46
+ _sum_sq_pmapd = global_defs.pmap_for_my_devices(lambda data, mean, p: jnp.einsum('ij, ij, i -> j', jnp.conj(data - mean[None, ...]), (data - mean[None, ...]), p), in_axes=(0, None, 0))
47
+ mean_helper = global_defs.pmap_for_my_devices(lambda data, p: jnp.expand_dims(jnp.dot(p, data), axis=0), in_axes=(0, 0))
48
+ cov_helper = global_defs.pmap_for_my_devices(_cov_helper, in_axes=(0, 0))
49
+
50
+ pmapDevices = global_defs.myPmapDevices
51
+
52
+
53
+ def distribute_sampling(numSamples, localDevices=None, numChainsPerDevice=1) -> int:
54
+ """Distribute sampling tasks across processes and devices.
55
+
56
+ For a desired total number of samples this function determines how many samples
57
+ should be generated by each Monte Carlo chain.
58
+
59
+ It is assumed that a given number of MC chains is running in parallel on each device,
60
+ and that each MPI process can potentially utilize multiple devices. Since the numbers
61
+ of samples per chain have to be identical accross the devices of one MPI process, the
62
+ resulting total number of samples can slightly exceed the requested number of samples.
63
+
64
+ Arguments:
65
+ * ``numSamples``: Total number of samples.
66
+ * ``localDevices``: Number of devices per MPI process.
67
+ * ``numChainsPerDevice``: Number of chains run in parallel on each device.
68
+
69
+ Returns:
70
+ Number of samples to be generated per device to reach the desired total number of samples.
71
+ """
72
+
73
+ global globNumSamples
74
+
75
+ # Determine number of samples per process
76
+ samplesPerProcess = numSamples // commSize
77
+
78
+ if rank < numSamples % commSize:
79
+ samplesPerProcess += 1
80
+
81
+ if localDevices is None:
82
+
83
+ globNumSamples = numSamples
84
+
85
+ return samplesPerProcess
86
+
87
+ numChainsPerProcess = localDevices * numChainsPerDevice
88
+
89
+ def spc(spp):
90
+ return int((spp + numChainsPerProcess - 1) // numChainsPerProcess)
91
+
92
+ a = numSamples % commSize
93
+ globNumSamples = (a * spc(1 + numSamples // commSize) + (commSize - a) * spc(numSamples // commSize)) * numChainsPerProcess
94
+
95
+ return spc(samplesPerProcess)
96
+
97
+
98
+ def first_sample_id():
99
+
100
+ global globNumSamples
101
+
102
+ mySamples = globNumSamples // commSize
103
+
104
+ firstSampleId = rank * mySamples
105
+
106
+ if rank < globNumSamples % commSize:
107
+ firstSampleId += rank
108
+ else:
109
+ firstSampleId += globNumSamples % commSize
110
+
111
+ return firstSampleId
112
+
113
+
114
+ def global_sum(data):
115
+ """ Computes the sum of input data across MPI processes and device/batch dimensions.
116
+
117
+ On each MPI process the input data is assumed to be a ``jax.numpy.array`` with a leading
118
+ device dimension followed by a batch dimension. The data is reduced by summing up along
119
+ device and batch dimensions as well as accross MPI processes. Hence, the result is an
120
+ array of shape ``data.shape[2:]``.
121
+
122
+ Arguments:
123
+ * ``data``: Array of input data.
124
+
125
+ Returns:
126
+ Sum of data across MPI processes and device/batch dimensions.
127
+ """
128
+
129
+ jit_my_stuff()
130
+
131
+ data.block_until_ready()
132
+ t0 = time.perf_counter()
133
+
134
+ # Compute sum locally
135
+ localSum = np.array(_sum_up_pmapd(data)[0])
136
+
137
+ # Allocate memory for result
138
+ res = np.empty_like(localSum, dtype=localSum.dtype)
139
+
140
+ # Global sum
141
+ comm.Allreduce(localSum, res, op=MPI.SUM)
142
+
143
+ global communicationTime
144
+ communicationTime += time.perf_counter() - t0
145
+
146
+ # return jnp.array(res)
147
+ return jax.device_put(res, global_defs.myDevice)
148
+
149
+
150
+ def global_mean(data, p):
151
+ """ Computes the mean of input data across MPI processes and device/batch dimensions.
152
+
153
+ On each MPI process the input data is assumed to be a ``jax.numpy.array`` with a leading
154
+ device dimension followed by a batch dimension. The data is reduced by computing the mean
155
+ along device and batch dimensions as well as accross MPI processes. Hence, the result is
156
+ an array of shape ``data.shape[2:]``.
157
+
158
+ The mean is computed using the given probabilities, i.e.,
159
+
160
+ :math:`\\langle X\\rangle=\sum_{j=1}^{N_S} p_jX_j`
161
+
162
+ Arguments:
163
+ * ``data``: Array of input data.
164
+ * ``p``: Probabilities associated with the given data.
165
+
166
+ Returns:
167
+ Mean of data across MPI processes and device/batch dimensions.
168
+ """
169
+
170
+
171
+
172
+ jit_my_stuff()
173
+
174
+ return global_sum(mean_helper(data, p))
175
+
176
+
177
+ def global_variance(data, p):
178
+ """ Computes the variance of input data across MPI processes and device/batch dimensions.
179
+
180
+ On each MPI process the input data is assumed to be a ``jax.numpy.array`` with a leading
181
+ device dimension followed by a batch dimension. The data is reduced by computing the variance
182
+ along device and batch dimensions as well as accross MPI processes. Hence, the result is
183
+ an array of shape ``data.shape[2:]``.
184
+
185
+ The mean is computed using the given probabilities, i.e.,
186
+
187
+ :math:`\\text{Var}(X)=\sum_{j=1}^{N_S} p_j |X_j-\\langle X\\rangle|^2`
188
+
189
+ Arguments:
190
+ * ``data``: Array of input data.
191
+ * ``p``: Probabilities associated with the given data.
192
+
193
+ Returns:
194
+ Variance of data across MPI processes and device/batch dimensions.
195
+ """
196
+
197
+ jit_my_stuff()
198
+
199
+ data.block_until_ready()
200
+
201
+ mean = global_mean(data, p)
202
+ # Compute sum locally
203
+ localSum = np.array(_sum_sq_pmapd(data, mean, p)[0])
204
+
205
+ # Allocate memory for result
206
+ res = np.empty_like(localSum, dtype=localSum.dtype)
207
+
208
+ t0 = time.perf_counter()
209
+
210
+ # Global sum
211
+ global globNumSamples
212
+ comm.Allreduce(localSum, res, op=MPI.SUM)
213
+
214
+ global communicationTime
215
+ communicationTime += time.perf_counter() - t0
216
+
217
+ return jax.device_put(res, global_defs.myDevice)
218
+
219
+
220
+ def global_covariance(data, p):
221
+ """ Computes the covariance matrix of input data across MPI processes and device/batch dimensions.
222
+
223
+ On each MPI process the input data is assumed to be a ``jax.numpy.array`` with a leading
224
+ device dimension followed by a batch dimension and one data dimension.
225
+ The data is reduced by computing the covariance
226
+ matrix along device and batch dimensions as well as accross MPI processes. Hence, the result is
227
+ an array of shape ``data.shape[2]`` :math:`\\times` ``data.shape[2]``.
228
+
229
+ The mean is computed using the given probabilities, i.e.,
230
+
231
+ :math:`\\text{Cov}(X)=\sum_{j=1}^{N_S} p_jX_j\\cdot X_j^\\dagger - \\bigg(\sum_{j=1}^{N_S} p_jX_j\\bigg)\\cdot\\bigg(\sum_{j=1}^{N_S}p_jX_j^\\dagger\\bigg)`
232
+
233
+ Arguments:
234
+ * ``data``: Array of input data.
235
+ * ``p``: Probabilities associated with the given data.
236
+
237
+ Returns:
238
+ Covariance matrix of data across MPI processes and device/batch dimensions.
239
+ """
240
+
241
+ jit_my_stuff()
242
+
243
+ return global_sum(cov_helper(data, p))
244
+
245
+
246
+ def bcast_unknown_size(data, root=0):
247
+ """ Broadcast a one-dimensional array.
248
+
249
+ This function broadcasts the input data array to all MPI processes.
250
+
251
+ Arguments:
252
+ * ``data``: One dimensional array of datatype ``np.float64``.
253
+ * ``root``: Rank of root process.
254
+
255
+ Returns:
256
+ On each MPI process the data received from the root process.
257
+ """
258
+
259
+ if rank == root:
260
+ if data.dtype != np.float64:
261
+ raise TypeError("Datatype has to be float64.")
262
+
263
+ dim = None
264
+ buf = None
265
+ if rank == root:
266
+ dim = len(data)
267
+ comm.bcast(dim, root=root)
268
+ buf = np.array(data)
269
+ else:
270
+ dim = comm.bcast(None, root=root)
271
+ buf = np.empty(dim, dtype=np.float64)
272
+
273
+ comm.Bcast([buf, dim, MPI.DOUBLE], root=root)
274
+
275
+ return buf
276
+
277
+
278
+ def gather(data):
279
+ """ Gathers and distributes data all-to-all.
280
+ The returned data is therefore of shape ``(commSize * data.shape[0],) + data.shape[1:]``.
281
+
282
+ Arguments:
283
+ * ``data``: Array of input data.
284
+
285
+ Returns:
286
+ Concatenated data from all devices.
287
+ """
288
+
289
+ res = comm.allgather(data.reshape((-1,*data.shape[2:])))
290
+ res = np.concatenate(res)
291
+
292
+ return res
293
+
294
+
295
+ def get_communication_time():
296
+ global communicationTime
297
+ t = communicationTime
298
+ communicationTime = 0.
299
+ return t
@@ -0,0 +1,8 @@
1
+ from jVMC.nets.activation_functions import *
2
+ from jVMC.nets.rbm import *
3
+ from jVMC.nets.cnn import *
4
+ from jVMC.nets.ffn import *
5
+ from jVMC.nets.rnn1d_general import *
6
+ from jVMC.nets.rnn2d_general import *
7
+ from jVMC.nets.sym_wrapper import *
8
+ from jVMC.nets.two_nets_wrapper import *
@@ -0,0 +1,32 @@
1
+ import jax
2
+ import jax.numpy as jnp
3
+ import flax.linen as nn
4
+
5
+
6
+ def square(x):
7
+ return x**2
8
+
9
+
10
+ def poly6(x):
11
+ x = x**2
12
+ return ((0.022222222 * x - 0.083333333) * x + 0.5) * x
13
+
14
+
15
+ def poly5(x):
16
+ xsq = x**2
17
+ return ((0.133333333 * xsq - 0.333333333) * xsq + 1.) * x
18
+
19
+ def log_cosh(x):
20
+ sgn_x = -2 * jnp.signbit(x.real) + 1
21
+ x = x * sgn_x
22
+ return x + jnp.log1p(jnp.exp(-2.0 * x)) - jnp.log(2.0)
23
+
24
+
25
+ activationFunctions = {
26
+ "square": square,
27
+ "poly5": poly5,
28
+ "poly6": poly6,
29
+ "elu": nn.elu,
30
+ "relu": nn.relu,
31
+ "tanh": jnp.tanh
32
+ }