示例#1
0
 def testOperatorKet(self):
     """
     expect: operator and ket
     """
     N = 10
     op_N = num(N)
     op_a = destroy(N)
     for n in range(N):
         e = expect(op_N, fock(N, n))
         assert_(e == n)
         assert_(type(e) == float)
         e = expect(op_a, fock(N, n))
         assert_(e == 0)
         assert_(type(e) == complex)
示例#2
0
 def testOperatorKet(self):
     """
     expect: operator and ket
     """
     N = 10
     op_N = num(N)
     op_a = destroy(N)
     for n in range(N):
         e = expect(op_N, fock(N, n))
         assert_(e == n)
         assert_(type(e) == float)
         e = expect(op_a, fock(N, n))
         assert_(e == 0)
         assert_(type(e) == complex)
示例#3
0
def test_wigner_fock():
    "wigner: test wigner function calculation for Fock states"

    xvec = np.linspace(-5.0, 5.0, 100)
    yvec = xvec

    X, Y = np.meshgrid(xvec, yvec)

    a = X + 1j * Y  # consistent with g=2 option to wigner function

    dx = xvec[1] - xvec[0]
    dy = yvec[1] - yvec[0]

    N = 15

    for n in [2, 3, 4, 5, 6]:

        psi = fock(N, n)

        # calculate the wigner function using qutip and analytic formula
        W_qutip = wigner(psi, xvec, yvec, g=2)
        W_analytic = 2 / np.pi * (-1) ** n * \
            np.exp(-2 * abs(a) ** 2) * np.polyval(laguerre(n), 4 * abs(a) ** 2)

        # check difference
        assert_(np.sum(abs(W_qutip - W_analytic)) < 1e-4)

        # check normalization
        assert_(np.sum(W_qutip) * dx * dy - 1.0 < 1e-8)
        assert_(np.sum(W_analytic) * dx * dy - 1.0 < 1e-8)
示例#4
0
def test_wigner_fock():
    "wigner: test wigner function calculation for Fock states"

    xvec = np.linspace(-5.0, 5.0, 100)
    yvec = xvec

    X, Y = np.meshgrid(xvec, yvec)

    a = X + 1j * Y  # consistent with g=2 option to wigner function

    dx = xvec[1] - xvec[0]
    dy = yvec[1] - yvec[0]

    N = 15

    for n in [2, 3, 4, 5, 6]:

        psi = fock(N, n)

        # calculate the wigner function using qutip and analytic formula
        W_qutip = wigner(psi, xvec, yvec, g=2)
        W_analytic = 2 / np.pi * (-1) ** n * \
            np.exp(-2 * abs(a) ** 2) * np.polyval(laguerre(n), 4 * abs(a) ** 2)

        # check difference
        assert_(np.sum(abs(W_qutip - W_analytic)) < 1e-4)

        # check normalization
        assert_(np.sum(W_qutip) * dx * dy - 1.0 < 1e-8)
        assert_(np.sum(W_analytic) * dx * dy - 1.0 < 1e-8)
示例#5
0
    def testOperatorListState(self):
        """
        expect: operator list and state
        """
        res = expect([sigmax(), sigmay(), sigmaz()], fock(2, 0))
        assert_(len(res) == 3)
        assert_(all(abs(res - [0, 0, 1]) < 1e-12))

        res = expect([sigmax(), sigmay(), sigmaz()], fock_dm(2, 1))
        assert_(len(res) == 3)
        assert_(all(abs(res - [0, 0, -1]) < 1e-12))
示例#6
0
    def testOperatorListState(self):
        """
        expect: operator list and state
        """
        res = expect([sigmax(), sigmay(), sigmaz()], fock(2, 0))
        assert_(len(res) == 3)
        assert_(all(abs(res - [0, 0, 1]) < 1e-12))

        res = expect([sigmax(), sigmay(), sigmaz()], fock_dm(2, 1))
        assert_(len(res) == 3)
        assert_(all(abs(res - [0, 0, -1]) < 1e-12))
示例#7
0
    def testOperatorListStateList(self):
        """
        expect: operator list and state list
        """
        operators = [sigmax(), sigmay(), sigmaz(), sigmam(), sigmap()]
        states = [fock(2, 0), fock(2, 1), fock_dm(2, 0), fock_dm(2, 1)]
        res = expect(operators, states)

        assert_(len(res) == len(operators))

        for r_idx, r in enumerate(res):

            assert_(isinstance(r, np.ndarray))

            if operators[r_idx].isherm:
                assert_(r.dtype == np.float64)
            else:
                assert_(r.dtype == np.complex128)

            for s_idx, s in enumerate(states):
                assert_(r[s_idx] == expect(operators[r_idx], states[s_idx]))
示例#8
0
    def testOperatorListStateList(self):
        """
        expect: operator list and state list
        """
        operators = [sigmax(), sigmay(), sigmaz(), sigmam(), sigmap()]
        states = [fock(2, 0), fock(2, 1), fock_dm(2, 0), fock_dm(2, 1)]
        res = expect(operators, states)

        assert_(len(res) == len(operators))

        for r_idx, r in enumerate(res):

            assert_(isinstance(r, np.ndarray))

            if operators[r_idx].isherm:
                assert_(r.dtype == np.float64)
            else:
                assert_(r.dtype == np.complex128)

            for s_idx, s in enumerate(states):
                assert_(r[s_idx] == expect(operators[r_idx], states[s_idx]))
示例#9
0
    def testExpectSolverCompatibility(self):
        """
        expect: operator list and state list
        """
        c_ops = [0.0001 * sigmaz()]
        e_ops = [sigmax(), sigmay(), sigmaz(), sigmam(), sigmap()]
        times = np.linspace(0, 10, 100)

        res1 = mesolve(sigmax(), fock(2, 0), times, c_ops, e_ops)
        res2 = mesolve(sigmax(), fock(2, 0), times, c_ops, [])

        e1 = res1.expect
        e2 = expect(e_ops, res2.states)

        assert_(len(e1) == len(e2))

        for n in range(len(e1)):
            assert_(len(e1[n]) == len(e2[n]))
            assert_(isinstance(e1[n], np.ndarray))
            assert_(isinstance(e2[n], np.ndarray))
            assert_(e1[n].dtype == e2[n].dtype)
            assert_(all(abs(e1[n] - e2[n]) < 1e-12))
示例#10
0
    def testExpectSolverCompatibility(self):
        """
        expect: operator list and state list
        """
        c_ops = [0.0001 * sigmaz()]
        e_ops = [sigmax(), sigmay(), sigmaz(), sigmam(), sigmap()]
        times = np.linspace(0, 10, 100)

        res1 = mesolve(sigmax(), fock(2, 0), times, c_ops, e_ops)
        res2 = mesolve(sigmax(), fock(2, 0), times, c_ops, [])

        e1 = res1.expect
        e2 = expect(e_ops, res2.states)

        assert_(len(e1) == len(e2))

        for n in range(len(e1)):
            assert_(len(e1[n]) == len(e2[n]))
            assert_(isinstance(e1[n], np.ndarray))
            assert_(isinstance(e2[n], np.ndarray))
            assert_(e1[n].dtype == e2[n].dtype)
            assert_(all(abs(e1[n] - e2[n]) < 1e-12))
示例#11
0
文件: utils.py 项目: quantshah/qst-nn
def num(hilbert_size, probs=None, mu=0):
    """
    Generates the number states.

    For a detailed discussion on the definition see
    `Albert, Victor V. et al. “Performance and Structure of Single-Mode Bosonic Codes.” Physical Review A 97.3 (2018) <https://arxiv.org/abs/1708.05010>`_
    and `Ahmed, Shahnawaz et al., “Classification and reconstruction of quantum states with neural networks.” Journal <https://arxiv.org/abs/1708.05010>`_

    Args:
        hilbert_size (int): Hilbert space dimension (cutoff). For the well defined
                            number states that we use here, the Hilbert space size 
                            should not be less than 32. If the probabilities are not
                            supplied then we will randomly select a set.
        probs (None, optional): Probabilitiy vector for the number state. If not supplied then a
                                random vector is selected from the five different sets from the function
                                `_get_num_prob`.
        mu (int, optional): Logical encoding (0/1)
                            default: 0
    
    Returns:
        :class:`qutip.Qobj`: Number state ket.
    
    """
    if (probs == None) and (hilbert_size < 32):
        err = "Specify a larger Hilbert size for default\n"
        err += "num state if probabilities are not specified\n"
        raise ValueError(err)

    state = fock(hilbert_size, 0) * 0

    if probs == None:
        probs = _get_num_prob(0)

    for n, p in enumerate(probs[mu]):
        state += p * fock(hilbert_size, n)
    ket = state.unit()
    return ket
示例#12
0
    def testOperatorStateList(self):
        """
        expect: operator and state list
        """
        N = 10
        op = num(N)

        res = expect(op, [fock(N, n) for n in range(N)])
        assert_(all(res == range(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.float64)

        res = expect(op, [fock_dm(N, n) for n in range(N)])
        assert_(all(res == range(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.float64)

        op = destroy(N)

        res = expect(op, [fock(N, n) for n in range(N)])
        assert_(all(res == np.zeros(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.complex128)

        res = expect(op, [fock_dm(N, n) for n in range(N)])
        assert_(all(res == np.zeros(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.complex128)
示例#13
0
    def testOperatorStateList(self):
        """
        expect: operator and state list
        """
        N = 10
        op = num(N)

        res = expect(op, [fock(N, n) for n in range(N)])
        assert_(all(res == range(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.float64)

        res = expect(op, [fock_dm(N, n) for n in range(N)])
        assert_(all(res == range(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.float64)

        op = destroy(N)

        res = expect(op, [fock(N, n) for n in range(N)])
        assert_(all(res == np.zeros(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.complex128)

        res = expect(op, [fock_dm(N, n) for n in range(N)])
        assert_(all(res == np.zeros(N)))
        assert_(isinstance(res, np.ndarray) and res.dtype == np.complex128)
示例#14
0
文件: utils.py 项目: quantshah/qst-nn
def binomial(hilbert_size, S, N=None, mu=0):
    """
    Generates a binomial state.

    For a detailed discussion on the definition see
    `Albert, Victor V. et al. “Performance and Structure of Single-Mode Bosonic Codes.” Physical Review A 97.3 (2018) <https://arxiv.org/abs/1708.05010>`_
    and `Ahmed, Shahnawaz et al., “Classification and reconstruction of quantum states with neural networks.” Journal <https://arxiv.org/abs/1708.05010>`_
    
    Args:
        hilbert_size (int): Hilbert space size (cutoff).
        S (int): An integer parameter specifying 
        N (None, optional): A non-negative integer which specifies the order to which we can
                            correct dephasing errors and is similar to the ´alpha´ parameter
                            for cat states.
        mu (int, optional): Logical encoding (0/1)
                            default: 0
    
    Returns:
        :class:`qutip.Qobj`: Binomial state ket.
    """
    if N == None:
        Nmax = int((hilbert_size) / (S + 1)) - 1
        try:
            N = np.random.randint(0, Nmax)
        except:
            N = Nmax

    c = 1 / sqrt(2**(N + 1))

    psi = 0 * fock(hilbert_size, 0)

    for m in range(N):
        psi += (c * ((-1)**(mu * m)) * np.sqrt(binom(N + 1, m)) *
                fock(hilbert_size, (S + 1) * m))
    ket = psi.unit()
    return ket
@author: leonardo
'''
from numpy import pi
from qutip import tensor
from qutip.operators import sigmax, identity
from qutip.qip.circuit import QubitCircuit
from qutip.qip.gates import hadamard_transform, cnot, snot, swap, \
    gate_sequence_product
from qutip.qip.qubits import qubit_states
from qutip.states import basis, fock

print(basis(N=4, n=0))
print("\n")

print(fock(N=4, n=0))
print("\n")

print(qubit_states(N=2, states=[0, 0]))
print("\n")

x = sigmax()
print(x)
print("\n")

ket_zero = qubit_states(N=1, states=[0])

print(ket_zero)
print("\n")

print(x * ket_zero)