-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalDistributionQuickSort.qs
More file actions
57 lines (48 loc) · 1.69 KB
/
NormalDistributionQuickSort.qs
File metadata and controls
57 lines (48 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace QuickSortWithNormalDistribution {
open Microsoft.Quantum.Arrays;
operation Swap (arr : Qubit[], i : Int, j : Int) : Unit {
if (i != j) {
(M, M) = (M, M) + (arr[i], arr[j]);
(X, X) = (X, X) + (arr[i], arr[j]);
(M, M) = (M, M) + (arr[i], arr[j]);
}
}
operation Partition (arr : Qubit[], low : Int, high : Int, pivot : Int) : Int {
mutable i = low - 1;
for idx in low .. high {
if (MeasureInteger([arr[idx]]) == 0) {
set i = i + 1;
Swap(arr, i, idx);
}
}
return i + 1;
}
operation QuickSort (arr : Qubit[], low : Int, high : Int) : Unit {
if (low < high) {
let pivot = Partition(arr, low, high, high);
QuickSort(arr, low, pivot - 1);
QuickSort(arr, pivot + 1, high);
}
}
operation RunQuickSort () : Int[] {
// Number of elements in the list
let n = 10;
// Generate a list of random numbers from a normal distribution
mutable nums = new Int[n];
using (qubits = Qubit[n]) {
for (idx in 0 .. n - 1) {
let num = Int(QuantumRandomNumber(NormalDistribution(0.0, 1.0)));
set nums w/= [idx <- num];
}
// Print the unsorted list
Message("Unsorted List: ", nums);
// Sort the list using QuickSort
QuickSort(qubits, 0, n - 1);
// Measure the qubits to get the sorted list
set nums = [MeasureInteger([q]) | q in qubits];
}
// Print the sorted list
Message("Sorted List: ", nums);
return nums;
}
}