Skip to content
This repository was archived by the owner on Apr 1, 2018. It is now read-only.

Commit 3520801

Browse files
committed
Add Kronecker Product And Gates
1 parent 1e2bcb1 commit 3520801

File tree

2 files changed

+65
-27
lines changed

2 files changed

+65
-27
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ different projects created for CPU, CUDA and OpenCL. Setting one of these
4343
projects as start project and then running it will run the example with the
4444
specified backend.
4545

46+
## Notes
47+
48+
The minimum filling time of a matrix with (n_1 * n_2) X (m_1 * m_2) is O(n_1 * n_2 * m_1 * m_2).
49+
This is relevent to the kronecker product.
50+
Some other considerations for the future development of this software, and the speedup of this function is:
51+
52+
* Parallelize
53+
* Store on the fly (i.e. store M as kron of A and B, and calculate only when single element is needed)
54+
* Could Use ArrayFire Tile Function
4655

4756
## License
4857

src/main.cpp

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,60 @@
1+
#include <stdio.h>
12
#include <arrayfire.h>
2-
#include <cstdio>
3-
#include <cstdlib>
4-
5-
int main(int argc, char *argv[])
6-
{
7-
try {
8-
// Select a device and display arrayfire info
9-
int device = argc > 1 ? atoi(argv[1]) : 0;
10-
af::setDevice(device);
11-
af::info();
12-
printf("Running QR InPlace\n");
13-
af::array in = af::randu(5, 8);
14-
af_print(in);
15-
af::array qin = in.copy();
16-
af::array tau;
17-
qrInPlace(tau, qin);
18-
af_print(qin);
19-
af_print(tau);
20-
printf("Running QR with Q and R factorization\n");
21-
af::array q, r;
22-
qr(q, r, tau, in);
23-
af_print(q);
24-
af_print(r);
25-
af_print(tau);
26-
} catch (af::exception& e) {
27-
fprintf(stderr, "%s\n", e.what());
28-
throw;
3+
#include <complex>
4+
using namespace af;
5+
6+
namespace Gates {
7+
8+
// Pauli-X / Not Gate
9+
float X_coef[] = {
10+
0, 0, 1, 0,
11+
1, 0, 0, 0
12+
};
13+
14+
static array X = array(2, 2, (cfloat*) X_coef);
15+
16+
// Pauli-Y Gate
17+
float Y_coef[] = {
18+
0, 0, 0, -1,
19+
0, 1, 0, 0
20+
};
21+
22+
static array Y = array(2, 2, (cfloat*) Y_coef);
23+
24+
25+
// Pauli-Z Gate
26+
float Z_coef[] = {
27+
0, 0, 1, 0,
28+
1, 0, 0, 0
29+
};
30+
31+
static array Z = array(2, 2, (cfloat*) Z_coef);
32+
}
33+
34+
// TODO: GPU ACCELERATE THIS FUNCTION
35+
array kron(array A, array B) {
36+
int m = A.dims(0);
37+
int n = A.dims(1);
38+
int p = B.dims(0);
39+
int q = B.dims(1);
40+
dim4 dims(m * p, n * q);
41+
42+
array K(dims, c32);
43+
44+
for (int i = 0; i < m * p; i++) {
45+
for (int j = 0; j < n * q; j++) {
46+
K(i, j) = A(floor(i / p), floor(j / p)) * B(i % p, j % q);
47+
}
2948
}
49+
50+
return K;
51+
}
52+
53+
54+
int main(int argc, char **argv) {
55+
af::info();
56+
57+
af_print(kron(Gates::X, Gates::Y));
58+
3059
return 0;
3160
}

0 commit comments

Comments
 (0)