def test_rand_mix(self, mode): rho = rand_mix(3, mode=mode) assert rho.shape == (3, 3) assert type(rho) == np.matrix assert_almost_equal(tr(rho), 1.0) mixedness = tr(rho @ rho) assert mixedness < 1.0
def test_rand_mix(self, mode): rho = qu.rand_mix(3, mode=mode) assert rho.shape == (3, 3) assert type(rho) == qu.qarray assert_allclose(qu.tr(rho), 1.0) mixedness = qu.tr(rho @ rho) assert mixedness < 1.0
def test_normalized(self): x = [3j, 4j] p = qu.qu(x, 'k', normalized=False) assert_almost_equal(qu.tr(p.H @ p), 25.) p = qu.qu(x, 'k', normalized=True) assert_almost_equal(qu.tr(p.H @ p), 1.) p = qu.qu(x, 'dop', normalized=True) assert_almost_equal(qu.tr(p), 1.)
def test_mixed(self): for vec, op, val in zip(((.5, 0, 0), (0, .5, 0), (0, 0, .5), (-.5, 0, 0), (0, -.5, 0), (0, 0, -.5)), ("x", "y", "z", "x", "y", "z"), (.5, .5, .5, -.5, -.5, -.5)): x = tr(bloch_state(*vec) @ pauli(op)) assert_allclose(x, val)
def test_pure(self): for vec, op, val in zip(((1, 0, 0), (0, 1, 0), (0, 0, 1), (-1, 0, 0), (0, -1, 0), (0, 0, -1)), ("x", "y", "z", "x", "y", "z"), (1, 1, 1, -1, -1, -1)): x = tr(bloch_state(*vec) @ pauli(op)) assert_allclose(x, val)
def test_purify(self): for vec, op, val in zip(((.5, 0, 0), (0, .5, 0), (0, 0, .5), (-.5, 0, 0), (0, -.5, 0), (0, 0, -.5)), ("x", "y", "z", "x", "y", "z"), (1, 1, 1, -1, -1, -1)): x = tr(bloch_state(*vec, purified=True) @ pauli(op)) assert_allclose(x, val)
def test_sparse_convert_to_dop(self): x = [1, 0, 9e-16, 0, 3j] p = qu.qu(x, 'ket', sparse=True) q = qu.qu(p, 'dop', sparse=True) assert (q.shape == (5, 5)) assert (q.nnz == 9) assert_almost_equal(q[4, 4], 9.) q = qu.qu(p, 'dop', sparse=True, normalized=True) assert_almost_equal(qu.tr(q), 1.)
def test_mixed(self): rho = qu.dop(qu.bell_state('psi-')) IZ = qu.pauli('I') & qu.pauli('Z') ZI = qu.pauli('Z') & qu.pauli('I') res, rho_after = qu.measure(rho, IZ) # normalized assert qu.tr(rho_after) == pytest.approx(1.0) # anticorrelated assert qu.expectation(rho_after, IZ) == pytest.approx(res) assert qu.expectation(rho_after, ZI) == pytest.approx(-res) assert isinstance(rho_after, qu.qarray)
def test_entanglement(self): rho = rand_seperable([2, 3, 2], 10) assert_almost_equal(tr(rho), 1.0) assert isherm(rho) assert logneg(rho, [2, 6]) < 1e-12 assert logneg(rho, [6, 2]) < 1e-12 rho_a = ptr(rho, [2, 3, 2], 1) el = eigvals(rho_a) assert np.all(el < 1 - 1e-12) assert np.all(el > 1e-12)
def test_entanglement(self): rho = qu.rand_seperable([2, 3, 2], 10) assert_allclose(qu.tr(rho), 1.0) assert qu.isherm(rho) assert qu.logneg(rho, [2, 6]) < 1e-12 assert qu.logneg(rho, [6, 2]) < 1e-12 rho_a = qu.ptr(rho, [2, 3, 2], 1) el = qu.eigvalsh(rho_a) assert np.all(el < 1 - 1e-12) assert np.all(el > 1e-12)
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_rand_ket(self): ket = qu.rand_ket(3) assert ket.shape == (3, 1) assert type(ket) == np.matrix assert_allclose(qu.tr(ket.H @ ket), 1.0)
def test_rand_haar_state(self): ket = rand_haar_state(3) assert ket.shape == (3, 1) assert type(ket) == np.matrix assert_almost_equal(tr(ket.H @ ket), 1.0)
def test_up(self): p = up(qtype='dop') assert_allclose(tr(p @ pauli('z')), 1.0)
def test_rand_haar_state(self): ket = qu.rand_haar_state(3) assert ket.shape == (3, 1) assert type(ket) == qu.qarray assert_allclose(qu.tr(ket.H @ ket), 1.0)
def test_gen_rand_haar_states(self): kets = [*qu.gen_rand_haar_states(3, 6)] for ket in kets: assert ket.shape == (3, 1) assert type(ket) == qu.qarray assert_allclose(qu.tr(ket.H @ ket), 1.0)
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_thermal_state_cold(self): full = ham_j1j2(4, j2=0.1253) rhoth = thermal_state(full, 100) gs = groundstate(full) assert_allclose(tr(gs.H @ rhoth @ gs), 1.0, rtol=1e-4)
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_down(self): p = down(qtype='dop') assert_allclose(tr(p @ pauli('z')), -1.0)
def test_gen_rand_haar_states(self): kets = [*gen_rand_haar_states(3, 6)] for ket in kets: assert ket.shape == (3, 1) assert type(ket) == np.matrix assert_almost_equal(tr(ket.H @ ket), 1.0)
def test_plus(self): p = plus(qtype='dop') assert_allclose(tr(p @ pauli('x')), 1.0)
def test_yminus(self): p = yminus(qtype='dop') assert_allclose(tr(p @ pauli('y')), -1.0)
def test_thermal_state_normalization(self): full = rand_herm(2**4) for beta in (0, 0.5, 1, 10): rhoth = thermal_state(full, beta) assert_allclose(tr(rhoth), 1)
def test_rand_rho(self, dtype): rho = qu.rand_rho(3, dtype=dtype) assert rho.shape == (3, 3) assert type(rho) == qu.qarray assert rho.dtype == dtype assert_allclose(qu.tr(rho), 1.0)
def test_rand_rho_sparse(self): rho = rand_rho(3, sparse=True, density=0.3) assert rho.shape == (3, 3) assert type(rho) == sp.csr_matrix assert_almost_equal(tr(rho), 1.0)
def test_rand_rho_sparse(self, dtype): rho = qu.rand_rho(3, sparse=True, density=0.3, dtype=dtype) assert rho.shape == (3, 3) assert type(rho) == sp.csr_matrix assert rho.dtype == dtype assert_allclose(qu.tr(rho), 1.0)
def test_partial_trace_sparse_basic(self): a = rand_rho(4) b = partial_trace(a, [2, 2], 0) assert type(b) == np.matrix assert isherm(b) assert_allclose(tr(b), 1.0)
def test_rand_rho(self): rho = rand_rho(3) assert rho.shape == (3, 3) assert type(rho) == np.matrix assert_almost_equal(tr(rho), 1.0)