示例#1
0
 def test_ones(self):
     """Check loop hafnian(J_2n)=hyp1f1(-2n/2,1/2,-1/2)*(2n)!/(n!2^n)"""
     A = np.float64(np.ones([2 * self.n, 2 * self.n]))
     haf = hafnian(A, loop=True)
     expected = fac(2 * self.n) / (fac(self.n) *
                                   (2**self.n)) * hyp1f1[self.n]
     self.assertTrue(np.allclose(haf, expected))
示例#2
0
 def test_4x4(self):
     """Check 4x4 hafnian"""
     A = np.float64(np.random.random([4, 4]))
     A += A.T
     haf = hafnian(A)
     expected = A[0, 1]*A[2, 3] + \
         A[0, 2]*A[1, 3] + A[0, 3]*A[1, 2]
     self.assertTrue(np.allclose(haf, expected))
示例#3
0
 def test_block_ones(self):
     """Check hafnian([[0, I_n], [I_n, 0]])=n!"""
     O = np.zeros([self.n, self.n])
     B = np.ones([self.n, self.n])
     A = np.vstack([np.hstack([O, B]), np.hstack([B, O])])
     A = np.float64(A)
     haf = hafnian(A)
     expected = float(fac(self.n))
     self.assertTrue(np.allclose(haf, expected))
示例#4
0
 def test_4x4_zero_diag(self):
     """Check 4x4 loop hafnian with zero diagonals"""
     A = np.float64(np.random.random([4, 4]))
     A += A.T
     A -= np.diag(np.diag(A))
     haf = hafnian(A, loop=True)
     expected = A[0, 1]*A[2, 3] + \
         A[0, 2]*A[1, 3] + A[0, 3]*A[1, 2]
     self.assertTrue(np.allclose(haf, expected))
示例#5
0
 def test_4x4(self):
     """Check 4x4 loop hafnian"""
     A = np.float64(np.random.random([4, 4]))
     A += A.T
     haf = hafnian(A, loop=True)
     expected = A[0, 1]*A[2, 3] \
         + A[0, 2]*A[1, 3] + A[0, 3]*A[1, 2] \
         + A[0, 0]*A[1, 1]*A[2, 3] + A[0, 1]*A[2, 2]*A[3, 3] \
         + A[0, 2]*A[1, 1]*A[3, 3] + A[0, 0]*A[2, 2]*A[1, 3] \
         + A[0, 0]*A[3, 3]*A[1, 2] + A[0, 3]*A[1, 1]*A[2, 2] \
         + A[0, 0]*A[1, 1]*A[2, 2]*A[3, 3]
     self.assertTrue(np.allclose(haf, expected))
示例#6
0
 def test_integer_casting(self):
     """Check casting to integer"""
     A = np.int64(np.ones([2 * self.n, 2 * self.n]))
     haf = hafnian(A)
     expected = fac(2 * self.n) / (fac(self.n) * (2**self.n))
     self.assertTrue(np.allclose(haf, expected))
示例#7
0
 def test_ones(self):
     """Check hafnian(J_2n)=(2n)!/(n!2^n)"""
     A = np.float64(np.ones([2 * self.n, 2 * self.n]))
     haf = hafnian(A)
     expected = fac(2 * self.n) / (fac(self.n) * (2**self.n))
     self.assertTrue(np.allclose(haf, expected))
示例#8
0
 def test_identity(self):
     """Check hafnian(I)=0"""
     A = np.identity(self.n)
     haf = hafnian(A)
     self.assertEqual(haf, 0)
示例#9
0
 def test_2x2(self):
     """Check 2x2 hafnian"""
     A = np.float64(np.random.random([2, 2]))
     A = A + A.T
     haf = hafnian(A)
     self.assertTrue(np.allclose(haf, A[0, 1]))
示例#10
0
 def test_identity(self):
     """Check loop hafnian(I)=1"""
     A = np.identity(self.n)
     haf = hafnian(A, loop=True)
     self.assertTrue(np.allclose(haf, 1))
示例#11
0
 def test_2x2(self):
     """Check 2x2 loop hafnian"""
     A = np.float64(np.random.random([2, 2]))
     A = A + A.T
     haf = hafnian(A, loop=True)
     self.assertTrue(np.allclose(haf, A[0, 1] + A[0, 0] * A[1, 1]))