def random_gates(min_length, max_length, num_experiments): """Just create random sequences, find the best.""" base = [to_su2(ops.Hadamard()), to_su2(ops.Tgate())] U = (ops.RotationX(2.0 * np.pi * random.random()) @ ops.RotationY(2.0 * np.pi * random.random()) @ ops.RotationZ(2.0 * np.pi * random.random())) min_dist = 1000 for _ in range(num_experiments): seq_length = min_length + random.randint(0, max_length) U_approx = ops.Identity() for _ in range(seq_length): g = random.randint(0, 1) U_approx = U_approx @ base[g] dist = trace_dist(U, U_approx) min_dist = min(dist, min_dist) phi1 = U(state.zero) phi2 = U_approx(state.zero) print('Trace Dist: {:.4f} State: {:6.4f}%'. format(min_dist, 100.0 * (1.0 - np.real(np.dot(phi1, phi2.conj())))))
def main(argv): if len(argv) > 1: raise app.UsageError('Too many command-line arguments.') num_experiments = 10 depth = 8 recursion = 4 print('SK algorithm - depth: {}, recursion: {}, experiments: {}'. format(depth, recursion, num_experiments)) base = [to_su2(ops.Hadamard()), to_su2(ops.Tgate())] gates = create_unitaries(base, depth) sum_dist = 0.0 for i in range(num_experiments): U = (ops.RotationX(2.0 * np.pi * random.random()) @ ops.RotationY(2.0 * np.pi * random.random()) @ ops.RotationZ(2.0 * np.pi * random.random())) U_approx = sk_algo(U, gates, recursion) dist = trace_dist(U, U_approx) sum_dist += dist phi1 = U(state.zero) phi2 = U_approx(state.zero) print('[{:2d}]: Trace Dist: {:.4f} State: {:6.4f}%'. format(i, dist, 100.0 * (1.0 - np.real(np.dot(phi1, phi2.conj()))))) print('Gates: {}, Mean Trace Dist:: {:.4f}'. format(len(gates), sum_dist / num_experiments))
def test_rk(self): rk0 = ops.Rk(0) self.assertTrue(rk0.is_close(ops.Identity())) rk1 = ops.Rk(1) self.assertTrue(rk1.is_close(ops.PauliZ())) rk2 = ops.Rk(2) self.assertTrue(rk2.is_close(ops.Sgate())) rk3 = ops.Rk(3) self.assertTrue(rk3.is_close(ops.Tgate())) for idx in range(8): psi = state.zeros(2) psi = (ops.Rk(idx)**2 @ ops.Rk(-idx)**2)(psi) self.assertTrue(psi.is_close(state.zeros(2)))
def test_global_phase(self): """Exercise 4.14 in Nielson, Chuang, HTH == phase*rotX(pi/4).""" h = ops.Hadamard() op = h(ops.Tgate()(h)) # If equal up to a global phase, all values should be equal. phase = op / ops.RotationX(math.pi / 4) self.assertTrue( math.isclose(phase[0, 0].real, phase[0, 1].real, abs_tol=1e-6)) self.assertTrue( math.isclose(phase[0, 0].imag, phase[0, 1].imag, abs_tol=1e-6)) self.assertTrue( math.isclose(phase[0, 0].real, phase[1, 0].real, abs_tol=1e-6)) self.assertTrue( math.isclose(phase[0, 0].imag, phase[1, 0].imag, abs_tol=1e-6)) self.assertTrue( math.isclose(phase[0, 0].real, phase[1, 1].real, abs_tol=1e-6)) self.assertTrue( math.isclose(phase[0, 0].imag, phase[1, 1].imag, abs_tol=1e-6))
def simple_kick(): psi = state.bitstring(0, 0, 1) psi = ops.Hadamard(2)(psi) psi = ops.ControlledU(0, 2, ops.Sgate())(psi) psi = ops.ControlledU(1, 2, ops.Tgate())(psi, 1) psi.dump()
def t(self, idx: int): self.apply1(ops.Tgate(), idx, 't')
def test_t_gate(self): """T^2 == S.""" s = ops.Tgate() @ ops.Tgate() self.assertTrue(s.is_close(ops.Phase()))
def test_t_gate(self): """Test that T^2 == S.""" t = ops.Tgate() self.assertTrue(t(t).is_close(ops.Phase()))