microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
library/rotations/src/Tests.qs
47lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import Std.Diagnostics.CheckOperationsAreEqual, Std.Diagnostics.Fact; |
| 5 | import Std.Math.HammingWeightI, Std.Math.PI; |
| 6 | |
| 7 | import HammingWeightPhasing.HammingWeightPhasing, HammingWeightPhasing.WithHammingWeight; |
| 8 | |
| 9 | @Test() |
| 10 | operation TestHammingWeight() : Unit { |
| 11 | // exhaustive |
| 12 | use qs = Qubit[4]; |
| 13 | |
| 14 | for x in 0..2^Length(qs) - 1 { |
| 15 | ApplyXorInPlace(x, qs); |
| 16 | WithHammingWeight(qs, sum => { |
| 17 | Fact(MeasureInteger(sum) == HammingWeightI(x), $"wrong Hamming weight computed for x = {x}"); |
| 18 | }); |
| 19 | ResetAll(qs); |
| 20 | } |
| 21 | |
| 22 | // some explicit cases |
| 23 | for (width, number) in [ |
| 24 | (1, 1), |
| 25 | (2, 0), |
| 26 | (2, 3), |
| 27 | (8, 10), |
| 28 | (7, 99) |
| 29 | ] { |
| 30 | use qs = Qubit[width]; |
| 31 | |
| 32 | ApplyXorInPlace(number, qs); |
| 33 | WithHammingWeight(qs, sum => { |
| 34 | Fact(MeasureInteger(sum) == HammingWeightI(number), $"wrong Hamming weight computed for number = {number}"); |
| 35 | }); |
| 36 | ResetAll(qs); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | @Test() |
| 41 | operation TestPhasing() : Unit { |
| 42 | for theta in [1.0, 2.0, 0.0, -0.5, 5.0 * PI()] { |
| 43 | for numQubits in 1..6 { |
| 44 | Fact(CheckOperationsAreEqual(numQubits, qs => HammingWeightPhasing(theta, qs), qs => ApplyToEachA(Rz(theta, _), qs)), "Operations are not equal"); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |