def test_liouvillian(self): """ Test the calculation of the liouvillian matrix. """ true_L = [[-4, 0, 0, 3], [0, -3.54999995, 0, 0], [0, 0, -3.54999995, 0], [4, 0, 0, -3]] true_L = Qobj(true_L) true_L.dims = [[[2], [2]], [[2], [2]]] true_H = [[1. + 0.j, 1. + 0.j], [1. + 0.j, -1. + 0.j]] true_H = Qobj(true_H) true_H.dims = [[[2], [2]]] true_liouvillian = [[-4, -1.j, 1.j, 3], [-1.j, -3.54999995 + 2.j, 0, 1.j], [1.j, 0, -3.54999995 - 2.j, -1.j], [4, +1.j, -1.j, -3]] true_liouvillian = Qobj(true_liouvillian) true_liouvillian.dims = [[[2], [2]], [[2], [2]]] N = 1 test_piqs = Dicke(hamiltonian=sigmaz() + sigmax(), N=N, pumping=1, collective_pumping=2, emission=1, collective_emission=3, dephasing=0.1) test_liouvillian = test_piqs.liouvillian() test_hamiltonian = test_piqs.hamiltonian assert_array_almost_equal(test_liouvillian.full(), true_liouvillian.full()) assert_array_almost_equal(test_hamiltonian.full(), true_H.full()) assert_array_equal(test_liouvillian.dims, test_liouvillian.dims) # no Hamiltonian test_piqs = Dicke(N=N, pumping=1, collective_pumping=2, emission=1, collective_emission=3, dephasing=0.1) liouv = test_piqs.liouvillian() lindblad = test_piqs.lindbladian() assert_equal(liouv, lindblad)
def test_ghz(self): """ PIQS: Test the calculation of the density matrix of the GHZ state. PIQS: Test for N = 2 in the 'dicke' and in the 'uncoupled' basis. """ ghz_dicke = Qobj([[0.5, 0, 0.5, 0], [0, 0, 0, 0], [0.5, 0, 0.5, 0], [0, 0, 0, 0]]) ghz_uncoupled = Qobj([[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]) ghz_uncoupled.dims = [[2, 2], [2, 2]] assert_equal(ghz(2), ghz_dicke) assert_equal(ghz(2, "uncoupled"), ghz_uncoupled)
def test_ghz(self): """ PIQS: Test the calculation of the density matrix of the GHZ state. PIQS: Test for N = 2 in the 'dicke' and in the 'uncoupled' basis. """ ghz_dicke = Qobj([[0.5, 0, 0.5, 0], [0, 0, 0, 0], [0.5, 0, 0.5, 0], [0, 0, 0, 0]]) ghz_uncoupled = Qobj([[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]) ghz_uncoupled.dims = [[2, 2], [2, 2]] assert_equal(ghz(2), ghz_dicke) assert_equal(ghz(2, "uncoupled"), ghz_uncoupled)
def test_liouvillian(self): """ PIQS: Test the calculation of the liouvillian matrix. """ true_L = [[-4, 0, 0, 3], [0, -3.54999995, 0, 0], [0, 0, -3.54999995, 0], [4, 0, 0, -3]] true_L = Qobj(true_L) true_L.dims = [[[2], [2]], [[2], [2]]] true_H = [[1. + 0.j, 1. + 0.j], [1. + 0.j, -1. + 0.j]] true_H = Qobj(true_H) true_H.dims = [[[2], [2]]] true_liouvillian = [[-4, -1.j, 1.j, 3], [-1.j, -3.54999995 + 2.j, 0, 1.j], [1.j, 0, -3.54999995 - 2.j, -1.j], [4, +1.j, -1.j, -3]] true_liouvillian = Qobj(true_liouvillian) true_liouvillian.dims = [[[2], [2]], [[2], [2]]] N = 1 test_piqs = Dicke(hamiltonian=sigmaz() + sigmax(), N=N, pumping=1, collective_pumping=2, emission=1, collective_emission=3, dephasing=0.1) test_liouvillian = test_piqs.liouvillian() test_hamiltonian = test_piqs.hamiltonian assert_array_almost_equal( test_liouvillian.full(), true_liouvillian.full()) assert_array_almost_equal(test_hamiltonian.full(), true_H.full()) assert_array_equal(test_liouvillian.dims, test_liouvillian.dims) # no Hamiltonian test_piqs = Dicke(N=N, pumping=1, collective_pumping=2, emission=1, collective_emission=3, dephasing=0.1) liouv = test_piqs.liouvillian() lindblad = test_piqs.lindbladian() assert_equal(liouv, lindblad)
def test_lindbladian_dims(self): """ PIQS: Test the calculation of the lindbladian matrix. """ true_L = [[-4, 0, 0, 3], [0, -3.54999995, 0, 0], [0, 0, -3.54999995, 0], [4, 0, 0, -3]] true_L = Qobj(true_L) true_L.dims = [[[2], [2]], [[2], [2]]] N = 1 test_dicke = _Dicke(N=N, pumping=1, collective_pumping=2, emission=1, collective_emission=3, dephasing=0.1) test_L = test_dicke.lindbladian() assert_array_almost_equal(test_L.full(), true_L.full()) assert_array_equal(test_L.dims, true_L.dims)
def test_lindbladian_dims(self): """ PIQS: Test the calculation of the lindbladian matrix. """ true_L = [[-4, 0, 0, 3], [0, -3.54999995, 0, 0], [0, 0, -3.54999995, 0], [4, 0, 0, -3]] true_L = Qobj(true_L) true_L.dims = [[[2], [2]], [[2], [2]]] N = 1 test_dicke = _Dicke(N=N, pumping=1, collective_pumping=2, emission=1, collective_emission=3, dephasing=0.1) test_L = test_dicke.lindbladian() assert_array_almost_equal(test_L.full(), true_L.full()) assert_array_equal(test_L.dims, true_L.dims)
def photon_dist(rho): """return diagonals of photon density matrix""" from operators import vector_to_operator from qutip import Qobj, ptrace from basis import ldim_p, ldim_s rho_small = convert_rho.dot(rho) rho_small = vector_to_operator(rho_small) rho_q = Qobj() rho_q.data = rho_small rho_q.dims = [[ldim_p, ldim_s], [ldim_p, ldim_s]] rho_q = ptrace(rho_q, 0) pops = rho_q.data.diagonal() return pops
def liouvillian_fast(H, c_op_list): """Assembles the Liouvillian superoperator from a Hamiltonian and a ``list`` of collapse operators. Like liouvillian, but with an experimental implementation which avoids creating extra Qobj instances, which can be advantageous for large systems. Parameters ---------- H : qobj System Hamiltonian. c_op_list : array_like A ``list`` or ``array`` of collpase operators. Returns ------- L : qobj Louvillian superoperator. .. note:: Experimental. """ d = H.dims[1] L = Qobj() L.dims = [[H.dims[0][:], d[:]], [H.dims[1][:], d[:]]] L.shape = [prod(L.dims[0][0]) * prod(L.dims[0][1]), prod(L.dims[1][0]) * prod(L.dims[1][1])] L.data = -1j * ( sp.kron(sp.identity(prod(d)), H.data, format="csr") - sp.kron(H.data.T, sp.identity(prod(d)), format="csr") ) n_op = len(c_op_list) for m in range(0, n_op): L.data = L.data + sp.kron(sp.identity(prod(d)), c_op_list[m].data, format="csr") * sp.kron( c_op_list[m].data.T.conj().T, sp.identity(prod(d)), format="csr" ) cdc = c_op_list[m].data.T.conj() * c_op_list[m].data L.data = L.data - 0.5 * sp.kron(sp.identity(prod(d)), cdc, format="csr") L.data = L.data - 0.5 * sp.kron(cdc.T, sp.identity(prod(d)), format="csr") return L
def wigner_comp(rho, xvec, yvec): """calculate wigner function of central site from density matrix rho at a grid of points defined by xvec and yvec""" global convert_rho from operators import expect, vector_to_operator from qutip import Qobj, wigner, ptrace from basis import ldim_p, ldim_s rho_small = convert_rho.dot(rho) rho_small = vector_to_operator(rho_small) rho_q = Qobj() rho_q.data = rho_small rho_q.dims = [[ldim_p, ldim_s], [ldim_p, ldim_s]] rho_q = ptrace(rho_q, 0) w = wigner(rho_q, xvec, yvec) return w
def spost(A): """Superoperator formed from post-multiplication by operator A Parameters ---------- A : qobj Quantum operator for post multiplication. Returns ------- super : qobj Superoperator formed from input qauntum object. """ if not isoper(A): raise TypeError('Input is not a quantum object') d=A.dims[0] S=Qobj() S.dims=[[d[:],A.dims[1][:]],[d[:],A.dims[0][:]]] S.shape=[prod(S.dims[0][0])*prod(S.dims[0][1]),prod(S.dims[1][0])*prod(S.dims[1][1])] S.data=sp.kron(A.data.T,sp.identity(prod(d))) return Qobj(S)
def spre(A): """Superoperator formed from pre-multiplication by operator A. Parameters ---------- A : qobj Quantum operator for pre-multiplication. Returns -------- super :qobj Superoperator formed from input quantum object. """ if not isoper(A): raise TypeError('Input is not a quantum object') d=A.dims[1] S=Qobj() S.dims=[[A.dims[0][:],d[:]],[A.dims[1][:],d[:]]] S.shape=[prod(S.dims[0][0])*prod(S.dims[0][1]),prod(S.dims[1][0])*prod(S.dims[1][1])] S.data=sp.kron(sp.identity(prod(d)),A.data,format='csr') return S
def spre(A): """Superoperator formed from pre-multiplication by operator A. Parameters ---------- A : qobj Quantum operator for pre-multiplication. Returns -------- super :qobj Superoperator formed from input quantum object. """ if not isoper(A): raise TypeError("Input is not a quantum object") d = A.dims[1] S = Qobj() S.dims = [[A.dims[0][:], d[:]], [A.dims[1][:], d[:]]] S.shape = [prod(S.dims[0][0]) * prod(S.dims[0][1]), prod(S.dims[1][0]) * prod(S.dims[1][1])] S.data = sp.kron(sp.identity(prod(d)), A.data, format="csr") return S
def liouvillian_fast(H, c_op_list): """Assembles the Liouvillian superoperator from a Hamiltonian and a ``list`` of collapse operators. Like liouvillian, but with an experimental implementation which avoids creating extra Qobj instances, which can be advantageous for large systems. Parameters ---------- H : qobj System Hamiltonian. c_op_list : array_like A ``list`` or ``array`` of collpase operators. Returns ------- L : qobj Louvillian superoperator. .. note:: Experimental. """ d=H.dims[1] L=Qobj() L.dims=[[H.dims[0][:],d[:]],[H.dims[1][:],d[:]]] L.shape=[prod(L.dims[0][0])*prod(L.dims[0][1]),prod(L.dims[1][0])*prod(L.dims[1][1])] L.data = -1j *(sp.kron(sp.identity(prod(d)), H.data,format='csr') - sp.kron(H.data.T,sp.identity(prod(d)),format='csr')) n_op = len(c_op_list) for m in range(0, n_op): L.data = L.data + sp.kron(sp.identity(prod(d)), c_op_list[m].data,format='csr') * sp.kron(c_op_list[m].data.T.conj().T,sp.identity(prod(d)),format='csr') cdc = c_op_list[m].data.T.conj()*c_op_list[m].data L.data = L.data - 0.5 * sp.kron(sp.identity(prod(d)), cdc,format='csr') L.data = L.data - 0.5 * sp.kron(cdc.T,sp.identity(prod(d)),format='csr') return L
import collections from qutip import Qobj from collections import defaultdict def calc_fidelity(state): if global_file.params.simulation_type == "bell_diagonal": return state[0] elif global_file.params.simulation_type in ["IP", "MP"]: return np.real((state * global_file.params.target_state).tr()) # Define special states max_corr_state = Qobj([[1 / 2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1 / 2]]) max_corr_state.dims = [[2, 2], [2, 2]] max_mix_state = Qobj([[1 / 4, 0, 0, 0], [0, 1 / 4, 0, 0], [0, 0, 1 / 4, 0], [0, 0, 0, 1 / 4]]) max_mix_state.dims = [[2, 2], [2, 2]] class Scheme: """ Holds the stored schemes and relevent attributes. state - state holds either the bell coefficients or the density matrix norm_f - normalised fidelity over the length of path, used for heuristics norm_fs - returns the norm_f of subscheme1 and subscheme2 """ def __init__(self, path,
def random_unitary_on_target(self, epsilon): rand_u = self.random_unitary(epsilon) qobj = Qobj(rand_u) qobj.dims = [[2] * self.num_qubits, [2] * self.num_qubits] return qobj * self.full_cNOT
def steady(L, maxiter=100, tol=1e-6, method='solve'): """Steady state for the evolution subject to the supplied Louvillian. Parameters ---------- L : qobj Liouvillian superoperator. maxiter : int Maximum number of iterations to perform, default = 100. tol : float Tolerance used by iterative solver, default = 1e-6. method : str Method for solving linear equations. Direct solver 'solve' (default) or iterative biconjugate gradient method 'bicg'. Returns -------- ket : qobj Ket vector for steady state. Notes ----- Uses the inverse power method. See any Linear Algebra book with an iterative methods section. """ eps = finfo(float).eps if (not isoper(L)) & (not issuper(L)): raise TypeError( 'Steady states can only be found for operators or superoperators.') rhoss = Qobj() sflag = issuper(L) if sflag: rhoss.dims = L.dims[0] rhoss.shape = [prod(rhoss.dims[0]), prod(rhoss.dims[1])] else: rhoss.dims = [L.dims[0], 1] rhoss.shape = [prod(rhoss.dims[0]), 1] n = prod(rhoss.shape) L1 = L.data + eps * sp_inf_norm(L) * sp.eye(n, n, format='csr') v = randn(n, 1) it = 0 while (la.norm(L.data * v, inf) > tol) and (it < maxiter): if method == 'bicg': v, check = bicg(L1, v, tol=tol) else: v = spsolve(L1, v, use_umfpack=False) v = v / la.norm(v, inf) it += 1 if it >= maxiter: raise ValueError('Failed to find steady state after ' + str(maxiter) + ' iterations') #normalise according to type of problem if sflag: trow = sp.eye(rhoss.shape[0], rhoss.shape[0], format='lil') trow = trow.reshape((1, n)).tocsr() data = v / sum(trow.dot(v)) else: data = data / la.norm(v) data = reshape(data, (rhoss.shape[0], rhoss.shape[1])).T data = sp.csr_matrix(data) rhoss.data = 0.5 * (data + data.conj().T) #data=sp.triu(data,format='csr')#take only upper triangle #rhoss.data=0.5*sp.eye(rhoss.shape[0],rhoss.shape[1],format='csr')*(data+data.conj().T) #output should be hermitian, but not guarenteed using iterative meth if qset.auto_tidyup: return Qobj(rhoss).tidyup() else: return Qobj(rhoss)
def steady(L,maxiter=100,tol=1e-6,method='solve'): """Steady state for the evolution subject to the supplied Louvillian. Parameters ---------- L : qobj Liouvillian superoperator. maxiter : int Maximum number of iterations to perform, default = 100. tol : float Tolerance used by iterative solver, default = 1e-6. method : str Method for solving linear equations. Direct solver 'solve' (default) or iterative biconjugate gradient method 'bicg'. Returns -------- ket : qobj Ket vector for steady state. Notes ----- Uses the inverse power method. See any Linear Algebra book with an iterative methods section. """ eps=finfo(float).eps if (not isoper(L)) & (not issuper(L)): raise TypeError('Steady states can only be found for operators or superoperators.') rhoss=Qobj() sflag=issuper(L) if sflag: rhoss.dims=L.dims[0] rhoss.shape=[prod(rhoss.dims[0]),prod(rhoss.dims[1])] else: rhoss.dims=[L.dims[0],1] rhoss.shape=[prod(rhoss.dims[0]),1] n=prod(rhoss.shape) L1=L.data+eps*_sp_inf_norm(L)*sp.eye(n,n,format='csr') v=randn(n,1) it=0 while (la.norm(L.data*v,np.inf)>tol) and (it<maxiter): if method=='bicg': v,check=bicg(L1,v,tol=tol) else: v=spsolve(L1,v,use_umfpack=False) v=v/la.norm(v,np.inf) it+=1 if it>=maxiter: raise ValueError('Failed to find steady state after ' + str(maxiter) +' iterations') #normalise according to type of problem if sflag: trow=sp.eye(rhoss.shape[0],rhoss.shape[0],format='lil') trow=trow.reshape((1,n)).tocsr() data=v/sum(trow.dot(v)) else: data=data/la.norm(v) data=reshape(data,(rhoss.shape[0],rhoss.shape[1])).T data=sp.csr_matrix(data) rhoss.data=0.5*(data+data.conj().T) #data=sp.triu(data,format='csr')#take only upper triangle #rhoss.data=0.5*sp.eye(rhoss.shape[0],rhoss.shape[1],format='csr')*(data+data.conj().T) #output should be hermitian, but not guarenteed using iterative meth if qset.auto_tidyup: return Qobj(rhoss).tidyup() else: return Qobj(rhoss)