Skip to content

Instantly share code, notes, and snippets.

@marekyggdrasil
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: http://mareknarozniak.com/2020/03/22/simulating-quantum-teleportation/
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: https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py
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 = ax.bar(x - width/2, outcomes, width, label='not corrected')
rects2 = ax.bar(x + width/2, corrs, width, label='corrected')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('fidelity')
ax.set_title(title)
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
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)
fig.tight_layout()
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(),
qeye(2)])
Ps.append(P)
# simulate measurement by performing the projection
psis_proj = []
for P in Ps:
psi_proj = (P*psi4).unit()
psis_proj.append(psi_proj)
# classical correction operators
X = rx(np.pi, N=3, target=2)
Z = rz(np.pi, N=3, target=2)
# perform classical correction
psis_corr = [
psis_proj[0],
X*psis_proj[1],
Z*psis_proj[2],
Z*X*psis_proj[3]
]
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])
psis_ref.append(psi_ref)
labels.append(r'$\left \vert {0}{1} \right \rangle$'.format(str(m0), str(m1)))
print()
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))
corrected_outcomes.append(fidelity)
print()
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))
outcomes.append(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 = 'https://mareknarozniak.com/2020/03/22/simulating-quantum-teleportation/'
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