forked from dakk/qc64
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqc.bas
More file actions
178 lines (159 loc) · 5.07 KB
/
qc.bas
File metadata and controls
178 lines (159 loc) · 5.07 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
05 rem r* are real parts and i* are imagenary parts of the sv
10 r0 = 1 : i0 = 0 : r1 = 0 : i1 = 0
15 r2 = 0 : i2 = 0 : r3 = 0 : i3 = 0
20 a0 = 0
25 shots = 28
27 rem setup colors and sound
28 poke 53280,6 : poke 53281,14 : rem border blue, bg light blue
29 poke 646,1 : rem text white
30 print chr$(147)
35 print chr$(18); : rem reverse on
40 print " c64 quantum simulator "
42 print chr$(146); : rem reverse off
45 print "created by davide gessa (dakk)"
46 print "enhanced with audio tracking"
47 print
50 poke 646,5 : rem text green
51 print "enter gate seq (x0,x1,y0,y1,z0,z1,h0,h1,cx,sw)"
60 poke 646,1 : input g$
65 poke 646,13 : print "calculating the statevector...";
70 for i = 1 to len(g$) step 2
80 gate$ = mid$(g$, i, 2)
90 gosub 200
91 print ".";
92 rem play sound for each gate
93 gosub 700
100 next i
101 poke 646,1 : print
102 sq = r0*r0 + i0*i0 + r1*r1 + i1*i1 + r2*r2 + i2*i2 + r3*r3 + i3*i3
103 if abs(sq - 1) > 0.00001 then gosub 600
105 poke 646,7 : print "running" shots "iterations..."
110 poke 646,1 : z0 = 0 : z1 = 0 : z2 = 0 : z3 = 0
115 p0 = (r0 * r0 + i0 * i0)
120 p1 = (r1 * r1 + i1 * i1) + p0
125 p2 = (r2 * r2 + i2 * i2) + p1
130 p3 = (r3 * r3 + i3 * i3) + p2
135 for i = 1 to shots
140 r = rnd(0)
141 if r < p0 then z0 = z0 + 1
142 if r >= p0 and r < p1 then z1 = z1 + 1
143 if r >= p1 and r < p2 then z2 = z2 + 1
144 if r >= p2 and r < p3 then z3 = z3 + 1
146 next i
150 print
151 poke 646,14 : print "results:"
152 poke 646,1
155 print "00: ["z0"] "; : poke 646,6 : for i = 1 to z0 : print chr$(81); : next i : print
160 poke 646,1 : print "10: ["z1"] "; : poke 646,5 : for i = 1 to z1 : print chr$(81); : next i : print
165 poke 646,1 : print "01: ["z2"] "; : poke 646,3 : for i = 1 to z2 : print chr$(81); : next i : print
170 poke 646,1 : print "11: ["z3"] "; : poke 646,2 : for i = 1 to z3 : print chr$(81); : next i : print
172 rem play completion sound
173 gosub 750
175 goto 1000
200 rem simulate gate operation
210 if gate$ = "x0" then gosub 400
220 if gate$ = "x1" then gosub 420
230 if gate$ = "y0" then gosub 440
240 if gate$ = "y1" then gosub 460
250 if gate$ = "z0" then gosub 480
260 if gate$ = "z1" then gosub 500
270 if gate$ = "h0" then gosub 520
280 if gate$ = "h1" then gosub 540
290 if gate$ = "cx" then gosub 560
300 if gate$ = "sw" then gosub 580
310 return
400 rem x0 gate
410 a0 = r0 : r0 = r1 : r1 = a0
411 a0 = i0 : i0 = i1 : i1 = a0
412 a0 = r2 : r2 = r3 : r3 = a0
413 a0 = i2 : i2 = i3 : i3 = a0
416 return
420 rem x1 gate
425 a0 = r1 : r1 = r3 : r3 = a0
426 a0 = i1 : i1 = i3 : i3 = a0
427 a0 = r0 : r0 = r2 : r2 = a0
428 a0 = i0 : i0 = i2 : i2 = a0
440 rem y0 gate
446 a0 = i0 : i0 = -r0 : r0 = a0
447 a0 = i1 : i1 = -r1 : r1 = a0
448 a0 = i2 : i2 = -r2 : r2 = a0
449 a0 = i3 : i3 = -r3 : r3 = a0
450 return
460 rem y1 gate
466 a0 = i1 : i1 = -r1 : r1 = a0
467 a0 = i3 : i3 = -r3 : r3 = a0
468 return
480 rem z0 gate
482 i2 = -i2 : i3 = -i3
483 return
500 rem z1 gate
502 i1 = -i1 : i3 = -i3
503 return
520 rem h0 gate
521 a0 = (r0 + r1) / sqr(2) : a1 = (i0 + i1) / sqr(2)
522 b0 = (r0 - r1) / sqr(2) : b1 = (i0 - i1) / sqr(2)
523 r0 = a0 : i0 = a1 : r1 = b0 : i1 = b1
525 a0 = (r2 + r3) / sqr(2) : a1 = (i2 + i3) / sqr(2)
526 b0 = (r2 - r3) / sqr(2) : b1 = (i2 - i3) / sqr(2)
527 r2 = a0 : i2 = a1 : r3 = b0 : i3 = b1
528 return
540 rem h1 gate
541 a0 = (r0 + r2) / sqr(2) : a1 = (i0 + i2) / sqr(2)
542 b0 = (r0 - r2) / sqr(2) : b1 = (i0 - i2) / sqr(2)
543 r0 = a0 : i0 = a1 : r2 = b0 : i2 = b1
545 a0 = (r1 + r3) / sqr(2) : a1 = (i1 + i3) / sqr(2)
546 b0 = (r1 - r3) / sqr(2) : b1 = (i1 - i3) / sqr(2)
547 r1 = a0 : i1 = a1 : r3 = b0 : i3 = b1
548 return
560 rem cx gate
561 a0 = r1 : r1 = r3 : r3 = a0
562 a0 = i1 : i1 = i3 : i3 = a0
579 return
580 rem sw gate
581 a0 = r1 : r1 = r2 : r2 = a0
582 a0 = i1 : i1 = i2 : i2 = a0
590 return
600 rem statevcector normalization
601 nf = sqr(1 / sq)
602 r0 = r0 * nf
603 i0 = i0 * nf
604 r1 = r1 * nf
605 i1 = i1 * nf
606 r2 = r2 * nf
607 i2 = i2 * nf
608 r3 = r3 * nf
609 i3 = i3 * nf
610 return
700 rem audio tracker - gate operation sound
701 rem sid chip setup: voice 1
702 poke 54296,15 : rem volume max
703 rem frequency based on gate type
704 fr = 5000 : rem default frequency
705 if gate$ = "h0" or gate$ = "h1" then fr = 8000
706 if gate$ = "cx" or gate$ = "sw" then fr = 6000
707 if gate$ = "x0" or gate$ = "x1" then fr = 7000
708 if gate$ = "y0" or gate$ = "y1" then fr = 5500
709 if gate$ = "z0" or gate$ = "z1" then fr = 4500
710 poke 54272,int(fr-int(fr/256)*256) : rem low freq
711 poke 54273,int(fr/256) : rem high freq
712 poke 54277,0 : poke 54278,240 : rem attack 0, decay max
713 poke 54276,0 : rem sustain 0, release 0
714 poke 54275,17 : rem waveform: triangle, gate on
715 for j = 1 to 5 : next j : rem short delay
716 poke 54275,16 : rem gate off
717 return
750 rem completion sound - ascending notes
751 poke 54296,15 : rem volume max
752 for nt = 1 to 3
753 fr = 4000 + nt * 2000
754 poke 54272,int(fr-int(fr/256)*256)
755 poke 54273,int(fr/256)
756 poke 54277,0 : poke 54278,240
757 poke 54276,0
758 poke 54275,17
759 for j = 1 to 10 : next j
760 poke 54275,16
761 for j = 1 to 5 : next j
762 next nt
763 return
1000 end