afqmctools.observables package#
Submodules#
afqmctools.observables.generic module#
- afqmctools.observables.generic.observable_1body(rmd_ij_batch, Oji)#
Compute the expectation value of a 1-body operator.
The rdms and Oij must have a relative transpose between them.
Specifically,
\[\langle \hat{O} \rangle = \sum_{ij} \langle \hat{c}_i^\dagger \hat{c}_j \rangle O_{ij}\]and
\[\langle \hat{c}_i^\dagger \hat{c}_j \rangle = \rho_{ji}\]then,
\[\langle \hat{O} \rangle = Tr(\rho O.T) \text{ (after swapping dummy indices i<->j)}\]By convention, we apply the transpose to the observable
- Parameters:
rmd_ij_batch (
ndarray) – Batched reduced matrix elements of shape (nbatch,norb*norb).Oji (
ndarray) – transpose of a 1-body operator with shape (norb*norb).
- Returns:
results – Expectation value of Oij for each rdm in the batch with shape (nbatch,).
- Return type:
np.ndarray
afqmctools.observables.greens module#
- Defines shared mathematical operations for
computing observables.
Contents: -
#TODO: if performance is an issue, use numba!
- afqmctools.observables.greens.greens_1body(psi_left, psi_right=None, X=None)#
Compute the 1-body Green’s function as:
\[G_{ij} = < \Psi_L | \sum_{i,j} \hat{c}^\dagger_i \hat{c}_j | \Psi_R > / <\Psi_L|\Psi_R> = [ \Psi_R ( \Psi_L^\dagger \Psi_R )^{-1} \Psi_L^\dagger ]_{ji} = [ \Psi_L^* ( \Psi_R^T \Psi_L^* )^{-1} \Psi_R^T ]_{ij}\]- Parameters:
psi_left (
array) – the Slater matrix of the “left” wavefunction \(|\Psi_L>\) with shape (Nmo,Nelec)psi_right (
array) – the Slater matrix of the “right” wavefunction \(|\Psi_R>\) with shape (Nmo,Nelec). If None, then psi_right = psi_left.X (
array) – a transformation matrix X with shape (Nmo,Nmo). If given, it is applied to the basis of psi_left as psi_left = X @ psi_left.
Examples
>>> import numpy as np >>> from afqmctools.observables.greens import greens_1body >>> Nmo,Nelec = 5,2 >>> psi_left = np.eye(Nmo,Nelec) >>> G = greens_1body(psi_left)
- afqmctools.observables.greens.sd_overlap(psi_left, psi_right, X=None)#
Compute the overlap between two sets of orbitals. Does not take into account spin.
\[O = < \Psi_L | \Psi_R >\]- Parameters:
psi_left (
array) – the Slater matrix of the “left” wavefunction \(|\Psi_L>\) with shape (Nmo,Nelec)psi_right (
array) – the Slater matrix of the “right” wavefunction \(|\Psi_R>\) with shape (Nmo,Nelec).X (
array) – a transformation matrix X with shape (Nmo,Nmo). If given, it is applied to the basis of psi_left as psi_left = X @ psi_left.
Examples
>>> import numpy as np >>> from afqmctools.observables.greens import sd_overlap >>> Nmo,Nelec = 5,2 >>> psi_l= np.eye(Nmo,Nelec) >>> psi_r= np.eye(Nmo,Nelec) >>> O = sd_overlap(psi_l,psi_r) >>> psi_l = np.stack([psi_l,psi_l]) # UHF like >>> psi_r = np.stack([psi_r,psi_r]) # UHF like >>> # wrap in prod to take into account spin >>> O = np.prod(sd_overlap(psi_l,psi_r))
afqmctools.observables.rhonk module#
- afqmctools.observables.rhonk.calc_nofk(dma, meta, orbs, kcut, **kwargs)#
calculate momentum distribution n(k)
- Parameters:
dma (numpy.ndarray) – shape (number of samples, norb, norb)
meta (dict) – dictionary containing system metadata
orbs (numpy.ndarray) – orbitals represented in terms of the FFT grid
kcut (float) – cutoff for k-points
TODO (get k-point weights)
- afqmctools.observables.rhonk.calc_rhor(dma, meta, orbs, pwcut=None, **kwargs)#
Compute charge density rho(r) from reciprocal-space orbitals
Formally, the charge density is given by:
\[\begin{split}\rho(\mathbf{r}) = \sum_{kk'} \sum_{ij} \rho^{kk'}_{ij} \\phi_{i}^{k}(\\mathbf{r}) \\phi_{j}^{k'*}(\\mathbf{r})\end{split}\]- Parameters:
dma (numpy.ndarray) – shape (number of samples, norb, norb)
meta (dict) – dictionary containing system metadata
orbs (numpy.ndarray) – orbitals represented in terms of the FFT grid
- afqmctools.observables.rhonk.calc_rhor_from_r(dma, meta, rorbs, **kwargs)#
Compute charge density rho(r) from real-space orbitals
- The density matrix may have multiple samples, and we use Welford’s
algorithm to compute the mean and standard deviation of the charge density to avoid storing all the samples of rho(r) in memory at the same time.
- Parameters:
dma (numpy.ndarray) – reduced one-body density matrix samples shape (number of samples, norb, norb)
meta (dict) – dictionary containing system metadata
rorbs (numpy.ndarray) – real-space orbitals with shape (number of k-points, norb, number of grid points)
- Returns:
rhom (numpy.ndarray) – mean charge density on real-space grid
rhoe (numpy.ndarray) – stochastic uncertainty in charge density on real-space grid
- afqmctools.observables.rhonk.calc_rhor_old(dma, meta, orbs, rvecs=None, **kwargs)#
calculate charge density rho(r) from k-space orbitals
- Parameters:
dma (numpy.ndarray) – shape (number of samples, norb, norb)
meta (dict) – dictionary containing system metadata
orbs (numpy.ndarray) – orbitals represented in terms of the FFT grid
rvecs (numpy.ndarray, optional) – real-space grid points. If not specified, will be generated from the metadata.
TODO (#)
- afqmctools.observables.rhonk.charge_density(rdm, error_rdm, orbital_source, rho_outfile, nsample=32, verbose=False)#
Calculate and save the charge density from the 1-RDM.
Will overwrite existing charge density file if it exists.
- Parameters:
rdm (np.ndarray) – The 1-RDM. Shape (nspins, norbs, norbs).
error_rdm (np.ndarray) – The error in the 1-RDM. Shape (nspins, norbs, norbs).
orbital_source (
Path) – The path to the orbitals file.rho_outfile (
Path) – The path to the output charge density file.nsample (
int) – The number of samples to use for resampling.verbose (bool) – Whether to print additional information.
- afqmctools.observables.rhonk.cubic_pos(spaces)#
- afqmctools.observables.rhonk.dm_in_basis(dma, orbs, imag_tol=1e-06)#
Put density matrix (dm) in orbital basis
- Parameters:
dma (numpy.ndarray) – shape (number of samples, norb, norb)
(Array) (orbs)
(float (imag_tol) – which should be zero for physical observables, default 1e-6
optional) (tolerance for imaginary component,) – which should be zero for physical observables, default 1e-6
- Returns:
ym (np.array) – mean of the rdm with shape (norb,norb)
ye (np.array) – standard deviation of the rdm with shape (norb,norb)
- afqmctools.observables.rhonk.get_gvecs(mesh)#
- afqmctools.observables.rhonk.get_rvecs(axes, mesh, center=False)#
- afqmctools.observables.rhonk.load_dict(fname)#
- afqmctools.observables.rhonk.open_read(fname, mode='r')#
- afqmctools.observables.rhonk.open_write(fname)#
- afqmctools.observables.rhonk.read_dm(fstat, symmetrize=False)#
- afqmctools.observables.rhonk.read_gaussian_cube(filename)#
Read a Gaussian cube file and return a dictionary with all volumetric data and metadata.
- Parameters:
filename (str) – Path to the Gaussian cube file
- Returns:
Dictionary containing the cube file data with the following keys:
comments: List of two comment lines
n_atoms: Number of atoms
origin: Origin coordinates [x, y, z]
grid_dimensions: Grid dimensions [nx, ny, nz]
vectors: Cell vectors (3x3 array)
units: Units for each dimension (‘Bohr’ or ‘Angstrom’)
- atoms: List of dictionaries for each atom with keys:
atomic_number
charge
position [x, y, z]
volumetric_data: 3D numpy array with the volumetric data
- Return type:
dict
- afqmctools.observables.rhonk.read_group(grp)#
- afqmctools.observables.rhonk.save_dict(arr_dict, h5file, slab=None)#
- afqmctools.observables.rhonk.save_vec(vec, h5file, slab, name)#
- afqmctools.observables.rhonk.write_dict(fname, data)#
- afqmctools.observables.rhonk.write_gaussian_cube(fcub, data, overwrite=False, **kwargs)#
Write Gaussian cube file from volumetric data.
- Parameters:
fcub (str) – The filename for the Gaussian cube file to be written.
data (dict) –
Dictionary containing the following keys:
’axes’: (3, 3) matrix representing the grid axes.
’data’: Volumetric data as a numpy array.
’elem’: (optional) List of atomic numbers, default is (1,).
’pos’: (optional) List of atomic positions, default is ((0, 0, 0),).
’origin’: (optional) Coordinates of the origin, default is (0, 0, 0).
’two_line_comment’: (optional) Comments at the file head, default is “cubenfilen”.
overwrite (bool, optional) – If True, overwrite the existing file if it exists. Default is False.
kwargs (dict, optional) – Additional keyword arguments to be passed to the write_gaussian_cube_text function.
Examples
>>> data = { ... "axes": np.diag((1.0, 1.0, 1.0)), ... "data": np.random.rand(10, 10, 10), # Volumetric data! ... "elem": [6, 6], ... "pos": [(0, 0, 0), (1, 1, 1)], ... "origin": (0, 0, 0), ... "two_line_comment": "This is a test cube file\nGenerated by write_gaussian_cube\n" ... } >>> write_gaussian_cube("test.cube", data, overwrite=True)
This will create a Gaussian cube file named “test.cube” with the provided data.
- afqmctools.observables.rhonk.write_gaussian_cube_text(vol, axes, elem=(1,), qs=None, pos=((0, 0, 0),), origin=(0, 0, 0), two_line_comment='cube\nfile\n')#
Write Gaussian cube file using volumetric data
- Parameters:
vol (np.array) – volumetric data, shape (nx, ny, nz)
axes (np.array) – grid basis, e.g. np.diag((dx, dy, dz))
elem (array-like, optional) – list of atomic numbers, default (1,)
qs (array-like, optional) – list of atomic charges, default (0,)
pos (array-like, optional) – list of atomic positions
origin (array-like, optional) – coordinates of the origin
two_line_comment (str, optional) – comments at file head
- Returns:
Gaussian file content
- Return type:
str
afqmctools.observables.spin module#
- Defines shared mathematical operations for calculating
angular momentum
Routines#
- afqmctools.observables.spin.local_spin(rdm, spin_symm=<SpinSymm.COLLINEAR: 2>)#
Compute the expectation value of the x,y,z-component of the spin operator on each site as:
\(\langle S^\alpha_i \rangle = \frac{1}{2}(n_{i\uparrow} - n_{i\downarrow})\)
- Parameters:
rdm (np.array|list) – the 1-body reduced density matrix (rdm). See spin_spin notes for correct input shape.
spin_symm (
SpinSymm) – the spin symmetry of the rdm. if spin_symm is a SpinSymm instance, it is used as is. if spin_symm is a string, it is converted to a SpinSymm instance. Accepted string values are {‘rhf’,’uhf’,’ghf’,’closed’,’collinear’,’noncollinear’}. String values are not case-sensitive.
- Returns:
spin_vec – (3,M) array; the expectation value of the spin operator at each site
- Return type:
np.array
- afqmctools.observables.spin.spin_spin(rdm, spin_symm=<SpinSymm.COLLINEAR: 2>, resolveXY=False)#
Compute the spin squared operator as:
\(<S_i S_j> = <S^z_i S^z_j + \frac{1}{2}(S^+_i S^-_j + S^-_i S^+_j)>\)
- Parameters:
rdm (np.array|list) – the 1-body reduced density matrix (rdm). See notes for correct input shape.
spin_symm (
SpinSymm) – the spin symmetry of the rdm. if spin_symm is a SpinSymm instance, it is used as is. if spin_symm is a string, it is converted to a SpinSymm instance. Accepted string values are {‘rhf’,’uhf’,’ghf’,’closed’,’collinear’,’noncollinear’}. String values are not case-sensitive.resolveXY (bool) – return a the XY and Z contributions separately
- Returns:
spin_spin – the expectation value of the spin-spin operator. If resolveXY is true, return (SXXYY,SZZ)
- Return type:
np.array
Notes
- Input rdm(s) are either a 2D numpy array or a list of 2D numpy arrays. If a list is provided,
it must have length of 1 for closed-shell or noncollinear rmds, or 2 for collinear systems. For Collinear systems, the first rdm in the list is the spin-up rdm and the second is the spin-down rdm.
- The correct input shape for the rdm depends on the spin symmetry of the system:
rhf / closed: an array of shape (M,M) where M is the number of spatial orbitals
uhf / collinear: a list of 2 arrays of shape (M,M) or an array of shape (2M,M) where M is the number of spatial orbitals
ghf / noncollinear: (2M,2M) where M is the number of spatial orbitals
- afqmctools.observables.spin.spin_squared(rdm, spin_symm=<SpinSymm.COLLINEAR: 2>, verbose=False)#
Compute the spin squared operator as:
\(<S^2> = <S_z^2 + \frac{1}{2}(S_+ S_- + S_- S_+)>\)
- Parameters:
rdm (np.array|list) – the 1-body reduced density matrix (rdm). See notes for correct input shape.
spin_symm (
SpinSymm) – the spin symmetry of the rdm. if spin_symm is a SpinSymm instance, it is used as is. if spin_symm is a string, it is converted to a SpinSymm instance. Accepted string values are {‘rhf’,’uhf’,’ghf’,’closed’,’collinear’,’noncollinear’}. String values are not case-sensitive.verbose (
bool) – print S^2 contributions if True; print nothing if False
- Returns:
spin_squared – the expectation value of the spin squared operator
- Return type:
float
Notes
- Input rdm(s) are either a 2D numpy array or a list of 2D numpy arrays. If a list is provided,
it must have length of 1 for closed-shell or noncollinear rmds, or 2 for collinear systems. For Collinear systems, the first rdm in the list is the spin-up rdm and the second is the spin-down rdm.
- The correct input shape for the rdm depends on the spin symmetry of the system:
rhf / closed: an array of shape (M,M) where M is the number of spatial orbitals
uhf / collinear: a list of 2 arrays of shape (M,M) or an array of shape (2M,M) where M is the number of spatial orbitals
ghf / noncollinear: (2M,2M) where M is the number of spatial orbitals
- afqmctools.observables.spin.spin_z(rdm, spin_symm=<SpinSymm.COLLINEAR: 2>)#
Compute the expectation value of the z-component of the spin operator as:
\(\langle S_z \rangle = \frac{1}{2}(\sum_{i} n_{i\uparrow} - n_{i\downarrow})\)
- Parameters:
rdm (np.array|list) – the 1-body reduced density matrix (rdm). See notes for correct input shape.
spin_symm (
SpinSymm) – the spin symmetry of the rdm. if spin_symm is a SpinSymm instance, it is used as is. if spin_symm is a string, it is converted to a SpinSymm instance. Accepted string values are {‘rhf’,’uhf’,’ghf’,’closed’,’collinear’,’noncollinear’}. String values are not case-sensitive.
- Returns:
spin_z – the expectation value of the z-component of the spin operator
- Return type:
float
Notes
- Input rdm(s) are either a 2D numpy array or a list of 2D numpy arrays. If a list is provided,
it must have length of 1 for closed-shell or noncollinear rmds, or 2 for collinear systems. For Collinear systems, the first rdm in the list is the spin-up rdm and the second is the spin-down rdm.
- The correct input shape for the rdm depends on the spin symmetry of the system:
rhf / closed: an array of shape (M,M) where M is the number of spatial orbitals
uhf / collinear: a list of 2 arrays of shape (M,M) or an array of shape (2M,M) where M is the number of spatial orbitals
ghf / noncollinear: (2M,2M) where M is the number of spatial orbitals