Example #1
0
def wigner_ops(hilbert_size, betas):
    """
    Constructs a list of TensorFlow operators for the Wigner function
    measurement at beta values.
    
    Args:
        hilbert_size (int): The hilbert size dimension for the operators
        betas (list/array): N complex values to construct the operator
        
    Returns:
        ops (:class:`tensorflow.Tensor`): A 3D tensor (N, hilbert_size, hilbert_size) of N
                                         operators
    """
    parity_op = sum([((-1)**i)*qutip_fock_dm(hilbert_size*2, i) for i in range(hilbert_size)])
    basis = []
    for beta in betas:
        D = displace(hilbert_size*2, beta)
        A = D*parity_op*D.dag()
        op = (A)*(2/np.pi)
        op = Qobj(op[:hilbert_size, :hilbert_size])
        basis.append(op)

    # ops = tf.convert_to_tensor([np.real(basis).astype(np.float64),
    #                            np.imag(basis).astype(np.float64)])
    return dm_to_tf(basis)
def GKP_1D_state(tensorstate, N, Delta):
    """ This function creates a gkp sensor state as tf.Tensor."""
    Sx, Sp = qt.displace(N, sqrt(pi)), qt.displace(N, 1j * sqrt(pi))

    # Define Hermitian stablizers and apply the envelope
    chan = envelope_operator(N, Delta)
    Sx = chan((Sx + Sx.dag()) / 2)
    Sp = chan((Sp + Sp.dag()) / 2)

    # find ground state of this Hamiltonian
    state = (-Sx - Sp).groundstate()[1].unit()
    if tensorstate: state = qt.tensor(qt.basis(2, 0), state)
    dim = N if not tensorstate else 2 * N

    tf_state = tf.cast(state.full().reshape([1, dim]), c64)

    return tf_state
Example #3
0
def _convert_local_operator_to_qutip(expr, full_space, mapping):
    """Convert a LocalOperator instance to qutip."""
    n = full_space.dimension
    if full_space != expr.space:
        all_spaces = full_space.local_factors
        own_space_index = all_spaces.index(expr.space)
        return qutip.tensor(
            *([qutip.qeye(s.dimension) for s in all_spaces[:own_space_index]] +
              [convert_to_qutip(expr, expr.space, mapping=mapping)] + [
                  qutip.qeye(s.dimension)
                  for s in all_spaces[own_space_index + 1:]
              ]))
    if isinstance(expr, Create):
        return qutip.create(n)
    elif isinstance(expr, Jz):
        return qutip.jmat((expr.space.dimension - 1) / 2.0, "z")
    elif isinstance(expr, Jplus):
        return qutip.jmat((expr.space.dimension - 1) / 2.0, "+")
    elif isinstance(expr, Jminus):
        return qutip.jmat((expr.space.dimension - 1) / 2.0, "-")
    elif isinstance(expr, Destroy):
        return qutip.destroy(n)
    elif isinstance(expr, Phase):
        arg = complex(expr.operands[1]) * arange(n)
        d = np_cos(arg) + 1j * np_sin(arg)
        return qutip.Qobj(np_diag(d))
    elif isinstance(expr, Displace):
        alpha = expr.operands[1]
        return qutip.displace(n, alpha)
    elif isinstance(expr, Squeeze):
        eta = expr.operands[1]
        return qutip.displace(n, eta)
    elif isinstance(expr, LocalSigma):
        j = expr.j
        k = expr.k
        if isinstance(j, str):
            j = expr.space.basis_labels.index(j)
        if isinstance(k, str):
            k = expr.space.basis_labels.index(k)
        ket = qutip.basis(n, j)
        bra = qutip.basis(n, k).dag()
        return ket * bra
    else:
        raise ValueError("Cannot convert '%s' of type %s" %
                         (str(expr), type(expr)))
def displaced_dm(rho,alpha):
    '''apply a displacement operator to a density matrix
    @ var rho: the input density matrix for the motion only
    @ var alpha: the displacement parameter
    
    returns rho_displaced: a density matrix Qobj
    '''
    N = shape(rho.data)[0] # assume N Fock states and two atom states
    D = displace(N, alpha)
    return D * rho * D.dag()
Example #5
0
def winger2d(rho, xvec, cavity):
    if (rho.type == 'ket'):
        rho = qt.ket2dm(rho)
    n = len(xvec)
    N = rho.dims[0][0]
    W = np.empty((n, n), dtype=float)
    a = qt.tensor(qt.destroy(N), qt.qeye(N))
    b = qt.tensor(qt.qeye(N), qt.destroy(N))
    P = (1j * np.pi * a.dag() * a).expm() * (1j * np.pi * b.dag() * b).expm()
    if (cavity == 'a'):
        for i, j in itertools.product(range(n), range(n)):
            D = qt.tensor(qt.displace(N, xvec[i] + xvec[j] * 1j), qt.qeye(N))
            W[i, j] = np.real((rho * D * P * D.dag()).tr())
    elif (cavity == 'b'):
        for i, j in itertools.product(range(n), range(n)):
            D = qt.tensor(qt.qeye(N), qt.displace(N, xvec[i] + xvec[j] * 1j))
            W[i, j] = np.real((rho * D * P * D.dag()).tr())

    return W
Example #6
0
def wigner4d(rho, xvec, cut_dim):
    if (rho.type == 'ket'):
        rho = qt.ket2dm(rho)
    n = len(xvec)
    N = rho.dims[0][0]
    W = np.empty((n, n), dtype=float)
    a = qt.tensor(qt.destroy(N), qt.qeye(N))
    b = qt.tensor(qt.qeye(N), qt.destroy(N))
    PJ = (1j * np.pi * a.dag() * a).expm() * (1j * np.pi * b.dag() * b).expm()
    if (cut_dim == 'im'):
        xvec = xvec.astype(complex) * 1j

    for i in range(n):
        DA = qt.tensor(qt.displace(N, xvec[i]), qt.qeye(N))
        for j in range(n):
            DB = qt.tensor(qt.qeye(N), qt.displace(N, xvec[j]))
            W[i, j] = np.real((rho * DA * DB * PJ * DB.dag() * DA.dag()).tr())

    return W
def displaced_dm(rho, alpha):
    '''apply a displacement operator to a density matrix
    @ var rho: the input density matrix for the motion only
    @ var alpha: the displacement parameter
    
    returns rho_displaced: a density matrix Qobj
    '''
    N = shape(rho.data)[0]  # assume N Fock states and two atom states
    D = displace(N, alpha)
    return D * rho * D.dag()
def GKP_1D_state(tensorstate, N, Delta):
    """ This function creates a gkp sensor state as qutip Qobj."""    
    Sx, Sp = qt.displace(N, sqrt(pi)), qt.displace(N, 1j*sqrt(pi))
     
    def envelope_operator(N, Delta=Delta):
        a = qt.destroy(N)
        n_op = a.dag()*a
        G = (-Delta**2 * n_op).expm()
        G_inv = (Delta**2 * n_op).expm()
        return lambda rho: G*rho*G_inv
    
    # Define Hermitian stablizers and apply the envelope
    chan = envelope_operator(N)
    Sx = chan((Sx + Sx.dag())/2)
    Sp = chan((Sp + Sp.dag())/2)
    
    # find ground state of this Hamiltonian
    state = (-Sx - Sp).groundstate()[1].unit()
    if tensorstate: state = qt.tensor(qt.identity(2), state)
    return state
Example #9
0
def wigner4d(rho, xvec):
    if (rho.type == 'ket'):
        rho = qt.ket2dm(rho)
    elif (rho.type is not 'oper'):
        print('invalid type')

    num_points = len(xvec)
    N = rho.shape[0]
    I = qt.qeye(N)
    a = qt.tensor(qt.destroy(N), qt.qeye(N))
    b = qt.tensor(qt.qeye(N), qt.destroy(N))
    PJ = (1j * np.pi * a.dag() * a).expm() * (1j * np.pi * b.dag() * b).expm()

    W = np.zeros((num_points, num_points), dtype=complex)
    for i, j in itertools.product(range(num_points), range(num_points)):
        print(i)
        DA = qt.tensor(qt.displace(N, xvec[i]), I)
        DB = qt.tensor(I, qt.displace(N, xvec[j]))
        W[i][j] = (rho * DA * DB * PJ * DB.dag() * DA.dag()).tr()
    return np.real(W)
Example #10
0
def g_0n(D, Num_max, NFock):
    """
    returns a D-squeezed 0-ancilla
    """

    r = np.log(1 / D)
    psi0 = qt.squeeze(NFock, r) * qt.basis(NFock, 0)
    psi = qt.Qobj()
    for n in np.arange(-Num_max, Num_max + 1):
        psi += np.exp(-2 * np.pi * D**2 * n**2) * qt.displace(
            NFock, n * np.sqrt(2 * np.pi)) * psi0
    return psi.unit()
Example #11
0
def test_displace():
    dp = qutip.displace(4, 0.25)
    dpmatrix = np.array([[
        0.96923323 + 0.j, -0.24230859 + 0.j, 0.04282883 + 0.j,
        -0.00626025 + 0.j
    ], [
        0.24230859 + 0.j, 0.90866411 + 0.j, -0.33183303 + 0.j, 0.07418172 + 0.j
    ], [
        0.04282883 + 0.j, 0.33183303 + 0.j, 0.84809499 + 0.j, -0.41083747 + 0.j
    ], [
        0.00626025 + 0.j, 0.07418172 + 0.j, 0.41083747 + 0.j, 0.90866411 + 0.j
    ]])
    np.testing.assert_allclose(dp.full(), dpmatrix, atol=1e-8)
Example #12
0
def test_displace():
    "Displacement operator"
    dp = displace(4, 0.25)
    dpmatrix = np.array(
        [[0.96923323 + 0.j, -0.24230859 + 0.j, 0.04282883 + 0.j, -
          0.00626025 + 0.j],
         [0.24230859 + 0.j, 0.90866411 + 0.j, -0.33183303 +
          0.j, 0.07418172 + 0.j],
         [0.04282883 + 0.j, 0.33183303 + 0.j, 0.84809499 +
          0.j, -0.41083747 + 0.j],
         [0.00626025 + 0.j, 0.07418172 + 0.j, 0.41083747 + 0.j,
          0.90866411 + 0.j]])

    assert_equal(np.allclose(dp.full(), dpmatrix), True)
Example #13
0
 def test_displaced_thermal(cls):
     '''
     compares the result of the computed distribution with the same result obtained through qutip
     '''
     alpha = np.sqrt(5) * np.random.ranf()
     nbar = 5 * np.random.ranf()
     test_entries = 100
     computed = cls.displaced_thermal(alpha, nbar, test_entries)
     from qutip import thermal_dm, displace
     thermal_dm = thermal_dm(test_entries, nbar, method = 'analytic')
     displace_operator = displace(test_entries, alpha)
     displaced_dm = displace_operator * thermal_dm * displace_operator.dag()
     qutip_result = displaced_dm.diag()
     return np.allclose(qutip_result, computed)
Example #14
0
    def __init__(self, Delta, fockdim):
        zero = qt.Qobj()
        one = qt.Qobj()
        lim = int(2 / Delta)
        for n1 in range(-lim, lim):
            for n2 in range(-2 * lim, 2 * lim):
                y = np.sqrt(np.pi / 2) * n2
                Dy = qt.displace(fockdim, 1j * y)
                x = np.sqrt(np.pi / 2) * (2 * n1)
                Dx = qt.displace(fockdim, x)
                alpha = x + 1j * y
                fac = np.exp(-Delta**2 * np.abs(alpha)**2)
                zero += fac * Dx * Dy * qt.basis(fockdim, 0)

                x = np.sqrt(np.pi / 2) * (2 * n1 + 1)
                Dx = qt.displace(fockdim, x)
                alpha = x + 1j * y
                fac = np.exp(-Delta**2 * np.abs(alpha)**2)
                one += fac * Dx * Dy * qt.basis(fockdim, 0)
        zero = zero / zero.norm()
        one = one / one.norm()
        RotationalCode.__init__(self, zero=zero, one=one)
        self._name = 'gkp'
Example #15
0
def test_displace():
    "Displacement operator"
    dp = displace(4, 0.25)
    dpmatrix = np.array(
        [[0.96923323 + 0.j, -0.24230859 + 0.j, 0.04282883 + 0.j, -
          0.00626025 + 0.j],
         [0.24230859 + 0.j, 0.90866411 + 0.j, -0.33183303 +
          0.j, 0.07418172 + 0.j],
         [0.04282883 + 0.j, 0.33183303 + 0.j, 0.84809499 +
          0.j, -0.41083747 + 0.j],
         [0.00626025 + 0.j, 0.07418172 + 0.j, 0.41083747 + 0.j,
          0.90866411 + 0.j]])

    assert_equal(np.allclose(dp.full(), dpmatrix), True)
 def test_displaced_thermal(cls):
     '''
     compares the result of the computed distribution with the same result obtained through qutip
     '''
     alpha = np.sqrt(5) * np.random.ranf()
     nbar = 5 * np.random.ranf()
     test_entries = 100
     computed = cls.displaced_thermal(alpha, nbar, test_entries)
     from qutip import thermal_dm, displace
     thermal_dm = thermal_dm(test_entries, nbar, method = 'analytic')
     displace_operator = displace(test_entries, alpha)
     displaced_dm = displace_operator * thermal_dm * displace_operator.dag()
     qutip_result = displaced_dm.diag()
     return np.allclose(qutip_result, computed)
Example #17
0
 def test_displaced_thermal(cls):
     '''
     compares the result of the computed distribution with the same result obtained through qutip
     '''
     alpha = np.sqrt(3) * np.random.ranf()
     nbar = 3 * np.random.ranf()
     test_entries = 10
     computed = cls.displaced_thermal(alpha, nbar, test_entries)
     from qutip import thermal_dm, displace
     #using a much higher dimension of 100 than the end result
     thermal_dm = thermal_dm(100, nbar)
     displace_operator = displace(100, alpha)
     displaced_dm = displace_operator * thermal_dm * displace_operator.dag()
     qutip_result = displaced_dm.diag()[0:test_entries]
     return np.allclose(qutip_result, computed)
Example #18
0
    def D(self, alpha, full_space=True):
        """Returns the displacement operator.

        Args:
            alpha (complex): Displacement amplitude.
            full_space (optional, bool): Whether to embed
                the operator in the full Hilbert space.
                Default: True.

        Returns:
            ``qutip.Qobj``: Displacement operator.
        """
        op = qutip.displace(self.levels, alpha)
        if full_space:
            return self.tensor_with_I(op)
        return op
Example #19
0
 def __init__(self, N, r, alpha, fockdim):
     zero = qt.Qobj()
     one = qt.Qobj()
     for m in range(2 * N):
         phi = m * np.pi / N
         D = qt.displace(fockdim, alpha * np.exp(1j * phi))
         S = qt.squeeze(fockdim, r * np.exp(2j * (phi - np.pi / 2)))
         blade = D * S * qt.basis(fockdim, 0)
         zero += blade
         one += (-1)**m * blade
     zero = zero / zero.norm()
     one = one / one.norm()
     self._alpha = alpha
     self._r = r
     RotationalCode.__init__(self, zero=zero, one=one, N=N)
     self._name = 'cat'
Example #20
0
    def test_displaced_thermal(cls):
        """
        compares the result of the computed distribution with the same result obtained through qutip
        """
        alpha = np.sqrt(3) * np.random.ranf()
        nbar = 3 * np.random.ranf()
        test_entries = 10
        computed = cls.displaced_thermal(alpha, nbar, test_entries)
        from qutip import thermal_dm, displace

        # using a much higher dimension of 100 than the end result
        thermal_dm = thermal_dm(100, nbar)
        displace_operator = displace(100, alpha)
        displaced_dm = displace_operator * thermal_dm * displace_operator.dag()
        qutip_result = displaced_dm.diag()[0:test_entries]
        return np.allclose(qutip_result, computed)
Example #21
0
def measure(alpha, rho=None):
    """
    Measures the photon number statistics for state rho when displaced
    by angle alpha.
    
    Parameters
    ----------    
    alpha: np.complex
        A complex displacement.
    Returns
    -------
    population: ndarray
        A 1D array for the probabilities for populations.
    """
    hilbertsize = rho.shape[0]
    D = displace(hilbertsize, -alpha)
    rho_disp = D*rho*D.dag()
    populations = np.real(np.diagonal(rho_disp.full()))
    return np.array(populations).reshape(-1)
Example #22
0
def build_m_lib(N, num_points, xvec, data_filepath):
    print('building m_lib')
    I = qt.qeye(N)
    a = qt.tensor(qt.destroy(N), qt.qeye(N))
    b = qt.tensor(qt.qeye(N), qt.destroy(N))
    PJ = (1j * np.pi * a.dag() * a).expm() * (1j * np.pi * b.dag() * b).expm()
    t_start = time.time()
    ''' reA vs imA '''
    m_lib = [[[0 for k in range(num_points)] for j in range(num_points)]
             for i in range(4)]
    DB = qt.tensor(I, I)
    for i, j in itertools.product(range(num_points), range(num_points)):
        DA = qt.tensor(qt.displace(N, xvec[i] + xvec[j] * 1j), I)
        m_lib[0][i][j] = DA * DB * PJ * DB.dag() * DA.dag()
    ''' reB vs imB '''
    DA = qt.tensor(I, I)
    for i, j in itertools.product(range(num_points), range(num_points)):
        DB = qt.tensor(I, qt.displace(N, xvec[i] + xvec[j] * 1j))
        m_lib[1][i][j] = DA * DB * PJ * DB.dag() * DA.dag()
    ''' reA vs reB '''
    for i, j in itertools.product(range(num_points), range(num_points)):
        DA = qt.tensor(qt.displace(N, xvec[i]), I)
        DB = qt.tensor(I, qt.displace(N, xvec[j]))
        m_lib[2][i][j] = DA * DB * PJ * DB.dag() * DA.dag()
    ''' imA vs imB '''
    for i, j in itertools.product(range(num_points), range(num_points)):
        DA = qt.tensor(qt.displace(N, xvec[i] * 1j), I)
        DB = qt.tensor(I, qt.displace(N, xvec[j] * 1j))
        m_lib[3][i][j] = DA * DB * PJ * DB.dag() * DA.dag()

    filepath = data_filepath + 'm_lib_[' + str(min(xvec)) + ',' + str(
        max(xvec)) + ']_' + str(num_points)
    qt.fileio.qsave(m_lib, name=filepath)
    print('saved to ' + data_filepath + 'm_lib_[' + str(min(xvec)) + ',' +
          str(max(xvec)) + ']_' + str(num_points))
    print('build time: ' + str(time.time() - t_start) + ' sec')
    return m_lib
Example #23
0
Created on Tue Apr  2 13:35:01 2019

@author: Eduardo Villasenor 
"""

import qutip as qt
import numpy as np

N = 20

vacuum = qt.basis(N, 0)

a = qt.create(N).dag()

alpha = 2 + 1j
D = qt.displace(N, alpha)

x = (a + a.dag()) / np.sqrt(2)
p = 1j * (-a + a.dag()) / np.sqrt(2)

basis = [x, p]

state = D * vacuum

print("<a>:", qt.expect(a, state))
print("<x>:", qt.expect(x, state))
print("<p>:", qt.expect(p, state))

cm = qt.covariance_matrix(basis, state)
print("CM:", cm)
def f(N, alpha):
    return qt.displace(N, alpha)
Example #25
0
def Disp(n, m):
    return qt.displace(NFock,
                       n * np.sqrt(np.pi / 2) + 1j * m * np.sqrt(np.pi / 2))
Example #26
0
kappa = 0.2
omega = 1
offset = 3
K = 0.03
T = 1
E = 1
V = E
V_q = V
V_p = V

MaxN = 30

NFock = 200

Stabilizer_q = qt.displace(NFock, 1j * np.sqrt(2 * np.pi))
Stabilizer_p = qt.displace(NFock, np.sqrt(2 * np.pi))
Z = qt.displace(NFock, 1j * np.sqrt(np.pi / 2))
X = qt.displace(NFock, np.sqrt(np.pi / 2))

a = qt.destroy(NFock)
a_dag = a.dag()

H_free = E / 2 * (qt.position(NFock)**2 + qt.momentum(NFock)**2)
H_K = K * qt.num(NFock)**2
H_Cass = K * (a**4 - offset**4).dag() * (a**4 - offset**4
                                         )  #+K*np.abs(offset)**2

H_stab = -V_q * (2 * np.sqrt(np.pi) * qt.position(NFock)).cosm() - V_p * (
    2 * np.sqrt(np.pi) * qt.momentum(NFock)).cosm()
Example #27
0
def test_displace_type():
    "Operator CSR Type: displace"
    op = displace(5, 0.1)
    assert_equal(isspmatrix_csr(op.data), True)
Example #28
0
def build_cos_SHO(dim, zeta, coeff):
    D = displace(dim, 1j * float(coeff * sqrt(zeta / 2)))  # exp(coeff*1j*x)
    op = (D + D.dag()) / 2
    return op
Example #29
0
def build_sin_SHO(dim, zeta, coeff):
    D = displace(dim, 1j * float(coeff * sqrt(zeta / 2)))
    op = (D - D.dag()) / (2 * 1j)
    return op
Example #30
0
def test_displace_type():
    "Operator CSR Type: displace"
    op = displace(5, 0.1)
    assert_equal(isspmatrix_csr(op.data), True)
Example #31
0
def U_kerr(t, dim, params, **kwargs):
    H = ham_kerr(dim, params, **kwargs)
    return linalg.expm(-1j*H*t)

def U_parity(t, dim, params):
    H = ham_parity(dim, params)
    return linalg.expm(-1j*H*t)
    

'''
Calculations
'''

if 0: # qutip diagonalization
    H = qt.num(dim) - 0.5*a*(qt.displace(dim,1j*x) + qt.displace(dim,-1j*x))
    ev = H.eigenenergies()
    
    if 1: # plot
        fig, ax = plt.subplots()
        ax.plot(range(dim-1),ev[1:]-ev[:-1],'o')
        ax.plot(range(dim-1),ev[1:]-ev[:-1])
        ax.set_xlim(0,4*x)

if 0: # manual diagonalization
    H = ham(dim, params)
    ev, ek = linalg.eigh(H)
    
    if 1: # plot
        fig, ax = plt.subplots()
        ax.plot(range(dim-1),ev[1:]-ev[:-1],'o')