/
Grover.cs
161 lines (131 loc) · 3.62 KB
/
Grover.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Quantum;
using Quantum.Operations;
using System;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;
namespace QuantumConsole
{
public static class GroverExtension
{
// Oracle is working on input register x (of width n)
// and output register y (of width 1)
public static void Oracle (this QuantumComputer comp, int target, Register x, Register y)
{
var controlBits = new RegisterRef[x.Width];
for (int i = 0; i < x.Width; i++)
{
if ((target & (1 << i)) == 0)
{
x.SigmaX(i);
}
controlBits[i] = x[i];
}
comp.Toffoli(y[0], controlBits); // Toffoli(<target_bit>, ... <control_bits> ...)
for (int i = 0; i < x.Width; i++)
{
if ((target & (1 << i)) == 0)
{
x.SigmaX(i);
}
}
}
public static void InverseOracle (this QuantumComputer comp, int target, Register x, Register y)
{
comp.Oracle(target, x, y);
}
// Inversion is working on n-width register
public static void Inversion (this QuantumComputer comp, Register x)
{
for (int i = 0; i < x.Width; i++)
{
x.Hadamard(i);
x.SigmaX(i);
}
x.Hadamard(0);
if(x.Width == 2)
{
x.CNot(target: 0, control: 1);
}
else
{
int[] controlBits = Enumerable.Range(1, x.Width - 1).ToArray();
x.Toffoli(0, controlBits); // Toffoli(<target_bit>, ... <control_bits> ...)
}
x.Hadamard(0);
for (int i = 0; i < x.Width; i++)
{
x.SigmaX(i);
x.Hadamard(i);
}
}
public static void InverseInversion (this QuantumComputer comp, Register x)
{
comp.Inversion(x);
}
// single iteration of Grover's algorithm
public static void Grover (this QuantumComputer comp, int target, Register x, Register y)
{
comp.Oracle(target, x, y);
comp.Inversion(x);
}
public static void InverseGrover (this QuantumComputer comp, int target, Register x, Register y)
{
comp.Inversion(x);
comp.Oracle(target, x, y);
}
}
public class QuantumTest
{
public static void Main()
{
int number = 5;
int width = 4; // width of search space
double iterations = Math.PI/4 * Math.Sqrt(1 << width);
QuantumComputer comp = QuantumComputer.GetInstance();
// input register set to 0
Register x = comp.NewRegister(0, width);
// output 1-qubit-register, set to 1
Register y = comp.NewRegister(1, 1);
comp.Walsh(x);
comp.Hadamard(y[0]);
Console.WriteLine("Iterations needed: PI/4 * Sqrt(2^n) = {0}", iterations);
var probs = x.GetProbabilities();
for (ulong i = 0; i < Math.Pow(2, width); i++)
{
Console.Write(i + ",");
}
Console.WriteLine();
for (int i = 1; i <= iterations; i++)
{
// Console.WriteLine("Iteration #{0}", i);
comp.Oracle(number, x, y);
//these Hadamard gates are only for viewing purposes !
y.Hadamard(0);
// here we can view amplitudes in simulator
// Console.WriteLine("After Oracle:");
var amplitudes = x.GetAmplitudes();
for (ulong j = 0; j < Math.Pow(2, width); j++)
{
Console.Write(amplitudes[j].Real.ToString() + ",");
}
Console.WriteLine();
//going back to the algorithm
y.Hadamard(0);
comp.Inversion(x);
//these Hadamard gates are only for viewing purposes !
y.Hadamard(0);
// here we can view amplitudes in simulator
// Console.WriteLine("After Inverse:");
amplitudes = x.GetAmplitudes();
for (ulong j = 0; j < Math.Pow(2, width); j++)
{
Console.Write(amplitudes[j].Real.ToString() + ",");
}
Console.WriteLine();
//going back to the algorithm
y.Hadamard(0);
}
}
}
}