예제 #1
0
파일: gate.py 프로젝트: jandom/SEAnalysis
def test():
    """Test script for gates.
    """
    # Ville Bergholm 2010

    from numpy.random import randn
    from base import sx, sy, sz, tol

    dim = (2, 4)

    I = id(dim)
    U = swap(*dim)
    assert_o((U.ctranspose() * U - I).norm(), 0, tol)  # swap' * swap = I

    # TODO test the output
    U = mod_add(2, 4, 3)
    U = mod_inc(3, dim, 5)
    V = mod_mul(2, dim, 5)
    dist(U, V)
    U = phase(randn(prod(dim)), dim)
    U = qft(dim)
    U = walsh(3)
    U = controlled(sz, (1, 0), dim)
    cnot = controlled(sx)
    U = single(sy, 0, dim)
    U = two(cnot, (2,0), (2,3,2))
예제 #2
0
파일: ho.py 프로젝트: jandom/SEAnalysis
def test():
    """Testing script for the harmonic oscillator module."""
    from numpy.random import randn
    from utils import assert_o

    def randc():
        """Random complex number."""
        return randn() + 1j*randn()

    a = mat(boson_ladder(default_n))

    alpha = randc()
    s = coherent_state(alpha)
    s0 = state(0, default_n)
    D = displace(alpha)
    
    assert_o((s - s0.u_propagate(D)).norm(), 0, tol)  # displacement

    z = randc()
    S = squeeze(z)

    Q = position(); P = momentum()
    q = randn(); p = randn()
    sq = position_state(q)
    sp = momentum_state(p)

    temp = 1e-1 # the truncation accuracy is not amazing here
    assert_o(sq.ev(Q), q, temp)  # Q, P eigenstates
    assert_o(sp.ev(P), p, temp)

    temp = ones(default_n);  temp[-1] = -default_n+1 # truncation...
    assert_o(norm(comm(Q,P) - 1j * diag(temp)), 0, tol) # [Q, P] = i

    assert_o(norm(mat(P)**2 +mat(Q)**2 - 2 * a.H * a -diag(temp)), 0, tol)  # P^2 +Q^2 = 2a^\dagger * a + 1