microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/tests-integration/devices/test_atom_e2e.py
160lines · 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 | %Qubit = type opaque |
| 43 | %Result = type opaque |
| 44 | |
| 45 | @empty_tag = internal constant [1 x i8] zeroinitializer |
| 46 | @0 = internal constant [6 x i8] c"0_a0r\\00" |
| 47 | @1 = internal constant [6 x i8] c"1_a1r\\00" |
| 48 | |
| 49 | define i64 @ENTRYPOINT__main() #0 { |
| 50 | block_0: |
| 51 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, %Qubit* null) |
| 52 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, %Qubit* inttoptr (i64 1 to %Qubit*)) |
| 53 | call void @__quantum__qis__sx__body(%Qubit* null) |
| 54 | call void @__quantum__qis__sx__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 55 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, %Qubit* null) |
| 56 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, %Qubit* inttoptr (i64 1 to %Qubit*)) |
| 57 | call void @__quantum__qis__cz__body(%Qubit* null, %Qubit* inttoptr (i64 1 to %Qubit*)) |
| 58 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, %Qubit* inttoptr (i64 1 to %Qubit*)) |
| 59 | call void @__quantum__qis__sx__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 60 | call void @__quantum__qis__rz__body(double 0x3FF921FB54442D18, %Qubit* inttoptr (i64 1 to %Qubit*)) |
| 61 | call void @__quantum__qis__mresetz__body(%Qubit* null, %Result* null) |
| 62 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 63 | call void @__quantum__rt__array_record_output(i64 2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @empty_tag, i64 0, i64 0)) |
| 64 | call void @__quantum__rt__result_record_output(%Result* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @0, i64 0, i64 0)) |
| 65 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @1, i64 0, i64 0)) |
| 66 | ret i64 0 |
| 67 | } |
| 68 | |
| 69 | declare void @__quantum__rt__array_record_output(i64, i8*) |
| 70 | |
| 71 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 72 | |
| 73 | declare void @__quantum__qis__sx__body(%Qubit*) |
| 74 | |
| 75 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) |
| 76 | |
| 77 | declare void @__quantum__qis__rz__body(double, %Qubit*) |
| 78 | |
| 79 | declare void @__quantum__qis__cz__body(%Qubit*, %Qubit*) |
| 80 | |
| 81 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 82 | |
| 83 | !llvm.module.flags = !{!0, !1, !2, !3} |
| 84 | |
| 85 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 86 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 87 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 88 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 89 | """, |
| 90 | ) |
| 91 | |
| 92 | |
| 93 | @pytest.mark.skipif(not PYQIR_AVAILABLE, reason=SKIP_REASON) |
| 94 | def test_device_simulate_with_cpu() -> None: |
| 95 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 96 | qir = qsharp.compile( |
| 97 | """ |
| 98 | { |
| 99 | use qs = Qubit[2]; |
| 100 | H(qs[0]); |
| 101 | CNOT(qs[0], qs[1]); |
| 102 | MResetEachZ(qs) |
| 103 | } |
| 104 | """ |
| 105 | ) |
| 106 | |
| 107 | device = NeutralAtomDevice() |
| 108 | compiled = device.compile(qir) |
| 109 | result = device.simulate(compiled, type="cpu") |
| 110 | |
| 111 | assert result == [[qsharp.Result.Zero, qsharp.Result.Zero]] or result == [ |
| 112 | [qsharp.Result.One, qsharp.Result.One] |
| 113 | ] |
| 114 | |
| 115 | |
| 116 | @pytest.mark.skipif(not PYQIR_AVAILABLE, reason=SKIP_REASON) |
| 117 | def test_device_simlate_with_clifford() -> None: |
| 118 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 119 | qir = qsharp.compile( |
| 120 | """ |
| 121 | { |
| 122 | use qs = Qubit[2]; |
| 123 | H(qs[0]); |
| 124 | CNOT(qs[0], qs[1]); |
| 125 | MResetEachZ(qs) |
| 126 | } |
| 127 | """ |
| 128 | ) |
| 129 | |
| 130 | device = NeutralAtomDevice() |
| 131 | compiled = device.compile(qir) |
| 132 | result = device.simulate(compiled, type="clifford") |
| 133 | |
| 134 | assert result == [[qsharp.Result.Zero, qsharp.Result.Zero]] or result == [ |
| 135 | [qsharp.Result.One, qsharp.Result.One] |
| 136 | ] |
| 137 | |
| 138 | |
| 139 | @pytest.mark.skipif(not PYQIR_AVAILABLE, reason=SKIP_REASON) |
| 140 | def test_device_simulate_with_loss() -> None: |
| 141 | qsharp.init(target_profile=qsharp.TargetProfile.Base) |
| 142 | qir = qsharp.compile( |
| 143 | """ |
| 144 | { |
| 145 | use qs = Qubit[2]; |
| 146 | H(qs[0]); |
| 147 | CNOT(qs[0], qs[1]); |
| 148 | MResetEachZ(qs) |
| 149 | } |
| 150 | """ |
| 151 | ) |
| 152 | |
| 153 | device = NeutralAtomDevice() |
| 154 | noise = NoiseConfig() |
| 155 | noise.mov.loss = 1.0 # Ensure loss occurs |
| 156 | result = device.simulate(qir, noise=noise, type="cpu") |
| 157 | result2 = device.simulate(qir, noise=noise, type="clifford") |
| 158 | |
| 159 | assert result == [[qsharp.Result.Loss, qsharp.Result.Loss]] |
| 160 | assert result2 == [[qsharp.Result.Loss, qsharp.Result.Loss]] |
| 161 | |