microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/resource_estimator/src/counts/tests.rs
238lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use std::convert::Into; |
| 5 | |
| 6 | use expect_test::{Expect, expect}; |
| 7 | use indoc::indoc; |
| 8 | use qsc::{ |
| 9 | LanguageFeatures, PackageType, SourceMap, TargetCapabilityFlags, |
| 10 | interpret::{GenericReceiver, Interpreter}, |
| 11 | target::Profile, |
| 12 | }; |
| 13 | |
| 14 | use super::LogicalCounter; |
| 15 | |
| 16 | fn verify_logical_counts(source: &str, entry: Option<&str>, expect: &Expect) { |
| 17 | let source_map = SourceMap::new([("test".into(), source.into())], entry.map(Into::into)); |
| 18 | let (std_id, store) = qsc::compile::package_store_with_stdlib(TargetCapabilityFlags::all()); |
| 19 | let mut interpreter = Interpreter::new( |
| 20 | source_map, |
| 21 | PackageType::Exe, |
| 22 | Profile::Unrestricted.into(), |
| 23 | LanguageFeatures::default(), |
| 24 | store, |
| 25 | &[(std_id, None)], |
| 26 | ) |
| 27 | .expect("compilation should succeed"); |
| 28 | let mut counter = LogicalCounter::default(); |
| 29 | let mut stdout = std::io::sink(); |
| 30 | let mut out = GenericReceiver::new(&mut stdout); |
| 31 | interpreter |
| 32 | .eval_entry_with_sim(&mut counter, &mut out) |
| 33 | .expect("evaluation should succeed"); |
| 34 | expect.assert_debug_eq(&counter.logical_resources()); |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn gates_are_counted() { |
| 39 | verify_logical_counts( |
| 40 | indoc! {" |
| 41 | namespace Test { |
| 42 | operation Rotate(qs: Qubit[]) : Unit { |
| 43 | for q in qs { |
| 44 | Rx(1.0, q); |
| 45 | Ry(1.0, q); |
| 46 | Rz(1.0, q); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | @EntryPoint() |
| 51 | operation Main() : Result[] { |
| 52 | use qs = Qubit[10]; |
| 53 | within { |
| 54 | T(qs[0]); |
| 55 | CCNOT(qs[0], qs[1], qs[2]); |
| 56 | } |
| 57 | apply { |
| 58 | Rotate(qs); |
| 59 | } |
| 60 | MResetEachZ(qs) |
| 61 | } |
| 62 | } |
| 63 | "}, |
| 64 | None, |
| 65 | &expect![[" |
| 66 | LogicalResourceCounts { |
| 67 | num_qubits: 10, |
| 68 | t_count: 2, |
| 69 | rotation_count: 30, |
| 70 | rotation_depth: 5, |
| 71 | ccz_count: 2, |
| 72 | ccix_count: 0, |
| 73 | measurement_count: 10, |
| 74 | } |
| 75 | "]], |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn estimate_caching_works() { |
| 81 | verify_logical_counts( |
| 82 | indoc! {r#" |
| 83 | namespace Test { |
| 84 | import Std.ResourceEstimation.*; |
| 85 | |
| 86 | operation Rotate(qs: Qubit[]) : Unit { |
| 87 | for q in qs { |
| 88 | Rx(1.0, q); |
| 89 | Ry(1.0, q); |
| 90 | Rz(1.0, q); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @EntryPoint() |
| 95 | operation Main() : Unit { |
| 96 | use qs = Qubit[10]; |
| 97 | mutable count = 0; |
| 98 | for _ in 1..10 { |
| 99 | if BeginEstimateCaching("Rotate", SingleVariant()) { |
| 100 | Rotate(qs); |
| 101 | set count += 1; |
| 102 | EndEstimateCaching(); |
| 103 | } |
| 104 | } |
| 105 | for _ in 1..count { |
| 106 | T(qs[0]); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | "#}, |
| 111 | None, |
| 112 | &expect![[" |
| 113 | LogicalResourceCounts { |
| 114 | num_qubits: 10, |
| 115 | t_count: 1, |
| 116 | rotation_count: 300, |
| 117 | rotation_depth: 30, |
| 118 | ccz_count: 0, |
| 119 | ccix_count: 0, |
| 120 | measurement_count: 0, |
| 121 | } |
| 122 | "]], |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn estimate_repeat_works() { |
| 128 | verify_logical_counts( |
| 129 | indoc! {r#" |
| 130 | namespace Test { |
| 131 | import Std.ResourceEstimation.*; |
| 132 | |
| 133 | operation Rotate(qs: Qubit[]) : Unit { |
| 134 | for q in qs { |
| 135 | Rx(1.0, q); |
| 136 | Ry(1.0, q); |
| 137 | Rz(1.0, q); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @EntryPoint() |
| 142 | operation Main() : Unit { |
| 143 | use qs = Qubit[10]; |
| 144 | mutable count = 0; |
| 145 | within { |
| 146 | RepeatEstimates(10); |
| 147 | } |
| 148 | apply { |
| 149 | Rotate(qs); |
| 150 | set count += 1; |
| 151 | } |
| 152 | for _ in 1..count { |
| 153 | T(qs[0]); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | "#}, |
| 158 | None, |
| 159 | &expect![[r#" |
| 160 | LogicalResourceCounts { |
| 161 | num_qubits: 10, |
| 162 | t_count: 1, |
| 163 | rotation_count: 300, |
| 164 | rotation_depth: 30, |
| 165 | ccz_count: 0, |
| 166 | ccix_count: 0, |
| 167 | measurement_count: 0, |
| 168 | } |
| 169 | "#]], |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn account_for_estimates_works() { |
| 175 | verify_logical_counts( |
| 176 | indoc! {" |
| 177 | namespace Test { |
| 178 | import Std.ResourceEstimation.*; |
| 179 | |
| 180 | @EntryPoint() |
| 181 | operation Main() : Unit { |
| 182 | use qs = Qubit[10]; |
| 183 | AccountForEstimates( |
| 184 | [ |
| 185 | AuxQubitCount(1), |
| 186 | TCount(2), |
| 187 | RotationCount(3), |
| 188 | RotationDepth(4), |
| 189 | CczCount(5), |
| 190 | MeasurementCount(6), |
| 191 | ], |
| 192 | PSSPCLayout(), |
| 193 | qs); |
| 194 | } |
| 195 | } |
| 196 | "}, |
| 197 | None, |
| 198 | &expect![[" |
| 199 | LogicalResourceCounts { |
| 200 | num_qubits: 11, |
| 201 | t_count: 2, |
| 202 | rotation_count: 3, |
| 203 | rotation_depth: 1, |
| 204 | ccz_count: 5, |
| 205 | ccix_count: 0, |
| 206 | measurement_count: 6, |
| 207 | } |
| 208 | "]], |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn pauli_i_rotation_for_global_phase_is_noop() { |
| 214 | verify_logical_counts( |
| 215 | indoc! {" |
| 216 | namespace Test { |
| 217 | @EntryPoint() |
| 218 | operation Main() : Unit { |
| 219 | use q = Qubit(); |
| 220 | T(q); |
| 221 | R(PauliI, 1.0, q); |
| 222 | } |
| 223 | } |
| 224 | "}, |
| 225 | None, |
| 226 | &expect![[r#" |
| 227 | LogicalResourceCounts { |
| 228 | num_qubits: 1, |
| 229 | t_count: 1, |
| 230 | rotation_count: 0, |
| 231 | rotation_depth: 0, |
| 232 | ccz_count: 0, |
| 233 | ccix_count: 0, |
| 234 | measurement_count: 0, |
| 235 | } |
| 236 | "#]], |
| 237 | ); |
| 238 | } |