def test_rps(self): a, b, c = (qu.rand_rho(2, sparse=True, density=0.5), qu.rand_rho(3, sparse=True, density=0.5), qu.rand_rho(2, sparse=True, density=0.5)) abc = a & b & c pc = qu.core._trace_keep(abc, [2, 3, 2], 2) assert_allclose(pc, c.A) pb = qu.core._trace_keep(abc, [2, 3, 2], 1) assert_allclose(pb, b.A) pa = qu.core._trace_keep(abc, [2, 3, 2], 0) assert_allclose(pa, a.A)
def test_rps(self): a, b, c = (qu.rand_rho(2, sparse=True, density=0.5), qu.rand_rho(3, sparse=True, density=0.5), qu.rand_rho(2, sparse=True, density=0.5)) abc = a & b & c pab = qu.core._trace_lose(abc, [2, 3, 2], 2) assert_allclose(pab, (a & b).A) pac = qu.core._trace_lose(abc, [2, 3, 2], 1) assert_allclose(pac, (a & c).A) pbc = qu.core._trace_lose(abc, [2, 3, 2], 0) assert_allclose(pbc, (b & c).A)
def test_dop_reverse_sparse(self): a = rand_rho(4, sparse=True, density=0.5) b = perm_eyepad(a, np.array([2, 2, 2]), [2, 0]) c = (a & eye(2)).A.reshape([2, 2, 2, 2, 2, 2]) \ .transpose([1, 2, 0, 4, 5, 3]) \ .reshape([8, 8]) assert_allclose(b.A, c)
def test_dop_spread(self): a = qu.rand_rho(4) b = qu.pkron(a, [2, 2, 2], [0, 2]) c = ((a & qu.eye(2)).A.reshape([2, 2, 2, 2, 2, 2]).transpose([0, 2, 1, 3, 5, 4]).reshape([8, 8])) assert_allclose(b, c)
def test_dop_spread(self): a = rand_rho(4) b = perm_eyepad(a, [2, 2, 2], [0, 2]) c = (a & eye(2)).A.reshape([2, 2, 2, 2, 2, 2]) \ .transpose([0, 2, 1, 3, 5, 4]) \ .reshape([8, 8]) assert_allclose(b, c)
def test_basic(self, rand_rank): rho = qu.rand_rho(9) ln = qu.logneg(rho, [3, 3]) for p in (0.2, 0.5, 0.8, 1.0): rho_d = qu.dephase(rho, p, rand_rank=rand_rank) assert qu.logneg(rho_d, [3, 3]) <= ln assert rho_d.tr() == pytest.approx(1.0)
def test_dop_reverse(self): a = rand_rho(4) b = perm_eyepad(a, np.array([2, 2, 2]), [2, 0]) c = (a & eye(2)).A.reshape([2, 2, 2, 2, 2, 2]) \ .transpose([1, 2, 0, 4, 5, 3]) \ .reshape([8, 8]) assert_allclose(b, c)
def test_partial_trace_dop_product_state(self): dims = [3, 2, 4, 2, 3] ps = [qu.rand_rho(dim) for dim in dims] pt = qu.kron(*ps) for i, dim in enumerate(dims): p = qu.partial_trace(pt, dims, i) assert_allclose(p, ps[i])
def test_pauli_reconstruct(self): p1 = qu.rand_rho(4) names_cffs = qu.pauli_decomp(p1, mode='c') pr = sum( qu.kron(*(qu.pauli(s) for s in name)) * names_cffs["".join(name)] for name in itertools.product('IXYZ', repeat=2)) assert_allclose(pr, p1)
def test_dop_reverse(self): a = qu.rand_rho(4) b = qu.pkron(a, np.array([2, 2, 2]), [2, 0]) c = ((a & qu.eye(2)).A.reshape([2, 2, 2, 2, 2, 2]).transpose([1, 2, 0, 4, 5, 3]).reshape([8, 8])) assert_allclose(b, c)
def test_dop_reverse_sparse(self): a = qu.rand_rho(4, sparse=True, density=0.5) b = qu.pkron(a, np.array([2, 2, 2]), [2, 0]) c = ((a & qu.eye(2)).A.reshape([2, 2, 2, 2, 2, 2]).transpose([1, 2, 0, 4, 5, 3]).reshape([8, 8])) assert_allclose(b.A, c)
def test_ptr_compare_to_manual(self): a = qu.rand_rho(2**2) b = qu.partial_trace(a, [2, 2], 0) c = a.A.reshape([2, 2, 2, 2]).trace(axis1=1, axis2=3) assert_allclose(b, c) b = qu.partial_trace(a, [2, 2], 1) c = a.A.reshape([2, 2, 2, 2]).trace(axis1=0, axis2=2) assert_allclose(b, c)
def test_shapes_and_blocks(self, sz_blc, sz_p, calc_self_ent): if sz_p // sz_blc > 0: p = qu.rand_rho(2**sz_p) n = sz_p // sz_blc ecm = qu.ent_cross_matrix(p, sz_blc, calc_self_ent=calc_self_ent) assert ecm.shape[0] == n if not calc_self_ent: assert_allclose(np.diag(ecm), [np.nan] * n, equal_nan=True)
def test_vs_ptr(self): a = rand_rho(6, sparse=True, density=0.5) b = _trace_lose(a, [2, 3], 1) c = partial_trace(a.A, [2, 3], 0) assert_allclose(b, c) b = _trace_lose(a, [2, 3], 0) c = partial_trace(a.A, [2, 3], 1) assert_allclose(b, c)
def test_vs_ptr(self): a = qu.rand_rho(6, sparse=True, density=0.5) b = qu.core._trace_keep(a, [2, 3], 0) c = qu.partial_trace(a.A, [2, 3], 0) assert_allclose(b, c) b = qu.core._trace_keep(a, [2, 3], 1) c = qu.partial_trace(a.A, [2, 3], 1) assert_allclose(b, c)
def test_partial_trace_simple_single(self): a = qu.rand_rho(12, sparse=True, density=0.5) dims = [2, 3, 2] b = qu.partial_trace(a, dims, 1) c = a.A.reshape([*dims, *dims]) \ .trace(axis1=2, axis2=5) \ .trace(axis1=0, axis2=2) assert_allclose(c, b)
def test_partial_trace_simple_double(self): a = qu.rand_rho(12, sparse=True, density=0.5) dims = [2, 3, 2] b = qu.partial_trace(a, dims, [0, 2]) c = qu.partial_trace(a.A, dims, [0, 2]) assert_allclose(b, c) b = qu.partial_trace(a, dims, [1, 2]) c = qu.partial_trace(a.A, dims, [1, 2]) assert_allclose(b, c)
def srho_dot_ls(): rho = rand_rho(3) ham = rand_herm(3, sparse=True, density=0.5) gamma = 0.7 ls = [rand_matrix(3, sparse=True, density=0.5) for _ in range(3)] rhodl = -1.0j * (ham @ rho - rho @ ham) for l in ls: rhodl += gamma * (l @ rho @ l.H) rhodl -= gamma * 0.5 * (rho @ l.H @ l) rhodl -= gamma * 0.5 * (l.H @ l @ rho) return rho, ham, gamma, ls, rhodl
def rho_dot_ls(): np.random.seed(1) rho = rand_rho(3) ham = rand_herm(3) gamma = 0.7 ls = [rand_matrix(3) for _ in range(3)] rhodl = -1.0j * (ham @ rho - rho @ ham) for l in ls: rhodl += gamma * (l @ rho @ l.H) rhodl -= gamma * 0.5 * (rho @ l.H @ l) rhodl -= gamma * 0.5 * (l.H @ l @ rho) return rho, ham, gamma, ls, rhodl
def test_types(self, dims, op_sps, p_sps, pre_c): p = qu.rand_rho(4, sparse=p_sps) c = qu.correlation(p, qu.pauli('x', sparse=op_sps), qu.pauli('z', sparse=op_sps), 0, 1, dims=dims, precomp_func=pre_c) c = c(p) if pre_c else c assert c >= -1.0 assert c <= 1.0
def test_depolarize(self, stack): rho = qu.rand_rho(2) I, X, Y, Z = (qu.pauli(s) for s in 'IXYZ') es = [qu.expec(rho, A) for A in (X, Y, Z)] p = 0.1 Ek = [(1 - p)**0.5 * I, (p / 3)**0.5 * X, (p / 3)**0.5 * Y, (p / 3)**0.5 * Z] if stack: Ek = np.stack(Ek, axis=0) sigma = qu.kraus_op(rho, Ek, check=True) es2 = [qu.expec(sigma, A) for A in (X, Y, Z)] assert qu.tr(sigma) == pytest.approx(1.0) assert all(abs(e2) < abs(e) for e, e2 in zip(es, es2)) sig_exp = sum(E @ rho @ qu.dag(E) for E in Ek) assert_allclose(sig_exp, sigma)
def test_subsystem(self): rho = qu.rand_rho(6) dims = [3, 2] I, X, Y, Z = (qu.pauli(s) for s in 'IXYZ') mi_i = qu.mutual_information(rho, dims) p = 0.1 Ek = [(1 - p)**0.5 * I, (p / 3)**0.5 * X, (p / 3)**0.5 * Y, (p / 3)**0.5 * Z] with pytest.raises(ValueError): qu.kraus_op(rho, qu.randn((3, 2, 2)), check=True, dims=dims, where=1) sigma = qu.kraus_op(rho, Ek, check=True, dims=dims, where=1) mi_f = qu.mutual_information(sigma, dims) assert mi_f < mi_i assert qu.tr(sigma) == pytest.approx(1.0) sig_exp = sum( (qu.eye(3) & E) @ rho @ qu.dag(qu.eye(3) & E) for E in Ek) assert_allclose(sig_exp, sigma)
def test_subsystem(self): p = qu.rand_rho(2**4) e = qu.concurrence(p, [2, 2, 2, 2], 1, 2) assert 0 <= e <= 1
def test_partial_trace_sparse_basic(self): a = qu.rand_rho(4) b = qu.partial_trace(a, [2, 2], 0) assert type(b) == qu.qarray assert qu.isherm(b) assert_allclose(qu.tr(b), 1.0)
def test_auto_trace_out(self): p = qu.rand_rho(2**3) qd = qu.quantum_discord(p, [2, 2, 2], 0, 2) assert (0 <= qd and qd <= 1)
def test_partial_trace_order_doesnt_matter(self): a = qu.rand_rho(2**3) dims = np.array([2, 2, 2]) b1 = qu.partial_trace(a, dims, [0, 2]) b2 = qu.partial_trace(a, dims, [2, 0]) assert_allclose(b1, b2)
def test_partial_trace_supply_ndarray(self): a = qu.rand_rho(2**3) dims = np.array([2, 2, 2]) keep = np.array(1) b = qu.partial_trace(a, dims, keep) assert (b.shape[0] == 2)
def p1(): return qu.rand_rho(3)
def test_partial_trace_basic(self): a = qu.rand_rho(2**2) b = qu.partial_trace(a, [2, 2], 0) assert isinstance(b, qu.qarray) assert qu.isherm(b) assert_allclose(qu.tr(b), 1.0)
def test_permute_sparse_op(self): dims = [3, 2, 5, 4] a = qu.rand_rho(qu.prod(dims), sparse=True, density=0.5) b = qu.permute(a, dims, [3, 1, 2, 0]) c = qu.permute(a.A, dims, [3, 1, 2, 0]) assert_allclose(b.A, c)