Skip to content

Commit e7d3118

Browse files
Changes in error and exception outputs
1 parent 07c2dd8 commit e7d3118

File tree

3 files changed

+190
-51
lines changed

3 files changed

+190
-51
lines changed

.DS_Store

-2 KB
Binary file not shown.

QuaCLibs/QuaCExecutor.h

Lines changed: 108 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,17 @@ void executePrint(char *cmd, quReg *qr, QuregMap *qm) {
233233
void executeFunc(KeyTypes *keytypes, IdentifierMap *gates, int i, int qureg_size, quReg *qr, QuregMap *qm) {
234234
int cur_gate_num = 0;
235235
int ret_i_val_func = 0;
236+
char *func_name = (char*) malloc(strlen(keytypes[i].key) * sizeof(char));
237+
strcpy(func_name, keytypes[i].key);
236238

237-
IdentifierMap *im;
239+
IdentifierMap *imap;
238240

239241
while(keytypes[i].type != IDX_BRACKET_OPEN) i++;
240242

241243
while(keytypes[i].type != CMPND_STMT_CLOSE) {
242244

243245
if(cur_gate_num > qureg_size) {
244-
printf("[!] Too many gates in the row. [%d]\n", cur_gate_num);
246+
printf("[!] Too many gates in the function '%s'. [number of gates: %d]\n", func_name, cur_gate_num);
245247
exit(-1);
246248
}
247249

@@ -276,24 +278,32 @@ void executeFunc(KeyTypes *keytypes, IdentifierMap *gates, int i, int qureg_size
276278
qr = QSwap_reg(qr, cur_gate_num, idx2);
277279
}
278280
else if(keytypes[i].type == QFT) {
279-
281+
printf("QFT Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
282+
exit(0);
280283
}
281284
else if(keytypes[i].type == CONTROL) {
285+
printf("CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
286+
exit(0);
282287
}
283288
else if(keytypes[i].type == INV_CONTROL) {
289+
printf("INVERSE-CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
290+
exit(0);
284291
}
285292
else if(keytypes[i].type == X_AXIS_CONTROL) {
286-
293+
printf("X-AXIS-CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
294+
exit(0);
287295
}
288296
else if(keytypes[i].type == Y_AXIS_CONTROL) {
289-
297+
printf("Y-AXIS-CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
298+
exit(0);
290299
}
291300
else if(keytypes[i].type == IDENTITY) {
301+
292302
}
293-
else if((im = getIdentifier(keytypes[i].key, gates))) {
303+
else if((imap = getIdentifier(keytypes[i].key, gates))) {
294304
ret_i_val_func = i + 1;
295-
i = im->idx;
296-
executeGate(keytypes, im->idx, qr, cur_gate_num);
305+
i = imap->idx;
306+
executeGate(keytypes, i, qr, cur_gate_num);
297307
i = ret_i_val_func;
298308
}
299309
else if(keytypes[i].type == PRINT) {
@@ -313,7 +323,96 @@ void executeFunc(KeyTypes *keytypes, IdentifierMap *gates, int i, int qureg_size
313323
}
314324

315325
void executeGate(KeyTypes *keytypes, int i, quReg *qr, int gates_done) {
316-
326+
int gate_size = atoi(keytypes[i+2].key);
327+
int cur_qubit_idx = 0;
328+
int cur_gate_num = 0;
329+
char *gate_name = (char*) malloc(strlen(keytypes[i].key) * sizeof(char));
330+
strcpy(gate_name, keytypes[i].key);
331+
332+
if((gate_size + gates_done) > qr->size) {
333+
printf("[!] Too many gates in the row. Resize the custom gate '%s' or decrease the number of pre-defined gates in the row. [%d]\n", keytypes[i].key, (gates_done + gate_size));
334+
exit(-1);
335+
}
336+
337+
i += 2;
338+
339+
while(keytypes[i].type != IDX_BRACKET_OPEN) i++;
340+
341+
while(keytypes[i].type != CMPND_STMT_CLOSE) {
342+
if(cur_qubit_idx > gate_size) {
343+
printf("[!] Too many gates in the custom gate '%s'. [number of gates: %d]\n", gate_name, cur_qubit_idx);
344+
exit(-1);
345+
}
346+
347+
cur_gate_num = gates_done + cur_qubit_idx;
348+
349+
if(keytypes[i].type == COMMA) {
350+
}
351+
else if(keytypes[i].type == IDX_BRACKET_CLOSE || keytypes[i].type == IDX_BRACKET_OPEN || keytypes[i].type == CMPND_STMT_CLOSE) {
352+
cur_qubit_idx = 0;
353+
}
354+
else if(keytypes[i].type == PAULI_X) {
355+
qr = X_reg(qr, cur_gate_num);
356+
}
357+
else if(keytypes[i].type == PAULI_Y) {
358+
qr = Y_reg(qr, cur_gate_num);
359+
}
360+
else if(keytypes[i].type == PAULI_Z) {
361+
qr = Z_reg(qr, cur_gate_num);
362+
}
363+
else if(keytypes[i].type == PHASE) {
364+
qr = S_reg(qr, cur_gate_num);
365+
}
366+
else if(keytypes[i].type == ROTATION) {
367+
int angle = atoi((keytypes[i].key + 2));
368+
qr = R_reg(angle, qr, cur_gate_num);
369+
}
370+
else if(keytypes[i].type == HADAMARD) {
371+
qr = H_reg(qr, cur_gate_num);
372+
}
373+
else if(keytypes[i].type == SWAP) {
374+
int idx2 = cur_qubit_idx;
375+
while(keytypes[i].type != SWAP) idx2++;
376+
377+
qr = QSwap_reg(qr, cur_gate_num, idx2);
378+
}
379+
else if(keytypes[i].type == QFT) {
380+
printf("QFT Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
381+
exit(0);
382+
}
383+
else if(keytypes[i].type == CONTROL) {
384+
printf("CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
385+
exit(0);
386+
}
387+
else if(keytypes[i].type == INV_CONTROL) {
388+
printf("INVERSE-CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
389+
exit(0);
390+
}
391+
else if(keytypes[i].type == X_AXIS_CONTROL) {
392+
printf("X-AXIS-CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
393+
exit(0);
394+
}
395+
else if(keytypes[i].type == Y_AXIS_CONTROL) {
396+
printf("Y-AXIS-CONTROL Gate: This functionality is not added right now. Please wait for the next version to use this.\n");
397+
exit(0);
398+
}
399+
else if(keytypes[i].type == IDENTITY) {
400+
401+
}
402+
else if(keytypes[i].type == PRINT) {
403+
printf("[!] Cannot use print statement inside a gate.\n");
404+
exit(-1);
405+
}
406+
else {
407+
printf("[!] Invalid gate. Use one of the predefined or user defined gates.\n");
408+
exit(-1);
409+
}
410+
411+
if(keytypes[i].type != COMMA && keytypes[i].type != IDX_BRACKET_OPEN && keytypes[i].type != IDX_BRACKET_CLOSE) {
412+
cur_qubit_idx += 1;
413+
}
414+
i++;
415+
}
317416
}
318417

319418
#endif

README.md

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
# Qua-C
22

3-
Qua-C ( pronounced as /'kwɑ-si/) is a *domain specific programming language* for simulating **Quantum Computing Algorithms.**
3+
Qua-C (pronounced as /'kwɑ-si/) is a *domain specific programming language* for simulating **Quantum Computing Algorithms.**
44

55
Qua-C is a language under development to make the process of coding a Quantum Algorithm easy and in a visual syntax format.
66

7-
Qua-C has a different ideology from conventional programming languages, which have a sequential syntax format to apply functions and gates to data. Instead of a sequential construct, Qua-C executes code pseudo-parallel fashion for the set of qubits in the quregister.
7+
Qua-C has a different ideology from conventional programming languages, which have a sequential syntax format to apply functions and gates to data. Instead of a sequential construct, Qua-C executes code column-wise manner for a set of qubits in the quregister.
88

99
**The Qua-C Transpiler is developed completely in 'C' language currently.**
1010

1111
Also see the 'Examples' folder to know more about the functionality. The 'Examples' folder contains various examples like:-
1212

13-
1. CNOT Gate
14-
2. Hadamard Gate
15-
3. Rotation of qubits about X, Y, Z - Axes of Bloch Sphere
16-
4. Quantum NOT Gate
17-
5. Quantum Registers
18-
6. Quantum Rotation about imaginary axis
19-
7. Quantum swapping of two Qubits
13+
1. Basic Example
14+
2. **Grover search** for 2 qubits
15+
3. Hadamard Gate mania
16+
4. **Shor's period finding** algorithm
17+
5. **Grover Search** for 5 qubits with **custom gates**
2018

2119

2220
## Quantum Registers
@@ -25,21 +23,27 @@ This feature allows the use to make registers consisting of multiple qubits at a
2523

2624
To make the coding of these gates easier, a functionality of short-hand gates is provided. The gates can be applied to the register as follows:-
2725

28-
*** example.qc ***
26+
### example.qc ###
2927

30-
func test {
31-
Pa()
32-
[X, X]
33-
[Z, Y]
28+
gate MyOwnGate[2] {
3429
[H, H]
35-
[X, Z]
36-
[Y, X]
3730
}
3831

39-
quReg a = init(2)
40-
a.name = "quReg1: "
41-
a.prec = 6
32+
func test {
33+
[X, X] Pa()
34+
[Z, Y] Pa()
35+
[H, H] Pa()
36+
[X, Z] Pa()
37+
[Y, X] Pa()
38+
[MyOwnGate]
39+
}
4240

41+
#### Initialize ####
42+
quReg a = new quReg[2] => 'myqureg1'
43+
a.setPrecision(6)
44+
45+
#### Simulate ####
46+
a.Pnz()
4347
a.test()
4448
a.Pnz()
4549

@@ -48,48 +52,84 @@ OUTPUT:
4852

4953
./quacc example.qc
5054

51-
quReg2 =
52-
[0] => {1.000000 + 0.000000 i} |00> 100.00 %
53-
[1] => {0.000000 + 0.000000 i} |01> 0.00 %
54-
[2] => {0.000000 + 0.000000 i} |10> 0.00 %
55-
[3] => {0.000000 + 0.000000 i} |11> 0.00 %
55+
[+] Parsing Successful...!!!
56+
57+
Executing program...
58+
myqureg1 =
59+
[0] => {1.000000 + 0.000000 i} |00> 100.000000 %
60+
61+
62+
myqureg1 =
63+
[0] => {0.000000 + 0.000000 i} |00> 0.000000 %
64+
[1] => {0.000000 + 0.000000 i} |01> 0.000000 %
65+
[2] => {0.000000 + 0.000000 i} |10> 0.000000 %
66+
[3] => {1.000000 + 0.000000 i} |11> 100.000000 %
67+
68+
69+
myqureg1 =
70+
[0] => {0.000000 + 0.000000 i} |00> 0.000000 %
71+
[1] => {-0.000000 + 1.000000 i} |01> 100.000000 %
72+
[2] => {-0.000000 + 0.000000 i} |10> 0.000000 %
73+
[3] => {0.000000 + 0.000000 i} |11> 0.000000 %
5674

57-
quReg2 =
58-
[0] => {-0.000000 - 0.500000 i} |00> 25.00 %
59-
[1] => {-0.000000 + 0.500000 i} |01> 25.00 %
60-
[2] => {-0.000000 - 0.500000 i} |10> 25.00 %
61-
[3] => {-0.000000 + 0.500000 i} |11> 25.00 %
75+
76+
myqureg1 =
77+
[0] => {0.000000 + 0.500000 i} |00> 25.000000 %
78+
[1] => {0.000000 - 0.500000 i} |01> 25.000000 %
79+
[2] => {0.000000 + 0.500000 i} |10> 25.000000 %
80+
[3] => {0.000000 - 0.500000 i} |11> 25.000000 %
81+
82+
83+
myqureg1 =
84+
[0] => {0.000000 - 0.500000 i} |00> 25.000000 %
85+
[1] => {0.000000 + 0.500000 i} |01> 25.000000 %
86+
[2] => {-0.000000 + 0.500000 i} |10> 25.000000 %
87+
[3] => {-0.000000 - 0.500000 i} |11> 25.000000 %
88+
89+
90+
myqureg1 =
91+
[0] => {-0.500000 + 0.000000 i} |00> 25.000000 %
92+
[1] => {-0.500000 + 0.000000 i} |01> 25.000000 %
93+
[2] => {0.500000 + 0.000000 i} |10> 25.000000 %
94+
[3] => {0.500000 + 0.000000 i} |11> 25.000000 %
95+
96+
97+
myqureg1 =
98+
[2] => {-1.000000 + 0.000000 i} |10> 100.000000 %
99+
100+
101+
Time taken: 0 min, 0.000124 sec
62102

63103

64104
The above code segment has the following meaning:-
65105
- Initialize a new quRegister.
66-
- Display text for the quRegister is "quReg1: ".
67-
- Pa() -> Print all possible combinations of 'n' qubits in the quRegister.
106+
- Display text for the quRegister is "quReg1".
107+
- Pnz() -> Print only non-zero magnitude values of the 2 qubit quRegister.
68108
- Pauli gate 'X' is applied to [qubit[0], qubit[1]] respectively.
109+
- Print all states in the quRegister.
69110
- Pauli gate 'Z' is applied to qubit[0] and 'Y' to qubit[1] respectively, and so on.
70111
- Pnz() -> Print only non-zero magnitude values.
71112

72113
## List of short-hand gates
73114
- Hadamard Gate: **H**
74115
- Phase Gate: **S**
75-
- Rotation Gate: **R(x)**
116+
- Rotation Gate: **R_k**
76117

77-
x: Angle value (in degrees)(Int/Float)
78-
- CNOT Gate:
79-
- Control gate: **Co**
80-
- NOT gate: **Cx**
118+
k: power of n'th root of unity (w)
119+
- Control gate: **@**
120+
- Inverse Control gate: **o**
81121
- NOOP Gate: **-**
82122
- Pauli Gates
83123
- Pauli X gate: **X**
84124
- Pauli Y gate: **Y**
85125
- Pauli Z gate: **Z**
86-
- Swap Gate: **Sx**
87-
- Quantum Fourier Transform (QFT) Gate: **Q**
88-
- Inverse QFT Gate: **Qi**
126+
- Swap Gate: **x**
127+
- Quantum Fourier Transform (QFT) Gate: **~**
128+
89129

90-
*For more info, see the examples: https://github.com/AbeerVaishnav13/qua-C/tree/master/Examples*
130+
**For more info, see the examples:** *https://github.com/AbeerVaishnav13/qua-C/tree/master/Examples*
91131

92-
*\*Currently, the language supports registers upto 28-qubits with 8GB of memory.*
132+
**Currently, the language supports registers upto 29-qubits with 8GB of memory.**
93133

94134
## Installation Instructions
95135

@@ -104,7 +144,7 @@ The dependencies for QuaC language is the **gcc** compiler and **cmake**.
104144

105145
sudo brew install gcc cmake
106146

107-
(iii) For Windows platform - install MINGW GCC for windows or even better, install any Linux distribution of your choice :)
147+
(iii) For Windows platform - install MINGW GCC for windows or even better, install any Linux distribution of your choice :)
108148

109149

110150
### Install the compiler

0 commit comments

Comments
 (0)