def testExpandGate2toNSwap(self): """ gates: expand 2 to N (using swap) """ a, b = np.random.rand(), np.random.rand() k1 = (a * basis(2, 0) + b * basis(2, 1)).unit() c, d = np.random.rand(), np.random.rand() k2 = (c * basis(2, 0) + b * basis(2, 1)).unit() N = 6 kets = [rand_ket(2) for k in range(N)] for m in range(N): for n in set(range(N)) - {m}: psi_in = tensor([ k1 if k == m else k2 if k == n else kets[k] for k in range(N) ]) psi_out = tensor([ k2 if k == m else k1 if k == n else kets[k] for k in range(N) ]) G = swap(N, m, n) psi_res = G * psi_in assert_((psi_out - G * psi_in).norm() < 1e-12)
def testSwapGate(self): """ gates: swap gate """ a, b = np.random.rand(), np.random.rand() psi1 = (a * basis(2, 0) + b * basis(2, 1)).unit() c, d = np.random.rand(), np.random.rand() psi2 = (c * basis(2, 0) + b * basis(2, 1)).unit() psi_in = tensor(psi1, psi2) psi_out = tensor(psi2, psi1) psi_res = swap() * psi_in assert_((psi_out - psi_res).norm() < 1e-12) psi_res = swap() * swap() * psi_in assert_((psi_in - psi_res).norm() < 1e-12)