File size: 14,924 Bytes
227c43a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import React, { useEffect, useRef, useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { BarChart3, Activity, Zap, Info, Maximize2 } from 'lucide-react';
import PropTypes from 'prop-types';
import FullscreenCircuitViewer from './FullscreenCircuitViewer';

const QuantumVisualization = ({ result, type = 'auto' }) => {
  const [activeVisualization, setActiveVisualization] = useState('circuit');
  const [showFullscreen, setShowFullscreen] = useState(false);

  // Bloch Sphere Visualization
  const BlochSphere = ({ stateVector }) => {
    const canvasRef = useRef(null);

    useEffect(() => {
      if (!canvasRef.current || !stateVector) return;

      const canvas = canvasRef.current;
      const ctx = canvas.getContext('2d');
      const centerX = canvas.width / 2;
      const centerY = canvas.height / 2;
      const radius = Math.min(centerX, centerY) - 20;

      // Clear canvas
      ctx.clearRect(0, 0, canvas.width, canvas.height);

      // Draw sphere outline
      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
      ctx.strokeStyle = '#e5e7eb';
      ctx.lineWidth = 2;
      ctx.stroke();

      // Draw axes
      ctx.strokeStyle = '#9ca3af';
      ctx.lineWidth = 1;
      
      // X axis
      ctx.beginPath();
      ctx.moveTo(centerX - radius, centerY);
      ctx.lineTo(centerX + radius, centerY);
      ctx.stroke();
      
      // Y axis (vertical)
      ctx.beginPath();
      ctx.moveTo(centerX, centerY - radius);
      ctx.lineTo(centerX, centerY + radius);
      ctx.stroke();

      // Calculate state vector position on Bloch sphere
      if (stateVector && stateVector.length >= 2) {
        const alpha = stateVector[0];
        const beta = stateVector[1];
        
        // Convert to Bloch sphere coordinates
        const theta = 2 * Math.acos(Math.abs(alpha));
        const phi = Math.arg ? Math.arg(beta / alpha) : 0;
        
        const x = radius * Math.sin(theta) * Math.cos(phi);
        const z = radius * Math.cos(theta);

        // Project to 2D (simple projection)
        const projX = centerX + x;
        const projY = centerY - z; // Flip Y for canvas coordinates

        // Draw state vector
        ctx.beginPath();
        ctx.moveTo(centerX, centerY);
        ctx.lineTo(projX, projY);
        ctx.strokeStyle = '#6366f1';
        ctx.lineWidth = 3;
        ctx.stroke();

        // Draw state point
        ctx.beginPath();
        ctx.arc(projX, projY, 6, 0, 2 * Math.PI);
        ctx.fillStyle = '#6366f1';
        ctx.fill();
      }

      // Add labels
      ctx.fillStyle = '#374151';
      ctx.font = '12px Inter';
      ctx.textAlign = 'center';
      ctx.fillText('|0⟩', centerX, centerY - radius - 10);
      ctx.fillText('|1⟩', centerX, centerY + radius + 20);
      ctx.textAlign = 'left';
      ctx.fillText('|+⟩', centerX + radius + 10, centerY + 5);
      ctx.textAlign = 'right';
      ctx.fillText('|-⟩', centerX - radius - 10, centerY + 5);
    }, [stateVector]);

    return (
      <canvas

        ref={canvasRef}

        width={300}

        height={300}

        className="border border-neutral-200 dark:border-neutral-600 rounded-lg bg-base-100 dark:bg-base-200"

      />
    );
  };

  // Probability Bar Chart
  const ProbabilityChart = ({ probabilities }) => {
    if (!probabilities || typeof probabilities !== 'object') return null;

    const entries = Object.entries(probabilities);
    const maxProb = Math.max(...entries.map(([, prob]) => prob));

    return (
      <div className="space-y-3">

        {entries.map(([state, probability]) => (

          <motion.div

            key={state}

            initial={{ opacity: 0, x: -20 }}

            animate={{ opacity: 1, x: 0 }}

            className="flex items-center gap-3"

          >

            <div className="w-12 text-sm font-mono text-neutral-600">|{state}⟩</div>

            <div className="flex-1 bg-base-200 dark:bg-neutral-800 rounded-full h-6 relative overflow-hidden">

              <motion.div

                initial={{ width: 0 }}

                animate={{ width: `${(probability / maxProb) * 100}%` }}

                transition={{ duration: 0.8, ease: "easeOut" }}

                className="h-full bg-gradient-to-r from-primary-400 to-primary-600 rounded-full"

              />

              <div className="absolute inset-0 flex items-center justify-center text-xs font-medium text-neutral-700">

                {(probability * 100).toFixed(1)}%

              </div>

            </div>

          </motion.div>

        ))}

      </div>
    );
  };

  // Circuit SVG Display
  const CircuitDisplay = ({ circuitSvg }) => {
    const [zoom, setZoom] = useState(1);
    const [isDragging, setIsDragging] = useState(false);
    const [dragStart, setDragStart] = useState({ x: 0, y: 0 });
    const [scrollPos, setScrollPos] = useState({ x: 0, y: 0 });
    const containerRef = useRef(null);

    if (!circuitSvg) return null;

    const handleZoomIn = () => setZoom(prev => Math.min(prev + 0.2, 3));
    const handleZoomOut = () => setZoom(prev => Math.max(prev - 0.2, 0.5));
    const handleResetZoom = () => setZoom(1);

    const handleMouseDown = (e) => {
      setIsDragging(true);
      setDragStart({ x: e.clientX, y: e.clientY });
    };

    const handleMouseMove = (e) => {
      if (!isDragging || !containerRef.current) return;
      
      const deltaX = e.clientX - dragStart.x;
      const deltaY = e.clientY - dragStart.y;
      
      containerRef.current.scrollLeft = scrollPos.x - deltaX;
      containerRef.current.scrollTop = scrollPos.y - deltaY;
    };

    const handleMouseUp = () => {
      if (containerRef.current) {
        setScrollPos({
          x: containerRef.current.scrollLeft,
          y: containerRef.current.scrollTop
        });
      }
      setIsDragging(false);
    };

    return (
      <motion.div

        initial={{ opacity: 0, scale: 0.95 }}

        animate={{ opacity: 1, scale: 1 }}

        className="bg-base-100 dark:bg-base-200 p-6 rounded-xl border border-neutral-200 dark:border-neutral-700 overflow-hidden"

      >

        <div className="mb-4 flex items-center justify-between">

          <div>

            <h4 className="text-sm font-medium text-neutral-700 dark:text-neutral-300 mb-1">

              Quantum Circuit Diagram

            </h4>

            <p className="text-xs text-neutral-500 dark:text-neutral-400">

              Scroll or drag to navigate • Use zoom controls for better viewing

            </p>

          </div>

          

          {/* Controls */}

          <div className="flex items-center gap-2">

            {/* Zoom Controls */}

            <div className="flex items-center gap-1 sm:gap-2 bg-base-200 dark:bg-neutral-800 rounded-lg p-1">

              <button

                onClick={handleZoomOut}

                className="p-2 sm:p-1 hover:bg-neutral-200 dark:hover:bg-neutral-700 rounded text-neutral-600 dark:text-neutral-400 transition-colors min-h-[44px] sm:min-h-[32px] min-w-[44px] sm:min-w-[32px] flex items-center justify-center"

                title="Zoom Out"

              >

                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">

                  <circle cx="11" cy="11" r="8"/>

                  <path d="M21 21l-4.35-4.35"/>

                  <line x1="8" y1="11" x2="14" y2="11"/>

                </svg>

              </button>

              

              <span className="text-xs text-neutral-600 dark:text-neutral-400 min-w-[3rem] text-center px-1">

                {Math.round(zoom * 100)}%

              </span>

              

              <button

                onClick={handleZoomIn}

                className="p-2 sm:p-1 hover:bg-neutral-200 dark:hover:bg-neutral-700 rounded text-neutral-600 dark:text-neutral-400 transition-colors min-h-[44px] sm:min-h-[32px] min-w-[44px] sm:min-w-[32px] flex items-center justify-center"

                title="Zoom In"

              >

                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">

                  <circle cx="11" cy="11" r="8"/>

                  <path d="M21 21l-4.35-4.35"/>

                  <line x1="8" y1="11" x2="14" y2="11"/>

                  <line x1="11" y1="8" x2="11" y2="14"/>

                </svg>

              </button>

              

              <button

                onClick={handleResetZoom}

                className="p-2 sm:p-1 hover:bg-neutral-200 dark:hover:bg-neutral-700 rounded text-neutral-600 dark:text-neutral-400 transition-colors text-xs min-h-[44px] sm:min-h-[32px] px-2 sm:px-1"

                title="Reset Zoom"

              >

                <span className="hidden sm:inline">Reset</span>

                <span className="sm:hidden"></span>

              </button>

            </div>

            

            {/* Fullscreen Button */}

            <button

              onClick={() => setShowFullscreen(true)}

              className="p-2 bg-primary-500 hover:bg-primary-600 text-white rounded-lg transition-colors min-h-[44px] sm:min-h-[40px] min-w-[44px] sm:min-w-[40px] flex items-center justify-center"

              title="View in Fullscreen"

            >

              <Maximize2 size={16} />

            </button>

          </div>

        </div>

        

        <div 

          ref={containerRef}

          className="overflow-auto bg-base-100 dark:bg-neutral-800 rounded-lg border border-neutral-100 dark:border-neutral-600 p-2 sm:p-4 cursor-grab active:cursor-grabbing touch-pan-x touch-pan-y"

          style={{ 

            maxHeight: '400px',

            scrollbarWidth: 'thin',

            scrollbarColor: '#cbd5e1 #f1f5f9',

            WebkitOverflowScrolling: 'touch'

          }}

          onMouseDown={handleMouseDown}

          onMouseMove={handleMouseMove}

          onMouseUp={handleMouseUp}

          onMouseLeave={handleMouseUp}

          onTouchStart={(e) => {

            const touch = e.touches[0];

            handleMouseDown({ clientX: touch.clientX, clientY: touch.clientY });

          }}

          onTouchMove={(e) => {

            const touch = e.touches[0];

            handleMouseMove({ clientX: touch.clientX, clientY: touch.clientY });

          }}

          onTouchEnd={handleMouseUp}

        >

          <div

            dangerouslySetInnerHTML={{ __html: circuitSvg }}

            className="min-w-max transition-transform duration-200"

            style={{ 

              minWidth: 'fit-content',

              display: 'inline-block',

              transform: `scale(${zoom})`,

              transformOrigin: 'top left'

            }}

          />

        </div>

        

        {/* Navigation hints */}

        <div className="mt-3 flex items-center justify-between text-xs text-neutral-400 dark:text-neutral-500">

          <span className="flex items-center gap-1">

            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">

              <path d="M9 11H1m22 0h-8M9 11l3-3m-3 3l3 3"/>

            </svg>

            <span className="hidden sm:inline">Drag to pan • Scroll to navigate</span>

            <span className="sm:hidden">Drag or swipe to navigate</span>

          </span>

          

          <span className="flex items-center gap-1">

            <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">

              <circle cx="11" cy="11" r="8"/>

              <path d="M21 21l-4.35-4.35"/>

            </svg>

            Use zoom controls for detail

          </span>

        </div>

      </motion.div>
    );
  };

  const visualizations = [
    {
      id: 'circuit',
      label: 'Circuit',
      icon: <Activity size={16} />,
      component: <CircuitDisplay circuitSvg={result?.output?.circuit_svg} />,
      available: !!result?.output?.circuit_svg,
    },
    {
      id: 'probabilities',
      label: 'Probabilities',
      icon: <BarChart3 size={16} />,
      component: <ProbabilityChart probabilities={result?.output?.probabilities} />,
      available: !!result?.output?.probabilities,
    },
    {
      id: 'bloch',
      label: 'Bloch Sphere',
      icon: <Zap size={16} />,
      component: <BlochSphere stateVector={result?.output?.state_vector} />,
      available: !!result?.output?.state_vector,
    },
  ];

  const availableVisualizations = visualizations.filter(viz => viz.available);

  if (!result || availableVisualizations.length === 0) {
    return (
      <div className="flex flex-col items-center justify-center py-12 text-neutral-500">

        <Info size={48} className="mb-4 opacity-50" />

        <p className="text-lg font-medium">No visualization data available</p>

        <p className="text-sm">Run a simulation to see results</p>

      </div>
    );
  }

  return (
    <div className="space-y-6">

      {availableVisualizations.length > 1 && (

        <div className="flex gap-2 p-1 bg-base-200 rounded-lg">

          {availableVisualizations.map((viz) => (

            <button

              key={viz.id}

              onClick={() => setActiveVisualization(viz.id)}

              className={`

                flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-all

                ${activeVisualization === viz.id

                  ? 'bg-base-100 text-neutral-900 dark:text-neutral-100 shadow-sm'

                  : 'text-neutral-600 dark:text-neutral-300 hover:text-neutral-900 dark:hover:text-neutral-100 hover:bg-base-100/50'

                }

              `}

            >

              {viz.icon}

              {viz.label}

            </button>

          ))}

        </div>

      )}



      <AnimatePresence mode="wait">

        {availableVisualizations.map((viz) => (

          viz.id === activeVisualization && (

            <motion.div

              key={viz.id}

              initial={{ opacity: 0, y: 20 }}

              animate={{ opacity: 1, y: 0 }}

              exit={{ opacity: 0, y: -20 }}

              transition={{ duration: 0.3 }}

            >

              {viz.component}

            </motion.div>

          )

        ))}

      </AnimatePresence>



      {/* Fullscreen Circuit Viewer */}

      <FullscreenCircuitViewer

        circuitSvg={result?.output?.circuit_svg}

        isOpen={showFullscreen}

        onClose={() => setShowFullscreen(false)}

      />

    </div>
  );
};

QuantumVisualization.propTypes = {
  result: PropTypes.object,
  type: PropTypes.string,
};

export default QuantumVisualization;