microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dbwy/random_seed

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/algorithms/HypercubeLookup/src/ClassicalSearch.qs

39lines · modecode

1import Std.Diagnostics.*;
2import Std.Arrays.*;
3
4/// # Summary
5/// Classically finds the index of a vertex in the list of vertices
6/// whose distance to the query vertex is below the given threshold.
7function GetIndexBelowThresholdClassically(
8 vertices : Bool[][],
9 query : Bool[],
10 threshold : Int
11) : Int {
12 Fact(not IsEmpty(vertices), "Vertex list must not be empty.");
13 mutable foundIndex = -1;
14 for idx in IndexRange(vertices) {
15 let vertex = vertices[idx];
16 let distance = HammingDistance(vertex, query);
17 if distance < threshold {
18 Fact(foundIndex == -1, "More than one vertex found below the threshold.");
19 set foundIndex = idx;
20 }
21 }
22 Fact(foundIndex != -1, "No vertex found below the threshold.");
23 return foundIndex;
24}
25
26/// # Summary
27/// Computes the Hamming distance between two bit strings.
28/// This serves as a multidimensional Manhattan distance (L₁ distance)
29/// for vertex coordinates represented as bit strings.
30function HammingDistance(a : Bool[], b : Bool[]) : Int {
31 Fact(Length(a) == Length(b), "Arrays must be of the same length.");
32 mutable distance = 0;
33 for i in IndexRange(a) {
34 if a[i] != b[i] {
35 distance += 1;
36 }
37 }
38 return distance;
39}
40