microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc/src/codegen/tests.rs
1127lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use expect_test::expect; |
| 5 | use qsc_data_structures::{language_features::LanguageFeatures, target::TargetCapabilityFlags}; |
| 6 | use qsc_frontend::compile::SourceMap; |
| 7 | |
| 8 | use crate::codegen::qir::get_qir; |
| 9 | |
| 10 | #[test] |
| 11 | fn code_with_errors_returns_errors() { |
| 12 | let source = "namespace Test { |
| 13 | @EntryPoint() |
| 14 | operation Main() : Unit { |
| 15 | use q = Qubit() |
| 16 | let pi_over_two = 4.0 / 2.0; |
| 17 | } |
| 18 | }"; |
| 19 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 20 | let language_features = LanguageFeatures::default(); |
| 21 | let capabilities = TargetCapabilityFlags::empty(); |
| 22 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 23 | |
| 24 | expect![[r#" |
| 25 | Err( |
| 26 | [ |
| 27 | Compile( |
| 28 | WithSource { |
| 29 | sources: [ |
| 30 | Source { |
| 31 | name: "test.qs", |
| 32 | contents: "namespace Test {\n @EntryPoint()\n operation Main() : Unit {\n use q = Qubit()\n let pi_over_two = 4.0 / 2.0;\n }\n }", |
| 33 | offset: 0, |
| 34 | }, |
| 35 | ], |
| 36 | error: Frontend( |
| 37 | Error( |
| 38 | Parse( |
| 39 | Error( |
| 40 | Token( |
| 41 | Semi, |
| 42 | Keyword( |
| 43 | Let, |
| 44 | ), |
| 45 | Span { |
| 46 | lo: 129, |
| 47 | hi: 132, |
| 48 | }, |
| 49 | ), |
| 50 | ), |
| 51 | ), |
| 52 | ), |
| 53 | ), |
| 54 | }, |
| 55 | ), |
| 56 | ], |
| 57 | ) |
| 58 | "#]] |
| 59 | .assert_debug_eq(&get_qir(sources, language_features, capabilities, store, &[(std_id, None)])); |
| 60 | } |
| 61 | |
| 62 | mod base_profile { |
| 63 | use expect_test::expect; |
| 64 | use qsc_data_structures::{language_features::LanguageFeatures, target::TargetCapabilityFlags}; |
| 65 | use qsc_frontend::compile::SourceMap; |
| 66 | |
| 67 | use crate::codegen::qir::get_qir; |
| 68 | |
| 69 | #[test] |
| 70 | fn simple() { |
| 71 | let source = "namespace Test { |
| 72 | import Std.Math.*; |
| 73 | open QIR.Intrinsic; |
| 74 | @EntryPoint() |
| 75 | operation Main() : Result { |
| 76 | use q = Qubit(); |
| 77 | let pi_over_two = 4.0 / 2.0; |
| 78 | __quantum__qis__rz__body(pi_over_two, q); |
| 79 | mutable some_angle = ArcSin(0.0); |
| 80 | __quantum__qis__rz__body(some_angle, q); |
| 81 | set some_angle = ArcCos(-1.0) / PI(); |
| 82 | __quantum__qis__rz__body(some_angle, q); |
| 83 | __quantum__qis__mresetz__body(q) |
| 84 | } |
| 85 | }"; |
| 86 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 87 | let language_features = LanguageFeatures::default(); |
| 88 | let capabilities = TargetCapabilityFlags::empty(); |
| 89 | |
| 90 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 91 | let qir = get_qir( |
| 92 | sources, |
| 93 | language_features, |
| 94 | capabilities, |
| 95 | store, |
| 96 | &[(std_id, None)], |
| 97 | ) |
| 98 | .expect("Failed to generate QIR"); |
| 99 | expect![[r#" |
| 100 | %Result = type opaque |
| 101 | %Qubit = type opaque |
| 102 | |
| 103 | define void @ENTRYPOINT__main() #0 { |
| 104 | block_0: |
| 105 | call void @__quantum__qis__rz__body(double 2.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 106 | call void @__quantum__qis__rz__body(double 0.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 107 | call void @__quantum__qis__rz__body(double 1.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 108 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 109 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 110 | ret void |
| 111 | } |
| 112 | |
| 113 | declare void @__quantum__qis__rz__body(double, %Qubit*) |
| 114 | |
| 115 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 116 | |
| 117 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 118 | |
| 119 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="1" "required_num_results"="1" } |
| 120 | attributes #1 = { "irreversible" } |
| 121 | |
| 122 | ; module flags |
| 123 | |
| 124 | !llvm.module.flags = !{!0, !1, !2, !3} |
| 125 | |
| 126 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 127 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 128 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 129 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 130 | "#]] |
| 131 | .assert_eq(&qir); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn qubit_reuse_triggers_reindexing() { |
| 136 | let source = "namespace Test { |
| 137 | @EntryPoint() |
| 138 | operation Main() : (Result, Result) { |
| 139 | use q = Qubit(); |
| 140 | (MResetZ(q), MResetZ(q)) |
| 141 | } |
| 142 | }"; |
| 143 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 144 | let language_features = LanguageFeatures::default(); |
| 145 | let capabilities = TargetCapabilityFlags::empty(); |
| 146 | |
| 147 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 148 | let qir = get_qir( |
| 149 | sources, |
| 150 | language_features, |
| 151 | capabilities, |
| 152 | store, |
| 153 | &[(std_id, None)], |
| 154 | ) |
| 155 | .expect("Failed to generate QIR"); |
| 156 | expect![[r#" |
| 157 | %Result = type opaque |
| 158 | %Qubit = type opaque |
| 159 | |
| 160 | define void @ENTRYPOINT__main() #0 { |
| 161 | block_0: |
| 162 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 163 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 164 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 165 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 166 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 167 | ret void |
| 168 | } |
| 169 | |
| 170 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 171 | |
| 172 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 173 | |
| 174 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 175 | |
| 176 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 177 | attributes #1 = { "irreversible" } |
| 178 | |
| 179 | ; module flags |
| 180 | |
| 181 | !llvm.module.flags = !{!0, !1, !2, !3} |
| 182 | |
| 183 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 184 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 185 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 186 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 187 | "#]].assert_eq(&qir); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn qubit_measurements_get_deferred() { |
| 192 | let source = "namespace Test { |
| 193 | @EntryPoint() |
| 194 | operation Main() : Result[] { |
| 195 | use (q0, q1) = (Qubit(), Qubit()); |
| 196 | X(q0); |
| 197 | let r0 = MResetZ(q0); |
| 198 | X(q1); |
| 199 | let r1 = MResetZ(q1); |
| 200 | [r0, r1] |
| 201 | } |
| 202 | }"; |
| 203 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 204 | let language_features = LanguageFeatures::default(); |
| 205 | let capabilities = TargetCapabilityFlags::empty(); |
| 206 | |
| 207 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 208 | let qir = get_qir( |
| 209 | sources, |
| 210 | language_features, |
| 211 | capabilities, |
| 212 | store, |
| 213 | &[(std_id, None)], |
| 214 | ) |
| 215 | .expect("Failed to generate QIR"); |
| 216 | expect![[r#" |
| 217 | %Result = type opaque |
| 218 | %Qubit = type opaque |
| 219 | |
| 220 | define void @ENTRYPOINT__main() #0 { |
| 221 | block_0: |
| 222 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 223 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 224 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 225 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 226 | call void @__quantum__rt__array_record_output(i64 2, i8* null) |
| 227 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 228 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 229 | ret void |
| 230 | } |
| 231 | |
| 232 | declare void @__quantum__qis__x__body(%Qubit*) |
| 233 | |
| 234 | declare void @__quantum__rt__array_record_output(i64, i8*) |
| 235 | |
| 236 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 237 | |
| 238 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 239 | |
| 240 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 241 | attributes #1 = { "irreversible" } |
| 242 | |
| 243 | ; module flags |
| 244 | |
| 245 | !llvm.module.flags = !{!0, !1, !2, !3} |
| 246 | |
| 247 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 248 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 249 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 250 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 251 | "#]].assert_eq(&qir); |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn qubit_id_swap_results_in_different_id_usage() { |
| 256 | let source = "namespace Test { |
| 257 | @EntryPoint() |
| 258 | operation Main() : (Result, Result) { |
| 259 | use (q0, q1) = (Qubit(), Qubit()); |
| 260 | X(q0); |
| 261 | Relabel([q0, q1], [q1, q0]); |
| 262 | X(q1); |
| 263 | (MResetZ(q0), MResetZ(q1)) |
| 264 | } |
| 265 | }"; |
| 266 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 267 | let language_features = LanguageFeatures::default(); |
| 268 | let capabilities = TargetCapabilityFlags::empty(); |
| 269 | |
| 270 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 271 | let qir = get_qir( |
| 272 | sources, |
| 273 | language_features, |
| 274 | capabilities, |
| 275 | store, |
| 276 | &[(std_id, None)], |
| 277 | ) |
| 278 | .expect("Failed to generate QIR"); |
| 279 | expect![[r#" |
| 280 | %Result = type opaque |
| 281 | %Qubit = type opaque |
| 282 | |
| 283 | define void @ENTRYPOINT__main() #0 { |
| 284 | block_0: |
| 285 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 286 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 287 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 288 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 289 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 290 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 291 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 292 | ret void |
| 293 | } |
| 294 | |
| 295 | declare void @__quantum__qis__x__body(%Qubit*) |
| 296 | |
| 297 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 298 | |
| 299 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 300 | |
| 301 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 302 | |
| 303 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 304 | attributes #1 = { "irreversible" } |
| 305 | |
| 306 | ; module flags |
| 307 | |
| 308 | !llvm.module.flags = !{!0, !1, !2, !3} |
| 309 | |
| 310 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 311 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 312 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 313 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 314 | "#]].assert_eq(&qir); |
| 315 | } |
| 316 | |
| 317 | #[test] |
| 318 | fn qubit_id_swap_across_reset_uses_updated_ids() { |
| 319 | let source = "namespace Test { |
| 320 | @EntryPoint() |
| 321 | operation Main() : (Result, Result) { |
| 322 | { |
| 323 | use (q0, q1) = (Qubit(), Qubit()); |
| 324 | X(q0); |
| 325 | Relabel([q0, q1], [q1, q0]); |
| 326 | X(q1); |
| 327 | Reset(q0); |
| 328 | Reset(q1); |
| 329 | } |
| 330 | use (q0, q1) = (Qubit(), Qubit()); |
| 331 | (MResetZ(q0), MResetZ(q1)) |
| 332 | } |
| 333 | }"; |
| 334 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 335 | let language_features = LanguageFeatures::default(); |
| 336 | let capabilities = TargetCapabilityFlags::empty(); |
| 337 | |
| 338 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 339 | let qir = get_qir( |
| 340 | sources, |
| 341 | language_features, |
| 342 | capabilities, |
| 343 | store, |
| 344 | &[(std_id, None)], |
| 345 | ) |
| 346 | .expect("Failed to generate QIR"); |
| 347 | expect![[r#" |
| 348 | %Result = type opaque |
| 349 | %Qubit = type opaque |
| 350 | |
| 351 | define void @ENTRYPOINT__main() #0 { |
| 352 | block_0: |
| 353 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 354 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 355 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 3 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 356 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 2 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 357 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 358 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 359 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 360 | ret void |
| 361 | } |
| 362 | |
| 363 | declare void @__quantum__qis__x__body(%Qubit*) |
| 364 | |
| 365 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 366 | |
| 367 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 368 | |
| 369 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 370 | |
| 371 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="base_profile" "required_num_qubits"="4" "required_num_results"="2" } |
| 372 | attributes #1 = { "irreversible" } |
| 373 | |
| 374 | ; module flags |
| 375 | |
| 376 | !llvm.module.flags = !{!0, !1, !2, !3} |
| 377 | |
| 378 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 379 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 380 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 381 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 382 | "#]].assert_eq(&qir); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | mod adaptive_profile { |
| 387 | use expect_test::expect; |
| 388 | use qsc_data_structures::{language_features::LanguageFeatures, target::TargetCapabilityFlags}; |
| 389 | use qsc_frontend::compile::SourceMap; |
| 390 | |
| 391 | use crate::codegen::qir::get_qir; |
| 392 | |
| 393 | #[test] |
| 394 | fn simple() { |
| 395 | let source = "namespace Test { |
| 396 | import Std.Math.*; |
| 397 | open QIR.Intrinsic; |
| 398 | @EntryPoint() |
| 399 | operation Main() : Result { |
| 400 | use q = Qubit(); |
| 401 | let pi_over_two = 4.0 / 2.0; |
| 402 | __quantum__qis__rz__body(pi_over_two, q); |
| 403 | mutable some_angle = ArcSin(0.0); |
| 404 | __quantum__qis__rz__body(some_angle, q); |
| 405 | set some_angle = ArcCos(-1.0) / PI(); |
| 406 | __quantum__qis__rz__body(some_angle, q); |
| 407 | __quantum__qis__mresetz__body(q) |
| 408 | } |
| 409 | }"; |
| 410 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 411 | let language_features = LanguageFeatures::default(); |
| 412 | let capabilities = TargetCapabilityFlags::Adaptive; |
| 413 | |
| 414 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 415 | let qir = get_qir( |
| 416 | sources, |
| 417 | language_features, |
| 418 | capabilities, |
| 419 | store, |
| 420 | &[(std_id, None)], |
| 421 | ) |
| 422 | .expect("Failed to generate QIR"); |
| 423 | expect![[r#" |
| 424 | %Result = type opaque |
| 425 | %Qubit = type opaque |
| 426 | |
| 427 | define void @ENTRYPOINT__main() #0 { |
| 428 | block_0: |
| 429 | call void @__quantum__qis__rz__body(double 2.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 430 | call void @__quantum__qis__rz__body(double 0.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 431 | call void @__quantum__qis__rz__body(double 1.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 432 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 433 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 434 | ret void |
| 435 | } |
| 436 | |
| 437 | declare void @__quantum__qis__rz__body(double, %Qubit*) |
| 438 | |
| 439 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 440 | |
| 441 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 442 | |
| 443 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="1" "required_num_results"="1" } |
| 444 | attributes #1 = { "irreversible" } |
| 445 | |
| 446 | ; module flags |
| 447 | |
| 448 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 449 | |
| 450 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 451 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 452 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 453 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 454 | !4 = !{i32 1, !"classical_ints", i1 false} |
| 455 | !5 = !{i32 1, !"classical_floats", i1 false} |
| 456 | !6 = !{i32 1, !"backwards_branching", i1 false} |
| 457 | !7 = !{i32 1, !"qubit_resetting", i1 false} |
| 458 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 459 | !9 = !{i32 1, !"user_functions", i1 false} |
| 460 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 461 | "#]] |
| 462 | .assert_eq(&qir); |
| 463 | } |
| 464 | |
| 465 | #[test] |
| 466 | fn qubit_reuse_triggers_reindexing() { |
| 467 | let source = "namespace Test { |
| 468 | @EntryPoint() |
| 469 | operation Main() : (Result, Result) { |
| 470 | use q = Qubit(); |
| 471 | (MResetZ(q), MResetZ(q)) |
| 472 | } |
| 473 | }"; |
| 474 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 475 | let language_features = LanguageFeatures::default(); |
| 476 | let capabilities = TargetCapabilityFlags::Adaptive; |
| 477 | |
| 478 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 479 | let qir = get_qir( |
| 480 | sources, |
| 481 | language_features, |
| 482 | capabilities, |
| 483 | store, |
| 484 | &[(std_id, None)], |
| 485 | ) |
| 486 | .expect("Failed to generate QIR"); |
| 487 | expect![[r#" |
| 488 | %Result = type opaque |
| 489 | %Qubit = type opaque |
| 490 | |
| 491 | define void @ENTRYPOINT__main() #0 { |
| 492 | block_0: |
| 493 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 494 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 495 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 496 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 497 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 498 | ret void |
| 499 | } |
| 500 | |
| 501 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 502 | |
| 503 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 504 | |
| 505 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 506 | |
| 507 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 508 | attributes #1 = { "irreversible" } |
| 509 | |
| 510 | ; module flags |
| 511 | |
| 512 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 513 | |
| 514 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 515 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 516 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 517 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 518 | !4 = !{i32 1, !"classical_ints", i1 false} |
| 519 | !5 = !{i32 1, !"classical_floats", i1 false} |
| 520 | !6 = !{i32 1, !"backwards_branching", i1 false} |
| 521 | !7 = !{i32 1, !"qubit_resetting", i1 false} |
| 522 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 523 | !9 = !{i32 1, !"user_functions", i1 false} |
| 524 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 525 | "#]].assert_eq(&qir); |
| 526 | } |
| 527 | |
| 528 | #[test] |
| 529 | fn qubit_measurements_not_deferred() { |
| 530 | let source = "namespace Test { |
| 531 | @EntryPoint() |
| 532 | operation Main() : Result[] { |
| 533 | use (q0, q1) = (Qubit(), Qubit()); |
| 534 | X(q0); |
| 535 | let r0 = MResetZ(q0); |
| 536 | X(q1); |
| 537 | let r1 = MResetZ(q1); |
| 538 | [r0, r1] |
| 539 | } |
| 540 | }"; |
| 541 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 542 | let language_features = LanguageFeatures::default(); |
| 543 | let capabilities = TargetCapabilityFlags::Adaptive; |
| 544 | |
| 545 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 546 | let qir = get_qir( |
| 547 | sources, |
| 548 | language_features, |
| 549 | capabilities, |
| 550 | store, |
| 551 | &[(std_id, None)], |
| 552 | ) |
| 553 | .expect("Failed to generate QIR"); |
| 554 | expect![[r#" |
| 555 | %Result = type opaque |
| 556 | %Qubit = type opaque |
| 557 | |
| 558 | define void @ENTRYPOINT__main() #0 { |
| 559 | block_0: |
| 560 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 561 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 562 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 563 | call void @__quantum__qis__m__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 564 | call void @__quantum__rt__array_record_output(i64 2, i8* null) |
| 565 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 566 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 567 | ret void |
| 568 | } |
| 569 | |
| 570 | declare void @__quantum__qis__x__body(%Qubit*) |
| 571 | |
| 572 | declare void @__quantum__rt__array_record_output(i64, i8*) |
| 573 | |
| 574 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 575 | |
| 576 | declare void @__quantum__qis__m__body(%Qubit*, %Result*) #1 |
| 577 | |
| 578 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 579 | attributes #1 = { "irreversible" } |
| 580 | |
| 581 | ; module flags |
| 582 | |
| 583 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 584 | |
| 585 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 586 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 587 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 588 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 589 | !4 = !{i32 1, !"classical_ints", i1 false} |
| 590 | !5 = !{i32 1, !"classical_floats", i1 false} |
| 591 | !6 = !{i32 1, !"backwards_branching", i1 false} |
| 592 | !7 = !{i32 1, !"qubit_resetting", i1 false} |
| 593 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 594 | !9 = !{i32 1, !"user_functions", i1 false} |
| 595 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 596 | "#]].assert_eq(&qir); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | mod adaptive_ri_profile { |
| 601 | |
| 602 | use expect_test::expect; |
| 603 | use qsc_data_structures::{language_features::LanguageFeatures, target::TargetCapabilityFlags}; |
| 604 | use qsc_frontend::compile::SourceMap; |
| 605 | |
| 606 | use crate::codegen::qir::get_qir; |
| 607 | |
| 608 | #[test] |
| 609 | fn simple() { |
| 610 | let source = "namespace Test { |
| 611 | import Std.Math.*; |
| 612 | open QIR.Intrinsic; |
| 613 | @EntryPoint() |
| 614 | operation Main() : Result { |
| 615 | use q = Qubit(); |
| 616 | let pi_over_two = 4.0 / 2.0; |
| 617 | __quantum__qis__rz__body(pi_over_two, q); |
| 618 | mutable some_angle = ArcSin(0.0); |
| 619 | __quantum__qis__rz__body(some_angle, q); |
| 620 | set some_angle = ArcCos(-1.0) / PI(); |
| 621 | __quantum__qis__rz__body(some_angle, q); |
| 622 | __quantum__qis__mresetz__body(q) |
| 623 | } |
| 624 | }"; |
| 625 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 626 | let language_features = LanguageFeatures::default(); |
| 627 | let capabilities = TargetCapabilityFlags::Adaptive |
| 628 | | TargetCapabilityFlags::QubitReset |
| 629 | | TargetCapabilityFlags::IntegerComputations; |
| 630 | |
| 631 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 632 | let qir = get_qir( |
| 633 | sources, |
| 634 | language_features, |
| 635 | capabilities, |
| 636 | store, |
| 637 | &[(std_id, None)], |
| 638 | ) |
| 639 | .expect("Failed to generate QIR"); |
| 640 | expect![[r#" |
| 641 | %Result = type opaque |
| 642 | %Qubit = type opaque |
| 643 | |
| 644 | define void @ENTRYPOINT__main() #0 { |
| 645 | block_0: |
| 646 | call void @__quantum__qis__rz__body(double 2.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 647 | call void @__quantum__qis__rz__body(double 0.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 648 | call void @__quantum__qis__rz__body(double 1.0, %Qubit* inttoptr (i64 0 to %Qubit*)) |
| 649 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 650 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 651 | ret void |
| 652 | } |
| 653 | |
| 654 | declare void @__quantum__qis__rz__body(double, %Qubit*) |
| 655 | |
| 656 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) #1 |
| 657 | |
| 658 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 659 | |
| 660 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="1" "required_num_results"="1" } |
| 661 | attributes #1 = { "irreversible" } |
| 662 | |
| 663 | ; module flags |
| 664 | |
| 665 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 666 | |
| 667 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 668 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 669 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 670 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 671 | !4 = !{i32 1, !"classical_ints", i1 true} |
| 672 | !5 = !{i32 1, !"qubit_resetting", i1 true} |
| 673 | !6 = !{i32 1, !"classical_floats", i1 false} |
| 674 | !7 = !{i32 1, !"backwards_branching", i1 false} |
| 675 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 676 | !9 = !{i32 1, !"user_functions", i1 false} |
| 677 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 678 | "#]] |
| 679 | .assert_eq(&qir); |
| 680 | } |
| 681 | |
| 682 | #[test] |
| 683 | fn qubit_reuse_allowed() { |
| 684 | let source = "namespace Test { |
| 685 | @EntryPoint() |
| 686 | operation Main() : (Result, Result) { |
| 687 | use q = Qubit(); |
| 688 | (MResetZ(q), MResetZ(q)) |
| 689 | } |
| 690 | }"; |
| 691 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 692 | let language_features = LanguageFeatures::default(); |
| 693 | let capabilities = TargetCapabilityFlags::Adaptive |
| 694 | | TargetCapabilityFlags::QubitReset |
| 695 | | TargetCapabilityFlags::IntegerComputations; |
| 696 | |
| 697 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 698 | let qir = get_qir( |
| 699 | sources, |
| 700 | language_features, |
| 701 | capabilities, |
| 702 | store, |
| 703 | &[(std_id, None)], |
| 704 | ) |
| 705 | .expect("Failed to generate QIR"); |
| 706 | expect![[r#" |
| 707 | %Result = type opaque |
| 708 | %Qubit = type opaque |
| 709 | |
| 710 | define void @ENTRYPOINT__main() #0 { |
| 711 | block_0: |
| 712 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 713 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 714 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 715 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 716 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 717 | ret void |
| 718 | } |
| 719 | |
| 720 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) #1 |
| 721 | |
| 722 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 723 | |
| 724 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 725 | |
| 726 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="1" "required_num_results"="2" } |
| 727 | attributes #1 = { "irreversible" } |
| 728 | |
| 729 | ; module flags |
| 730 | |
| 731 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 732 | |
| 733 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 734 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 735 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 736 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 737 | !4 = !{i32 1, !"classical_ints", i1 true} |
| 738 | !5 = !{i32 1, !"qubit_resetting", i1 true} |
| 739 | !6 = !{i32 1, !"classical_floats", i1 false} |
| 740 | !7 = !{i32 1, !"backwards_branching", i1 false} |
| 741 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 742 | !9 = !{i32 1, !"user_functions", i1 false} |
| 743 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 744 | "#]].assert_eq(&qir); |
| 745 | } |
| 746 | |
| 747 | #[test] |
| 748 | fn qubit_measurements_not_deferred() { |
| 749 | let source = "namespace Test { |
| 750 | @EntryPoint() |
| 751 | operation Main() : Result[] { |
| 752 | use (q0, q1) = (Qubit(), Qubit()); |
| 753 | X(q0); |
| 754 | let r0 = MResetZ(q0); |
| 755 | X(q1); |
| 756 | let r1 = MResetZ(q1); |
| 757 | [r0, r1] |
| 758 | } |
| 759 | }"; |
| 760 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 761 | let language_features = LanguageFeatures::default(); |
| 762 | let capabilities = TargetCapabilityFlags::Adaptive |
| 763 | | TargetCapabilityFlags::QubitReset |
| 764 | | TargetCapabilityFlags::IntegerComputations; |
| 765 | |
| 766 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 767 | let qir = get_qir( |
| 768 | sources, |
| 769 | language_features, |
| 770 | capabilities, |
| 771 | store, |
| 772 | &[(std_id, None)], |
| 773 | ) |
| 774 | .expect("Failed to generate QIR"); |
| 775 | expect![[r#" |
| 776 | %Result = type opaque |
| 777 | %Qubit = type opaque |
| 778 | |
| 779 | define void @ENTRYPOINT__main() #0 { |
| 780 | block_0: |
| 781 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 782 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 783 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 784 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 785 | call void @__quantum__rt__array_record_output(i64 2, i8* null) |
| 786 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 787 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 788 | ret void |
| 789 | } |
| 790 | |
| 791 | declare void @__quantum__qis__x__body(%Qubit*) |
| 792 | |
| 793 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) #1 |
| 794 | |
| 795 | declare void @__quantum__rt__array_record_output(i64, i8*) |
| 796 | |
| 797 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 798 | |
| 799 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 800 | attributes #1 = { "irreversible" } |
| 801 | |
| 802 | ; module flags |
| 803 | |
| 804 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 805 | |
| 806 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 807 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 808 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 809 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 810 | !4 = !{i32 1, !"classical_ints", i1 true} |
| 811 | !5 = !{i32 1, !"qubit_resetting", i1 true} |
| 812 | !6 = !{i32 1, !"classical_floats", i1 false} |
| 813 | !7 = !{i32 1, !"backwards_branching", i1 false} |
| 814 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 815 | !9 = !{i32 1, !"user_functions", i1 false} |
| 816 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 817 | "#]].assert_eq(&qir); |
| 818 | } |
| 819 | |
| 820 | #[test] |
| 821 | fn qubit_id_swap_results_in_different_id_usage() { |
| 822 | let source = "namespace Test { |
| 823 | @EntryPoint() |
| 824 | operation Main() : (Result, Result) { |
| 825 | use (q0, q1) = (Qubit(), Qubit()); |
| 826 | X(q0); |
| 827 | Relabel([q0, q1], [q1, q0]); |
| 828 | X(q1); |
| 829 | (MResetZ(q0), MResetZ(q1)) |
| 830 | } |
| 831 | }"; |
| 832 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 833 | let language_features = LanguageFeatures::default(); |
| 834 | let capabilities = TargetCapabilityFlags::Adaptive |
| 835 | | TargetCapabilityFlags::QubitReset |
| 836 | | TargetCapabilityFlags::IntegerComputations; |
| 837 | |
| 838 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 839 | let qir = get_qir( |
| 840 | sources, |
| 841 | language_features, |
| 842 | capabilities, |
| 843 | store, |
| 844 | &[(std_id, None)], |
| 845 | ) |
| 846 | .expect("Failed to generate QIR"); |
| 847 | expect![[r#" |
| 848 | %Result = type opaque |
| 849 | %Qubit = type opaque |
| 850 | |
| 851 | define void @ENTRYPOINT__main() #0 { |
| 852 | block_0: |
| 853 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 854 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 855 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 856 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 857 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 858 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 859 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 860 | ret void |
| 861 | } |
| 862 | |
| 863 | declare void @__quantum__qis__x__body(%Qubit*) |
| 864 | |
| 865 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) #1 |
| 866 | |
| 867 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 868 | |
| 869 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 870 | |
| 871 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 872 | attributes #1 = { "irreversible" } |
| 873 | |
| 874 | ; module flags |
| 875 | |
| 876 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 877 | |
| 878 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 879 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 880 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 881 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 882 | !4 = !{i32 1, !"classical_ints", i1 true} |
| 883 | !5 = !{i32 1, !"qubit_resetting", i1 true} |
| 884 | !6 = !{i32 1, !"classical_floats", i1 false} |
| 885 | !7 = !{i32 1, !"backwards_branching", i1 false} |
| 886 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 887 | !9 = !{i32 1, !"user_functions", i1 false} |
| 888 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 889 | "#]].assert_eq(&qir); |
| 890 | } |
| 891 | |
| 892 | #[test] |
| 893 | fn qubit_id_swap_across_reset_uses_updated_ids() { |
| 894 | let source = "namespace Test { |
| 895 | @EntryPoint() |
| 896 | operation Main() : (Result, Result) { |
| 897 | { |
| 898 | use (q0, q1) = (Qubit(), Qubit()); |
| 899 | X(q0); |
| 900 | Relabel([q0, q1], [q1, q0]); |
| 901 | X(q1); |
| 902 | Reset(q0); |
| 903 | Reset(q1); |
| 904 | } |
| 905 | use (q0, q1) = (Qubit(), Qubit()); |
| 906 | (MResetZ(q0), MResetZ(q1)) |
| 907 | } |
| 908 | }"; |
| 909 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 910 | let language_features = LanguageFeatures::default(); |
| 911 | let capabilities = TargetCapabilityFlags::Adaptive |
| 912 | | TargetCapabilityFlags::QubitReset |
| 913 | | TargetCapabilityFlags::IntegerComputations; |
| 914 | |
| 915 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 916 | let qir = get_qir( |
| 917 | sources, |
| 918 | language_features, |
| 919 | capabilities, |
| 920 | store, |
| 921 | &[(std_id, None)], |
| 922 | ) |
| 923 | .expect("Failed to generate QIR"); |
| 924 | expect![[r#" |
| 925 | %Result = type opaque |
| 926 | %Qubit = type opaque |
| 927 | |
| 928 | define void @ENTRYPOINT__main() #0 { |
| 929 | block_0: |
| 930 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 931 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 932 | call void @__quantum__qis__reset__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 933 | call void @__quantum__qis__reset__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 934 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 935 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 936 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 937 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 938 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 939 | ret void |
| 940 | } |
| 941 | |
| 942 | declare void @__quantum__qis__x__body(%Qubit*) |
| 943 | |
| 944 | declare void @__quantum__qis__reset__body(%Qubit*) |
| 945 | |
| 946 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) #1 |
| 947 | |
| 948 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 949 | |
| 950 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 951 | |
| 952 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="2" "required_num_results"="2" } |
| 953 | attributes #1 = { "irreversible" } |
| 954 | |
| 955 | ; module flags |
| 956 | |
| 957 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 958 | |
| 959 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 960 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 961 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 962 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 963 | !4 = !{i32 1, !"classical_ints", i1 true} |
| 964 | !5 = !{i32 1, !"qubit_resetting", i1 true} |
| 965 | !6 = !{i32 1, !"classical_floats", i1 false} |
| 966 | !7 = !{i32 1, !"backwards_branching", i1 false} |
| 967 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 968 | !9 = !{i32 1, !"user_functions", i1 false} |
| 969 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 970 | "#]].assert_eq(&qir); |
| 971 | } |
| 972 | |
| 973 | #[test] |
| 974 | fn qubit_id_swap_with_out_of_order_release_uses_correct_ids() { |
| 975 | let source = "namespace Test { |
| 976 | @EntryPoint() |
| 977 | operation Main() : (Result, Result) { |
| 978 | let q0 = QIR.Runtime.__quantum__rt__qubit_allocate(); |
| 979 | let q1 = QIR.Runtime.__quantum__rt__qubit_allocate(); |
| 980 | let q2 = QIR.Runtime.__quantum__rt__qubit_allocate(); |
| 981 | X(q0); |
| 982 | X(q1); |
| 983 | X(q2); |
| 984 | Relabel([q0, q1], [q1, q0]); |
| 985 | QIR.Runtime.__quantum__rt__qubit_release(q0); |
| 986 | let q3 = QIR.Runtime.__quantum__rt__qubit_allocate(); |
| 987 | X(q3); |
| 988 | (MResetZ(q3), MResetZ(q1)) |
| 989 | } |
| 990 | }"; |
| 991 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 992 | let language_features = LanguageFeatures::default(); |
| 993 | let capabilities = TargetCapabilityFlags::Adaptive |
| 994 | | TargetCapabilityFlags::QubitReset |
| 995 | | TargetCapabilityFlags::IntegerComputations; |
| 996 | |
| 997 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 998 | let qir = get_qir( |
| 999 | sources, |
| 1000 | language_features, |
| 1001 | capabilities, |
| 1002 | store, |
| 1003 | &[(std_id, None)], |
| 1004 | ) |
| 1005 | .expect("Failed to generate QIR"); |
| 1006 | expect![[r#" |
| 1007 | %Result = type opaque |
| 1008 | %Qubit = type opaque |
| 1009 | |
| 1010 | define void @ENTRYPOINT__main() #0 { |
| 1011 | block_0: |
| 1012 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 1013 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 1014 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 2 to %Qubit*)) |
| 1015 | call void @__quantum__qis__x__body(%Qubit* inttoptr (i64 1 to %Qubit*)) |
| 1016 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 1 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 1017 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 1 to %Result*)) |
| 1018 | call void @__quantum__rt__tuple_record_output(i64 2, i8* null) |
| 1019 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 0 to %Result*), i8* null) |
| 1020 | call void @__quantum__rt__result_record_output(%Result* inttoptr (i64 1 to %Result*), i8* null) |
| 1021 | ret void |
| 1022 | } |
| 1023 | |
| 1024 | declare void @__quantum__qis__x__body(%Qubit*) |
| 1025 | |
| 1026 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) #1 |
| 1027 | |
| 1028 | declare void @__quantum__rt__tuple_record_output(i64, i8*) |
| 1029 | |
| 1030 | declare void @__quantum__rt__result_record_output(%Result*, i8*) |
| 1031 | |
| 1032 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="3" "required_num_results"="2" } |
| 1033 | attributes #1 = { "irreversible" } |
| 1034 | |
| 1035 | ; module flags |
| 1036 | |
| 1037 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 1038 | |
| 1039 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 1040 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 1041 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 1042 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 1043 | !4 = !{i32 1, !"classical_ints", i1 true} |
| 1044 | !5 = !{i32 1, !"qubit_resetting", i1 true} |
| 1045 | !6 = !{i32 1, !"classical_floats", i1 false} |
| 1046 | !7 = !{i32 1, !"backwards_branching", i1 false} |
| 1047 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 1048 | !9 = !{i32 1, !"user_functions", i1 false} |
| 1049 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 1050 | "#]].assert_eq(&qir); |
| 1051 | } |
| 1052 | |
| 1053 | #[test] |
| 1054 | fn dynamic_integer_with_branch_and_phi_supported() { |
| 1055 | let source = "namespace Test { |
| 1056 | @EntryPoint() |
| 1057 | operation Main() : Int { |
| 1058 | use q = Qubit(); |
| 1059 | H(q); |
| 1060 | MResetZ(q) == Zero ? 0 | 1 |
| 1061 | } |
| 1062 | }"; |
| 1063 | let sources = SourceMap::new([("test.qs".into(), source.into())], None); |
| 1064 | let language_features = LanguageFeatures::default(); |
| 1065 | let capabilities = TargetCapabilityFlags::Adaptive |
| 1066 | | TargetCapabilityFlags::QubitReset |
| 1067 | | TargetCapabilityFlags::IntegerComputations; |
| 1068 | |
| 1069 | let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities); |
| 1070 | let qir = get_qir( |
| 1071 | sources, |
| 1072 | language_features, |
| 1073 | capabilities, |
| 1074 | store, |
| 1075 | &[(std_id, None)], |
| 1076 | ) |
| 1077 | .expect("Failed to generate QIR"); |
| 1078 | expect![[r#" |
| 1079 | %Result = type opaque |
| 1080 | %Qubit = type opaque |
| 1081 | |
| 1082 | define void @ENTRYPOINT__main() #0 { |
| 1083 | block_0: |
| 1084 | call void @__quantum__qis__h__body(%Qubit* inttoptr (i64 0 to %Qubit*)) |
| 1085 | call void @__quantum__qis__mresetz__body(%Qubit* inttoptr (i64 0 to %Qubit*), %Result* inttoptr (i64 0 to %Result*)) |
| 1086 | %var_0 = call i1 @__quantum__qis__read_result__body(%Result* inttoptr (i64 0 to %Result*)) |
| 1087 | %var_1 = icmp eq i1 %var_0, false |
| 1088 | br i1 %var_1, label %block_1, label %block_2 |
| 1089 | block_1: |
| 1090 | br label %block_3 |
| 1091 | block_2: |
| 1092 | br label %block_3 |
| 1093 | block_3: |
| 1094 | %var_3 = phi i64 [0, %block_1], [1, %block_2] |
| 1095 | call void @__quantum__rt__int_record_output(i64 %var_3, i8* null) |
| 1096 | ret void |
| 1097 | } |
| 1098 | |
| 1099 | declare void @__quantum__qis__h__body(%Qubit*) |
| 1100 | |
| 1101 | declare void @__quantum__qis__mresetz__body(%Qubit*, %Result*) #1 |
| 1102 | |
| 1103 | declare i1 @__quantum__qis__read_result__body(%Result*) |
| 1104 | |
| 1105 | declare void @__quantum__rt__int_record_output(i64, i8*) |
| 1106 | |
| 1107 | attributes #0 = { "entry_point" "output_labeling_schema" "qir_profiles"="adaptive_profile" "required_num_qubits"="1" "required_num_results"="1" } |
| 1108 | attributes #1 = { "irreversible" } |
| 1109 | |
| 1110 | ; module flags |
| 1111 | |
| 1112 | !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10} |
| 1113 | |
| 1114 | !0 = !{i32 1, !"qir_major_version", i32 1} |
| 1115 | !1 = !{i32 7, !"qir_minor_version", i32 0} |
| 1116 | !2 = !{i32 1, !"dynamic_qubit_management", i1 false} |
| 1117 | !3 = !{i32 1, !"dynamic_result_management", i1 false} |
| 1118 | !4 = !{i32 1, !"classical_ints", i1 true} |
| 1119 | !5 = !{i32 1, !"qubit_resetting", i1 true} |
| 1120 | !6 = !{i32 1, !"classical_floats", i1 false} |
| 1121 | !7 = !{i32 1, !"backwards_branching", i1 false} |
| 1122 | !8 = !{i32 1, !"classical_fixed_points", i1 false} |
| 1123 | !9 = !{i32 1, !"user_functions", i1 false} |
| 1124 | !10 = !{i32 1, !"multiple_target_branching", i1 false} |
| 1125 | "#]].assert_eq(&qir); |
| 1126 | } |
| 1127 | } |
| 1128 | |