-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcircuit.cpp
239 lines (204 loc) · 5.77 KB
/
circuit.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*--------------------------------------------------------------------
QCViewer
Copyright (C) 2011 Alex Parent and The University of Waterloo,
Institute for Quantum Computing, Quantum Circuits Group
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
See also 'ADDITIONAL TERMS' at the end of the included LICENSE file.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
QCViewer is a trademark of the of the The University of Waterloo,
Institute for Quantum Computing, Quantum Circuits Group
Authors: Alex Parent, Jacob Parker, Marc Burns
---------------------------------------------------------------------*/
#include "circuit.h"
#include "subcircuit.h"
#include "utility.h"
#include <map>
#include <fstream>
#include <iostream>
using namespace std;
Circuit::Circuit()
{
allExpanded = false;
}
Circuit::~Circuit ()
{ }
void Circuit::expandAll()
{
allExpanded = !(allExpanded);
for(unsigned int i = 0; i<numGates(); i++) {
shared_ptr<Gate> g = getGate(i);
shared_ptr<Subcircuit> s = dynamic_pointer_cast<Subcircuit>(g);
if (s) {
s->expand = allExpanded;
}
}
for ( map<string,std::shared_ptr<Circuit>>::iterator it = subcircuits.begin(); it != subcircuits.end(); ++it) {
it->second->expandAll();
}
}
void Circuit::swapGate (unsigned int i, unsigned int j)
{
swap(gates[i],gates[j]);
}
void Circuit::addGate(shared_ptr<Gate> newGate)
{
gates.push_back(newGate);
}
void Circuit::addGate(std::shared_ptr<Gate> newGate, unsigned int pos)
{
gates.insert(gates.begin()+pos, newGate);
}
void Circuit::setGate(std::shared_ptr<Gate> newGate, unsigned int pos)
{
gates.at(pos) = newGate;
}
void Circuit::removeGate (unsigned int pos)
{
gates.erase (gates.begin () + pos);
}
std::shared_ptr<Gate> Circuit::getGate(int pos) const
{
return gates.at(pos);
}
unsigned int Circuit::numGates() const
{
return gates.size();
}
unsigned int Circuit::totalGates() const
{
unsigned int numGates = 0;
for(unsigned int i = 0; i < gates.size(); i++) {
numGates += gates.at(i)->getNumGates();
}
return numGates;
}
unsigned int Circuit::gateCount(string gateName)
{
unsigned int numGates = 0;
for(unsigned int i = 0; i < gates.size(); i++) {
shared_ptr<Subcircuit> s = dynamic_pointer_cast<Subcircuit>(gates.at(i));
if (s) {
numGates += s->getCircuit()->gateCount(gateName);
} else if (gates.at(i)->getName().compare(gateName) == 0) {
numGates++;
}
}
return numGates;
}
unsigned int Circuit::depth()
{
unsigned int depth = 0;
for(unsigned int i = 0; i < gates.size(); i++) {
shared_ptr<Subcircuit> s = dynamic_pointer_cast<Subcircuit>(gates.at(i));
if (s) {
depth += s->getCircuit()->depth()-1;
}
}
depth += getParallel().size() + 1;
return depth;
}
unsigned int Circuit::tDepth()
{
unsigned int depth = 0;
for(unsigned int i = 0; i < gates.size(); i++) {
shared_ptr<Subcircuit> s = dynamic_pointer_cast<Subcircuit>(gates.at(i));
string gName = gates.at(i)->getName();
if (s) {
depth += s->getCircuit()->tDepth();
} else if (gName.compare("T") == 0 || gName.compare("T*") == 0 ) {
depth++;
while ((gName.compare("T") == 0 || gName.compare("T*") == 0) && i < gates.size()) {
i++;
gName = gates.at(i)->getName();
}
}
}
return depth;
}
int Circuit::QCost()
{
return 0;
}
string Line::getInputLabel() const
{
if (constant) {
return intToString(initValue);
}
return lineName;
}
string Line::getOutputLabel() const
{
if (garbage) {
return "Garbage";
}
if (outLabel.compare("")==0) {
return lineName;
}
return outLabel;
}
unsigned int Circuit::numLines() const
{
return lines.size();
}
const Line& Circuit::getLine(int pos) const
{
return lines.at(pos);
}
Line& Circuit::getLineModify(int pos)
{
return lines.at(pos);
}
Line::Line(string name)
{
lineName = name;
garbage = true;
constant = true;
initValue = 0;
}
void Circuit::addLine(string lineName)
{
lines.push_back(Line(lineName));
}
vector<int> Circuit::getParallel() const
{
vector<int> returnValue;
map<int,int> linesUsed;
for(unsigned int i = 0; i<numGates(); i++) {
shared_ptr<Gate> g = getGate(i);
start:
for(unsigned int j = 0; j < g->controls.size(); j++) {
if (linesUsed.find(g->controls[j].wire) != linesUsed.end()) {
returnValue.push_back(i - 1); //Push back the gate number before this one
linesUsed.clear();
goto start; //Go back to begining of main loop (redo this iteration because this gate is in the next block)
}
linesUsed[g->controls[j].wire];
}
for(unsigned int j = 0; j < g->targets.size(); j++) {
if (linesUsed.find(g->targets[j]) != linesUsed.end()) {
returnValue.push_back(i - 1);
linesUsed.clear();
goto start;
}
linesUsed[g->targets[j]];
}
}
returnValue.push_back(numGates()-1);
return returnValue;
}
void Circuit::setName(string n_name)
{
name = n_name;
}
string Circuit::getName()
{
return name;
}