Skip to content

Instantly share code, notes, and snippets.

Last active July 9, 2021 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marekyggdrasil/862333a779915a0ee39d6cab77bde8c4 to your computer and use it in GitHub Desktop.
Save marekyggdrasil/862333a779915a0ee39d6cab77bde8c4 to your computer and use it in GitHub Desktop.
A brief tutorial how to simulate quantum teleportation with QuTip quantum computing library in Python. More detailed explanation available here:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import itertools
from qutip import basis, tensor, rand_ket, snot, cnot, rx, rz, qeye
# from:
def plotTeleportationOutcomes(outcomes, corrs, labels, title, url=None, filename=None):
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots(1, 1, constrained_layout=True)
rects1 = - width/2, outcomes, width, label='not corrected')
rects2 = + width/2, corrs, width, label='corrected')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)
if url is not None:
ax.text(0, -0.1, url, fontsize=7)
if filename is not None:
fig.savefig(filename, transparent=True)
# create random state
psi = rand_ket(2)
# initial state
psi0 = tensor([psi, basis(2, 0), basis(2, 0)])
# unitary time-evolution for quantum teleportation
psi1 = snot(N=3, target=1)*psi0
psi2 = cnot(N=3, control=1, target=2)*psi1
psi3 = cnot(N=3, control=0, target=1)*psi2
psi4 = snot(N=3, target=0)*psi3
# all the possible outcomes of measurements on first two qubits
confs = list(itertools.product([0, 1], repeat=2))
# projection operators
Ps = []
for m0, m1 in confs:
P = tensor([
basis(2, m0).proj(),
basis(2, m1).proj(),
# simulate measurement by performing the projection
psis_proj = []
for P in Ps:
psi_proj = (P*psi4).unit()
# classical correction operators
X = rx(np.pi, N=3, target=2)
Z = rz(np.pi, N=3, target=2)
# perform classical correction
psis_corr = [
labels = []
outcomes = []
corrected_outcomes = []
# produce reference states for fidelity calculation
psis_ref = []
for m0, m1 in confs:
psi_ref = tensor([basis(2, m0), basis(2, m1), psi])
labels.append(r'$\left \vert {0}{1} \right \rangle$'.format(str(m0), str(m1)))
print('classically corrected outcomes fidelities')
print('{0:2} {1:2} {2:8}'.format('m0', 'm1', 'fidelity'))
for conf, psi_corr, psi_ref in zip(confs, psis_corr, psis_ref):
fidelity = np.round(np.abs(psi_corr.overlap(psi_ref))**2., 3)
m0, m1 = conf
print('{0:2} {1:2} {2:8}'.format(m0, m1, fidelity))
print('fidelities without classical correction')
print('{0:2} {1:2} {2:8}'.format('m0', 'm1', 'fidelity'))
for conf, psi_proj, psi_ref in zip(confs, psis_proj, psis_ref):
fidelity = np.round(np.abs(psi_proj.overlap(psi_ref))**2., 3)
m0, m1 = conf
print('{0:2} {1:2} {2:8}'.format(m0, m1, fidelity))
# plot the results
outcomes = [np.round(out, decimals=2) for out in outcomes]
corrected_outcomes = [np.round(out, decimals=2) for out in corrected_outcomes]
url = ''
title = 'Effect of classical correction on fidelities per outcome'
filename = 'outcomes.png'
plotTeleportationOutcomes(outcomes, corrected_outcomes, labels, title, url=url, filename=filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment