-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
337 lines (308 loc) · 9.91 KB
/
Copy pathserver.js
File metadata and controls
337 lines (308 loc) · 9.91 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// @ts-check
const http = require("http");
const path = require("path");
const { spawnSync } = require("child_process");
const PORT = 3741;
// ─── Executa o circuito Python ──────────────────────────────────────────────
function runQuantumCircuit() {
const scriptPath = path.join(__dirname, "quantum_circuit.py");
const result = spawnSync("python", [scriptPath], { encoding: "utf8" });
if (result.status !== 0) throw new Error(result.stderr);
return JSON.parse(result.stdout.trim());
}
// ─── Gera barra de progresso SVG ────────────────────────────────────────────
function progressBar(pct, color) {
const w = Math.round(pct * 3.2); // max ~320px
return `<div class="bar-wrap">
<div class="bar" style="width:${w}px;background:${color};"></div>
<span>${pct.toFixed(1)}%</span>
</div>`;
}
// ─── Gera HTML completo ──────────────────────────────────────────────────────
function buildHTML(data) {
const counts = data.counts;
const probs = data.probabilities;
const total = data.shots;
const states = Object.keys(counts).sort();
const stateColors = {
"00": "#4ade80",
"11": "#60a5fa",
"01": "#f87171",
"10": "#facc15",
};
const barsHTML = states.map(s => {
const pct = (counts[s] / total) * 100;
const color = stateColors[s] || "#a78bfa";
return `
<div class="state-row">
<div class="state-label">|${s}⟩</div>
${progressBar(pct, color)}
<div class="state-count">${counts[s]} shots</div>
</div>`;
}).join("");
const gatesHTML = Object.entries(data.gates).map(([g, d]) => `
<div class="gate-card">
<span class="gate-name">${g}</span>
<span class="gate-desc">${d}</span>
</div>`).join("");
return `<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>⚛ Simulador Quântico — Hello World</title>
<style>
:root {
--bg: #0a0e1a;
--card: #111827;
--border: #1e3a5f;
--cyan: #22d3ee;
--yellow: #facc15;
--green: #4ade80;
--blue: #60a5fa;
--purple: #a78bfa;
--text: #e2e8f0;
--muted: #64748b;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: var(--bg);
color: var(--text);
font-family: 'Segoe UI', system-ui, sans-serif;
min-height: 100vh;
padding: 2rem;
}
/* ── Header ── */
.header {
text-align: center;
margin-bottom: 2.5rem;
}
.header h1 {
font-size: 2.4rem;
background: linear-gradient(135deg, var(--cyan), var(--yellow));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
letter-spacing: 0.04em;
}
.header p { color: var(--muted); margin-top: .5rem; font-size: .95rem; }
/* ── Grid ── */
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1.5rem;
max-width: 1000px;
margin: 0 auto;
}
@media (max-width: 720px) { .grid { grid-template-columns: 1fr; } }
.full { grid-column: 1 / -1; }
/* ── Card ── */
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 1rem;
padding: 1.5rem;
}
.card-title {
font-size: .75rem;
letter-spacing: .12em;
text-transform: uppercase;
color: var(--muted);
margin-bottom: 1rem;
}
/* ── Circuito ASCII ── */
.circuit-wrap {
overflow-x: auto;
}
pre.circuit {
font-family: 'Cascadia Code', 'Fira Code', 'Courier New', monospace;
font-size: 1rem;
color: var(--cyan);
line-height: 1.6;
white-space: pre;
}
/* ── Stats ── */
.stats { display: flex; gap: 2rem; flex-wrap: wrap; }
.stat { text-align: center; }
.stat .val {
font-size: 2rem;
font-weight: 700;
color: var(--yellow);
}
.stat .lbl { font-size: .8rem; color: var(--muted); margin-top: .2rem; }
/* ── Histograma ── */
.state-row {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: .85rem;
}
.state-label {
font-family: monospace;
font-size: 1.1rem;
color: var(--text);
min-width: 3rem;
}
.bar-wrap { display: flex; align-items: center; gap: .6rem; flex: 1; }
.bar {
height: 28px;
border-radius: 4px;
transition: width .5s ease;
min-width: 4px;
}
.bar-wrap span { font-size: .9rem; color: var(--text); white-space: nowrap; }
.state-count { font-size: .8rem; color: var(--muted); white-space: nowrap; }
/* ── Portas ── */
.gate-card {
display: flex;
align-items: center;
gap: 1rem;
padding: .7rem 0;
border-bottom: 1px solid var(--border);
}
.gate-card:last-child { border-bottom: none; }
.gate-name {
background: #1e3a5f;
color: var(--cyan);
font-family: monospace;
font-weight: 700;
padding: .3rem .7rem;
border-radius: .4rem;
min-width: 3.5rem;
text-align: center;
}
.gate-desc { color: var(--muted); font-size: .9rem; }
/* ── Bell state ── */
.bell {
font-family: 'Segoe UI', sans-serif;
font-size: 1.3rem;
text-align: center;
color: var(--text);
line-height: 2.2;
}
.bell strong { color: var(--cyan); }
.ket { color: var(--green); font-family: monospace; }
/* ── Hello World banner ── */
.hw-banner {
text-align: center;
padding: 1.5rem 0;
}
.hw-banner .title {
font-size: 2rem;
font-weight: 900;
background: linear-gradient(90deg, var(--yellow), var(--cyan), var(--purple));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
letter-spacing: .05em;
}
.hw-banner .sub {
margin-top: .6rem;
color: var(--purple);
font-size: 1rem;
}
.atom { font-size: 2.5rem; animation: spin 6s linear infinite; display: inline-block; }
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* ── Reload ── */
.reload-btn {
display: block;
margin: 1.5rem auto 0;
padding: .6rem 2rem;
border: 1px solid var(--cyan);
background: transparent;
color: var(--cyan);
border-radius: .5rem;
cursor: pointer;
font-size: .95rem;
letter-spacing: .05em;
transition: background .2s;
}
.reload-btn:hover { background: #0e3047; }
</style>
</head>
<body>
<div class="header">
<h1>⚛ Simulador Quântico</h1>
<p>TypeScript + Qiskit (Python) · Estado Bell Phi+ · ${total} shots</p>
</div>
<div class="grid">
<!-- Hello World -->
<div class="card full hw-banner">
<div class="atom">⚛</div>
<div class="title">Hello, Quantum World!</div>
<div class="sub">Superposição + Emaranhamento Quântico demonstrados com sucesso</div>
</div>
<!-- Circuito -->
<div class="card full">
<div class="card-title">Circuito Quântico</div>
<div class="circuit-wrap">
<pre class="circuit">${data.circuit_ascii.replace(/</g,"<")}</pre>
</div>
</div>
<!-- Stats -->
<div class="card">
<div class="card-title">Estatísticas</div>
<div class="stats">
<div class="stat"><div class="val">${data.num_qubits}</div><div class="lbl">Qubits</div></div>
<div class="stat"><div class="val">${data.depth}</div><div class="lbl">Profundidade</div></div>
<div class="stat"><div class="val">${total.toLocaleString()}</div><div class="lbl">Shots</div></div>
<div class="stat"><div class="val">${states.length}</div><div class="lbl">Estados</div></div>
</div>
</div>
<!-- Portas -->
<div class="card">
<div class="card-title">Portas Utilizadas</div>
${gatesHTML}
</div>
<!-- Histograma -->
<div class="card full">
<div class="card-title">Histograma de Medições</div>
${barsHTML}
</div>
<!-- Bell state -->
<div class="card full">
<div class="card-title">Interpretação Física — Estado Bell Φ⁺</div>
<div class="bell">
<strong>|Ψ⟩</strong> = <span style="color:var(--yellow)">1/√2</span>
<span class="ket">|00⟩</span>
+
<span style="color:var(--yellow)">1/√2</span>
<span class="ket">|11⟩</span>
<br>
Os dois qubits estão <strong>emaranhados</strong>: ao medir um, o outro
colapsa <em>instantaneamente</em> para o mesmo estado.
<br>
Resultados possíveis: apenas <span class="ket">|00⟩</span>
(${(probs["00"]||0).toFixed(1)}%) ou
<span class="ket">|11⟩</span> (${(probs["11"]||0).toFixed(1)}%) —
nunca opostos.
</div>
<button class="reload-btn" onclick="location.reload()">↻ Nova simulação</button>
</div>
</div>
</body>
</html>`;
}
// ─── Servidor HTTP ───────────────────────────────────────────────────────────
const server = http.createServer((req, res) => {
if (req.url !== "/") {
res.writeHead(404);
res.end("Not found");
return;
}
try {
const data = runQuantumCircuit();
const html = buildHTML(data);
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(html);
} catch (err) {
res.writeHead(500, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Erro ao executar circuito:\n" + err.message);
}
});
server.listen(PORT, () => {
console.log(`Servidor quântico rodando em http://localhost:${PORT}`);
});