Example #1
0
    def mean_norm(self, ntype='fro'):
        """Computes the average frobenius norm of local terms. Also generates
        all terms if not already cached.
        """
        if self.n is None:
            return qu.norm(self(), ntype)

        nterms = self.n - int(not self.cyclic)
        return sum(qu.norm(self((i, i + 1)), ntype)
                   for i in range(nterms)) / nterms
Example #2
0
    def test_eigh(self):
        H = qu.ham_mbl(6, dh=2.5)
        a_el, a_ev = qu.eigh(H, autoblock=False)
        el, ev = qu.eigh(H, autoblock=True)

        assert qu.norm(ev @ qu.ldmul(el, ev.H) - H, 'fro') < 1e-12
        assert_allclose(a_el, el)
        assert_allclose(ev.H @ ev, np.eye(H.shape[0]), atol=1e-12)
Example #3
0
    def test_1(self):
        a = rand_herm(100, sparse=True)
        el, ev = eigh(a.A)
        which = abs(el) < 0.2
        el, ev = el[which], ev[:, which]

        offset = norm(a, 'fro')
        a = a + offset * sp.eye(a.shape[0])

        sl, sv = eigs_slepc(a, k=6, l_win=(-0.2 + offset, 0.2 + offset))
        sl -= offset
        assert_allclose(el, sl)
        assert_allclose(np.abs(sv.H @ ev), np.eye(el.size), atol=1e-11)
Example #4
0
    def test_rsvd(self, dtype, shape, sparse, q, p):
        X = rand_rect(*shape, dtype=dtype, sparse=sparse)

        k = 15
        U, s, V = qu.rsvd(X, k, q=q, p=p)

        assert U.shape == (shape[0], k)
        assert s.shape == (k, )
        assert V.shape == (k, shape[1])

        assert U.dtype == dtype
        assert V.dtype == dtype

        assert_allclose(U.conj().T @ U, np.eye(k), rtol=1e-5, atol=1e-5)
        assert_allclose(V @ V.conj().T, np.eye(k), rtol=1e-5, atol=1e-5)

        Ue, se, Ve = qu.svds(X, k)
        opt_err = qu.norm(X.A - usv2dense(Ue, se, Ve), 'fro')
        act_err = qu.norm(X.A - usv2dense(U, s, V), 'fro')

        assert act_err < 1.2 * opt_err

        assert_allclose(s[:k // 2], se[:k // 2], rtol=0.05)
Example #5
0
    def test_rsvd_adaptive(self, dtype, shape, q, p):
        X = rand_rank(*shape, 10, dtype=dtype)
        U, s, V = qu.rsvd(X, 1e-5, q=q, p=p, k_start=10)

        k = s.size
        assert 10 <= k <= 20

        assert U.dtype == dtype
        assert V.dtype == dtype

        assert_allclose(U.conj().T @ U, np.eye(k), rtol=1e-6, atol=1e-6)
        assert_allclose(V @ V.conj().T, np.eye(k), rtol=1e-6, atol=1e-6)

        Ue, se, Ve = qu.svds(X, k)
        act_err = qu.norm(X - usv2dense(U, s, V), 'fro')

        assert act_err < 1e-4

        assert_allclose(s[:k // 2], se[:k // 2], rtol=0.1)
Example #6
0
 def test_norm_trace_dense(self):
     a = qu.qarray(np.diag([-3, 1, 7]))
     assert qu.norm(a, "trace") == 11
     a = qu.rand_product_state(1, qtype="dop")
     assert_allclose(qu.norm(a, "nuc"), 1)
Example #7
0
 def test_norm_spectral_sparse(self, mat_nherm_sparse, backend):
     _, _, a = mat_nherm_sparse
     assert_allclose(qu.norm(a, "spectral", backend=backend), 4.)
Example #8
0
 def test_norm_fro_sparse(self):
     a = qu.sparse([[3, 0], [4j, 0]])
     assert qu.norm(a, "fro") == (9 + 16)**0.5
Example #9
0
 def test_norm_fro_dense(self):
     a = qu.quimbify([[1, 2], [3j, 4j]])
     assert qu.norm(a, "fro") == (1 + 4 + 9 + 16)**0.5
Example #10
0
 def test_norm_trace_dense(self):
     a = np.asmatrix(np.diag([-3, 1, 7]))
     assert norm(a, "trace") == 11
     a = rand_product_state(1, qtype="dop")
     assert_allclose(norm(a, "nuc"), 1)
Example #11
0
 def test_norm_fro_sparse(self):
     a = qu([[3, 0], [4j, 0]], sparse=True)
     assert norm(a, "fro") == (9 + 16)**0.5