Skip to content

Instantly share code, notes, and snippets.

@marekyggdrasil

marekyggdrasil/plotting.py

Last active Jan 11, 2021
Embed
What would you like to do?
Tutorial covering the concept of geometric phase for a qubit, tutorial available under https://mareknarozniak.com/2021/01/09/qubit-berry-phase/
import matplotlib.pyplot as plt
def plotQubit(angles, refrel, refimag, resrel, resimag, title, x_axis, filename, url=''):
fig, ax = plt.subplots(1, 1, constrained_layout=True)
ax.set_title(title)
x_axis += '\n' + url
ax.set_xlabel(x_axis)
ax.set_ylabel('phase')
ax.plot(angles, refrel, color='tab:blue', label='ref real')
ax.plot(angles, refimag, color='tab:orange', label='ref imaginary')
ax.plot(angles, resrel, color='tab:blue', linestyle='--', label='res real')
ax.plot(angles, resimag, color='tab:orange', linestyle='--', label='res imaginary')
ax.grid()
ax.legend()
# fig.show()
fig.savefig(filename)
import numpy as np
from qutip import Options, mesolve, sigmax, sigmay, sigmaz
def sweepPhaseDown(psi0, theta_min, theta_max, phi, tau):
def angle(t):
return (1-(t/tau))*theta_max + (t/tau)*theta_min
res = 300
times = np.linspace(0., tau, res)
opts = Options(store_final_state=True)
H = [
[sigmax(), (lambda t, args: np.cos(-phi)*np.sin(angle(t)))],
[sigmay(), (lambda t, args: np.sin(-phi)*np.sin(angle(t)))],
[sigmaz(), (lambda t, args: np.cos(angle(t)))]]
return mesolve(H, psi0, times, options=opts)
def sweepPhaseUp(psi0, theta_min, theta_max, phi, tau):
def angle(t):
return (1-(t/tau))*theta_min + (t/tau)*theta_max
res = 300
times = np.linspace(0., tau, res)
opts = Options(store_final_state=True)
H = [
[sigmax(), (lambda t, args: np.cos(-phi)*np.sin(angle(t)))],
[sigmay(), (lambda t, args: np.sin(-phi)*np.sin(angle(t)))],
[sigmaz(), (lambda t, args: np.cos(angle(t)))]]
return mesolve(H, psi0, times, options=opts)
def sweepPhaseRound(psi0, theta, phi_min, phi_max, tau):
def angle(t):
return (1-(t/tau))*phi_min + (t/tau)*phi_max
res = 300
times = np.linspace(0., tau, res)
opts = Options(store_final_state=True)
H = [
[sigmax(), (lambda t, args: np.cos(-angle(t))*np.sin(theta))],
[sigmay(), (lambda t, args: np.sin(-angle(t))*np.sin(theta))],
[sigmaz(), (lambda t, args: np.cos(theta))]]
return mesolve(H, psi0, times, options=opts)
import numpy as np
from qutip import basis
from qm import sweepPhaseDown, sweepPhaseUp, sweepPhaseRound
from plotting import plotQubit
url = 'https://mareknarozniak.com/2021/01/09/qubit-berry-phase/'
tau = 16.*np.pi
res = 60
angles = np.linspace(0., 2., res)
refrels = np.zeros(res)
refimag = np.zeros(res)
resrels = np.zeros(res)
resimag = np.zeros(res)
for i, angle in enumerate(angles):
theta = np.pi*angle
psi0 = np.cos(theta/2.)*basis(2, 0)+np.sin(theta/2.)*basis(2, 1)
result = sweepPhaseRound(psi0, theta, 0., 2.*np.pi, tau)
psif = result.final_state
gamma = -2.*np.pi*np.sin(theta/2.)**2.
dyn = np.pi
grefe = psif.overlap(psi0)
resrels[i] = grefe.real
resimag[i] = grefe.imag
ref = np.exp(1j*gamma)
refrels[i] = ref.real
refimag[i] = ref.imag
title = '$\\phi_f \\in [0, 2\\pi]$ and $\\theta$ fixed'
x_axis = '$\\theta$ [$\\pi$]'
filename = 'res_qubit.png'
plotQubit(angles, refrels, refimag, resrels, resimag, title, x_axis, filename, url=url)
refrels = np.zeros(res)
refimag = np.zeros(res)
resrels = np.zeros(res)
resimag = np.zeros(res)
# fix theta_min, vary phi
for i, angle in enumerate(angles):
theta_min = 3.*np.pi/4.
theta_max = 0.
phi_min = 0.
phi_max = np.pi*angle
psi0 = np.cos(theta_max/2.)*basis(2, 0)+np.sin(theta_max/2.)*basis(2, 1)
result = sweepPhaseDown(psi0, theta_min, theta_max, phi_min, tau)
psif = result.final_state
result = sweepPhaseRound(psif, theta_min, phi_min, phi_max, tau)
psif = result.final_state
result = sweepPhaseUp(psif, theta_min, theta_max, phi_max, tau)
psif = result.final_state
gamma = -(phi_max-phi_min)*np.sin(theta_min/2.)**2.
grefe = psif.overlap(psi0)
ref = np.exp(1j*gamma)
resrels[i] = grefe.real
resimag[i] = grefe.imag
refrels[i] = ref.real
refimag[i] = ref.imag
title = '$\\phi_f \\in [0, 2\\pi]$ and $\\theta = \\frac{3\\pi}{4}$ fixed'
x_axis = '$\\phi_f$ [$\\pi$]'
filename = 'res_qubit_2_1.png'
plotQubit(angles, refrels, refimag, resrels, resimag, title, x_axis, filename, url=url)
refrels = np.zeros(res)
refimag = np.zeros(res)
resrels = np.zeros(res)
resimag = np.zeros(res)
# fix phi_max, vary theta_min
for i, angle in enumerate(angles):
theta_min = np.pi*angle
theta_max = 0.
phi_min = 0.
phi_max = 3.*np.pi/4.
psi0 = np.cos(theta_max/2.)*basis(2, 0)+np.sin(theta_max/2.)*basis(2, 1)
result = sweepPhaseDown(psi0, theta_min, theta_max, phi_min, tau)
psif = result.final_state
result = sweepPhaseRound(psif, theta_min, phi_min, phi_max, tau)
psif = result.final_state
result = sweepPhaseUp(psif, theta_min, theta_max, phi_max, tau)
psif = result.final_state
gamma = -(phi_max-phi_min)*np.sin(theta_min/2.)**2.
grefe = psif.overlap(psi0)
ref = np.exp(1j*gamma)
resrels[i] = grefe.real
resimag[i] = grefe.imag
refrels[i] = ref.real
refimag[i] = ref.imag
title = '$\\theta \\in [0, 2\\pi]$ and $\\phi_f = \\frac{3\\pi}{4}$ fixed'
x_axis = '$\\theta$ [$\\pi$]'
filename = 'res_qubit_2_2.png'
plotQubit(angles, refrels, refimag, resrels, resimag, title, x_axis, filename, url=url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment