Skip to content

Commit d5828fc

Browse files
committed
redid application
1 parent 7eb04da commit d5828fc

8 files changed

+956
-940
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
A browser based quantum circuit editor and simulator.
44

5-
6-
5+
Application here: [http://davy.wtf/quantum](http://davy.wtf/quantum)

index.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
Workspace
1515
<ul>
1616
<li><a href="#" onclick="window.open(window.location)">New</a></li>
17+
<li><a id="importJSON" href="#">Load</a></li>
18+
<li><a id="exportJSON" href="#">Save</a></li>
1719
<li>
1820
Examples
1921
<ul id="examples">
2022
</ul>
2123
</li>
22-
<li><a id="importJSON" href="#">Import JSON</a></li>
23-
<li><a id="exportJSON" href="#">Export JSON</a></li>
2424
</ul>
2525
</li>
2626
<li>
@@ -51,7 +51,6 @@
5151
<li><a id="reset" href="#">Reset</a></li>
5252
</ul>
5353
</li>
54-
<li style="float:left"><a id="about" href="/quantum/tutorial">Tutorial</a></li>
5554
<li style="float:right"><a id="about" href="#">About</a></li>
5655
</ul>
5756
</div>

js/application.js

+36-31
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ module.exports = class Application {
7474
const tool = document.createElement('div');
7575
tool.dataset.type = name;
7676
tool.className = "gate";
77-
if (title) {
78-
tool.title = title;
79-
}
77+
tool.title = title || '';
8078
draw.clear();
8179
if (name == 'swap') {
8280
draw.swap(20, 20);
@@ -115,57 +113,65 @@ module.exports = class Application {
115113
if (json.gates) {
116114
for (let i = 0 ; i < json.gates.length; i++) {
117115
const gate = json.gates[i];
118-
const circuit = new Circuit(this, gate.qubits);
119-
for (let j = 0; j < gate.circuit.length; j++) {
120-
circuit.addGate(new Gate(
121-
this.workspace.gates[gate.circuit[j].type],
122-
gate.circuit[j].time + 1,
123-
gate.circuit[j].targets,
124-
gate.circuit[j].controls
125-
));
126-
}
127116
this.workspace.addGate({
128117
name: gate.name,
129118
qubits: gate.qubits,
130119
matrix: gate.matrix,
131120
fn: gate.fn,
132121
title: gate.title,
133-
circuit: circuit
122+
circuit: Circuit.load(this, gate.qubits, gate.circuit)
134123
});
135124
}
136125
}
137-
this.circuit = new Circuit(this, json.qubits);
138-
for (let j = 0; j < json.circuit.length; j++) {
139-
this.circuit.addGate(new Gate(
140-
this.workspace.gates[json.circuit[j].type],
141-
json.circuit[j].time + 1,
142-
json.circuit[j].targets,
143-
json.circuit[j].controls
144-
));
145-
}
126+
this.circuit = Circuit.load(this, json.qubits, json.circuit);
146127
this.editor.resize(this.circuit.nqubits, this.editor.length);
147128
this.editor.input = json.input;
148129
document.querySelector('#nqubits > span').innerHTML = 'Qubits: ' + this.circuit.nqubits;
149130
this.compileAll();
150131
this.editor.render();
151132
}
152133

134+
/*
135+
Return object representation of workspace capable of being exported to JSON.
136+
*/
137+
exportWorkspace() {
138+
const workspace = this.workspace;
139+
this.circuit.gates.sort((a, b) => a.time - b.time);
140+
const gates = [];
141+
for (let key in workspace.gates) {
142+
const gate = workspace.gates[key];
143+
if (gate.std) {
144+
continue;
145+
}
146+
gates.push({
147+
name: key,
148+
qubits: gate.circuit.nqubits,
149+
circuit: gate.circuit.toJSON(),
150+
title: ''
151+
});
152+
}
153+
return {
154+
gates: gates,
155+
circuit: this.circuit.toJSON(),
156+
qubits: this.circuit.nqubits,
157+
input: this.editor.input
158+
};
159+
}
160+
153161
/*
154162
Asynchronously compile every user defined gate in the workspace.
155-
XXX: This should probably be a method of Workspace
156163
*/
157164
compileAll() {
158165
const app = this;
159166
const todo = [];
160167
const workspace = this.workspace;
161168
document.querySelectorAll('#toolbar .user div.gate').forEach(el => {
162-
const name = el.dataset.type;
163-
const type = workspace.gates[name];
169+
const type = workspace.gates[el.dataset.type];
164170
if (!type.matrix) {
165171
todo.push(type);
166172
}
167173
});
168-
(function loop(i) {
174+
const loop = i => {
169175
if (i < todo.length) {
170176
const n = Math.pow(2, todo[i].circuit.nqubits);
171177
const I = new numeric.T(
@@ -174,12 +180,11 @@ module.exports = class Application {
174180
);
175181
app.applyCircuit(todo[i].circuit, I, U => {
176182
todo[i].matrix = U;
177-
setTimeout(function() {
178-
loop(i + 1);
179-
}, 5);
183+
setTimeout(() => loop(i + 1), 1);
180184
});
181185
}
182-
})(0);
186+
};
187+
loop(0);
183188
}
184189

185190
/*
@@ -202,7 +207,7 @@ module.exports = class Application {
202207

203208

204209
/*
205-
Search ancestors in DOM.
210+
Search for ancestor in DOM.
206211
*/
207212
const findParent = (el, test) => {
208213
while (el.parentNode && !test(el)) {

0 commit comments

Comments
 (0)