microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/tests/test_noisy_config.py
132lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from qsharp._simulation import NoiseConfig |
| 5 | import pytest |
| 6 | |
| 7 | |
| 8 | def test_setting_1q_noise(): |
| 9 | noise = NoiseConfig() |
| 10 | noise.h.set_pauli_noise("X", 0.01) |
| 11 | assert noise.h.x == 0.01 |
| 12 | |
| 13 | |
| 14 | def test_setting_1q_noise_through_attr(): |
| 15 | noise = NoiseConfig() |
| 16 | noise.h.x = 0.01 |
| 17 | assert noise.h.x == 0.01 |
| 18 | |
| 19 | |
| 20 | def test_setting_2q_noise(): |
| 21 | noise = NoiseConfig() |
| 22 | noise.cz.set_pauli_noise("IZ", 0.01) |
| 23 | noise.cz.set_pauli_noise("ZZ", 0.02) |
| 24 | assert noise.cz.iz == 0.01 |
| 25 | assert noise.cz.zz == 0.02 |
| 26 | |
| 27 | |
| 28 | def test_setting_2q_noise_through_attr(): |
| 29 | noise = NoiseConfig() |
| 30 | noise.cz.set_pauli_noise("IZ", 0.01) |
| 31 | noise.cz.set_pauli_noise("ZZ", 0.02) |
| 32 | assert noise.cz.iz == 0.01 |
| 33 | assert noise.cz.zz == 0.02 |
| 34 | |
| 35 | |
| 36 | def test_setting_1q_depolarizing_noise(): |
| 37 | noise = NoiseConfig() |
| 38 | noise.h.set_depolarizing(0.3) |
| 39 | p = 0.3 / 3 |
| 40 | assert noise.h.x == p |
| 41 | assert noise.h.y == p |
| 42 | assert noise.h.z == p |
| 43 | |
| 44 | |
| 45 | def test_setting_2q_depolarizing_noise(): |
| 46 | noise = NoiseConfig() |
| 47 | noise.cz.set_depolarizing(0.15) |
| 48 | p = 0.15 / 15 |
| 49 | assert noise.cz.ix == p |
| 50 | assert noise.cz.iy == p |
| 51 | assert noise.cz.iz == p |
| 52 | assert noise.cz.xx == p |
| 53 | assert noise.cz.xy == p |
| 54 | assert noise.cz.xz == p |
| 55 | assert noise.cz.yx == p |
| 56 | assert noise.cz.yy == p |
| 57 | assert noise.cz.yz == p |
| 58 | assert noise.cz.zx == p |
| 59 | assert noise.cz.zy == p |
| 60 | assert noise.cz.zz == p |
| 61 | |
| 62 | |
| 63 | def test_setting_2q_noise_on_1q_op_errors(): |
| 64 | noise = NoiseConfig() |
| 65 | with pytest.raises(AttributeError): |
| 66 | noise.h.set_pauli_noise("ZZ", 0.01) |
| 67 | |
| 68 | |
| 69 | def test_setting_2q_noise_on_1q_op_through_attr_errors(): |
| 70 | noise = NoiseConfig() |
| 71 | with pytest.raises(AttributeError): |
| 72 | noise.h.zz = 0.01 |
| 73 | |
| 74 | |
| 75 | def test_setting_1q_noise_on_2q_op_errors(): |
| 76 | noise = NoiseConfig() |
| 77 | with pytest.raises(AttributeError): |
| 78 | noise.cz.set_pauli_noise("Z", 0.01) |
| 79 | |
| 80 | |
| 81 | def test_setting_1q_noise_on_2q_op_through_attr_errors(): |
| 82 | noise = NoiseConfig() |
| 83 | with pytest.raises(AttributeError): |
| 84 | noise.cz.z = 0.01 |
| 85 | |
| 86 | |
| 87 | def test_setting_non_valid_pauli_errors(): |
| 88 | noise = NoiseConfig() |
| 89 | with pytest.raises(AttributeError): |
| 90 | noise.h.set_pauli_noise("W", 0.01) |
| 91 | |
| 92 | |
| 93 | def test_setting_non_valid_pauli_through_attr_errors(): |
| 94 | noise = NoiseConfig() |
| 95 | with pytest.raises(AttributeError): |
| 96 | noise.h.w = 0.01 |
| 97 | |
| 98 | |
| 99 | def test_accessing_non_set_pauli_attr_errors(): |
| 100 | noise = NoiseConfig() |
| 101 | with pytest.raises(AttributeError): |
| 102 | noise.h.x |
| 103 | |
| 104 | |
| 105 | def test_accessing_non_valid_pauli_attr_errors(): |
| 106 | noise = NoiseConfig() |
| 107 | with pytest.raises(AttributeError): |
| 108 | noise.h.w |
| 109 | |
| 110 | |
| 111 | def test_setting_bitflip_on_1q_op(): |
| 112 | noise = NoiseConfig() |
| 113 | noise.h.set_bitflip(0.01) |
| 114 | assert noise.h.x == 0.01 |
| 115 | |
| 116 | |
| 117 | def test_setting_bitflip_on_2q_op_errors(): |
| 118 | noise = NoiseConfig() |
| 119 | with pytest.raises(AttributeError): |
| 120 | noise.cz.set_bitflip(0.01) |
| 121 | |
| 122 | |
| 123 | def test_setting_phaseflip_on_1q_op(): |
| 124 | noise = NoiseConfig() |
| 125 | noise.h.set_phaseflip(0.01) |
| 126 | assert noise.h.z == 0.01 |
| 127 | |
| 128 | |
| 129 | def test_setting_phaseflip_on_2q_op_errors(): |
| 130 | noise = NoiseConfig() |
| 131 | with pytest.raises(AttributeError): |
| 132 | noise.cz.set_phaseflip(0.01) |
| 133 | |