コード例 #1
0
ファイル: test_p2N.py プロジェクト: cmendl/fermifab
    def _p2N_err(self, orbs, p, N):
        shape_data = int(binom(orbs, p)), int(binom(orbs, p))
        h = fermifab.FermiOp(orbs, p, p, fermifab.crand(*shape_data))

        # random wavefunction "psi"
        psi = fermifab.FermiState(orbs,
                                  N,
                                  data=fermifab.crand(int(binom(orbs, N))))

        return abs((psi.H @ fermifab.p2N(h, N) @ psi).data[0] -
                   fermifab.trace(h @ fermifab.rdm(psi, p)))
コード例 #2
0
    def _norbs_err(self, orbs, N, real_valued=True):
        """norbs - 'natural orbitals': eigenstates of the 1-body RDM"""

        if real_valued:
            psi = fermifab.FermiState(orbs,
                                      N,
                                      data=np.random.rand(int(binom(orbs, N))))
        else:
            psi = fermifab.FermiState(orbs,
                                      N,
                                      data=fermifab.crand(int(binom(orbs, N))))

        # compute the one-body reduced density matrix
        rho = fermifab.rdm(psi, 1)

        # diagonalize rho and construct base change matrix operating on N-fold tensor product
        _, U = fermifab.eig(rho)
        UN = fermifab.tensor_op(U, N)
        # apply inverse base change matrix to psi
        psi = UN.H @ psi

        G = fermifab.rdm(psi, 1)

        # G should be a diagonal matrix
        return np.linalg.norm(np.diag(np.diag(G.data)) - G.data)
コード例 #3
0
    def test_calcT1(self):
        psi = fermifab.FermiState(6, 4)
        x = fermifab.crand(int(binom(6, 4)))
        psi.data = x / np.linalg.norm(x)

        err = self._err_calcT1(psi)
        self.assertAlmostEqual(err, 0)
コード例 #4
0
    def test_arithmetic_operators(self):
        orbs = 6
        N = 4
        data1 = fermifab.crand(int(binom(orbs, N)), int(binom(orbs, N)))
        data2 = fermifab.crand(int(binom(orbs, N)), int(binom(orbs, N)))
        S = fermifab.FermiOp(orbs, N, N, data=data1)
        R = fermifab.FermiOp(orbs, N, N, data=data2)
        a, b = fermifab.crand(2)

        err = np.linalg.norm((S + R).data - (S.data + R.data))
        self.assertAlmostEqual(err, 0)

        err = np.linalg.norm((S - R).data - (S.data - R.data))
        self.assertAlmostEqual(err, 0)

        err = np.linalg.norm((a * S * b).data - a * S.data * b)
        self.assertAlmostEqual(err, 0)

        err = np.linalg.norm((S / a).data - S.data / a)
        self.assertAlmostEqual(err, 0)
コード例 #5
0
    def test_arithmetic_operators(self):
        orbs = 6
        N = 4
        data1 = fermifab.crand(int(binom(orbs, N)))
        data2 = fermifab.crand(int(binom(orbs, N)))
        psi = fermifab.FermiState(orbs, N, data=data1)
        phi = fermifab.FermiState(orbs, N, data=data2)
        a, b = np.random.rand(2)

        err = np.linalg.norm((psi + phi).data - (psi.data + phi.data))
        self.assertAlmostEqual(err, 0)

        err = np.linalg.norm((psi - phi).data - (psi.data - phi.data))
        self.assertAlmostEqual(err, 0)

        err = np.linalg.norm((a * psi * b).data - a * psi.data * b)
        self.assertAlmostEqual(err, 0)

        err = np.linalg.norm((psi / b).data - psi.data / b)
        self.assertAlmostEqual(err, 0)

        err = fermifab.norm(psi) - np.linalg.norm(psi.data)
        self.assertAlmostEqual(err, 0)
コード例 #6
0
    def test_density_matrix(self):
        orbs = 6
        N = 4
        data = fermifab.crand(int(binom(orbs, N)))
        psi = fermifab.FermiState(orbs, N, data=data)
        psi = psi / fermifab.norm(psi)
        rho = psi @ psi.H

        self.assertAlmostEqual((psi.H @ psi).data[0], 1)

        err = np.linalg.norm((rho @ psi).data - psi.data)
        self.assertAlmostEqual(err, 0)

        err = fermifab.norm(rho @ rho - rho)
        self.assertAlmostEqual(err, 0)
コード例 #7
0
ファイル: test_tensor_op.py プロジェクト: cmendl/fermifab
    def _tensor_op_err(self, orbs, p, N):
        err = 0

        # random wavefunction "psi"
        psi = fermifab.FermiState(orbs, N, data=fermifab.crand(int(binom(orbs, N))))

        # compute one-body reduced density matrix
        rho = fermifab.rdm(psi, 1)
        U, _ = np.linalg.qr(rho.data)

        U = fermifab.FermiOp(orbs, 1, 1, U)

        Up = fermifab.tensor_op(U, p)
        UN = fermifab.tensor_op(U, N)

        err += np.linalg.norm((UN.H @ UN).data - np.eye(*UN.shape))
        err += np.linalg.norm((Up.H @ fermifab.rdm(UN @ psi, p) @ Up - fermifab.rdm(psi, p)).data)
        return err
コード例 #8
0
ファイル: test_calcQ.py プロジェクト: cmendl/fermifab
 def test_calcQ(self):
     psi = fermifab.FermiState(6, 4, fermifab.crand(int(binom(6, 4))))
     err = self._err_calcQ(psi)
     self.assertAlmostEqual(err, 0)