Example #1
0
    def test_apply_spinful_fermionop(self):
        """
        Make sure the spin-orbital reordering is working by comparing
        apply operation
        """
        wfn = Wavefunction([[2, 0, 2]])
        wfn.set_wfn(strategy='random')
        wfn.normalize()
        cirq_wf = to_cirq(wfn).reshape((-1, 1))

        op_to_apply = FermionOperator()
        test_state = copy.deepcopy(wfn)
        test_state.set_wfn('zero')
        for p, q, r, s in product(range(2), repeat=4):
            op = FermionOperator(
                ((2 * p, 1), (2 * q + 1, 1), (2 * r + 1, 0), (2 * s, 0)),
                coefficient=numpy.random.randn())
            op_to_apply += op + hermitian_conjugated(op)
            test_state += wfn.apply(op + hermitian_conjugated(op))

        opmat = get_sparse_operator(op_to_apply, n_qubits=4).toarray()
        new_state_cirq = opmat @ cirq_wf

        # this part is because we need to pass a normalized wavefunction
        norm_constant = new_state_cirq.conj().T @ new_state_cirq
        new_state_cirq /= numpy.sqrt(norm_constant)
        new_state_wfn = from_cirq(new_state_cirq.flatten(), thresh=1.0E-12)
        new_state_wfn.scale(numpy.sqrt(norm_constant))

        self.assertTrue(
            numpy.allclose(test_state.get_coeff((2, 0)),
                           new_state_wfn.get_coeff((2, 0))))
Example #2
0
    def test_evolve_spinful_fermionop(self):
        """
        Make sure the spin-orbital reordering is working by comparing
        time evolution
        """
        wfn = Wavefunction([[2, 0, 2]])
        wfn.set_wfn(strategy='random')
        wfn.normalize()
        cirq_wf = to_cirq(wfn).reshape((-1, 1))

        op_to_apply = FermionOperator()
        for p, q, r, s in product(range(2), repeat=4):
            op = FermionOperator(
                ((2 * p, 1), (2 * q + 1, 1), (2 * r + 1, 0), (2 * s, 0)),
                coefficient=numpy.random.randn())
            op_to_apply += op + hermitian_conjugated(op)

        opmat = get_sparse_operator(op_to_apply, n_qubits=4).toarray()
        dt = 0.765
        new_state_cirq = scipy.linalg.expm(-1j * dt * opmat) @ cirq_wf
        new_state_wfn = from_cirq(new_state_cirq.flatten(), thresh=1.0E-12)
        test_state = wfn.time_evolve(dt, op_to_apply)
        self.assertTrue(
            numpy.allclose(test_state.get_coeff((2, 0)),
                           new_state_wfn.get_coeff((2, 0))))
Example #3
0
    def test_wick(self):
        """Check that wick performs the proper restructuring of the
        density matrix given a string of indexes.
        """
        norb = 4
        nele = 4
        s_z = 0
        wfn = Wavefunction([[nele, s_z, norb]])
        numpy.random.seed(seed=1)
        wfn.set_wfn(strategy='random')
        wfn.normalize()
        rdms = wfn._compute_rdm(4)
        out1 = wick.wick('k j^', list(rdms), True)
        two = numpy.eye(norb, dtype=out1.dtype) * 2.0
        self.assertRaises(ValueError, wick.wick, 'k0 j', list(rdms))
        self.assertTrue(numpy.allclose(two - out1.T, rdms[0]))

        self.assertRaises(ValueError, wick.wick, 'k^ l i^ j', list(rdms), True)
        out2 = wick.wick('k l i^ j^', list(rdms), True)

        h_1 = numpy.zeros_like(out1)
        for i in range(norb):
            h_1[:, :] += out2[:, i, :, i] / (norb * 2 - nele - 1)
        self.assertAlmostEqual(numpy.std(out1 + h_1), 0.)

        out2a = wick.wick('k l^ i^ j', list(rdms), True)
        self.assertAlmostEqual(out2a[2, 3, 0, 1], -rdms[1][0, 3, 2, 1])

        out3 = wick.wick('k l m i^ j^ n^', list(rdms), True)
        h_2 = numpy.zeros_like(out2)
        for i in range(norb):
            h_2[:, :, :, :] += out3[:, i, :, :, i, :] / (norb * 2 - nele - 2)
        self.assertAlmostEqual(numpy.std(out2 - h_2), 0.)

        out4 = wick.wick('k l m x i^ j^ n^ y^', list(rdms), True)
        h_3 = numpy.zeros_like(out3)
        for i in range(norb):
            h_3[:, :, :, :, :, :] += out4[:, i, :, :, :, i, :, :] / (norb * 2 -
                                                                     nele - 3)
        self.assertAlmostEqual(numpy.std(out3 + h_3), 0.)