Skip to content

Instantly share code, notes, and snippets.

@qubyte
Created September 23, 2012 04:45
Show Gist options
  • Save qubyte/3768908 to your computer and use it in GitHub Desktop.
Save qubyte/3768908 to your computer and use it in GitHub Desktop.
Simple quantum random walk
"""
Requires matplotlib for plotting. Tested with python 27. If you want to try this
without plotting, remove the final two lines and the pylab import. The guts only
depends on math and will work with vanilla python.
"""
import math
import pylab
def probabilities(posn):
"""Returns a list of the probabilies for each place."""
return [sum([abs(amp) ** 2 for amp in place]) for place in posn]
def normalise(posn):
"""Normalise function to normalise an input 1D line."""
N = math.sqrt(sum(probabilities(posn)))
return [[amp / N for amp in place] for place in posn]
def timestep(posn):
"""Defines action of a timestep, i.e. a Hadamard gate on each element."""
return normalise([[x[0] + x[1], x[0] - x[1]] for x in posn])
def shift(coin):
"""Shift the up elements leftwards and the down elements rightwards."""
newposn = [[0, 0] for i in range(len(coin))]
for j in range(1, len(posn) - 1):
newposn[j + 1][0] += coin[j][0]
newposn[j - 1][1] += coin[j][1]
return normalise(newposn)
# Initialise lists.
min, max = -500, 501
posn = [[0, 0] for i in range(min, max)]
posn[-min] = [1 / math.sqrt(2), 1j / math.sqrt(2)]
# Run for some steps...
for time in range(-min):
posn = shift(timestep(posn))
# Plot.
pylab.plot(range(min, max), probabilities(posn))
pylab.show()
@qubyte
Copy link
Author

qubyte commented Sep 23, 2012

This is some old code I wrote to demonstrate some simple python to a physics student some years ago. The intention was to show them some neat syntax with a simple algorithm that they were already familiar with. I came across it today and was surprised to find that it still works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment