def test_dv_dtype(self): D = ch.QuantumState(ch._vec(np.eye(2, dtype=np.complex128) * .5), 'dv') self.assertEqual(type(D._dv.density_vector), type(np.array([]))) #Changed Type self.assertEqual(D._dv.density_vector.dtype, np.complex128) D = ch.QuantumState(ch._vec(np.eye(2) * .5), 'dv') self.assertEqual(type(D._dv.density_vector), type(np.array([]))) #Changed Type self.assertEqual(D._dv.density_vector.dtype, np.complex128) self.assertEqual(type(D.density_vector()), type(np.array([]))) #Changed Type self.assertEqual(D.density_vector().dtype, np.complex128) self.assertEqual(type(D.density_matrix()), type(np.array([]))) #Changed Type self.assertEqual(D.density_matrix().dtype, np.complex128) self.assertEqual(type(D.bloch_vector()), type(np.array([]))) #Changed Type self.assertEqual(D.bloch_vector().dtype, np.float64) w, b = D.state_mixture() self.assertEqual(type(w), np.ndarray) self.assertEqual(w.dtype, np.float64) self.assertTrue(all([type(bb) == type(np.array([])) for bb in b])) #Changed Type self.assertTrue(all([bb.dtype == np.complex128 for bb in b]))
def test_kraus_to_all(self): paulis = [ch._sigmaI, ch._sigmaX, ch._sigmaY, ch._sigmaZ] weights = np.random.randn(4) + 1j * np.random.randn(4) weights = weights / la.norm(weights) kraus = [w * p for w, p in zip(weights, paulis)] D = ch.QuantumChannel(kraus, 'kraus') kraus2 = D.kraus() errs = [la.norm(k1 - k2) for k1, k2 in zip(kraus, kraus2)] self.assertEqual(np.max(errs), 0) S = np.zeros((8, 2), dtype=np.complex128) for i in range(4): S[i * 2:(i + 1) * 2, :] = kraus[i] D = ch.QuantumChannel(kraus, 'kraus') S2 = D.stiefel() self.assertEqual(la.norm(S - S2), 0) S = np.zeros((8, 2), dtype=np.complex128) for i in range(4): S[i::4, :] = kraus[i] D = ch.QuantumChannel(kraus, 'kraus') S2 = D.stiefel2() self.assertEqual(la.norm(S - S2), 0) D = ch.QuantumChannel(kraus, 'kraus') L = np.sum([ w.conj() * w * np.kron(p.conj(), p) for w, p in zip(weights, paulis) ], 0) L2 = D.liouvillian() self.assertAlmostEqual(la.norm(L - L2), 0, 15) ptms = [np.eye(4) for _ in paulis] for i in range(1, 4): for j in range(1, 4): if i != j: ptms[i][j, j] = -1 PTM = np.sum([w.conj() * w * p for w, p in zip(weights, ptms)], 0) D = ch.QuantumChannel(kraus, 'kraus') PTM2 = D.ptm() self.assertAlmostEqual(la.norm(PTM - PTM2), 0) choi = np.sum([ w.conj() * w * ch._vec(p) * ch._vec(p).conj().T for w, p in zip(weights, paulis) ], 0) D = ch.QuantumChannel(kraus, 'kraus') choi2 = D.choi() self.assertAlmostEqual(la.norm(choi - choi2), 0) chi = np.diag([w.conj() * w for w in weights]) D = ch.QuantumChannel(kraus, 'kraus') chi2 = D.chi() self.assertAlmostEqual(la.norm(chi - chi2), 0, 15)
def test_ptm_to_all(self): paulis = [ch._sigmaI, ch._sigmaX, ch._sigmaY, ch._sigmaZ] weights = np.random.randn(4) + 1j * np.random.randn(4) weights = weights / la.norm(weights) ptms = [np.eye(4) for _ in paulis] for i in range(1, 4): for j in range(1, 4): if i != j: ptms[i][j, j] = -1 PTM = np.sum([w.conj() * w * p for w, p in zip(weights, ptms)], 0) D = ch.QuantumChannel(PTM, 'ptm') PTM2 = D.ptm() self.assertEqual(la.norm(PTM - PTM2), 0) kraus = [w * p for w, p in zip(weights, paulis)] D = ch.QuantumChannel(PTM, 'ptm') kraus2 = D.kraus() #errs = [la.norm(k1-k2) for k1,k2 in zip(kraus,kraus2)] errs = [ np.min([ la.norm(np.kron(k1.conj(), k1) - np.kron(k2.conj(), k2)) for k2 in kraus2 ]) for k1 in kraus ] self.assertAlmostEqual(la.norm(errs), 0, 12) #Skip stiefel since it is tedious due to phase ambiguity #S = np.zeros((8,2),dtype=np.complex128) #for i in range(4): # S[i*2:(i+1)*2,:] = kraus[i] D = ch.QuantumChannel(PTM, 'ptm') S2 = D.stiefel() #errs = [np.min([la.norm(np.kron(k1.conj(),k1)-np.kron(k2.conj(),k2)) for k2 in kraus2]) for k1 in kraus] #self.AssertEqual(la.norm(S-S2),0) D = ch.QuantumChannel(PTM, 'ptm') L = np.sum([ w.conj() * w * np.kron(p.conj(), p) for w, p in zip(weights, paulis) ], 0) L2 = D.liouvillian() self.assertAlmostEqual(la.norm(L - L2), 0, 12) choi = np.sum([ w.conj() * w * ch._vec(p) * ch._vec(p).conj().T for w, p in zip(weights, paulis) ], 0) D = ch.QuantumChannel(PTM, 'ptm') choi2 = D.choi() self.assertAlmostEqual(la.norm(choi - choi2), 0, 12) chi = np.diag([w.conj() * w for w in weights]) D = ch.QuantumChannel(PTM, 'ptm') chi2 = D.chi() self.assertAlmostEqual(la.norm(chi - chi2), 0, 12)
def test_liou_to_choi(self): paulis = [ch._sigmaI, ch._sigmaX, ch._sigmaY, ch._sigmaZ] for i, gate in enumerate(paulis): L = np.kron(gate.conj(), gate) D = ch.QuantumChannel(L, 'liou') choi = D.choi() choi_true = ch._vec(gate) * ch._vec(gate).conj().T self.assertEqual(la.norm(choi - choi_true), 0)
def test_dv_to_dv(self): G = np.random.randn(2, 2) + 1j * np.random.randn(2, 2) Q, R = la.qr(G) d = np.random.rand(2) d = d / np.sum(d) Q = np.array(Q) dm = np.dot(Q, np.dot(np.diag(d), Q.conj().T)) dv = ch._vec(dm) p = ch.QuantumState(dv, 'dv') err1 = la.norm(dv - p.density_vector()) self.assertEqual(err1, 0) dm = p.density_matrix() p2 = ch.QuantumState(dm, 'dm') err2 = la.norm(dv - p2.density_vector()) self.assertEqual(err2, 0) bv = p.bloch_vector() self.assertLessEqual(la.norm(bv), 1.) p3 = ch.QuantumState(bv, 'bv') err3 = la.norm(dv - p3.density_vector()) self.assertAlmostEqual(err3, 0, 12) w, b = p.state_mixture() p4 = ch.QuantumState((w, b), 'sm') err4 = la.norm(dv - p4.density_vector()) self.assertAlmostEqual(err4, 0, 12)