microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/tests-integration/devices/test_atom_e2e.py
192lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | import pytest |
| 5 | from expecttest import assert_expected_inline |
| 6 | |
| 7 | import qsharp |
| 8 | from qsharp._device._atom import NeutralAtomDevice, NoiseConfig |
| 9 | |
| 10 | try: |
| 11 | import pyqir |
| 12 | |
| 13 | PYQIR_AVAILABLE = True |
| 14 | except ImportError: |
| 15 | PYQIR_AVAILABLE = False |
| 16 | |
| 17 | SKIP_REASON = "PyQIR is not available" |
| 18 | |
| 19 | |
| 20 | @pytest.mark.skipif(not PYQIR_AVAILABLE, reason=SKIP_REASON) |
| 21 | def test_device_compile() -> None: |
| 22 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 23 | qir = qsharp.compile( |
| 24 | """ |
| 25 | { |
| 26 | use qs = Qubit[2]; |
| 27 | H(qs[0]); |
| 28 | CNOT(qs[0], qs[1]); |
| 29 | MResetEachZ(qs) |
| 30 | } |
| 31 | """ |
| 32 | ) |
| 33 | |
| 34 | device = NeutralAtomDevice() |
| 35 | compiled = device.compile(qir) |
| 36 | compiled_qir = str(compiled) |
| 37 | |
| 38 | assert_expected_inline( |
| 39 | compiled_qir, |
| 40 | """\ |
| 41 | |
| 42 | @0 = internal constant [4 x i8] c"0_a\\00" |
| 43 | @1 = internal constant [6 x i8] c"1_a0r\\00" |
| 44 | @2 = internal constant [6 x i8] c"2_a1r\\00" |
| 45 | |
| 46 | define i64 @ENTRYPOINT__main() #0 { |
| 47 | block_0: |
| 48 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, ptr null) |
| 49 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, ptr inttoptr (i64 1 to ptr)) |
| 50 | call void @__quantum__qis__sx__body(ptr null) |
| 51 | call void @__quantum__qis__sx__body(ptr inttoptr (i64 1 to ptr)) |
| 52 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, ptr null) |
| 53 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, ptr inttoptr (i64 1 to ptr)) |
| 54 | call void @__quantum__qis__cz__body(ptr null, ptr inttoptr (i64 1 to ptr)) |
| 55 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, ptr inttoptr (i64 1 to ptr)) |
| 56 | call void @__quantum__qis__sx__body(ptr inttoptr (i64 1 to ptr)) |
| 57 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, ptr inttoptr (i64 1 to ptr)) |
| 58 | call void @__quantum__qis__mresetz__body(ptr null, ptr null) |
| 59 | call void @__quantum__qis__mresetz__body(ptr inttoptr (i64 1 to ptr), ptr inttoptr (i64 1 to ptr)) |
| 60 | call void @__quantum__rt__array_record_output(i64 2, ptr @0) |
| 61 | call void @__quantum__rt__result_record_output(ptr null, ptr @1) |
| 62 | call void @__quantum__rt__result_record_output(ptr inttoptr (i64 1 to ptr), ptr @2) |
| 63 | ret i64 0 |
| 64 | } |
| 65 | |
| 66 | declare void @__quantum__rt__array_record_output(i64, ptr) |
| 67 | |
| 68 | declare void @__quantum__rt__result_record_output(ptr, ptr) |
| 69 | |
| 70 | declare void @__quantum__qis__sx__body(ptr) |
| 71 | |
| 72 | declare void @__quantum__qis__mresetz__body(ptr, ptr) |
| 73 | |
| 74 | declare void @__quantum__qis__rz__body(double, ptr) |
| 75 | |
| 76 | declare void @__quantum__qis__cz__body(ptr, ptr) |
| 77 | |
| 78 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 79 | |
| 80 | !llvm.module.flags = !{!0, !1, !2, !3} |
| 81 | |
| 82 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 83 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 84 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 85 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 86 | """, |
| 87 | ) |
| 88 | |
| 89 | |
| 90 | @pytest.mark.skipif(not PYQIR_AVAILABLE, reason=SKIP_REASON) |
| 91 | def test_device_simulate_with_cpu() -> None: |
| 92 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 93 | qir = qsharp.compile( |
| 94 | """ |
| 95 | { |
| 96 | use qs = Qubit[2]; |
| 97 | H(qs[0]); |
| 98 | CNOT(qs[0], qs[1]); |
| 99 | MResetEachZ(qs) |
| 100 | } |
| 101 | """ |
| 102 | ) |
| 103 | |
| 104 | device = NeutralAtomDevice() |
| 105 | compiled = device.compile(qir) |
| 106 | result = device.simulate(compiled, type="cpu") |
| 107 | |
| 108 | assert result == [[qsharp.Result.Zero, qsharp.Result.Zero]] or result == [ |
| 109 | [qsharp.Result.One, qsharp.Result.One] |
| 110 | ] |
| 111 | |
| 112 | |
| 113 | @pytest.mark.skipif(not PYQIR_AVAILABLE, reason=SKIP_REASON) |
| 114 | def test_device_simlate_with_clifford() -> None: |
| 115 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 116 | qir = qsharp.compile( |
| 117 | """ |
| 118 | { |
| 119 | use qs = Qubit[2]; |
| 120 | H(qs[0]); |
| 121 | CNOT(qs[0], qs[1]); |
| 122 | MResetEachZ(qs) |
| 123 | } |
| 124 | """ |
| 125 | ) |
| 126 | |
| 127 | device = NeutralAtomDevice() |
| 128 | compiled = device.compile(qir) |
| 129 | result = device.simulate(compiled, type="clifford") |
| 130 | |
| 131 | assert result == [[qsharp.Result.Zero, qsharp.Result.Zero]] or result == [ |
| 132 | [qsharp.Result.One, qsharp.Result.One] |
| 133 | ] |
| 134 | |
| 135 | |
| 136 | @pytest.mark.skipif(not PYQIR_AVAILABLE, reason=SKIP_REASON) |
| 137 | def test_device_simulate_with_loss() -> None: |
| 138 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 139 | qir = qsharp.compile( |
| 140 | """ |
| 141 | { |
| 142 | use qs = Qubit[2]; |
| 143 | H(qs[0]); |
| 144 | CNOT(qs[0], qs[1]); |
| 145 | MResetEachZ(qs) |
| 146 | } |
| 147 | """ |
| 148 | ) |
| 149 | |
| 150 | device = NeutralAtomDevice() |
| 151 | noise = NoiseConfig() |
| 152 | noise.mov.loss = 1.0 # Ensure loss occurs |
| 153 | result = device.simulate(qir, noise=noise, type="cpu") |
| 154 | result2 = device.simulate(qir, noise=noise, type="clifford") |
| 155 | |
| 156 | assert result == [[qsharp.Result.Loss, qsharp.Result.Loss]] |
| 157 | assert result2 == [[qsharp.Result.Loss, qsharp.Result.Loss]] |
| 158 | |
| 159 | |
| 160 | def test_s_noise_inherits_from_rz(): |
| 161 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 162 | qsharp.eval("operation Main() : Result { use q = Qubit(); S(q); MResetZ(q) }") |
| 163 | ir = qsharp.compile("Main()") |
| 164 | noise = NoiseConfig() |
| 165 | noise.rz.x = 1.0 |
| 166 | device = NeutralAtomDevice() |
| 167 | output = device.simulate(ir, 1, noise) |
| 168 | assert output == [qsharp.Result.One] |
| 169 | |
| 170 | |
| 171 | def test_z_noise_inherits_from_rz(): |
| 172 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 173 | qsharp.eval("operation Main() : Result { use q = Qubit(); Z(q); MResetZ(q) }") |
| 174 | ir = qsharp.compile("Main()") |
| 175 | noise = NoiseConfig() |
| 176 | noise.rz.x = 1.0 |
| 177 | device = NeutralAtomDevice() |
| 178 | output = device.simulate(ir, 1, noise) |
| 179 | assert output == [qsharp.Result.One] |
| 180 | |
| 181 | |
| 182 | def test_s_adj_noise_inherits_from_rz(): |
| 183 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 184 | qsharp.eval( |
| 185 | "operation Main() : Result { use q = Qubit(); Adjoint S(q); MResetZ(q) }" |
| 186 | ) |
| 187 | ir = qsharp.compile("Main()") |
| 188 | noise = NoiseConfig() |
| 189 | noise.rz.x = 1.0 |
| 190 | device = NeutralAtomDevice() |
| 191 | output = device.simulate(ir, 1, noise) |
| 192 | assert output == [qsharp.Result.One] |