Exemple #1
0
def test_floaty():
    """ More tests using a precomputed permanent """
    np.random.seed(1234)
    m = np.random.uniform(0, 1, 16) + 1j*np.random.uniform(0, 1, 16)
    m = m.reshape(4,4)
    p = permanent(m)
    assert np.allclose(p, -8.766131870776363+1.072095650303524j)
def simulate(input_state, unitary, patterns, mode="probability", **kwargs):
    """ Simulates a given circuit, for a given input state, looking at certain terms in the output state """
    output_state = defaultdict(complex)
    for cols, amplitude in input_state.items():
        cols = list(cols)
        n1 = normalization(cols)
        for rows in patterns:
            n2 = normalization(rows)
            perm = permanent(unitary[list(rows)][:, cols])
            value = amplitude * perm / sqrt(n1 * n2)
            output_state[rows] += value
    return output_state
Exemple #3
0
def get_amplitudes(input_state, unitary, patterns):
    """ Simulates a given circuit, for a given input state, looking at certain terms in the output state """
    output_state = State()
    for cols, amplitude in input_state.items():
        cols = list(cols)
        n1 = normalization(cols)
        for rows in patterns:
            n2 = normalization(rows)
            perm = permanent(unitary[list(rows)][:, cols])
            value = amplitude * perm / np.sqrt(n1 * n2)
            output_state[rows] += value
    return output_state
Exemple #4
0
def get_amplitudes(input_state, unitary, patterns):
    """ Simulates a given circuit, for a given input state, looking at certain terms in the output state """
    output_state = defaultdict(complex)
    for cols, amplitude in input_state.items():
        cols = list(cols)
        n1 = normalization(cols)
        for rows in patterns:
            n2 = normalization(rows)
            perm = permanent(unitary[list(rows)][:, cols])
            value = amplitude * perm / np.sqrt(n1 * n2)
            if np.abs(value) > precision:
                output_state[rows] += value
    return output_state
Exemple #5
0
def simulate(input_state, unitary, patterns, mode="probability", **kwargs):
    """ Simulates a given circuit, for a given input state, looking at certain terms in the output state """
    output_state = defaultdict(complex)
    u2 = np.abs(unitary) ** 2
    for cols, amplitude in input_state.items():
        cols = list(cols)
        n1 = normalization(cols)
        for rows in patterns:
            n2 = normalization(rows)
            perm = permanent(unitary[list(rows)][:, cols])
            value = amplitude * perm / np.sqrt(n1 * n2)
            if value != 0:
                output_state[rows] += value

    if mode == "probability":
        for key, value in output_state.items():
            output_state[key] = np.abs(value) ** 2
    return output_state
Exemple #6
0
def test_permanent():
    """ Dumb tests """
    m = np.eye(10, dtype=complex)
    assert permanent(m) == 1
    m = np.zeros((10, 10), dtype=complex)
    assert permanent(m) == 0
Exemple #7
0
def test_error():
    """ Should raise a TypeError as we are using the wrong dtype """
    m = np.eye(10, dtype=float)
    permanent(m)
Exemple #8
0
import permanent

print permanent.permanent('awd')