>

$\mathrm{with}\left(\mathrm{QuantumChemistry}\right)\:$

We demonstrate the command by using it to purify an approximate 2RDM of hydrogen fluoride generated from secondorder manybody perturbation (MP2) theory. First, we generate the data from applying the MP2 command to the geometry of hydrogen fluoride where we include the keyword parameter return_rdm="rdm1_and_rdm2" to return the 2RDM.
>

$\mathrm{hf}\u2254\mathrm{MolecularGeometry}\left(''hydrogen\; fluoride''\right)semi;$

${\mathrm{hf}}{\u2254}\left[\left[{''F''}{\,}{0}{\,}{0}{\,}{0}\right]{\,}\left[{''H''}{\,}{0.43580000}{\,}{\mathrm{0.14770000}}{\,}{\mathrm{0.81880000}}\right]\right]$
 (1) 
>

$\mathrm{data\_hf}\u2254\mathrm{MP2}\left(\mathrm{hf}comma;\mathrm{return\_rdm}equals;''rdm1\_and\_rdm2''\right)colon;$

Second, we reshape the data stored in a 4index tensor into a 2index matrix
>

$\mathrm{D2}\u2254\mathrm{Matrix}\left(36comma;36comma;\mathrm{shape}equals;\mathrm{symmetric}\right)colon;\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathrm{D2}\left(..\right)\u2254\mathrm{ArrayTools}:\mathrm{Permute}\left(\mathrm{data\_hf}\left[\mathrm{rdm2}\right]comma;\left[1comma;3comma;2comma;4\right]\right)semi;$

${{\mathrm{\_rtable}}}_{{36893488667168780028}}$
 (2) 
and compute the eigenvalues of the 2RDM with the Eigenvalues command in the LinearAlgebra package
>

$\mathrm{eigenvals}\u2254\mathrm{LinearAlgebra}:\mathrm{Eigenvalues}\left(\mathrm{D2}\right)semi;$

${{\mathrm{\_rtable}}}_{{36893488667357649364}}$
 (3) 
Notice that the lowest eigenvalue of the approximate 2RDM is negative (0.0143), but because each eigenvalue represents the probability of being in a twoelectron quantum state, all of the eigenvalues of the 2RDM should be nonnegative! Finally, we use the Purify2RDM command to correct this violation as well as more subtle violations of the physical requirements for the 2RDM
>

$\mathrm{data\_hf\_purified}\u2254\mathrm{Purify2RDM}\left(\mathrm{data\_hf}\left[\mathrm{rdm2}\right]\right)semi;$

${\mathrm{table}}{}\left({\mathrm{\%id}}{\=}{36893488667421936796}\right)$
 (4) 
To check the correction of the 2RDM, as before we rearrange the outputed 4index tensor into a 2index matrix
>

$\mathrm{D2p}\u2254\mathrm{Matrix}\left(36comma;36comma;\mathrm{shape}equals;\mathrm{symmetric}\right)colon;\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\phantom{\rule[0.0ex]{0.0em}{0.0ex}}\mathrm{D2p}\left(..\right)\u2254\mathrm{ArrayTools}:\mathrm{Permute}\left(\mathrm{data\_hf\_purified}\left[\mathrm{rdm2}\right]comma;\left[1comma;3comma;2comma;4\right]\right)semi;$

${{\mathrm{\_rtable}}}_{{36893488667168782084}}$
 (5) 
which we diagonalize with the LinearAlgebra:Eigenvalues command
>

$\mathrm{eigenvals}\u2254\mathrm{LinearAlgebra}:\mathrm{Eigenvalues}\left(\mathrm{D2p}\right)semi;$

${{\mathrm{\_rtable}}}_{{36893488667357640572}}$
 (6) 
All of the eigenvalues, we find, are now nonnegative.