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_density_to_cartesian(self): """Test density to cartesian conversion.""" q0 = state.zeros(1) rho = q0.density() x, y, z = helper.density_to_cartesian(rho) self.assertEqual(x, 0.0) self.assertEqual(y, 0.0) self.assertEqual(z, 1.0) q1 = state.ones(1) rho = q1.density() x, y, z = helper.density_to_cartesian(rho) self.assertEqual(x, 0.0) self.assertEqual(y, 0.0) self.assertEqual(z, -1.0) qh = ops.Hadamard()(q0) rho = qh.density() x, y, z = helper.density_to_cartesian(rho) self.assertTrue(math.isclose(np.real(x), 1.0, abs_tol=1e-6)) self.assertTrue(math.isclose(np.real(y), 0.0)) self.assertTrue(math.isclose(np.real(z), 0.0, abs_tol=1e-6)) qr = ops.RotationZ(90.0 * math.pi / 180.0)(qh) rho = qr.density() x, y, z = helper.density_to_cartesian(rho) self.assertTrue(math.isclose(np.real(x), 0.0, abs_tol=1e-6)) self.assertTrue(math.isclose(np.real(y), -1.0, abs_tol=1e-6)) self.assertTrue(math.isclose(np.real(z), 0.0, abs_tol=1e-6))
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 test_phase(self): psi = state.zero psi = ops.RotationZ(math.pi/2)(psi) phase = psi.phase(0) # Note that Rotation rotates by theta/2. self.assertTrue(math.isclose(phase, -45.0, abs_tol=1e-6)) # Test all other angles, check for sign flips. for i in range(360): self.check_rotation(float(i)/2) for i in range(360): self.check_rotation(float(-i)/2)
def test_control_equalities(self): """Exercise 4.31 Nielson, Chung.""" i, x, y, z = ops.Pauli() x1 = x * i x2 = i * x y1 = y * i y2 = i * y z1 = z * i z2 = i * z c = ops.Cnot(0, 1) theta = 25.0 * math.pi / 180.0 rx2 = i * ops.RotationX(theta) rz1 = ops.RotationZ(theta) * i self.assertTrue(c(x1(c)).is_close(x1(x2))) self.assertTrue((c @ x1 @ c).is_close(x1 @ x2)) self.assertTrue((c @ y1 @ c).is_close(y1 @ x2)) self.assertTrue((c @ z1 @ c).is_close(z1)) self.assertTrue((c @ x2 @ c).is_close(x2)) self.assertTrue((c @ y2 @ c).is_close(z1 @ y2)) self.assertTrue((c @ z2 @ c).is_close(z1 @ z2)) self.assertTrue((rz1 @ c).is_close(c @ rz1)) self.assertTrue((rx2 @ c).is_close(c @ rx2))
def rz(self, idx: int, theta: float): self.apply1(ops.RotationZ(theta), idx, 'rz', val=theta)
def make_u(alpha, beta, gamma, delta): """Construct unitary from the 4 parameters.""" return ( (ops.RotationZ(beta) @ ops.RotationY(gamma) @ ops.RotationZ(delta)) * cmath.exp(1.0j * alpha))
def check_rotation(self, angle): # Note that RotationZ rotates by theta/2 psi = ops.RotationZ(math.pi/180.0*angle)(state.zero) self.assertTrue(math.isclose(-angle/2, psi.phase(0), abs_tol=1e-5))
def rz(self, idx, theta): self.apply1(ops.RotationZ(theta), idx, 'rz', val=theta)