microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/algorithms/HypercubeLookup/src/Data.qs
81lines · modecode
| 1 | // Hypercube vertex data |
| 2 | |
| 3 | import ClassicalSearch.GetIndexBelowThresholdClassically; |
| 4 | import Std.Diagnostics.*; |
| 5 | import Std.Math.BitSizeI; |
| 6 | |
| 7 | /// # Summary |
| 8 | /// Selected hypercube vertices represented as coordinate bit strings. |
| 9 | function HypercubeVertices() : Bool[][] { |
| 10 | [ |
| 11 | [false, false, false, false, false, false], // 0: d = 6 |
| 12 | [false, true, false, false, false, false], // 1: d = 5 |
| 13 | [false, true, true, true, false, false], // 2: d = 3 |
| 14 | [true, false, true, false, false, false], // 3: d = 4 |
| 15 | [true, true, false, true, false, false], // 4: d = 3 |
| 16 | [false, false, true, true, false, false], // 5: d = 4 |
| 17 | [true, false, true, true, true, false], // 6: d = 2 |
| 18 | [true, true, false, false, false, true], // 7: d = 3 |
| 19 | ] |
| 20 | } |
| 21 | |
| 22 | /// # Summary |
| 23 | /// The query vertex represented as a coordinate bit string. |
| 24 | /// We are searching for a selected vertex closest to this one. |
| 25 | function QueryVertex() : Bool[] { |
| 26 | [true, true, true, true, true, true] |
| 27 | } |
| 28 | |
| 29 | /// # Summary |
| 30 | /// The number of dimensions of the hypercube. |
| 31 | /// Also verifies the assumptions about the data. |
| 32 | function HypercubeDimentions() : Int { |
| 33 | Length(QueryVertex()) |
| 34 | } |
| 35 | |
| 36 | /// # Summary |
| 37 | /// The number of selected vertices of the hypercube. |
| 38 | function TableLength() : Int { |
| 39 | Length(HypercubeVertices()) |
| 40 | } |
| 41 | |
| 42 | /// # Summary |
| 43 | /// We are finding a vertex under this threshold. |
| 44 | /// Only one vertex should satisfy this condition. |
| 45 | function DistanceThreshold() : Int { |
| 46 | 3 |
| 47 | } |
| 48 | |
| 49 | /// # Summary |
| 50 | /// The maximum possible distance in the hypercube. |
| 51 | function MaxDistance() : Int { |
| 52 | HypercubeDimentions() |
| 53 | } |
| 54 | |
| 55 | /// # Summary |
| 56 | /// The number of bits needed to represent the maximum distance. |
| 57 | function MaxDistanceBits() : Int { |
| 58 | BitSizeI(MaxDistance()) |
| 59 | } |
| 60 | |
| 61 | /// # Summary |
| 62 | /// The number of bits needed to address all selected vertices. |
| 63 | /// Also veifies all assumptions about the table data. |
| 64 | function TableAddressBits() : Int { |
| 65 | let dimensions = Length(QueryVertex()); |
| 66 | for vertex in HypercubeVertices() { |
| 67 | Fact(Length(vertex) == dimensions, "All vertices must have the same number of dimensions as the query vertex."); |
| 68 | } |
| 69 | |
| 70 | let tableSize = TableLength(); |
| 71 | Fact((tableSize &&& (tableSize - 1)) == 0, "Table length needs to be a power of two. Otherwise, respectExcessiveAddress option must be true."); |
| 72 | |
| 73 | let index = GetIndexBelowThresholdClassically( |
| 74 | HypercubeVertices(), |
| 75 | QueryVertex(), |
| 76 | DistanceThreshold() |
| 77 | ); |
| 78 | Fact(index >= 0 and index < TableLength(), "One table index must satisfy the distance threshold condition."); |
| 79 | |
| 80 | BitSizeI(TableLength() - 1) |
| 81 | } |
| 82 | |