microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
library/table_lookup/src/Tests.qs
84lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import Std.Diagnostics.*; |
| 5 | |
| 6 | import Main.*; |
| 7 | |
| 8 | internal operation MatchLookupToStd( |
| 9 | options : LookupOptions |
| 10 | ) : Unit { |
| 11 | let n = 3; |
| 12 | let width = 4; |
| 13 | let data = [[true, false, false, false], [false, true, false, false], [false, false, true, false], [false, false, false, false], [true, true, false, false], [false, true, true, false], [true, false, true, true], [true, true, true, true]]; |
| 14 | |
| 15 | // Use adjoint Std.TableLookup.Select because this check takes adjoint of that. |
| 16 | let equal = CheckOperationsAreEqual( |
| 17 | n + width, |
| 18 | qs => Lookup(options, data, qs[0..n-1], qs[n...]), |
| 19 | qs => Adjoint Std.TableLookup.Select(data, qs[0..n-1], qs[n...]) |
| 20 | ); |
| 21 | Fact(equal, "Lookup should match Std.TableLookup.Select."); |
| 22 | } |
| 23 | |
| 24 | internal operation MatchControlledLookupToMCX( |
| 25 | options : LookupOptions |
| 26 | ) : Unit { |
| 27 | let n = 2; |
| 28 | let width = 3; |
| 29 | let data = [[true, false, false], [false, true, false], [false, false, true], [true, true, true]]; |
| 30 | |
| 31 | |
| 32 | // CheckOperationsAreEqual uses adjoint variant of the reference operation (seond operation). |
| 33 | // Select from the standard library uses assumptions that the target is in zero state, |
| 34 | // so its adjoint always returns target to zero state. So it won't work for CheckOperationsAreEqual directly. |
| 35 | // Instead, we compare controlled Select to controlled LookupViaMCX, which works in all cases. |
| 36 | let equal = CheckOperationsAreEqual( |
| 37 | 1 + n + width, |
| 38 | qs => Controlled Lookup( |
| 39 | [qs[0]], |
| 40 | (options, data, qs[1..n], qs[n + 1...]) |
| 41 | ), |
| 42 | qs => Controlled LookupViaMCX( |
| 43 | [qs[0]], |
| 44 | (data, qs[1..n], qs[n + 1...]) |
| 45 | ) |
| 46 | ); |
| 47 | Fact(equal, "Controlled Lookup should match controlled LookupViaMCX."); |
| 48 | } |
| 49 | |
| 50 | internal operation TestOnAllAlgorithms(op : LookupOptions => Unit) : Unit { |
| 51 | let algorithms = [ |
| 52 | DoStdLookup(), |
| 53 | DoMCXLookup(), |
| 54 | DoRecursiveSelectLookup(), |
| 55 | DoPPLookup(), |
| 56 | DoSplitPPLookup() |
| 57 | ]; |
| 58 | for algorithm in algorithms { |
| 59 | let options = new LookupOptions { |
| 60 | lookupAlgorithm = algorithm, |
| 61 | unlookupAlgorithm = DoUnlookupViaLookup(), |
| 62 | failOnLongData = false, |
| 63 | failOnShortData = false, |
| 64 | respectExcessiveAddress = false, |
| 65 | preferMeasurementBasedUncomputation = true, |
| 66 | }; |
| 67 | op(options); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Test() |
| 72 | operation TestDefaultLookupMatchesStd() : Unit { |
| 73 | MatchLookupToStd(DefaultLookupOptions()); |
| 74 | } |
| 75 | |
| 76 | @Test() |
| 77 | operation TestLookupMatchesStd() : Unit { |
| 78 | TestOnAllAlgorithms(MatchLookupToStd); |
| 79 | } |
| 80 | |
| 81 | @Test() |
| 82 | operation TestControlledLookupMatchesMCX() : Unit { |
| 83 | TestOnAllAlgorithms(MatchControlledLookupToMCX); |
| 84 | } |
| 85 | |