{
num = (num << 8) | block[i];
}
+ free(block);
return ((double)num) / ((double)UINT32_MAX);
}
void qansel_cnot(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t bitA, uint8_t bitB)
{
- uint32_t retLen = (uint8_t)pow(2, qubitCount);
+ uint32_t retLen = (uint32_t)pow(2, qubitCount);
cpx_mtx_t ret;
cpx_mtx_init(&ret, 1, retLen);
cpx_t n;
void qansel_swap(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t bitA, uint8_t bitB)
{
- uint32_t retLen = (uint8_t)pow(2, qubitCount);
+ uint32_t retLen = (uint32_t)pow(2, qubitCount);
cpx_mtx_t ret;
cpx_mtx_init(&ret, 1, retLen);
cpx_t n;
void qansel_fredkin(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t bitA, uint8_t bitB, uint8_t bitC)
{
- uint32_t retLen = (uint8_t)pow(2, qubitCount);
+ uint32_t retLen = (uint32_t)pow(2, qubitCount);
cpx_mtx_t ret;
cpx_mtx_init(&ret, 1, retLen);
cpx_t n;
void qansel_toffoli(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t bitA, uint8_t bitB, uint8_t bitC)
{
- uint32_t retLen = (uint8_t)pow(2, qubitCount);
+ uint32_t retLen = (uint32_t)pow(2, qubitCount);
cpx_mtx_t ret;
cpx_mtx_init(&ret, 1, retLen);
cpx_t n;
for (uint8_t i = 1; i < qubitCount; i++)
{
- cpx_mtx_init(&tmp, filter.rows * 2, filter.cols * 2);
if (qubit == i)
{
gate.ptr = gate_ptr;
{
gate.ptr = Identity;
}
- cpx_mtx_knk(&tmp, &filter, &gate);
+
+ tmp.rows = filter.rows * gate.rows;
+ tmp.cols = filter.cols * gate.cols;
+ tmp.ptr = malloc((tmp.rows * 2) * (tmp.cols * 2) * sizeof(double));
+ cpx_ncpx_knk_mt
+ (
+ tmp.ptr, tmp.rows, tmp.cols,
+ filter.ptr, filter.rows, filter.cols,
+ gate.ptr, gate.rows, gate.cols
+ );
+
free(filter.ptr);
filter.ptr = tmp.ptr;
filter.rows = tmp.rows;
}
cpx_mtx_init(&tmp, stateVector->rows, stateVector->cols);
- cpx_mtx_mul(&tmp, stateVector, &filter);
+ cpx_ncpx_mmul_mt
+ (
+ tmp.ptr, stateVector->ptr, filter.ptr,
+ stateVector->rows * 2, filter.cols * 2, stateVector->cols * 2
+ );
free(stateVector->ptr);
stateVector->ptr = tmp.ptr;
free(filter.ptr);
if (instr->n[0] == 'u') free(gate_ptr);
}
-
uint8_t qansel_measure(cpx_mtx_t* stateVector, uint8_t qubitCount, uint8_t qubit)
{
uint32_t qubitCountPow2 = (uint32_t)pow(2, qubitCount);
}
printf("%.00f%%\n", prob * 100.0);
}
- else if (strcmp(instr[i].n, "reset") == 0)
+ else if (strcmp(instr[i].n, "reset_all") == 0)
{
cpx_mtx_set2(&stateVector, 0, 0, 1, 0);
for (uint32_t j = 1; j < qubitCountPow2; j++)
bitVector[j] = 0;
}
}
+ else if (strcmp(instr[i].n, "resetq") == 0)
+ {
+ uint8_t bit = qansel_measure(&stateVector, qubitCount, instr[i].q0);
+ if (bit)
+ {
+ instr[i].n[0] = 'x';
+ instr[i].n[1] = 0;
+ qansel_instruction(&stateVector, qubitCount, instr + i);
+ }
+ }
+ else if (strcmp(instr[i].n, "resetc") == 0)
+ {
+ bitVector[instr[i].q0] = 0;
+ }
else
{
qansel_instruction(&stateVector, qubitCount, instr + i);
cpx_mtx_free(&stateVector);
}
-void main()
+void main(int argc, char** argv)
{
char** lines = malloc(0);
uint32_t* lineIDs = malloc(0);
else if (comment || (c == ' ' && skipSpaces)) {}
else if (c != '\n' && c != '\t' && c != ';' && (c != ')' || inGate))
{
+ if (commentM == 1)
+ {
+ text = realloc(text, textLen + 1);
+ text[textLen++] = '/';
+ commentM = 0;
+ }
skipSpaces = 0;
if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
if (c == 'u') inGate = 1;
+ if (c == 'r') inGate = 1;
text = realloc(text, textLen + 1);
text[textLen++] = c;
pc = c;
if (strlen(text) > 0)
{
free(text);
- fprintf(stderr, "QAnsel: Invalid trailing text.\n");
+ fprintf(stderr, "QAnsel: Invalid trailing text");
exit(1);
}
free(text);
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Instruction before initialization.\n");
+ fprintf(stderr, "QAnsel: Instruction before initialization");
errFound = 1;
break;
}
if (q0 < 0 || q0 >= qubitCount)
{
- fprintf(stderr, "QAnsel: Invalid index ");
+ fprintf(stderr, "QAnsel: Invalid index");
errFound = 1;
break;
}
instr[instrLen++].arg2 = a2;
}
else if
+ (
+ memcmp("rx(", lines[i], 3) == 0
+ || memcmp("ry(", lines[i], 3) == 0
+ || memcmp("rz(", lines[i], 3) == 0
+ )
+ {
+ double angle;
+ char ty;
+ if (sscanf(lines[i], "r%c(%f/%f) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ {
+ angle = a0 / a1;
+ }
+ else if (sscanf(lines[i], "r%c(%f/%fpi) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ {
+ angle = a0 / (a1 * M_PI);
+ }
+ else if (sscanf(lines[i], "r%c(%f/pi) q[%i]", &ty, &a0, &q0) == 3)
+ {
+ angle = a0 / M_PI;
+ }
+ else if (sscanf(lines[i], "r%c(%f/-pi) q[%i]", &ty, &a0, &q0) == 3)
+ {
+ angle = a0 / -M_PI;
+ }
+ else if (sscanf(lines[i], "r%c(%fpi/%f) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ {
+ angle = (a0 * M_PI) / a1;
+ }
+ else if (sscanf(lines[i], "r%c(pi/%f) q[%i]", &ty, &a0, &q0) == 3)
+ {
+ angle = M_PI / a0;
+ }
+ else if (sscanf(lines[i], "r%c(-pi/%f) q[%i]", &ty, &a0, &q0) == 3)
+ {
+ angle = -M_PI / a0;
+ }
+ else if (sscanf(lines[i], "r%c(%fpi/%fpi) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ {
+ angle = (a0 * M_PI) / (a1 * M_PI);
+ }
+ else if (sscanf(lines[i], "r%c(pi/pi) q[%i]", &ty, &q0) == 2)
+ {
+ angle = 1;
+ }
+ else if (sscanf(lines[i], "r%c(-pi/pi) q[%i]", &ty, &q0) == 2)
+ {
+ angle = -1;
+ }
+ else if (sscanf(lines[i], "r%c(pi/-pi) q[%i]", &ty, &q0) == 2)
+ {
+ angle = -1;
+ }
+ else if (sscanf(lines[i], "r%c(-pi/-pi) q[%i]", &ty, &q0) == 2)
+ {
+ angle = 1;
+ }
+ else if (sscanf(lines[i], "r%c(%fpi) q[%i]", &ty, &a0, &q0) == 3)
+ {
+ angle = a0 * M_PI;
+ }
+ else if (sscanf(lines[i], "r%c(pi) q[%i]", &ty, &q0) == 2)
+ {
+ angle = M_PI;
+ }
+ else if (sscanf(lines[i], "r%c(-pi) q[%i]", &ty, &q0) == 2)
+ {
+ angle = -M_PI;
+ }
+ else if (sscanf(lines[i], "r%c(%f) q[%i]", &ty, &a0, &q0) == 2)
+ {
+ angle = a0;
+ }
+ else
+ {
+ fprintf(stderr, "QAnsel: Syntax error");
+ errFound = 1;
+ break;
+ }
+ if (qubitCount == 0xFF)
+ {
+ fprintf(stderr, "QAnsel: Instruction before initialization");
+ errFound = 1;
+ break;
+ }
+ if (q0 < 0 || q0 >= qubitCount)
+ {
+ fprintf(stderr, "QAnsel: Invalid index");
+ errFound = 1;
+ break;
+ }
+
+ instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
+ instr[instrLen].n[0] = 'u';
+ instr[instrLen].n[1] = 0;
+ instr[instrLen].q0 = q0;
+ switch (ty)
+ {
+ case 'x':
+ instr[instrLen].arg0 = M_PI / 2;
+ instr[instrLen].arg1 = -M_PI / 2;
+ instr[instrLen].arg2 = angle - (M_PI / 2);
+ break;
+ case 'y':
+ instr[instrLen].arg0 = angle;
+ instr[instrLen].arg1 = 0;
+ instr[instrLen].arg2 = 0;
+ break;
+ case 'z':
+ instr[instrLen].arg0 = 0;
+ instr[instrLen].arg1 = 0;
+ instr[instrLen].arg2 = angle;
+ break;
+ }
+ instrLen++;
+ }
+ else if
(
sscanf(lines[i], "h q[%i]", &q0) == 1
|| sscanf(lines[i], "x q[%i]", &q0) == 1
|| sscanf(lines[i], "y q[%i]", &q0) == 1
|| sscanf(lines[i], "z q[%i]", &q0) == 1
|| sscanf(lines[i], "t q[%i]", &q0) == 1
- || sscanf(lines[i], "z q[%i]", &q0) == 1
+ || sscanf(lines[i], "s q[%i]", &q0) == 1
)
{
g = lines[i][0];
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Instruction before initialization.\n");
+ fprintf(stderr, "QAnsel: Instruction before initialization");
errFound = 1;
break;
}
if (q0 < 0 || q0 >= qubitCount)
{
- fprintf(stderr, "QAnsel: Invalid index ");
+ fprintf(stderr, "QAnsel: Invalid index");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Instruction before initialization.\n");
+ fprintf(stderr, "QAnsel: Instruction before initialization");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Instruction before initialization.\n");
+ fprintf(stderr, "QAnsel: Instruction before initialization");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Instruction before initialization.\n");
+ fprintf(stderr, "QAnsel: Instruction before initialization");
errFound = 1;
break;
}
if (qubitCount < 3)
{
- fprintf(stderr, "QAnsel: Three qubit gate used with insufficient qubits initialized.\n");
+ fprintf(stderr, "QAnsel: Three qubit gate used with insufficient qubits initialized");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Instruction before initialization.\n");
+ fprintf(stderr, "QAnsel: Instruction before initialization");
errFound = 1;
break;
}
if (qubitCount < 3)
{
- fprintf(stderr, "QAnsel: Three qubit gate used with insufficient qubits initialized.\n");
+ fprintf(stderr, "QAnsel: Three qubit gate used with insufficient qubits initialized");
errFound = 1;
break;
}
{
if (bitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Measure instruction used before bit initialization.\n");
+ fprintf(stderr, "QAnsel: Measure instruction used before bit initialization");
errFound = 1;
break;
}
{
if (bitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: If instruction used before bit initialization.\n");
+ fprintf(stderr, "QAnsel: If instruction used before bit initialization");
errFound = 1;
break;
}
{
if (bitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: If instruction used before bit initialization.\n");
+ fprintf(stderr, "QAnsel: If instruction used before bit initialization");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Qubit instruction used before initialization.\n");
+ fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
errFound = 1;
break;
}
{
if (bitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Bit instruction used before initialization.\n");
+ fprintf(stderr, "QAnsel: Bit instruction used before initialization");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Qubit instruction used before initialization.\n");
+ fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
errFound = 1;
break;
}
{
if (bitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Bit instruction used before initialization.\n");
+ fprintf(stderr, "QAnsel: Bit instruction used before initialization");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Qubit instruction used before initialization.\n");
+ fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
errFound = 1;
break;
}
{
if (qubitCount == 0xFF)
{
- fprintf(stderr, "QAnsel: Qubit instruction used before initialization.\n");
+ fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
errFound = 1;
break;
}
strcpy(instr[instrLen].n, "sample");
instr[instrLen++].q0 = q0;
}
- else if (strcmp(lines[i], "reset") == 0)
+ else if (sscanf(lines[i], "reset q[%i]", &q0) == 1)
{
- if (qubitCount == 0xFF || bitCount == 0xFF)
+ if (qubitCount == 0xFF)
+ {
+ fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
+ errFound = 1;
+ break;
+ }
+ if (q0 >= qubitCount || q0 < 0)
+ {
+ fprintf(stderr, "QAnsel: Invalid index");
+ errFound = 1;
+ break;
+ }
+ instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
+ strcpy(instr[instrLen].n, "resetq");
+ instr[instrLen++].q0 = q0;
+ }
+ else if (sscanf(lines[i], "reset c[%i]", &q0) == 1)
+ {
+ if (bitCount == 0xFF)
+ {
+ fprintf(stderr, "QAnsel: Bit instruction used before initialization");
+ errFound = 1;
+ break;
+ }
+ if (q0 >= bitCount || q0 < 0)
{
- fprintf(stderr, "QAnsel: Instruction used before initialization.\n");
+ fprintf(stderr, "QAnsel: Invalid index");
errFound = 1;
break;
}
instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
- strcpy(instr[instrLen].n, "reset");
+ strcpy(instr[instrLen].n, "resetc");
+ instr[instrLen++].q0 = q0;
+ }
+ else if (strcmp(lines[i], "reset") == 0)
+ {
+ instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
+ strcpy(instr[instrLen].n, "reset_all");
instrLen++;
}
+ else if (strcmp(lines[i], "barrier q") == 0)
+ {
+ if (qubitCount == 0xFF)
+ {
+ fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
+ errFound = 1;
+ break;
+ }
+ //do nothing as there are currently no
+ // optimizations that this instruction
+ // would prevent
+ }
+ else if (sscanf(lines[i], "barrier q[%i]", &q0) == 1)
+ {
+ if (qubitCount == 0xFF || bitCount == 0xFF)
+ {
+ fprintf(stderr, "QAnsel: Instruction used before initialization");
+ errFound = 1;
+ break;
+ }
+ if (q0 >= qubitCount || q0 < 0)
+ {
+ fprintf(stderr, "QAnsel: Invalid index");
+ errFound = 1;
+ break;
+ }
+ //do nothing as there are currently no
+ // optimizations that this instruction
+ // would prevent
+ }
else
{
fprintf(stderr, "QAnsel: Syntax error");
exit(1);
}
- qansel_run(qubitCount, bitCount, instr, instrLen, 0);
+ if (argc == 2)
+ {
+ if (strcmp(argv[1], "-d") == 0)
+ {
+ qansel_run(qubitCount, bitCount, instr, instrLen, 1);
+ }
+ }
+ else
+ {
+ qansel_run(qubitCount, bitCount, instr, instrLen, 0);
+ }
free(instr);
free(lineIDs);
}
#include <stdint.h>
#include <stddef.h>
#include <math.h>
+#include <pthread.h>
+#include <string.h>
+#include "cores.c"
typedef struct
{
}
}
+typedef struct
+{
+ size_t ID;
+ size_t Threads;
+ size_t Last;
+ size_t Loops;
+ size_t Continue;
+ size_t BlockSize;
+ double* ptrR;
+ double* ptrA;
+ double* ptrB;
+ size_t rowsA;
+ size_t colsB;
+ size_t shared;
+} cpx_mul_shared;
+
+void* cpx_ncpx_mmul_mtc(void *context)
+{
+ cpx_mul_shared* data = (cpx_mul_shared*)context;
+ double* ptrR = data->ptrR;
+ double* ptrA = data->ptrA;
+ double* ptrB = data->ptrB;
+ size_t rowsA = data->rowsA;
+ size_t colsB = data->colsB;
+ size_t shared = data->shared;
+
+ size_t colsA = data->shared;
+ size_t rowsB = data->shared;
+ size_t rowsR = data->rowsA;
+ size_t colsR = data->colsB;
+
+ for (size_t rowR = 0; rowR < rowsR; rowR++)
+ {
+ size_t a = data->ID * data->BlockSize;
+ size_t b = (data->ID + 1) * data->BlockSize;
+ if (data->ID == data->Last) b += data->Continue;
+
+ //printf("%i;%i\n", a, b);
+
+ for (size_t colR = a; colR < b; colR++)
+ {
+ size_t posR = colR + rowR * colsR;
+ size_t rowA = rowR;
+ size_t colB = colR;
+ ptrR[posR] = 0;
+ for (size_t i = 0; i < data->shared; i++)
+ {
+ size_t posA = i + rowA * colsA;
+ size_t posB = colB + i * colsB;
+ data->ptrR[posR] += data->ptrA[posA] * data->ptrB[posB];
+ }
+ }
+ }
+}
+
+void cpx_ncpx_mmul_mt(double* ptrR, double* ptrA, double* ptrB, size_t rowsA, size_t colsB, size_t shared)
+{
+ cpx_mul_shared share;
+ share.Threads = get_core_count();
+ share.ptrR = ptrR;
+ share.ptrA = ptrA;
+ share.ptrB = ptrB;
+ share.rowsA = rowsA;
+ share.colsB = colsB;
+ share.shared = shared;
+ if (colsB <= share.Threads)
+ {
+ share.Threads = colsB;
+ }
+ share.BlockSize = (size_t)floor(((double)colsB) / ((double)share.Threads));
+ share.Loops = (size_t)floor(((double)colsB) / ((double)share.BlockSize));
+ share.Last = share.Loops - 1;
+ share.Continue = (size_t)(((double)colsB) - ((double)share.Loops) * ((double)share.BlockSize));
+
+ pthread_t threads[share.Loops];
+ cpx_mul_shared contexts[share.Loops];
+ for (size_t i = 0; i < share.Loops; i++)
+ {
+ pthread_t tid;
+ threads[i] = tid;
+ memcpy(contexts + i, &share, sizeof(cpx_mul_shared));
+ contexts[i].ID = i;
+ }
+
+ for (size_t i = 0; i < share.Loops; i++)
+ {
+ if (pthread_create(threads + i, NULL, &cpx_ncpx_mmul_mtc, contexts + i))
+ {
+ fprintf(stderr, "QAnsel: Thread error. (1)\n");
+ exit(1);
+ }
+ }
+
+ for (uint32_t i = 0; i < share.Loops; i++)
+ {
+ pthread_t tid;
+ tid = threads[i];
+ if (pthread_join(tid, NULL))
+ {
+ fprintf(stderr, "QAnsel: Thread error. (2)\n");
+ }
+ }
+}
+
//non-complex kronecker product
void cpx_ncpx_mknk(double* ptrR, double* ptrA, double* ptrB, size_t rowsA, size_t colsA, size_t rowsB, size_t colsB)
{
row *= 2;
col *= 2;
size_t cols = m->cols * 2;
+
+ //printf("qqq\n");
n->real = m->ptr[(col + 1) + (row + 1) * cols];
+ //printf("ppp\n");
n->imaginary = m->ptr[col + (row + 1) * cols];
+ //printf("ggg\n");
}
double cpx_mtx_get_real(cpx_mtx_t* m, size_t row, size_t col)
m->cols = 0;
}
-void cpx_mtx_knk2
+typedef struct
+{
+ size_t ID;
+ size_t Threads;
+ size_t Last;
+ size_t Loops;
+ size_t Continue;
+ size_t BlockSize;
+ double* ptrR;
+ size_t rowsR;
+ size_t colsR;
+ double* ptrA;
+ size_t rowsA;
+ size_t colsA;
+ double* ptrB;
+ size_t rowsB;
+ size_t colsB;
+} cpx_knk_shared;
+
+void* cpx_ncpx_knk_mtc(void *context)
+{
+ cpx_knk_shared* data = (cpx_knk_shared*)context;
+ double* ptrR = data->ptrR;
+ size_t rowsR = data->rowsR;
+ size_t colsR = data->colsR;
+ double* ptrA = data->ptrA;
+ size_t rowsA = data->rowsA;
+ size_t colsA = data->colsA;
+ double* ptrB = data->ptrB;
+ size_t rowsB = data->rowsB;
+ size_t colsB = data->colsB;
+
+ for (size_t rowR = 0; rowR < rowsR; rowR++)
+ {
+ size_t a = data->ID * data->BlockSize;
+ size_t b = (data->ID + 1) * data->BlockSize;
+ if (data->ID == data->Last) b += data->Continue;
+ for (size_t colR = a; colR < b; colR++)
+ {
+ size_t rowA = rowR / rowsB;
+ size_t colA = colR / colsB;
+ size_t rowB = rowR % rowsB;
+ size_t colB = colR % colsB;
+
+ double r1 = ptrA[((colA * 2) + 1) + ((rowA * 2) + 1) * (colsA * 2)];
+ double i1 = ptrA[(colA * 2) + ((rowA * 2) + 1) * (colsA * 2)];
+ double r2 = ptrB[((colB * 2) + 1) + ((rowB * 2) + 1) * (colsB * 2)];
+ double i2 = ptrB[(colB * 2) + ((rowB * 2) + 1) * (colsB * 2)];
+
+ double first = r1 * r2; //real
+ double outer = r1 * i2; //imaginary
+ double inner = i1 * r2; //imaginary
+ double last = -(i1 * i2); //real
+ r1 = first + last;
+ i1 = outer + inner;
+
+ ptrR[(colR * 2) + (rowR * 2) * (colsR * 2)] = r1;
+ ptrR[((colR * 2) + 1) + (rowR * 2) * (colsR * 2)] = -i1;
+ ptrR[(colR * 2) + ((rowR * 2) + 1) * (colsR * 2)] = i1;
+ ptrR[((colR * 2) + 1) + ((rowR * 2) + 1) * (colsR * 2)] = r1;
+
+ }
+ }
+}
+
+void cpx_ncpx_knk_mt
+(
+ double* ptrR,
+ size_t rowsR,
+ size_t colsR,
+ double* ptrA,
+ size_t rowsA,
+ size_t colsA,
+ double* ptrB,
+ size_t rowsB,
+ size_t colsB
+)
+{
+ cpx_knk_shared share;
+ share.Threads = get_core_count();
+ share.ptrR = ptrR;
+ share.rowsR = rowsR;
+ share.colsR = colsR;
+ share.ptrA = ptrA;
+ share.rowsA = rowsA;
+ share.colsA = colsA;
+ share.ptrB = ptrB;
+ share.rowsB = rowsB;
+ share.colsB = colsB;
+
+ if (colsR <= share.Threads)
+ {
+ share.Threads = colsR;
+ }
+ share.BlockSize = (size_t)floor(((double)colsR) / ((double)share.Threads));
+ share.Loops = (size_t)floor(((double)colsR) / ((double)share.BlockSize));
+ share.Last = share.Loops - 1;
+ share.Continue = (size_t)(((double)colsR) - ((double)share.Loops) * ((double)share.BlockSize));
+
+ pthread_t threads[share.Loops];
+ cpx_knk_shared contexts[share.Loops];
+ for (size_t i = 0; i < share.Loops; i++)
+ {
+ pthread_t tid;
+ threads[i] = tid;
+ memcpy(contexts + i, &share, sizeof(cpx_knk_shared));
+ contexts[i].ID = i;
+ }
+
+ for (size_t i = 0; i < share.Loops; i++)
+ {
+ if (pthread_create(threads + i, NULL, &cpx_ncpx_knk_mtc, contexts + i))
+ {
+ fprintf(stderr, "QAnsel: Thread error. (1)\n");
+ exit(1);
+ }
+ }
+
+ for (uint32_t i = 0; i < share.Loops; i++)
+ {
+ pthread_t tid;
+ tid = threads[i];
+ if (pthread_join(tid, NULL))
+ {
+ fprintf(stderr, "QAnsel: Thread error. (2)\n");
+ }
+ }
+}
+
+void cpx_ncpx_knk
(
double* ptrR,
size_t rowsR,
size_t colsB = b->cols;
size_t rowsR = rowsA * rowsB;
size_t colsR = colsA * colsB;
- cpx_mtx_init(r, rowsR, colsR);
for (size_t rowR = 0; rowR < rowsR; rowR++)
{
for (size_t colR = 0; colR < colsR; colR++)
}
}
-
-void cpx_mtx_(cpx_mtx_t* m)
-{
- for (size_t r = 0; r < m->rows * 2; r++)
- {
- for (size_t c = 0; c < m->cols * 2; c++)
- {
- if (c > 0) printf(", ");
- printf("%f", m->ptr[c + r * m->cols * 2]);
- }
- printf("\n");
- }
-}
-
void cpx_mtx_print(cpx_mtx_t* m)
{
for (size_t r = 0; r < m->rows; r++)
}
}
+void display_wait(uint32_t i)
+{
+ SDL_Delay(i);
+}
+
void display(cpx_mtx_t* stateVector, uint8_t qubitCount)
{
uint32_t qubitCountPow2 = (uint32_t)pow(2, qubitCount);
static SDL_Renderer* renderer = NULL;
int SCREEN_WIDTH = 640 * 2;
int SCREEN_HEIGHT = 480;
+ int SCREEN_HALF = SCREEN_HEIGHT / 2;
if (stateVector == NULL)
{
}
window = SDL_CreateWindow
(
- "D",
+ "QAnsel",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
double p0 = 0;
double i0 = 0;
- for (int i = -20; i < SCREEN_WIDTH; i++)
+ for (int i = 0; i < SCREEN_WIDTH; i++)
{
double p1 = 0;
double i1 = 0;
{
cpx_t n;
cpx_mtx_get(stateVector, 0, j - 1, &n);
- p1 += (sin(i * ((2 * M_PI) / (SCREEN_WIDTH / j))) * (SCREEN_HEIGHT / 4)) * (n.real * n.real) * (n.real < 0 ? -1 : 1);
- i1 += (cos(i * ((2 * M_PI) / (SCREEN_WIDTH / j))) * (SCREEN_HEIGHT / 4)) * (n.imaginary * n.imaginary) * (n.imaginary < 0 ? -1 : 1);
+ p1 += (sin(i * ((2 * M_PI) / (SCREEN_WIDTH / j))) * (SCREEN_HEIGHT / 4)) * pow(n.real, 2) * (n.real < 0 ? -1 : 1);
+ i1 += (cos(i * ((2 * M_PI) / (SCREEN_WIDTH / j))) * (SCREEN_HEIGHT / 4)) * pow(n.imaginary, 2) * (n.imaginary < 0 ? -1 : 1);
}
int x0 = i - 1;
int x1 = i;
int y1 = p1;
- x0 += i0 / 4;
- y0 += i0 / 2;
- x1 += i1 / 4;
- y1 += i1 / 2;
-
y0 += SCREEN_HEIGHT / 2;
y1 += SCREEN_HEIGHT / 2;
- if ( (i0 + i1) / 2 < 0)
+ if (i > 0)
{
- SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
- DrawThickLine(renderer, x0, SCREEN_HEIGHT / 2, x1, SCREEN_HEIGHT / 2, 5);
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
DrawThickLine(renderer, x0, y0, x1, y1, 5);
+ SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0x00);
+ DrawThickLine(renderer, x0, SCREEN_HALF, x1, SCREEN_HALF, 5);
}
- else
+
+ y0 = i0;
+ y1 = i1;
+
+ y0 += SCREEN_HEIGHT / 2;
+ y1 += SCREEN_HEIGHT / 2;
+
+ if (i > 0)
{
+ SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
DrawThickLine(renderer, x0, y0, x1, y1, 5);
- SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
- DrawThickLine(renderer, x0, SCREEN_HEIGHT / 2, x1, SCREEN_HEIGHT / 2, 5);
- SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
+ SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0x00);
+ DrawThickLine(renderer, x0, SCREEN_HALF, x1, SCREEN_HALF, 5);
}
p0 = p1;
i0 = i1;