microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/testing/classical_values/src/TestCode.qs
27lines · modecode
| 1 | import Std.Diagnostics.Fact; |
| 2 | import Std.Arrays.*; |
| 3 | import Std.Random.*; |
| 4 | import ClassicalFunction.Square; |
| 5 | import Measurement.MeasureBasisState; |
| 6 | |
| 7 | /// # Summary |
| 8 | /// Test code that verifies the classical values returned by the rest of the code. |
| 9 | /// Throw exceptions if the test fails. |
| 10 | |
| 11 | function TestSquare() : Unit { |
| 12 | for i in -10..10 { |
| 13 | let (actual, expected) = (Square(i), i * i); |
| 14 | Fact(actual == expected, $"Incorrect function value for {i}: expected {expected}, got {actual}"); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | operation TestMeasurement() : Unit { |
| 19 | for _ in 1..10 { |
| 20 | let n = DrawRandomInt(2, 10); |
| 21 | let bits = ForEach(x => DrawRandomBool(0.5), [0, size = n]); |
| 22 | let res = MeasureBasisState(bits); |
| 23 | for (bit, resBit) in Zipped(bits, res) { |
| 24 | Fact(bit == (resBit == One), $"Incorrect measurement result for {bit}"); |
| 25 | } |
| 26 | } |
| 27 | } |