Exemplo n.º 1
0
def _single_expect(oper, state):
    """
    Private function used by expect
    """
    if isinstance(oper, Qobj) and isinstance(state, Qobj):
        if isoper(oper):
            if state.type == 'oper':
                # calculates expectation value via TR(op*rho)
                prod = oper.data * state.data
                tr = sum(prod.diagonal())  # sum of diagonal elements
                if oper.isherm and state.isherm:  # if hermitian
                    return float(np.real(tr))
                else:  # not hermitian
                    return tr
            elif state.type == 'ket':
                # calculates expectation value via <psi|op|psi>
                # prod = state.data.conj().T * (oper.data * state.data)
                prod = state.data.conj().T.dot(oper.data * state.data)
                if oper.isherm:
                    return float(np.real(prod[0, 0]))
                else:
                    return prod[0, 0]
        else:
            raise TypeError('Invalid operand types')
    # eseries
    #
    elif isinstance(oper, Qobj) and isinstance(state, eseries):
        out = eseries()

        if isoper(state.ampl[0]):

            out.rates = state.rates
            out.ampl = np.array([expect(oper, a) for a in state.ampl])

        else:

            out.rates = np.array([])
            out.ampl = np.array([])

            for m in range(len(state.rates)):

                op_m = state.ampl[m].data.conj().T * oper.data

                for n in range(len(state.rates)):

                    a = op_m * state.ampl[n].data

                    if isinstance(a, sp.spmatrix):
                        a = a.todense()

                    out.rates = np.append(out.rates,
                                          state.rates[n] - state.rates[m])
                    out.ampl = np.append(out.ampl, a)

        return out
    else:  # unsupported types
        raise TypeError('Arguments must be quantum objects or eseries')
Exemplo n.º 2
0
def _single_expect(oper, state):
    """
    Private function used by expect
    """
    if isinstance(oper, Qobj) and isinstance(state, Qobj):
        if isoper(oper):
            if state.type == 'oper':
                # calculates expectation value via TR(op*rho)
                prod = oper.data * state.data
                tr = sum(prod.diagonal())  # sum of diagonal elements
                if oper.isherm and state.isherm:  # if hermitian
                    return float(np.real(tr))
                else:  # not hermitian
                    return tr
            elif state.type == 'ket':
                # calculates expectation value via <psi|op|psi>
                # prod = state.data.conj().T * (oper.data * state.data)
                prod = state.data.conj().T.dot(oper.data * state.data)
                if oper.isherm:
                    return float(np.real(prod[0, 0]))
                else:
                    return prod[0, 0]
        else:
            raise TypeError('Invalid operand types')
    # eseries
    #
    elif isinstance(oper, Qobj) and isinstance(state, eseries):
        out = eseries()

        if isoper(state.ampl[0]):

            out.rates = state.rates
            out.ampl = np.array([expect(oper, a) for a in state.ampl])

        else:

            out.rates = np.array([])
            out.ampl = np.array([])

            for m in range(len(state.rates)):

                op_m = state.ampl[m].data.conj().T * oper.data

                for n in range(len(state.rates)):

                    a = op_m * state.ampl[n].data

                    if isinstance(a, sp.spmatrix):
                        a = a.todense()

                    out.rates = np.append(out.rates, state.rates[n] -
                                          state.rates[m])
                    out.ampl = np.append(out.ampl, a)

        return out
    else:  # unsupported types
        raise TypeError('Arguments must be quantum objects or eseries')
Exemplo n.º 3
0
def ode2es(L, rho0):
    """Creates an exponential series that describes the time evolution for the
    initial density matrix (or state vector) `rho0`, given the Liouvillian 
    (or Hamiltonian) `L`.
    
    Parameters
    ----------
    L : qobj
        Liouvillian of the system.
    
    rho0 : qobj
        Initial state vector or density matrix.
    
    Returns
    ------- 
    ode_series : eseries 
        ``eseries`` represention of the system dynamics.
    
    """

    if issuper(L):
 
        # check initial state
        if isket(rho0):
            # Got a wave function as initial state: convert to density matrix.
            rho0 = rho0 * rho0.dag()
    
        w, v = la.eig(L.full())
        # w[i]   = eigenvalue i
        # v[:,i] = eigenvector i
    
        rlen = prod(rho0.shape)
        r0 = mat2vec(rho0.full())
        v0 = la.solve(v,r0)
        vv = v * sp.spdiags(v0.T, 0, rlen, rlen)
    
        out = None
        for i in range(rlen):
            qo = Qobj(vec2mat(vv[:,i]), dims=rho0.dims, shape=rho0.shape)
            if out:
                out += eseries(qo, w[i])
            else:
                out  = eseries(qo, w[i])

    elif isoper(L):

        if not isket(rho0):
            raise TypeError('Second argument must be a ket if first is a Hamiltonian.')

        w, v = la.eig(L.full())
        # w[i]   = eigenvalue i
        # v[:,i] = eigenvector i

        rlen = prod(rho0.shape)
        r0 = rho0.full()
        v0 = la.solve(v,r0)
        vv = v * sp.spdiags(v0.T, 0, rlen, rlen)

        out = None
        for i in range(rlen):
            qo = Qobj(matrix(vv[:,i]).T, dims=rho0.dims, shape=rho0.shape)
            if out:
                out += eseries(qo, -1.0j * w[i])
            else:
                out  = eseries(qo, -1.0j * w[i])

    else:
        raise TypeError('First argument must be a Hamiltonian or Liouvillian.')
        
    return estidy(out)
# Aesthetic function
def wait():
    inp = input("\nProceed? [Y/n]: ")
    while inp != 'y' and inp != 'Y':
        inp = input("Proceed? [Y/n]: ")
    print("\n")


""" --------------- </Exponential Series Objects Examples> --------------- """

print("Exponential Series:\n")
print("sigma-x * exp(i.w.t)")

omega = 1.0  # w
es1 = eseries(sigmax(), 1j * omega)
print(es1)

print("\nsigma-x * cos(w.t)")

omega = 1.0
es2 = eseries(0.5 * sigmax(), 1j * omega) + eseries(0.5 * sigmax(),
                                                    -1j * omega)
print(es2)

wait()
""" --------------- </Evaluate the eseries object at various times> --------------- """

# At t = 0
print("sigma-x * cos(w.t) at time t = 0\n", esval(es2, 0.0))