Skip to content

Instantly share code, notes, and snippets.

@farice
Created December 29, 2020 07:15
Show Gist options
  • Save farice/7126e839d657273f9673253247f862d9 to your computer and use it in GitHub Desktop.
Save farice/7126e839d657273f9673253247f862d9 to your computer and use it in GitHub Desktop.
from qutip import *
import numpy as np
# Consider image relative to |+> in cos(\theta) |+> + e^(i\phi) |-> representation.
def extract_theta_phi(single_qubit_gate, b=snot() * basis(2,0)):
# apply gate to |+>
ket = single_qubit_gate * b
alpha = ket.full()[0][0]
beta = ket.full()[1][0]
# rewrite in x-basis
ket_raw = [(alpha + beta) / 2, (alpha - beta) / 2]
ket_raw = ket_raw / np.linalg.norm(ket_raw)
theta = 0
phi = 0
if ket_raw[0] * ket_raw[0].conj() < 1e-6:
theta = np.pi
phi = 0
elif ket_raw[1] * ket_raw[1].conj() < 1e-6:
theta = 0
phi = 0
else:
theta = 2 * np.arccos(np.sqrt(ket_raw[0] * ket_raw[0].conj()))
phi = (np.angle(ket_raw[0].conj() * ket_raw[1]
/ (np.sqrt(ket_raw[0] * ket_raw[0].conj())
* np.sqrt(ket_raw[1] * ket_raw[1].conj()))))
return theta, phi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment