Animating the Quantum Drunkard’s Walk
2017-12-02, post № 186
ImageMagick, PIL, programming, Python, #animation, #automation, #cellular, #PPCG, #quantum mechanics
A recent PPCG challenge titled The Quantum Drunkard’s Walk was about a tiny drunken person for which quantum mechanics apply and who — being drunk — will randomly walk in one of the four cardinal directions each step they take.
As they experience the strange world of quanta, they can simultaneously take every path at once and their paths influence each other. The challenge then asked to output all possible positions the quantum drunkard could occupy, all paths they could take in ASCII representation.
The question also states this problem’s equivalence to a cellular automaton, when one removes the story boilerplate.
Cells in this cellular automaton are square and can occupy one of three states: empty, awake or sleeping. Each iteration, all cells change according to three rules.
- An empty cell wakes up iff there is exactly one awake cell amongst its cardinal neighbours, else it stays empty.
- An awake cell goes to sleep.
- A sleeping cell continues to sleep.
Being code golf, the aim was to come up with a minimally sized source code; my attempt required 𝟤𝟣𝟦 bytes and prints a nested array containing one-length strings (characters), as this output method is cheaper than concatenating all characters to one string.
However, one user took the challenge idea a bit further and created an animated gif showing the walk’s, or cellular automaton’s, progression over time with an increasing number of iterations. My Python program shown in this post does exactly that, generating an animated gif showing the automaton’s progression. I even implemented rainbow support, possibly improving the automaton’s visual appearance.
Python source code can be downloaded and seen below.
I use the Python Imaging Library to produce all frames and use a shell call to let ImageMagick convert all frames to an animated gif. Animation parameters are taken via shell arguments, a full list of features follows (also available via the -h
flag).
--iterations N
: Number of iterations (initial frame not counted)
--colorempty C
: Empty cell color (#rrggbb)--colorawake C
: Awake cell color (#rrggbb)--colorsleeping C
: Sleeping cell color (#rrggbb)--rainbow
: Use rainbow colors (overrides color settings)--scale N
: Cell square pixel size
--convert
: Execute ImageMagick’s convert command--delay N
: Delay between frames in output image.--loop N
: Gif loops (0 means for indefinite looping)--keepfiles
: Do not delete files when converting
Footnotes
- ▲ [2020-08-07]
quantum.py
was renamed toanimating-the-quantum-drunkards-walk.py
.