Exemplo n.º 1
0
    def set_dense(self, m):
        """
        Set the dense-matrix value of this operation.

        Attempts to modify operation parameters so that the specified raw
        operation matrix becomes mx.  Will raise ValueError if this operation
        is not possible.

        Parameters
        ----------
        m : array_like or LinearOperator
            An array of shape (dim, dim) or LinearOperator representing the operation action.

        Returns
        -------
        None
        """
        mx = _LinearOperator.convert_to_matrix(m)
        if (mx.shape != (self.dim, self.dim)):
            raise ValueError("Argument must be a (%d,%d) matrix!" %
                             (self.dim, self.dim))
        if not (_np.isclose(mx[0, 0], 1.0) and _np.allclose(mx[0, 1:], 0.0)):
            raise ValueError("Cannot set FullTPOp: "
                             "invalid form for 1st row!")
            #For further debugging:  + "\n".join([str(e) for e in mx[0,:]])
        self._ptr[1:, :] = mx[1:, :]
        self._ptr_has_changed()
        self.dirty = True
Exemplo n.º 2
0
    def set_dense(self, m):
        """
        Set the dense-matrix value of this operation.

        Attempts to modify operation parameters so that the specified raw
        operation matrix becomes mx.  Will raise ValueError if this operation
        is not possible.

        Parameters
        ----------
        m : array_like or LinearOperator
            An array of shape (dim, dim) or LinearOperator representing the operation action.

        Returns
        -------
        None
        """
        mx = _LinearOperator.convert_to_matrix(m)
        udim = self.state_space.udim  # maybe create a self.udim?
        if (mx.shape != (udim, udim)):
            raise ValueError("Argument must be a (%d,%d) matrix!" %
                             (udim, udim))
        self._ptr[:, :] = _np.array(mx)
        self._ptr_has_changed()
        self.dirty = True
Exemplo n.º 3
0
    def __init__(self, m, evotype="default", state_space=None):
        """
        Initialize a FullTPOp object.

        Parameters
        ----------
        m : array_like or LinearOperator
            a square 2D numpy array representing the operation action.  The
            shape of this array sets the dimension of the operation.
        """
        #LinearOperator.__init__(self, LinearOperator.convert_to_matrix(m))
        mx = _LinearOperator.convert_to_matrix(m)
        assert (_np.isrealobj(mx)), "FullTPOp must have *real* values!"
        if not (_np.isclose(mx[0, 0], 1.0) and _np.allclose(mx[0, 1:], 0.0)):
            raise ValueError("Cannot create FullTPOp: "
                             "invalid form for 1st row!")
        _DenseOperator.__init__(self, mx, evotype, state_space)
        assert (self._rep.base.flags['C_CONTIGUOUS']
                and self._rep.base.flags['OWNDATA'])
        assert (isinstance(self._ptr, _ProtectedArray))
        self._paramlbls = _np.array([
            "MxElement %d,%d" % (i, j) for i in range(1, self.dim)
            for j in range(self.dim)
        ],
                                    dtype=object)
Exemplo n.º 4
0
    def __init__(self, mx, basis, evotype, state_space):
        """ Initialize a new LinearOperator """
        mx = _LinearOperator.convert_to_matrix(mx)
        state_space = _statespace.default_space_for_udim(mx.shape[0]) if (state_space is None) \
            else _statespace.StateSpace.cast(state_space)
        basis = _Basis.cast(
            basis,
            state_space.dim)  # basis for Hilbert-Schmidt (superop) space
        evotype = _Evotype.cast(evotype)

        #Try to create a dense unitary rep.  If this fails, see if a dense superop rep
        # can be created, as this type of rep can also hold arbitrary unitary ops.
        try:
            rep = evotype.create_dense_unitary_rep(mx, basis, state_space)
            self._reptype = 'unitary'
            self._unitary = None
        except Exception:
            if mx.shape[0] == basis.dim and _np.linalg.norm(mx.imag) < 1e-10:
                # Special case when a *superop* was provided instead of a unitary mx
                superop_mx = mx.real  # used as a convenience case that really shouldn't be used
            else:
                superop_mx = _ot.unitary_to_superop(mx, basis)
            rep = evotype.create_dense_superop_rep(superop_mx, state_space)
            self._reptype = 'superop'
            self._unitary = mx
        self._basis = basis

        _LinearOperator.__init__(self, rep, evotype)
        DenseOperatorInterface.__init__(self)
Exemplo n.º 5
0
    def set_dense(self, m):
        """
        Set the dense-matrix value of this operation.

        Attempts to modify operation parameters so that the specified raw
        operation matrix becomes mx.  Will raise ValueError if this operation
        is not possible.

        Parameters
        ----------
        m : array_like or LinearOperator
            An array of shape (dim, dim) or LinearOperator representing the operation action.

        Returns
        -------
        None
        """
        mx = _LinearOperator.convert_to_matrix(m)
        errgen_cls = self.errorgen.__class__

        #Note: this only really works for LindbladErrorGen objects now... make more general in FUTURE?
        truncate = TODENSE_TRUNCATE  # can't just be 'True' since we need to throw errors when appropriate
        new_errgen = errgen_cls.from_operation_matrix_and_blocks(
            mx, self.errorgen.coefficient_blocks, 'auto',
            self.errorgen.matrix_basis, truncate, self.errorgen.evotype,
            self.errorgen.state_space)
        self.errorgen.from_vector(new_errgen.to_vector())
        self._update_rep()  # needed to rebuild exponentiated error gen
        self.dirty = True
Exemplo n.º 6
0
    def __init__(self, base_matrix, parameter_array, parameter_to_base_indices_map,
                 left_transform=None, right_transform=None, real=False, evotype="default", state_space=None):

        base_matrix = _np.array(_LinearOperator.convert_to_matrix(base_matrix), 'complex')
        #complex, even if passed all real base matrix

        elementExpressions = {}
        for p, ij_tuples in parameter_to_base_indices_map.items():
            for i, j in ij_tuples:
                assert((i, j) not in elementExpressions)  # only one parameter allowed per base index pair
                elementExpressions[(i, j)] = [LinearlyParameterizedElementTerm(1.0, [p])]

        typ = "d" if real else "complex"
        mx = _np.empty(base_matrix.shape, typ)
        self.baseMatrix = base_matrix
        self.parameterArray = parameter_array
        self.numParams = len(parameter_array)
        self.elementExpressions = elementExpressions
        assert(_np.isrealobj(self.parameterArray)), "Parameter array must be real-valued!"

        I = _np.identity(self.baseMatrix.shape[0], 'd')  # LinearlyParameterizedGates are currently assumed to be real
        self.leftTrans = left_transform if (left_transform is not None) else I
        self.rightTrans = right_transform if (right_transform is not None) else I
        self.enforceReal = real

        #Note: dense op reps *always* own their own data so setting writeable flag is OK
        _DenseOperator.__init__(self, mx, evotype, state_space)
        self._ptr.flags.writeable = False  # only _construct_matrix can change array
        self._construct_matrix()  # construct base from the parameters
Exemplo n.º 7
0
 def __init__(self, mx, evotype, state_space=None):
     """ Initialize a new LinearOperator """
     mx = _LinearOperator.convert_to_matrix(mx)
     state_space = _statespace.default_space_for_dim(mx.shape[0]) if (state_space is None) \
         else _statespace.StateSpace.cast(state_space)
     evotype = _Evotype.cast(evotype)
     rep = evotype.create_dense_superop_rep(mx, state_space)
     _LinearOperator.__init__(self, rep, evotype)
     DenseOperatorInterface.__init__(self)