Ejemplo n.º 1
0
 def test_vectorize(self):
     mat = [[1, 2], [3, 4]]
     col = [1, 3, 2, 4]
     row = [1, 2, 3, 4]
     paul = [5, 5, -1j, -3]
     test_pass = (np.linalg.norm(vectorize(mat) - col) == 0 and
                  np.linalg.norm(vectorize(mat, method='col') - col) == 0 and
                  np.linalg.norm(vectorize(mat, method='row') - row) == 0 and
                  np.linalg.norm(vectorize(mat, method='pauli') - paul) == 0)
     self.assertTrue(test_pass)
Ejemplo n.º 2
0
 def test_vectorize(self):
     mat = [[1, 2], [3, 4]]
     col = [1, 3, 2, 4]
     row = [1, 2, 3, 4]
     paul = [5, 5, -1j, -3]
     test_pass = np.linalg.norm(vectorize(mat) - col) == 0 and \
         np.linalg.norm(vectorize(mat, method='col') - col) == 0 and \
         np.linalg.norm(vectorize(mat, method='row') - row) == 0 and \
         np.linalg.norm(vectorize(mat, method='pauli') - paul) == 0
     self.assertTrue(test_pass)
Ejemplo n.º 3
0
def __tomo_basis_matrix(meas_basis):
    """Return a matrix of vectorized measurement operators.

    Args:
        meas_basis(list(array_like)): measurement operators [M_j].
    Returns:
        The operators S = sum_j |j><M_j|.
    """
    n = len(meas_basis)
    d = meas_basis[0].size
    S = np.array([vectorize(m).conj() for m in meas_basis])
    return S.reshape(n, d)
Ejemplo n.º 4
0
def __tomo_basis_matrix(meas_basis):
    """Return a matrix of vectorized measurement operators.

    Args:
        meas_basis(list(array_like)): measurement operators [M_j].
    Returns:
        The operators S = sum_j |j><M_j|.
    """
    n = len(meas_basis)
    d = meas_basis[0].size
    S = np.array([vectorize(m).conj() for m in meas_basis])
    return S.reshape(n, d)
Ejemplo n.º 5
0
def __tomo_linear_inv(freqs, ops, weights=None, trace=None):
    """
    Reconstruct a matrix through linear inversion.

    Args:
        freqs (list[float]): list of observed frequences.
        ops (list[np.array]): list of corresponding projectors.
        weights (list[float] or array_like):
            weights to be used for weighted fitting.
        trace (float or None): trace of returned operator.

    Returns:
        numpy.array: A numpy array of the reconstructed operator.
    """
    # get weights matrix
    if weights is not None:
        W = np.array(weights)
        if W.ndim == 1:
            W = np.diag(W)

    # Get basis S matrix
    S = np.array([vectorize(m).conj()
                  for m in ops]).reshape(len(ops), ops[0].size)
    if weights is not None:
        S = np.dot(W, S)  # W.S

    # get frequencies vec
    v = np.array(freqs)  # |f>
    if weights is not None:
        v = np.dot(W, freqs)  # W.|f>
    Sdg = S.T.conj()  # S^*.W^*
    inv = np.linalg.pinv(np.dot(Sdg, S))  # (S^*.W^*.W.S)^-1

    # linear inversion of freqs
    ret = devectorize(np.dot(inv, np.dot(Sdg, v)))
    # renormalize to input trace value
    if trace is not None:
        ret = trace * ret / np.trace(ret)
    return ret
Ejemplo n.º 6
0
def __tomo_linear_inv(freqs, ops, weights=None, trace=None):
    """
    Reconstruct a matrix through linear inversion.

    Args:
        freqs (list[float]): list of observed frequences.
        ops (list[np.array]): list of corresponding projectors.
        weights (list[float] or array_like):
            weights to be used for weighted fitting.
        trace (float or None): trace of returned operator.

    Returns:
        numpy.array: A numpy array of the reconstructed operator.
    """
    # get weights matrix
    if weights is not None:
        W = np.array(weights)
        if W.ndim == 1:
            W = np.diag(W)

    # Get basis S matrix
    S = np.array([vectorize(m).conj()
                  for m in ops]).reshape(len(ops), ops[0].size)
    if weights is not None:
        S = np.dot(W, S)  # W.S

    # get frequencies vec
    v = np.array(freqs)  # |f>
    if weights is not None:
        v = np.dot(W, freqs)  # W.|f>
    Sdg = S.T.conj()  # S^*.W^*
    inv = np.linalg.pinv(np.dot(Sdg, S))  # (S^*.W^*.W.S)^-1

    # linear inversion of freqs
    ret = devectorize(np.dot(inv, np.dot(Sdg, v)))
    # renormalize to input trace value
    if trace is not None:
        ret = trace * ret / np.trace(ret)
    return ret