Quantum Computing on a Commodore 64 in 200 Lines of BASIC

In an age where companies are selling two-qubit quantum computers for a sum of money that would make your wallet recoil in horror, here we are, stepping off the beaten path and walking down memory lane to a time when computers were, well, somewhat simpler.

QC64 preparing a Bell’s state

Join me on this delightful journey where we shatter the belief that quantum simulations need groundbreaking technologies. Get ready to be amazed as we simulate two-qubit quantum computing operations on none other than our retro powerhouse, the Commodore 64.

You heard it right! Our beloved Commodore 64, an 8-bit home computer introduced in January 1982, is ready to show those fancy, pricey quantum computers how it’s done. Let’s roll up our sleeves, dust off those old systems, and dive into the world of quantum simulations!

The Commodore Quantum Simulator

The Commodore Quantum Simulator is a BASIC program capable of simulating various quantum gate operations on a two-qubit system. With this system, you can simulate operations such as Pauli-X, Pauli-Y, Pauli-Z, Hadamard, CNOT, and he SWAP gate.

Upon launching the simulator, it defaults to the initial state of |00>. The simulator then prompts for a sequence of quantum gates to be applied. For instance, inputting “H0CX” instructs the simulator to apply the Hadamard gate on qubit 0, followed by a CNOT gate operation. Subsequent to this state manipulation, the simulator proceeds to run a defined number of iterations, simulating the quantum state’s evolution with each step. At the end of this process, it then provides the user with the distribution of quantum states, offering a tangible insight into the fascinating probabilistic nature of quantum mechanics.

Conclusion

Quantum computing might seem like a domain only for the most advanced hardware on the planet. But at its heart, it’s a series of mathematical transformations, all of which are perfectly suited for our good old Commodore 64.

So next time when someone brags about their ultra-advanced quantum computer, remind them gently of the time when you performed quantum simulations on an 8-bit computer from the 1980s. The Commodore 64 might not break any speed records, but it will definitely break some preconceived notions about quantum computing.

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
30 print chr$(147)
40 print "c64 quantum simulator"
45 print "created by davide gessa (dakk)"
50 print "enter gate seq (x0,x1,y0,y1,z0,z1,h0,h1,cx,sw)"
60 input g$
65 print "calculating the statevector...";
70 for i = 1 to len(g$) step 2
80  gate$ = mid$(g$, i, 2)
90  gosub 200
91  print ".";
100 next i
101 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 print "running" shots "iterations..."
110 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 "results:"
155 print "00: ["z0"] "; : for i = 1 to z0 : print "Q"; : next i : print
165 print "01: ["z2"] "; : for i = 1 to z2 : print "Q"; : next i : print 
160 print "10: ["z1"] "; : for i = 1 to z1 : print "Q"; : next i : print
170 print "11: ["z3"] "; : for i = 1 to z3 : print "Q"; : next i : print
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

1000 end

The source code is accessible here: https://github.com/dakk/qc64 Twitter: @dagide

Article on medium.

2024

DQPU, the decentralized quantum simulator

2 minute read

During my Quantum Computing journey, I often needed to simulate some quantum circuits; sometimes they are small, but some other times they are bigger enough ...

The Time I Built a Probabilistic Computer

6 minute read

In early 2023, I embarked on a journey to explore the field of probabilistic computing. This endeavor culminated in the construction of a hardware prototype,...

Back to Top ↑

2023

Expanding the Commodore 64 Quantum Emulator

1 minute read

In a recent article I wrote, “Quantum Computing on a Commodore 64 in 200 Lines of BASIC”, published both on Medium and Hackaday.com, shows a two-qubit quantu...

Back to Top ↑

2020

OCaml and Quantum Computing

1 minute read

Qiskit is a python SDK developed by IBM and allows everyone to create quantum circuits, simulate them locally and also run the quantum circuit on a real quan...

Yallo, a new Tezos language

2 minute read

As someone noticed from the previous post, last weeks I started to write a new programming language for Tezos smart contracts. This project was initially int...

King of Tezos: a smart-ponzi on Tezos

2 minute read

While writing a new programming language, it is often useful to write some real use-cases to test the syntax, the language expressiveness and the code cleann...

Favorite dev quote

less than 1 minute read

Documentation is like sex: when it is good, it is very, very good; and when it is bad, it is better than nothing

New blog

less than 1 minute read

This is my new blog, based on jekyll. I’ll soon import old posts from my old blog.

Back to Top ↑

2016

Contractvm: decentralized applications on Bitcoin

less than 1 minute read

Contractvm is a general-purpose decentralized framework based on blockchain. The framework allows to implement arbitrary decentralized applications in an eas...

Back to Top ↑

2015

Dices provably fair - Nonce overflow vulnerability

less than 1 minute read

Most of bitcoin dice software use a system to prove the fair play of the server for each bet. Most of them implement this mechanism using two seed (server se...

Back to Top ↑

2014

Apache2: redirect different domains to subfolder

less than 1 minute read

In the aim to merge two of my server on digitalocean, today I tried to write a mod_rewrite rule to redirect a secondary domain to a subfolder. After one hour...

Back to Top ↑

2013

MineML: F# miner

less than 1 minute read

MineML is a multithread CPU based bitcoin miner written in F#. At the moment it’s a slow implementation, but the class structure offers the possibility to im...

Back to Top ↑