def test_devectorize(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(devectorize(col) - mat) == 0 and np.linalg.norm(devectorize(col, method='col') - mat) == 0 and np.linalg.norm(devectorize(row, method='row') - mat) == 0 and np.linalg.norm(devectorize(paul, method='pauli') - mat) == 0) self.assertTrue(test_pass)
def test_devectorize(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(devectorize(col) - mat) == 0 and \ np.linalg.norm(devectorize(col, method='col') - mat) == 0 and \ np.linalg.norm(devectorize(row, method='row') - mat) == 0 and \ np.linalg.norm(devectorize(paul, method='pauli') - mat) == 0 self.assertTrue(test_pass)
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
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