This repository was archived by the owner on Apr 1, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +42
-17
lines changed
Expand file tree Collapse file tree 3 files changed +42
-17
lines changed Original file line number Diff line number Diff line change 1+ #include < arrayfire.h>
2+ #include < random>
3+
4+ int select (af::array weights) {
5+ // Assumes sum of the weights are 1
6+
7+ std::random_device rd;
8+ std::mt19937 gen (rd ());
9+ std::uniform_real_distribution<> dis (0.0 , 1.0 );
10+ float key = dis (gen);
11+
12+ int index = 0 ;
13+ for (; index < weights.dims (0 ); index++) {
14+ key = key - weights (index).scalar <float >();
15+
16+ if (key <= 0 ) {
17+ return index;
18+ }
19+ }
20+
21+ return index;
22+ }
Original file line number Diff line number Diff line change 1+ #ifndef HELPER_H_INCLUDED
2+ #define HELPER_H_INCLUDED
3+ #include <arrayfire.h>
4+
5+ int select (af ::array weights );
6+
7+ #endif
Original file line number Diff line number Diff line change 22
33#include " kron.h"
44#include " gates.h"
5+ #include " helper.h"
56
67using namespace af ;
78
89class QuantumRegister {
910 private:
1011 array amplitudes;
12+ int numQubits;
1113 bool measured = false ;
1214 public:
13- QuantumRegister (int numQubits) {
15+ QuantumRegister (int n) {
16+ numQubits = n;
1417 // The number of amplitudes needed is 2^n = 2 << (n - 1),
1518 // Where N is the number of qubits. The constant(0, n) function
1619 // Creates an array of n 0s
17- amplitudes = constant (0 , 2 << (numQubits - 1 ), c32);
20+ amplitudes = constant (0 , 2 << (n - 1 ), c32);
1821 // Set the probability of getting all zeros when measured to 1
1922 amplitudes (0 ) = 1 ;
2023 }
@@ -23,28 +26,21 @@ class QuantumRegister {
2326 amplitudes = matmul (gate, amplitudes);
2427 }
2528
26- array measure () {
29+ int measure () {
2730 array probabilities = pow (abs (amplitudes), 2 );
28- af_print (probabilities);
2931
30- return probabilities;
32+ return select ( probabilities) ;
3133 }
3234};
3335
34- void run () {
35- QuantumRegister q (20 );
36- // q.applyGate(Gates::H);
37-
38- q.measure ();
39-
40- return ;
41- }
42-
4336int main (int argc, char **argv) {
4437 // af::info();
45- // QuantumRegister q(2);
46- // q.measure();
47- af_print (kron (Gates::X, Gates::Id));
38+ int count = 0 ;
39+
40+ QuantumRegister q (1 );
41+ q.applyGate (Gates::H);
42+ count += q.measure ();
43+
4844
4945 return 0 ;
5046}
You can’t perform that action at this time.
0 commit comments