char g;
int q0, q1, q2;
float a0, a1, a2;
-
- if (sscanf(lines[i], "qreg q[%i]", &q0) == 1)
+ uint32_t matchedCount;
+ uint32_t lineLen = strlen(lines[i]);
+ if (lineLen > 1)
+ {
+ while (lineLen > 1 && (lines[i][lineLen - 1] == ' ' || lines[i][lineLen - 1] == '\t'))
+ {
+ lines[i][lineLen - 1] = 0;
+ lineLen--;
+ }
+ }
+
+ if (sscanf(lines[i], "qreg q[%i]%n", &q0, &matchedCount) == 1)
{
if (qubitCount == 0xFF)
{
break;
}
}
- else if (sscanf(lines[i], "creg c[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "creg c[%i]%n", &q0, &matchedCount) == 1)
{
if (bitCount == 0xFF)
{
break;
}
}
- else if (sscanf(lines[i], "u(%f,%f,%f) q[%i]", &a0, &a1, &a2, &q0) == 4)
+ else if (sscanf(lines[i], "u(%f,%f,%f) q[%i]%n", &a0, &a1, &a2, &q0, &matchedCount) == 4)
{
if (qubitCount == 0xFF)
{
{
double angle;
char ty;
- if (sscanf(lines[i], "r%c(%f/%f) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ if (sscanf(lines[i], "r%c(%f/%f) q[%i]%n", &ty, &a0, &a1, &q0, &matchedCount) == 4)
{
angle = a0 / a1;
}
- else if (sscanf(lines[i], "r%c(%f/%fpi) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ else if (sscanf(lines[i], "r%c(%f/%fpi) q[%i]%n", &ty, &a0, &a1, &q0, &matchedCount) == 4)
{
angle = a0 / (a1 * M_PI);
}
- else if (sscanf(lines[i], "r%c(%f/pi) q[%i]", &ty, &a0, &q0) == 3)
+ else if (sscanf(lines[i], "r%c(%f/pi) q[%i]%n", &ty, &a0, &q0, &matchedCount) == 3)
{
angle = a0 / M_PI;
}
- else if (sscanf(lines[i], "r%c(%f/-pi) q[%i]", &ty, &a0, &q0) == 3)
+ else if (sscanf(lines[i], "r%c(%f/-pi) q[%i]%n", &ty, &a0, &q0, &matchedCount) == 3)
{
angle = a0 / -M_PI;
}
- else if (sscanf(lines[i], "r%c(%fpi/%f) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ else if (sscanf(lines[i], "r%c(%fpi/%f) q[%i]%n", &ty, &a0, &a1, &q0, &matchedCount) == 4)
{
angle = (a0 * M_PI) / a1;
}
- else if (sscanf(lines[i], "r%c(pi/%f) q[%i]", &ty, &a0, &q0) == 3)
+ else if (sscanf(lines[i], "r%c(pi/%f) q[%i]%n", &ty, &a0, &q0, &matchedCount) == 3)
{
angle = M_PI / a0;
}
- else if (sscanf(lines[i], "r%c(-pi/%f) q[%i]", &ty, &a0, &q0) == 3)
+ else if (sscanf(lines[i], "r%c(-pi/%f) q[%i]%n", &ty, &a0, &q0, &matchedCount) == 3)
{
angle = -M_PI / a0;
}
- else if (sscanf(lines[i], "r%c(%fpi/%fpi) q[%i]", &ty, &a0, &a1, &q0) == 4)
+ else if (sscanf(lines[i], "r%c(%fpi/%fpi) q[%i]%n", &ty, &a0, &a1, &q0, &matchedCount) == 4)
{
angle = (a0 * M_PI) / (a1 * M_PI);
}
- else if (sscanf(lines[i], "r%c(pi/pi) q[%i]", &ty, &q0) == 2)
+ else if (sscanf(lines[i], "r%c(pi/pi) q[%i]%n", &ty, &q0, &matchedCount) == 2)
{
angle = 1;
}
- else if (sscanf(lines[i], "r%c(-pi/pi) q[%i]", &ty, &q0) == 2)
+ else if (sscanf(lines[i], "r%c(-pi/pi) q[%i]%n", &ty, &q0, &matchedCount) == 2)
{
angle = -1;
}
- else if (sscanf(lines[i], "r%c(pi/-pi) q[%i]", &ty, &q0) == 2)
+ else if (sscanf(lines[i], "r%c(pi/-pi) q[%i]%n", &ty, &q0, &matchedCount) == 2)
{
angle = -1;
}
- else if (sscanf(lines[i], "r%c(-pi/-pi) q[%i]", &ty, &q0) == 2)
+ else if (sscanf(lines[i], "r%c(-pi/-pi) q[%i]%n", &ty, &q0, &matchedCount) == 2)
{
angle = 1;
}
- else if (sscanf(lines[i], "r%c(%fpi) q[%i]", &ty, &a0, &q0) == 3)
+ else if (sscanf(lines[i], "r%c(%fpi) q[%i]%n", &ty, &a0, &q0, &matchedCount) == 3)
{
angle = a0 * M_PI;
}
- else if (sscanf(lines[i], "r%c(pi) q[%i]", &ty, &q0) == 2)
+ else if (sscanf(lines[i], "r%c(pi) q[%i]%n", &ty, &q0, &matchedCount) == 2)
{
angle = M_PI;
}
- else if (sscanf(lines[i], "r%c(-pi) q[%i]", &ty, &q0) == 2)
+ else if (sscanf(lines[i], "r%c(-pi) q[%i]%n", &ty, &q0, &matchedCount) == 2)
{
angle = -M_PI;
}
- else if (sscanf(lines[i], "r%c(%f) q[%i]", &ty, &a0, &q0) == 3)
+ else if (sscanf(lines[i], "r%c(%f) q[%i]%n", &ty, &a0, &q0, &matchedCount) == 3)
{
angle = a0;
}
}
instrLen++;
}
- else if (sscanf(lines[i], "density q[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "density q[%i]%n", &q0, &matchedCount) == 1)
{
g = lines[i][0];
if (qubitCount == 0xFF)
}
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], "s q[%i]", &q0) == 1
+ sscanf(lines[i], "h q[%i]%n", &q0, &matchedCount) == 1
+ || sscanf(lines[i], "x q[%i]%n", &q0, &matchedCount) == 1
+ || sscanf(lines[i], "y q[%i]%n", &q0, &matchedCount) == 1
+ || sscanf(lines[i], "z q[%i]%n", &q0, &matchedCount) == 1
+ || sscanf(lines[i], "t q[%i]%n", &q0, &matchedCount) == 1
+ || sscanf(lines[i], "s q[%i]%n", &q0, &matchedCount) == 1
)
{
g = lines[i][0];
instr[instrLen].n[1] = 0;
instr[instrLen++].q0 = (uint8_t)q0;
}
- else if (sscanf(lines[i], "cx q[%i], q[%i]", &q0, &q1) == 2)
+ else if (sscanf(lines[i], "cx q[%i], q[%i]%n", &q0, &q1, &matchedCount) == 2)
{
if (qubitCount == 0xFF)
{
instr[instrLen].q0 = q0;
instr[instrLen++].q1 = q1;
}
- else if (sscanf(lines[i], "swap q[%i], q[%i]", &q0, &q1) == 2)
+ else if (sscanf(lines[i], "swap q[%i], q[%i]%n", &q0, &q1, &matchedCount) == 2)
{
if (qubitCount == 0xFF)
{
}
else if
(
- sscanf(lines[i], "cswap q[%i], q[%i], q[%i]", &q0, &q1, &q2) == 3
- || sscanf(lines[i], "fredkin q[%i], q[%i], q[%i]", &q0, &q1, &q2) == 3
+ sscanf(lines[i], "cswap q[%i], q[%i], q[%i]%n", &q0, &q1, &q2, &matchedCount) == 3
+ || sscanf(lines[i], "fredkin q[%i], q[%i], q[%i]%n", &q0, &q1, &q2, &matchedCount) == 3
)
{
if (qubitCount == 0xFF)
}
else if
(
- sscanf(lines[i], "ccx q[%i], q[%i], q[%i]", &q0, &q1, &q2) == 3
- || sscanf(lines[i], "toffoli q[%i], q[%i], q[%i]", &q0, &q1, &q2) == 3
+ sscanf(lines[i], "ccx q[%i], q[%i], q[%i]%n", &q0, &q1, &q2, &matchedCount) == 3
+ || sscanf(lines[i], "toffoli q[%i], q[%i], q[%i]%n", &q0, &q1, &q2, &matchedCount) == 3
)
{
if (qubitCount == 0xFF)
instr[instrLen].q1 = q1;
instr[instrLen++].q2 = q2;
}
- else if (sscanf(lines[i], "measure q[%i] -> c[%i]", &q0, &q1) == 2)
+ else if (sscanf(lines[i], "measure q[%i] -> c[%i]%n", &q0, &q1, &matchedCount) == 2)
{
if (bitCount == 0xFF)
{
instr[instrLen].q0 = q0;
instr[instrLen++].q1 = q1;
}
- else if (sscanf(lines[i], "if(c==%i)", &q0) == 1)
+ else if (sscanf(lines[i], "if(c==%i)%n", &q0, &matchedCount) == 1)
{
if (bitCount == 0xFF)
{
strcpy(instr[instrLen].n, "if_all");
instr[instrLen++].q0 = q0;
}
- else if (sscanf(lines[i], "if(c[%i]==%i)", &q0, &q1) == 2)
+ else if (sscanf(lines[i], "if(c[%i]==%i)%n", &q0, &q1, &matchedCount) == 2)
{
if (bitCount == 0xFF)
{
}*/
else if (strcmp(lines[i], "print q") == 0)
{
+ matchedCount = lineLen;
if (qubitCount == 0xFF)
{
fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
}
else if (strcmp(lines[i], "print c") == 0)
{
+ matchedCount = lineLen;
if (bitCount == 0xFF)
{
fprintf(stderr, "QAnsel: Bit instruction used before initialization");
strcpy(instr[instrLen].n, "printc_all");
instrLen++;
}
- else if (sscanf(lines[i], "print q[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "print q[%i]%n", &q0, &matchedCount) == 1)
{
if (qubitCount == 0xFF)
{
strcpy(instr[instrLen].n, "printq");
instr[instrLen++].q0 = q0;
}
- else if (sscanf(lines[i], "print c[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "print c[%i]%n", &q0, &matchedCount) == 1)
{
if (bitCount == 0xFF)
{
}
else if (strcmp(lines[i], "born") == 0 || strcmp(lines[i], "born q") == 0)
{
+ matchedCount = lineLen;
if (qubitCount == 0xFF)
{
fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
strcpy(instr[instrLen].n, "born_all");
instrLen++;
}
- else if (sscanf(lines[i], "born q[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "born q[%i]%n", &q0, &matchedCount) == 1)
{
if (qubitCount == 0xFF)
{
strcpy(instr[instrLen].n, "born");
instr[instrLen++].q0 = q0;
}
- else if (sscanf(lines[i], "reset q[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "reset q[%i]%n", &q0, &matchedCount) == 1)
{
if (qubitCount == 0xFF)
{
strcpy(instr[instrLen].n, "resetq");
instr[instrLen++].q0 = q0;
}
- else if (sscanf(lines[i], "reset c[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "reset c[%i]%n", &q0, &matchedCount) == 1)
{
if (bitCount == 0xFF)
{
}
else if (strcmp(lines[i], "reset") == 0)
{
+ matchedCount = lineLen;
instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
strcpy(instr[instrLen].n, "reset_all");
instrLen++;
}
else if (strcmp(lines[i], "barrier q") == 0)
{
+ matchedCount = lineLen;
if (qubitCount == 0xFF)
{
fprintf(stderr, "QAnsel: Qubit instruction used before initialization");
// optimizations that this instruction
// would prevent
}
- else if (sscanf(lines[i], "barrier q[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "barrier q[%i]%n", &q0, &matchedCount) == 1)
{
if (qubitCount == 0xFF || bitCount == 0xFF)
{
// optimizations that this instruction
// would prevent
}
- else if (sscanf(lines[i], "hvar %f", &a0) == 1)
+ else if (sscanf(lines[i], "hvar %f%n", &a0, &matchedCount) == 1)
{
instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
strcpy(instr[instrLen].n, "hvar");
}
else if (strcmp(lines[i], "rand") == 0)
{
+ matchedCount = lineLen;
instr = realloc(instr, (instrLen + 1) * sizeof(QInstr));
strcpy(instr[instrLen].n, "rand");
instrLen++;
}
else if (strcmp(lines[i], "sample") == 0 || strcmp(lines[i], "sample c") == 0)
{
+ matchedCount = lineLen;
if (i != linesLen - 1)
{
fprintf(stderr, "QAnsel: Sampling should be performed at the end of the program;");
}
fullSample = 255;
}
- else if (sscanf(lines[i], "sample c[%i]", &q0) == 1)
+ else if (sscanf(lines[i], "sample c[%i]%n", &q0, &matchedCount) == 1)
{
if (i != linesLen - 1)
{
errFound = 1;
break;
}
+ printf(">%s: %i;%i<\n", lines[i], strlen(lines[i]), matchedCount);
+ if (lineLen != matchedCount)
+ {
+ fprintf(stderr, "QAnsel: Invalid trailing text");
+ errFound = 1;
+ break;
+ }
+
}
for (uint32_t i = 0; i < linesLen; i++) free(lines[i]);