@@ -74,9 +74,7 @@ module.exports = class Application {
74
74
const tool = document . createElement ( 'div' ) ;
75
75
tool . dataset . type = name ;
76
76
tool . className = "gate" ;
77
- if ( title ) {
78
- tool . title = title ;
79
- }
77
+ tool . title = title || '' ;
80
78
draw . clear ( ) ;
81
79
if ( name == 'swap' ) {
82
80
draw . swap ( 20 , 20 ) ;
@@ -115,57 +113,65 @@ module.exports = class Application {
115
113
if ( json . gates ) {
116
114
for ( let i = 0 ; i < json . gates . length ; i ++ ) {
117
115
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
- }
127
116
this . workspace . addGate ( {
128
117
name : gate . name ,
129
118
qubits : gate . qubits ,
130
119
matrix : gate . matrix ,
131
120
fn : gate . fn ,
132
121
title : gate . title ,
133
- circuit : circuit
122
+ circuit : Circuit . load ( this , gate . qubits , gate . circuit )
134
123
} ) ;
135
124
}
136
125
}
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 ) ;
146
127
this . editor . resize ( this . circuit . nqubits , this . editor . length ) ;
147
128
this . editor . input = json . input ;
148
129
document . querySelector ( '#nqubits > span' ) . innerHTML = 'Qubits: ' + this . circuit . nqubits ;
149
130
this . compileAll ( ) ;
150
131
this . editor . render ( ) ;
151
132
}
152
133
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
+
153
161
/*
154
162
Asynchronously compile every user defined gate in the workspace.
155
- XXX: This should probably be a method of Workspace
156
163
*/
157
164
compileAll ( ) {
158
165
const app = this ;
159
166
const todo = [ ] ;
160
167
const workspace = this . workspace ;
161
168
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 ] ;
164
170
if ( ! type . matrix ) {
165
171
todo . push ( type ) ;
166
172
}
167
173
} ) ;
168
- ( function loop ( i ) {
174
+ const loop = i => {
169
175
if ( i < todo . length ) {
170
176
const n = Math . pow ( 2 , todo [ i ] . circuit . nqubits ) ;
171
177
const I = new numeric . T (
@@ -174,12 +180,11 @@ module.exports = class Application {
174
180
) ;
175
181
app . applyCircuit ( todo [ i ] . circuit , I , U => {
176
182
todo [ i ] . matrix = U ;
177
- setTimeout ( function ( ) {
178
- loop ( i + 1 ) ;
179
- } , 5 ) ;
183
+ setTimeout ( ( ) => loop ( i + 1 ) , 1 ) ;
180
184
} ) ;
181
185
}
182
- } ) ( 0 ) ;
186
+ } ;
187
+ loop ( 0 ) ;
183
188
}
184
189
185
190
/*
@@ -202,7 +207,7 @@ module.exports = class Application {
202
207
203
208
204
209
/*
205
- Search ancestors in DOM.
210
+ Search for ancestor in DOM.
206
211
*/
207
212
const findParent = ( el , test ) => {
208
213
while ( el . parentNode && ! test ( el ) ) {
0 commit comments