def test_absolute(): t = bk.astensor([-2.25 + 4.75j, -3.25 + 5.75j]) t = bk.absolute(t) assert np.allclose(bk.evaluate(t), [5.25594902, 6.60492229]) t = bk.astensor([-2.25 + 4.75j]) t = bk.absolute(t) print(bk.evaluate(t)) assert np.allclose(bk.evaluate(t), [5.25594902])
def test_outer(): s0 = np.random.normal(size=[2, 2]) + 1.0j * np.random.normal(size=[2, 2]) s1 = np.random.normal(size=[2, 2, 2]) \ + 1.0j * np.random.normal(size=[2, 2, 2]) res = bk.astensorproduct(bk.outer(bk.astensor(s0), bk.astensor(s1))) assert bk.rank(res) == 5 res2 = np.outer(s0, s1).reshape([2]*5) assert np.allclose(bk.evaluate(res), res2)
def test_inner(): v0 = np.random.normal(size=[2, 2, 2, 2]) \ + 1.0j * np.random.normal(size=[2, 2, 2, 2]) v1 = np.random.normal(size=[2, 2, 2, 2]) \ + 1.0j * np.random.normal(size=[2, 2, 2, 2]) res = np.vdot(v0, v1) bkres = bk.evaluate(bk.inner(bk.astensor(v0), bk.astensor(v1))) print(bkres) assert np.abs(res-bkres) == ALMOST_ZERO
def test_size(): """size(tensor) should return the number of elements""" t = bk.astensor([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) assert bk.size(t) == 16
def test_trace(): tensor = bk.astensor(np.asarray([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 2.7, 1], [0, 0, 1, 0.3j]])) tensor = bk.reshape(tensor, (4, 4)) # FIXME astensor should not reshape tr = bk.evaluate(bk.trace(tensor)) print(tr) assert tr - (2.7+0.3j) == ALMOST_ZERO
def test_expectation(): ket = qf.zero_state(4) M = np.zeros(shape=([2] * 4)) M[0, 0, 0, 0] = 42 M[1, 0, 0, 0] = 1 M[0, 1, 0, 0] = 2 M[0, 0, 1, 0] = 3 M[0, 0, 0, 1] = 4 M = bk.astensor(M) avg = ket.expectation(M) assert qf.asarray(avg) == 42 ket = qf.w_state(4) assert qf.asarray(ket.expectation(M)) == 2.5
def test_real_imag(): tensor = bk.astensor([1.0 + 2.0j, 0.5 - 0.2j]) t = bk.real(tensor) t = bk.evaluate(t) assert np.allclose(bk.evaluate(bk.real(tensor)), [1.0, 0.5]) assert np.allclose(bk.evaluate(bk.imag(tensor)), [2.0, -0.2])