#!/usr/bin/env python1# coding: utf-823# In[53]:456from sympy import *7from numpy import sqrt8from math import pi9from qiskit import *10from qiskit.visualization import plot_bloch_multivector111213# ## Two Qubit Systems1415# When considering sytems with two or more parties, a new mechanical feature arises - entangelement.1617# Seperability - if a state $ \rho $ can be written as the convex combo of product states:1819# $$ \rho = \sum p* \rho (A) \otimes \rho (B) $$2021# then $ \rho $ is called seperable.2223# Entanglement is if a state $ \rho $ is not seperable.2425# A major challenge is in determining the degree of entanglement in a system. FOr a two qubit system, the concurrence is a good measure of entanglement.2627# If we consider a pure state, we can express it in the complete basis constructed from maximally entangled bell states:2829# $$ \left| e1 \right\rangle = {1\over\sqrt{2}}(\left| 00 \right\rangle + \left| 11 \right\rangle )$$3031# $$ \left| e2 \right\rangle = {i\over\sqrt{2}}(\left| 00 \right\rangle - \left| 11 \right\rangle )$$3233# $$ \left| e3 \right\rangle = {i\over\sqrt{2}}(\left| 01 \right\rangle + \left| 10 \right\rangle )$$3435# $$ \left| e4 \right\rangle = {1\over\sqrt{2}}(\left| 01 \right\rangle - \left| 10 \right\rangle )$$3637# Thus, we can write3839# $$ \left| \psi \right\rangle = \sum \alpha \left| e \right\rangle $$4041# The concurrence is defined as4243# $$ C(\left| \psi \right\rangle) = \sum \alpha^{2}$$4445# This is zero for a seperable state and 1 for a maximally entangled state.4647# In a mixed state, we can perform a spin-flip operation like4849# $$ \rho ' = (\sigma 2 \otimes \sigma2) \rho^{*} (\sigma 2 \otimes \sigma 2) $$5051# where $\sigma 2$ is a pauli matrix. Now we can define $$ R^{2} = \rho * \rho ' $$ where the lambda terms are the squra roots of the eigenvalues of the matrix R^2. The concurrence of the mixed state is then given by:5253# $$ C(\rho) = max(0, \lambda 1 - \lambda 2 - \lambda 3 - \lambda 4) $$5455# ### Bell States:5657# All pure and maximally entangled states are called Bell states. For a 2 qubit system, a set of four Bell states58# build a basis for all states. This basis is:59# $$ \left| 00 \right\rangle , \left| 01 \right\rangle , \left| 10 \right\rangle , \left| 11 \right\rangle $$6061# These can be expressed in vector form as:62# $$ \left| e1 \right\rangle =63# \begin{bmatrix}64# 1 \\65# 0 \\66# 0 \\67# 0 \\68# \end{bmatrix}69# $$7071# $$ \left| e2 \right\rangle =72# \begin{bmatrix}73# 0 \\74# 1 \\75# 0 \\76# 0 \\77# \end{bmatrix} $$7879# $$ \left| e3 \right\rangle =80# \begin{bmatrix}81# 0 \\82# 0 \\83# 1 \\84# 0 \\85# \end{bmatrix} $$8687# $$ \left| e4 \right\rangle =88# \begin{bmatrix}89# 0 \\90# 0 \\91# 0 \\92# 1 \\93# \end{bmatrix} $$9495# The bell state we will use is96# $$ \left| \psi \right\rangle = {i\over\sqrt{2}}(\left| 01 \right\rangle + \left| 10 \right\rangle ) $$9798# The corresponding density matrix is99#100# $$ \left| \psi \right\rangle \left\langle \psi \right| = {1 \over 2} \begin{equation}101# \begin{bmatrix}102# 0 & 0 & 0 & 0\\103# 0 & 1 & -1 & 0\\104# 0 & -1 & 1 & 0\\105# 0 & 0 & 0 & 0106# \end{bmatrix}107# \end{equation}108# $$109110# ### The Geometry of Qubits111112# We will talk about the simplest case - a qubit with two degrees of freedom, spin up and down (1/2 & -1/2).113114# The qubit is given by:115# $$ \left| \psi \right\rangle = \alpha \left| 0 \right\rangle + \beta \left| 1 \right\rangle $$116117# Using the normalization condition, we can reparameretize the qubit state vector to118# $$ \left| \psi \right\rangle = \cos \theta /2 \left| 0 \right\rangle + \sin \theta / 2 e^{i \phi} \left| 1 \right\rangle $$119120# The angular parameters define a point on the unit sphere in 3 dimensional space. This space is known as Bloch sphere. Here is an example for the $ \left| 1 \right\rangle $ state.121122# In[22]:123124125psi = [0,1]126plot_bloch_multivector(psi)127128129# ## Exercise130# ### Decoherence of a Two Qubit System131132# In order to tackle this problem, we are going to write a Markovian quantum master equation to describe how the system evolves in time. We will use a specific form of this master equation, the Linblad master equation (below), of a quantum system weakly coupled to a Markovian reservoir. Getting to this point is not necessarily straightforward so we'll not delve into that discussion.133134# $$\frac{d\rho}{dt} = -i[H, \rho] - \mathcal{D}(\rho)$$135136# To get to the functional form of the operator used here, we futher assume that Linblad operators (which make up $\mathcal{D}$) are Hermitian and commuting with the Hamiltonian. We can then replace the Linblad operators with projection operators and arrive at137138# $\mathcal{D}(\rho) = \frac{1}{2} \sum_{n=k}^{d} \lambda_k (P_k \rho + \rho P_k -2P_k \rho P_k)$139140# Here the $P_k$ project onto the different k's of the eigenbasis $\left| e_k \right\rangle$ as below:141142# $$P_k = \left| e_k \right\rangle \left\langle e_k\right|$$143144# Taking all this into account, and that the projeciton operators must sum to 1, $\mathcal{D}(\rho)$ becomes145146# $$\mathcal{D}(\rho) = \lambda (\rho - \sum_{n=k}^{d} P_k \rho P_k)$$147148# The advantage of using this dissipator is that the positive and real parameters $\lambda_k$ are the decoherence parameters and represent the strength the the system coupled to its surroundings, which is a process that reduces the purity of a microstate. The master equation is now:149150# $$\frac{d\rho}{dt} = -\mathcal{D}(\rho)$$151152# We will now try to solve the master equation with this decoherence formalism for a 2 qubit system. Before we write down any specific density matrices, we will first determine how the dissipator acts on different bases. We will call the first decoherence mode simply $E{\otimes}E$, where both qubits are written in the Hamiltonian eigebasis. Therefore, we find that the dissipator acted on a general density matrix looks like153154# $$ \begin{pmatrix}155# \rho_{11} & 0 & 0 & 0 \\156# 0 & \rho_{22} & 0 & 0 \\157# 0 & 0 & \rho_{33} & 0 \\158# 0 & 0 & 0 & \rho_{44}159# \end{pmatrix} $$160161# In this case, differential equations decouple and we are left with the solutions:162163# $$\frac{d\rho_{ij}}{dt} = -\lambda,164# \frac{d\rho_{ii}}{dt} = 0 $$165166# In the general rotated basis, we find a different story. Consider the following rotation operator:167168# $$ \left| \alpha \right \rangle =169# \begin{pmatrix}170# \cos\frac{\theta}{2} & \sin\frac{\theta}{2} \\171# -\sin\frac{\theta}{2} & \cos\frac{\theta}{2}172# \end{pmatrix} $$173174# Which operates on either of the following:175176# $$ \begin{pmatrix}177# \left| e_1 \right\rangle \\178# \left| e_3 \right\rangle179# \end{pmatrix}180# \begin{pmatrix}181# \left| e_2 \right\rangle \\182# \left| e_4 \right\rangle183# \end{pmatrix} $$184185# Now we have a situation where each of the four basis vectors, under this general rotation operator, will give four new basis vectors $\left| \beta_k \right\rangle$ which have projection operators: $P_k = \left| \beta_k \right\rangle \left\langle \beta_k\right|$. We call this new space $GR \otimes E$. Now we play the same game again and assemble the dissipator operator. Below is the right side (summation part):186187# $$ \begin{pmatrix}188# (\cos^{4}\frac{\theta}{2}+\sin^{2}\frac{\theta}{4})(\rho_{11}+\rho_{33}) & 0 & \sin\frac{\theta}{2}\cos\frac{\theta}{2}((\cos^{2}\frac{\theta}{2}-\sin^{2}\frac{\theta}{2})(\rho_{11}-\rho_{33})-2\cos\frac{\theta}{2}\cos\frac{\theta}{2}(\rho_{13}+\rho_{31})) & 0 \\189# 0 & (\cos^{4}\frac{\theta}{2}+\sin^{4}\frac{\theta}{4})(\rho_{22}+\rho_{44}) & 0 & \sin\frac{\theta}{2}\cos\frac{\theta}{2}((\cos^{2}\frac{\theta}{2}-\sin^{2}\frac{\theta}{2})(\rho_{22}-\rho_{44})-2\cos\frac{\theta}{2}\cos\frac{\theta}{2}(\rho_{24}+\rho_{42})) \\190# \sin\frac{\theta}{2}\cos\frac{\theta}{2}((\cos^{2}\frac{\theta}{2}-\sin^{2}\frac{\theta}{2})(\rho_{11}-\rho_{33})-2\cos\frac{\theta}{2}\cos\frac{\theta}{2}(\rho_{13}+\rho_{31})) & 0 & (\cos^{4}\frac{\theta}{2}+\sin^{4}\frac{\theta}{4})(\rho_{11}+\rho_{33}) & 0 \\191# 0 & \sin\frac{\theta}{2}\cos\frac{\theta}{2}((\cos^{2}\frac{\theta}{2}-\sin^{2}\frac{\theta}{2})(\rho_{22}-\rho_{44})-2\cos\frac{\theta}{2}\cos\frac{\theta}{2}(\rho_{24}+\rho_{42})) & 0 & (\cos^{4}\frac{\theta}{2}+\sin^{4}\frac{\theta}{4})(\rho_{22}+\rho_{44})192# \end{pmatrix} $$193194# This matrix now gets subtracted from $\rho$, which is messy algebra. Anyways, we will compute this problem numerically. So let's now turn our attention to the python script.195196# In[25]:197198199import math200import numpy as np201from numpy import linalg as LA202#import scipy.integrate as integrate203import matplotlib.pyplot as plt204205rho = [[1,1,1,1], [1,1,1,1], [1,1,2,1], [1,1,1,1]] # trial rho206l = 1 # lambda207x = (math.pi)/2 # angle208209change_rho = np.zeros((4,4))210211def densityMatrix(v):212return np.outer(v, v)213214def DecoherenceA(change_rho, rho, l):215for i in range(4):216for j in range(4):217if i == j:218change_rho[i][j] = 0219else:220change_rho[i][j] = -l*rho[i][j]221222return(change_rho)223224def oneRotated(change_rho, rho, l):225226for i in range(4):227for j in range(4):228if i == j:229if i < 2:230change_rho[i][j] = ((math.cos(x/2)**4)+(math.sin(x/2)**4))*(rho[i][i]+rho[i+2][i+2])231else:232change_rho[i][j] = ((math.cos(x/2)**4)+(math.sin(x/2)**4))*(rho[i][i]+rho[i-2][i-2])233elif j == i+2:234change_rho[i][j] = (math.sin(x/2)*math.cos(x/2))*(((math.cos(x/2)**4)-(math.sin(x/2)**4))*(rho[i][i]-rho[i+2][i+2])+(2*math.cos(x/2)*math.sin(x/2))*(rho[i][i+2] + rho[i+2][i]))235elif i == j+2:236change_rho[i][j] = (math.sin(x/2)*math.cos(x/2))*(((math.cos(x/2)**4)-(math.sin(x/2)**4))*(rho[i][i]-rho[i-2][i-2])+(2*math.cos(x/2)*math.sin(x/2))*(rho[i][i-2] + rho[i-2][i]))237else:238change_rho[i][j] = 0239240new_rho = -l*np.subtract(rho, change_rho)241return(new_rho)242243def PlotRho(f, rho, change_rho, i, j):244drho = np.zeros((4,4))245rho_list = []246time = []247248steps = np.arange(0, 50, 1)249250for n in steps:251if n == 0:252rho_list.append(rho[i][j])253time.append(0)254255drho = f(change_rho, rho, 1)256rho = drho*0.1 + rho257t = n*0.1258259rho_list.append(rho[i][j])260time.append(t)261262plt.title('Density matrix')263plt.xlabel('time / a.u.')264plt.ylabel('Density matrix terms')265plt.plot(time, rho_list, 'b')266267def purity(rho):268return np.trace(np.matmul(rho,rho))269270def concurrence(rho):271y_mat = np.array([[0, -1j], [1j, 0]], dtype=np.complex)272y = np.tensordot(y_mat, y_mat)273rho_tilda = y*rho*y274R2 = np.sqrt(rho)*rho_tilda*np.sqrt(rho)275R = np.sqrt(R2)276277evals = LA.eigvals(R)278evals_asc = np.sort(evals)279evals_desc = evals_asc[::-1]280#print(evals_desc)281282c = (evals_desc[0] - evals_desc[1] - evals_desc[2] - evals_desc[3])/2283284if c > 0:285return c286else:287return 0288289def PlotPurity(rho, f, purity):290drho = np.zeros((4,4))291rho2_list = []292time = []293294steps = np.arange(0, 50, 1)295296for n in steps:297if n == 0:298rho2_list.append(purity(rho))299time.append(0)300301drho = f(change_rho, rho, 1)302rho = drho*0.1 + rho303t = n*0.1304305rho2_list.append(purity(rho))306time.append(t)307308plt.title('Density matrix')309plt.xlabel('time / a.u.')310plt.ylabel('Density matrix terms')311plt.plot(time, rho2_list, 'g')312313def PlotConcurrence(rho, f, concurrence):314drho = np.zeros((4,4))315rho2_list = []316time = []317318steps = np.arange(0, 50, 1)319320for n in steps:321if n == 0:322rho2_list.append(concurrence(rho))323time.append(0)324325drho = f(change_rho, rho, 1)326rho = drho*0.1 + rho327#print(rho)328t = n*0.1329330rho2_list.append(concurrence(rho))331time.append(t)332333plt.title('Density matrix')334plt.xlabel('time / a.u.')335plt.ylabel('Density matrix terms')336plt.plot(time, rho2_list, 'b')337#print(rho2_list)338339singlet =np.array([0,1/math.sqrt(2),-1/math.sqrt(2),0], dtype=np.complex)340singlet_densmat = densityMatrix(singlet)341PlotConcurrence(singlet_densmat, DecoherenceA, concurrence)342PlotConcurrence(singlet_densmat, oneRotated, concurrence)343344345# ## Questions346347# 1. Using the qubit state vector equation, rotate the (0,1) vector by three different angles (thetas) and plot them on a bloch sphere. What do you observe is happening in geometric space? What value will give you the pure (1,0) state?348# Now, we are going to visual this using a quantum circuit. Create a quantum circuit using qc = QuantumCircuit(1). We will then apply a Hadmard gate using qc.h(0). This will create a superposition state of spin up and down. Next we will apply a rotation gate using qc.rz(angle1,angle2). You can then draw the gate using qc.draw(). To visualize your final rotated superposition state in vector and geomertric form, we will use a quantum simulator:349350# In[ ]:351352353backend = Aer.get_backend('statevector_simulator')354final_state = execute(qc,backend).result().get_statevector()355print(final_state)356plot_bloch_multivector(final_state)357358359# Simulate the rotations that you applied numerically on your quantum circuit.360361# 2. Now let's plot the $GR \otimes E$ case for the singlet initial state. First plot a test case of $\theta = \frac{\theta}{2}$ and show the time dependence of the $\rho_{11}$, $\rho_{12}$, and $\rho_{13}$ terms. What happens? Then, choosing an arbitrary time, modify the code to plot the $\theta$ dependence of each of these terms.362363# 3. A useful way for us to describe these density matrices is by computing the purity and concurrence of the matrix, as defined below. A pure and maximally entangled state will have P=1 and C=1. What happens to these values for a singlet initial state that evolves by decoherence mode A? What about the $GR \otimes E$ case?364365# $$P = Tr[\rho^{2}], C = max[0, \lambda_1 - \lambda_2 - \lambda_3 - \lambda_4]$$366367# Where $\lambda$ are the eigenvalues of the matrix $\sqrt{\rho \widetilde{\rho} \rho}$. Here $\rho$ is the density matrix and $\widetilde{\rho}$ is the matrix $(\sigma_y \otimes \sigma_y)\rho^{*}(\sigma_y \otimes \sigma_y)$. We again compute these two numerically at every chosen time point.368369# In[ ]:3703713721. Answer:373374375# In[55]:376377378qc = QuantumCircuit(1)379qc.h(0)380qc.rz(pi/4, 0)381# See the circuit:382qc.draw()383384385# In[56]:386387388# Let's see the result389backend = Aer.get_backend('statevector_simulator')390final_state = execute(qc,backend).result().get_statevector()391print(final_state)392plot_bloch_multivector(final_state)393394395396