def test_circuit_with_noise_errors(): c = Circuit(2, density_matrix=True) c.add([gates.H(0), gates.H(1), gates.PauliNoiseChannel(0, px=0.2)]) with pytest.raises(ValueError): noisy_c = c.with_noise((0.2, 0.3, 0.0)) c = Circuit(2, density_matrix=True) c.add([gates.H(0), gates.H(1)]) with pytest.raises(ValueError): noisy_c = c.with_noise((0.2, 0.3)) with pytest.raises(ValueError): noisy_c = c.with_noise({0: (0.2, 0.3, 0.1), 1: (0.3, 0.1)}) with pytest.raises(ValueError): noisy_c = c.with_noise({0: (0.2, 0.3, 0.1)}) with pytest.raises(TypeError): noisy_c = c.with_noise({0, 1})
def test_circuit_with_noise_gates(): c = Circuit(2, density_matrix=True) c.add([gates.H(0), gates.H(1), gates.CNOT(0, 1)]) noisy_c = c.with_noise((0.1, 0.2, 0.3)) assert noisy_c.depth == 4 assert noisy_c.ngates == 7 for i in [1, 3, 5, 6]: assert noisy_c.queue[i].__class__.__name__ == "PauliNoiseChannel"
def test_circuit_with_noise_execution(backend): c = Circuit(2, density_matrix=True) c.add([gates.H(0), gates.H(1)]) noisy_c = c.with_noise((0.1, 0.2, 0.3)) final_state = noisy_c() target_c = Circuit(2, density_matrix=True) target_c.add(gates.H(0)) target_c.add(gates.PauliNoiseChannel(0, 0.1, 0.2, 0.3)) target_c.add(gates.H(1)) target_c.add(gates.PauliNoiseChannel(1, 0.1, 0.2, 0.3)) target_state = target_c() K.assert_allclose(final_state, target_state)
def test_circuit_with_noise_measurements(backend): c = Circuit(2, density_matrix=True) c.add([gates.H(0), gates.H(1)]) c.add(gates.M(0)) noisy_c = c.with_noise(3 * (0.1, )) final_state = noisy_c() target_c = Circuit(2, density_matrix=True) target_c.add(gates.H(0)) target_c.add(gates.PauliNoiseChannel(0, 0.1, 0.1, 0.1)) target_c.add(gates.H(1)) target_c.add(gates.PauliNoiseChannel(1, 0.1, 0.1, 0.1)) target_state = target_c() K.assert_allclose(final_state, target_state)
def test_circuit_with_noise_noise_map(backend): noise_map = {0: (0.1, 0.2, 0.1), 1: (0.2, 0.3, 0.0), 2: (0.0, 0.0, 0.0)} c = Circuit(3, density_matrix=True) c.add([gates.H(0), gates.H(1), gates.X(2)]) c.add(gates.M(2)) noisy_c = c.with_noise(noise_map) final_state = noisy_c() target_c = Circuit(3, density_matrix=True) target_c.add(gates.H(0)) target_c.add(gates.PauliNoiseChannel(0, 0.1, 0.2, 0.1)) target_c.add(gates.H(1)) target_c.add(gates.PauliNoiseChannel(1, 0.2, 0.3, 0.0)) target_c.add(gates.X(2)) target_state = target_c() K.assert_allclose(final_state, target_state)
def test_repeated_execute_with_noise(backend): thetas = np.random.random(4) c = Circuit(4) c.add((gates.RY(i, t) for i, t in enumerate(thetas))) noisy_c = c.with_noise((0.2, 0.0, 0.1)) np.random.seed(1234) final_state = noisy_c(nshots=20) np.random.seed(1234) target_state = [] for _ in range(20): noiseless_c = Circuit(4) for i, t in enumerate(thetas): noiseless_c.add(gates.RY(i, theta=t)) if np.random.random() < 0.2: noiseless_c.add(gates.X(i)) if np.random.random() < 0.1: noiseless_c.add(gates.Z(i)) target_state.append(noiseless_c()) target_state = np.stack(target_state) K.assert_allclose(final_state, target_state)
def test_circuit_with_noise_probabilistic_channel(backend): original_backend = qibo.get_backend() qibo.set_backend(backend) thetas = np.random.random(4) c = Circuit(4) c.add((gates.RY(i, t) for i, t in enumerate(thetas))) noisy_c = c.with_noise((0.2, 0.0, 0.1)) np.random.seed(1234) final_state = noisy_c(nshots=20) np.random.seed(1234) target_state = [] for _ in range(20): noiseless_c = Circuit(4) for i, t in enumerate(thetas): noiseless_c.add(gates.RY(i, theta=t)) if np.random.random() < 0.2: noiseless_c.add(gates.X(i)) if np.random.random() < 0.1: noiseless_c.add(gates.Z(i)) target_state.append(noiseless_c().numpy()) target_state = np.stack(target_state) np.testing.assert_allclose(final_state, target_state) qibo.set_backend(original_backend)