-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQCircuit.c
61 lines (53 loc) · 2.23 KB
/
QCircuit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "QCircuit.h"
QCircuit* QCircuit_new(){
QCircuit * circ = malloc(sizeof(QCircuit));
circ->length = 0;
circ->capacity = 128;
circ->tape = calloc(circ->capacity, sizeof(Gate));
return circ;
}
int QCircuit_free(QCircuit * circ){
if(circ != NULL){
free(circ->tape);
free(circ);
circ = NULL;
}
return 0;
}
/*
* Appends a char to the end of the tape
* Doubling the length of the tape if necessary
*/
int QCircuit_append(QCircuit * circ, Gate g){
if(circ->length == circ->capacity)
{
circ->capacity *= 2;
circ->tape = realloc(circ->tape, circ->capacity*sizeof(Gate));
}
circ->tape[circ->length] = g;
circ->length += 1;
return 0;
}
QCircuit * QCircuit_daggered(QCircuit * circ){
QCircuit * new = QCircuit_new();
new->capacity = circ->length;
new->tape = realloc(new->tape, new->capacity*sizeof(Gate));
for(int i = 0; i <circ->length; i++){
if(circ->tape[circ->length-i-1].tag == S){
QCircuit_append(new, (Gate){.tag=S, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=S, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=S, .target=circ->tape[circ->length-i-1].target, .control = 0});
}else if(circ->tape[circ->length-i-1].tag == T){
QCircuit_append(new, (Gate){.tag=T, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=T, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=T, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=T, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=T, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=T, .target=circ->tape[circ->length-i-1].target, .control = 0});
QCircuit_append(new, (Gate){.tag=T, .target=circ->tape[circ->length-i-1].target, .control = 0});
}else{ //other gates are their own inverse
QCircuit_append(new, (Gate){.tag=circ->tape[circ->length-i-1].tag, .target=circ->tape[circ->length-i-1].target, .control=circ->tape[circ->length-i-1].control});
}
}
return new;
}