def test_2x2(self, random_matrix): """Check 2x2 permanent""" A = random_matrix(2) p = perm(A, method="ryser") expected = A[0, 0] * A[1, 1] + A[0, 1] * A[1, 0] assert p == expected p = perm(A, method="bbfg") assert p == expected
def test_1x1(self, random_matrix): """Check 1x1 permanent""" A = np.array([[random_matrix(1)]]) p = perm(A, method="ryser") expected = A[0, 0] assert p == expected p = perm(A, method="bbfg") assert p == expected
def test_0x0(self): """Check 0x0 permanent returns 1""" A = np.zeros((0, 0)) p = perm(A, method="ryser") expected = 1 assert p == expected p = perm(A, method="bbfg") assert p == expected
def test_3x3(self, random_matrix): """Check 3x3 permanent""" A = random_matrix(3) p = perm(A, method="ryser") expected = (A[0, 2] * A[1, 1] * A[2, 0] + A[0, 1] * A[1, 2] * A[2, 0] + A[0, 2] * A[1, 0] * A[2, 1] + A[0, 0] * A[1, 2] * A[2, 1] + A[0, 1] * A[1, 0] * A[2, 2] + A[0, 0] * A[1, 1] * A[2, 2]) assert p == expected p = perm(A, method="bbfg") assert p == expected
def test_complex_no_imag(self, random_matrix): """Check perm(A) == perm_real(A) and perm(A) == perm_BBFG_real(A) for a complex random matrix with zero imaginary parts.""" A = np.complex128(random_matrix(6)) p = perm(A, method="ryser") expected = perm_real(A.real) assert np.allclose(p, expected) A = np.complex128(random_matrix(6)) p = perm(A, method="ryser") expected = perm_BBFG_real(A.real) assert np.allclose(p, expected)
def test_complex(self, random_matrix): """Check perm(A) == perm_complex(A) and perm(A) == perm_BBFG_complex(A) for a complex.""" A = random_matrix(6) p = perm(A, method="ryser") expected = perm_complex(A) assert np.allclose(p, expected) A = random_matrix(6) p = perm(A, method="ryser") expected = perm_BBFG_complex(A) assert np.allclose(p, expected)
def test_real(self, random_matrix): """Check permanent(A)=perm_real(A) for a random real matrix. """ A = random_matrix(6) p = perm(A) expected = perm_real(A) assert p == expected A = random_matrix(6) A = np.array(A, dtype=np.complex128) p = perm(A) expected = perm_real(np.float64(A.real)) assert p == expected
def test_complex(self, random_matrix): """Check perm(A)=perm_complex(A) for a random matrix. """ A = random_matrix(6) p = perm(A) expected = perm_complex(A) assert np.allclose(p, expected)
def test_real(self, random_matrix): """Check perm(A) == perm_real(A) and perm(A, method="bbfg") == perm_BBFG_real(A) for a random real matrix.""" A = random_matrix(6) p = perm(A, method="ryser") expected = perm_real(A) assert np.allclose(p, expected) A = random_matrix(6) A = np.array(A, dtype=np.complex128) p = perm(A, method="ryser") expected = perm_real(np.float64(A.real)) assert np.allclose(p, expected) A = random_matrix(6) p = perm(A, method="bbfg") expected = perm_BBFG_real(A) assert np.allclose(p, expected) A = random_matrix(6) A = np.array(A, dtype=np.complex128) p = perm(A, method="bbfg") expected = perm_BBFG_real(np.float64(A.real)) assert np.allclose(p, expected)
def get_amp(out_modes): sub_matr = U[np.ix_(out_modes, in_modes)] return perm(sub_matr)
def test_2x2(self, random_matrix): """Check 2x2 permanent""" A = random_matrix(2) p = perm(A) assert p == A[0, 0] * A[1, 1] + A[0, 1] * A[1, 0]
def test_nan(self): """Check exception for non-finite matrix""" A = np.array([[2, 1], [1, np.nan]]) with pytest.raises(ValueError): perm(A)
def test_square_exception(self): """Check exception for non-square argument""" A = np.zeros([2, 3]) with pytest.raises(ValueError): perm(A)
def test_array_exception(self): """Check exception for non-matrix argument""" with pytest.raises(TypeError): perm(1)
from itertools import chain, product import pytest import numpy as np from scipy.special import factorial as fac from scipy.linalg import sqrtm from scipy.stats import unitary_group from thewalrus import perm, permanent_repeated, brs, ubrs from thewalrus._permanent import fock_prob, fock_threshold_prob perm_real = perm perm_complex = perm perm_BBFG_real = lambda x: perm(x, method="bbfg") perm_BBFG_complex = lambda x: perm(x, method="bbfg") class TestPermanentWrapper: """Tests for the Permanent function""" def test_array_exception(self): """Check exception for non-matrix argument""" with pytest.raises(TypeError): perm(1) def test_square_exception(self): """Check exception for non-square argument""" A = np.zeros([2, 3]) with pytest.raises(ValueError): perm(A)