コード例 #1
0
ファイル: random_objects.py プロジェクト: qutip/qutip
def rand_ket(N, density=1, dims=None):
    """Creates a random Nx1 sparse ket vector.

    Parameters
    ----------
    N : int
        Number of rows for output quantum operator.
    density : float
        Density between [0,1] of output ket state.
    dims : list
        Left-dimensions of quantum object.  Used for specifying
        tensor structure. Default is dims=[[N]].

    Returns
    -------
    oper : qobj
        Nx1 ket state quantum operator.

    """
    if dims:
        _check_ket_dims(dims, N)
    X = sp.rand(N, 1, density, format="csr")
    X.data = X.data - 0.5
    Y = X.copy()
    Y.data = 1.0j * (np.random.random(len(X.data)) - 0.5)
    X = X + Y
    X.sort_indices()
    X = Qobj(X)
    if dims:
        return Qobj(X / X.norm(), dims=[dims, [1]], shape=[N, 1])
    else:
        return Qobj(X / X.norm())
コード例 #2
0
ファイル: random_objects.py プロジェクト: riviera2015/qutip
def rand_ket(N, density=1, dims=None):
    """Creates a random Nx1 sparse ket vector.

    Parameters
    ----------
    N : int
        Number of rows for output quantum operator.
    density : float
        Density between [0,1] of output ket state.
    dims : list
        Left-dimensions of quantum object.  Used for specifying
        tensor structure. Default is dims=[[N]].

    Returns
    -------
    oper : qobj
        Nx1 ket state quantum operator.

    """
    if dims:
        _check_ket_dims(dims, N)
    X = sp.rand(N, 1, density, format='csr')
    X.data = X.data - 0.5
    Y = X.copy()
    Y.data = 1.0j * (np.random.random(len(X.data)) - 0.5)
    X = X + Y
    X.sort_indices()
    X = Qobj(X)
    if dims:
        return Qobj(X / X.norm(), dims=dims)
    else:
        return Qobj(X / X.norm())
コード例 #3
0
ファイル: test_qobj.py プロジェクト: kafischer/qutip
def test_QobjNorm():
    "Qobj norm"
    # vector L2-norm test
    N = 20
    x = np.random.random(N) + 1j * np.random.random(N)
    A = Qobj(x)
    assert_equal(np.abs(A.norm() - la.norm(A.data.data, 2)) < 1e-12, True)
    # vector max (inf) norm test
    assert_equal(np.abs(A.norm("max") - la.norm(A.data.data, np.inf)) < 1e-12, True)
    # operator frobius norm
    x = np.random.random((N, N)) + 1j * np.random.random((N, N))
    A = Qobj(x)
    assert_equal(np.abs(A.norm("fro") - la.norm(A.full(), "fro")) < 1e-12, True)
コード例 #4
0
ファイル: test_qobj.py プロジェクト: Marata459/qutip
def test_QobjNorm():
    "Qobj norm"
    # vector L2-norm test
    N = 20
    x = np.random.random(N) + 1j * np.random.random(N)
    A = Qobj(x)
    assert_equal(np.abs(A.norm() - la.norm(A.data.data, 2)) < 1e-12, True)
    # vector max (inf) norm test
    assert_equal(
        np.abs(A.norm('max') - la.norm(A.data.data, np.inf)) < 1e-12, True)
    # operator frobius norm
    x = np.random.random((N, N)) + 1j * np.random.random((N, N))
    A = Qobj(x)
    assert_equal(
        np.abs(A.norm('fro') - la.norm(A.full(), 'fro')) < 1e-12, True)
コード例 #5
0
def test_QobjNorm():
    "Qobj norm"
    # vector L2-norm test
    N = 20
    x = np.random.random(N) + 1j * np.random.random(N)
    A = Qobj(x)
    assert np.abs(A.norm() - la.norm(A.data.data, 2)) < 1e-12
    # vector max (inf) norm test
    assert np.abs(A.norm('max') - la.norm(A.data.data, np.inf)) < 1e-12
    # operator frobius norm
    x = np.random.random((N, N)) + 1j * np.random.random((N, N))
    A = Qobj(x)
    assert np.abs(A.norm('fro') - la.norm(A.full(), 'fro')) < 1e-12
    # operator trace norm
    a = rand_herm(10, 0.25)
    assert np.allclose(a.norm(), (a * a.dag()).sqrtm().tr().real)
    b = rand_herm(10, 0.25) - 1j * rand_herm(10, 0.25)
    assert np.allclose(b.norm(), (b * b.dag()).sqrtm().tr().real)
コード例 #6
0
ファイル: test_qobj.py プロジェクト: arnelg/qutip
def test_QobjNorm():
    "Qobj norm"
    # vector L2-norm test
    N = 20
    x = np.random.random(N) + 1j * np.random.random(N)
    A = Qobj(x)
    assert_equal(np.abs(A.norm() - la.norm(A.data.data, 2)) < 1e-12, True)
    # vector max (inf) norm test
    assert_equal(
        np.abs(A.norm('max') - la.norm(A.data.data, np.inf)) < 1e-12, True)
    # operator frobius norm
    x = np.random.random((N, N)) + 1j * np.random.random((N, N))
    A = Qobj(x)
    assert_equal(
        np.abs(A.norm('fro') - la.norm(A.full(), 'fro')) < 1e-12, True)
    # operator trace norm
    a = rand_herm(10,0.25)
    assert_almost_equal(a.norm(), (a*a.dag()).sqrtm().tr().real)
    b = rand_herm(10,0.25) - 1j*rand_herm(10,0.25)
    assert_almost_equal(b.norm(), (b*b.dag()).sqrtm().tr().real)
コード例 #7
0
ファイル: random_objects.py プロジェクト: qutip/qutip
def rand_ket(N=None, density=1, dims=None, seed=None):
    """Creates a random Nx1 sparse ket vector.

    Parameters
    ----------
    N : int
        Number of rows for output quantum vector.
        If None, N is deduced from dims.
    density : float
        Density between [0,1] of output ket state.
    dims : list
        Dimensions of quantum object.  Used for specifying
        tensor structure. Default is dims=[[N],[1]].
    seed : int
        Seed for the random number generator.

    Returns
    -------
    oper : qobj
        Nx1 ket quantum state vector.

    Raises
    -------
    ValueError
        If neither `N` or `dims` are specified.

    """
    if seed is not None:
        np.random.seed(seed=seed)
    if N is None and dims is None:
        raise ValueError('Specify either the number of rows of state vector'
                         '(N) or dimensions of quantum object (dims)')
    if N is not None and dims:
        _check_dims(dims, N, 1)
    elif dims:
        N = np.prod(dims[0])
        _check_dims(dims, N, 1)
    else:
        dims = [[N], [1]]
    X = sp.rand(N, 1, density, format='csr')
    while X.nnz == 0:
        # ensure that the ket is not all zeros.
        X = sp.rand(N, 1, density + 1 / N, format='csr')
    X.data = X.data - 0.5
    Y = X.copy()
    Y.data = 1.0j * (np.random.random(len(X.data)) - 0.5)
    X = X + Y
    X.sort_indices()
    X = Qobj(X)
    return Qobj(X / X.norm(), dims=dims)
コード例 #8
0
def rand_ket(N=0, density=1, dims=None, seed=None):
    """Creates a random Nx1 sparse ket vector.

    Parameters
    ----------
    N : int
        Number of rows for output quantum operator.
        If None or 0, N is deduced from dims.
    density : float
        Density between [0,1] of output ket state.
    dims : list
        Dimensions of quantum object.  Used for specifying
        tensor structure. Default is dims=[[N],[1]].

    Returns
    -------
    oper : qobj
        Nx1 ket state quantum operator.

    """
    if seed is not None:
        np.random.seed(seed=seed)
    if N and dims:
        _check_dims(dims, N, 1)
    elif dims:
        N = prod(dims[0])
        _check_dims(dims, N, 1)
    else:
        dims = [[N],[1]]
    X = sp.rand(N, 1, density, format='csr')
    while X.nnz == 0:
        # ensure that the ket is not all zeros.
        X = sp.rand(N, 1, density+1/N, format='csr')
    X.data = X.data - 0.5
    Y = X.copy()
    Y.data = 1.0j * (np.random.random(len(X.data)) - 0.5)
    X = X + Y
    X.sort_indices()
    X = Qobj(X)
    return Qobj(X / X.norm(), dims=dims)
コード例 #9
0
def test_tidyup_default(tol):
    with auto_tidyup_tol(tol):
        small = Qobj(1) * 1e-10
        assert (small.norm() == 0) == (tol > 1e-10)