Skip to content

Commit 66f00a6

Browse files
author
Hau Nguyen
committed
Add new algorithm
1 parent 5df0fbe commit 66f00a6

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

graphs/dijkstra.qs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace QuantumDijkstra {
2+
open Microsoft.Quantum.Diagnostics;
3+
4+
operation Graph(): (Int, Int, Int, Int)[] {
5+
return [ (0, 1, 4, 0), (0, 2, 1, 0), (1, 2, 2, 0), (1, 3, 5, 0), (2, 3, 1, 0) ];
6+
}
7+
8+
operation FindMinimumDistanceNode(distances : Qubit[], n : Int) : Int {
9+
mutable minIndex = 0;
10+
mutable minValue = 1000;
11+
12+
for (i in 0 .. n - 1) {
13+
let value = M(distances[i]);
14+
if (value > 0 && value < minValue) {
15+
set minIndex = i;
16+
set minValue = value;
17+
}
18+
}
19+
return minIndex;
20+
}
21+
22+
operation Dijkstra() : Int[] {
23+
let graph = Graph();
24+
let n = 4;
25+
26+
mutable distances = new Qubit[n];
27+
for (i in 0 .. n - 1) {
28+
set distances w/= i <- Microsoft.Quantum.Convert.IntAsBool(i == 0);
29+
}
30+
31+
32+
for (i in 0 .. n - 1) {
33+
let minIndex = FindMinimumDistanceNode(distances, n);
34+
Message($"Visiting Node {minIndex}");
35+
36+
for (edge in graph) {
37+
if (edge[0] == minIndex) {
38+
let neighbor = edge[1];
39+
let weight = edge[2];
40+
mutable currentDistance = distances[neighbor];
41+
if (M(currentDistance) == 0) {
42+
set distances w/= neighbor <- Microsoft.Quantum.Convert.IntAsBool(NotBool(M(currentDistance)) or weight < M(distances[minIndex]) + weight);
43+
}
44+
}
45+
}
46+
set distances[minIndex] = One;
47+
}
48+
49+
mutable result = new Int[n];
50+
for (i in 0 .. n - 1) {
51+
set result w/= i <- M(distances[i]);
52+
Reset(distances[i]);
53+
}
54+
55+
return result;
56+
}
57+
58+
entrypoint() : Unit {
59+
let shortestDistances = Dijkstra();
60+
Message($"Shortest distances: {shortestDistances}");
61+
}
62+
}

search/BinarySearch.qs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace MyNamespace {
2+
open Microsoft.Quantum.Canon;
3+
open Microsoft.Quantum.Intrinsic;
4+
5+
operation BinarySearch (sortedArray : Int[], target : Int) : Int {
6+
mutable left = 0;
7+
mutable right = Length(sortedArray) - 1;
8+
9+
while (left <= right) {
10+
let mid = (left + right) / 2;
11+
let midValue = sortedArray[mid];
12+
13+
if (midValue == target) {
14+
return mid;
15+
} elif (midValue < target) {
16+
set left = mid + 1;
17+
} else {
18+
set right = mid - 1;
19+
}
20+
}
21+
return -1;
22+
}
23+
}

search/TreeTraversal.qs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace TreeTraversal {
2+
open Microsoft.Quantum.Intrinsic;
3+
newtype TreeNode = (Int, TreeNode?, TreeNode?);
4+
operation DepthFirstTraversal(root : TreeNode) : Unit {
5+
BodyTraverse(root);
6+
}
7+
operation BodyTraverse(node : TreeNode) : Unit {
8+
Message($"Visited Node: {node[0]}");
9+
if (not IsResultZero(M, node[1])) {
10+
BodyTraverse(M!Borrow(node[1]));
11+
}
12+
if (not IsResultZero(M, node[2])) {
13+
BodyTraverse(M!Borrow(node[2]));
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)