How to enforce a subspace constraint

How to add the additional constraint on solution (and the optimization/constraint variables) that they need to be contained in the span/linear combination of a group of vectors?

I’m especially interested in the case where i have a hierarchy of constraints that are connected.

You will have to be more specific. Perhaps what you want can be done with affine (linear) constraints?

As for connected hierarchy of connected constraints, I have no idea what you’re talking about. If they are not convex, your only hope is if you can use MIDCP capability.

More precise explanation:
I define my optimization variables as following:

variable  S(n,n,N+1) semidefinite;
    minimize(trace(S(:,:,N+1)));
    subject to
        -S(:,:,1) <=PT_rho;
        PT_rho<= S(:,:,1);
        
        for i = 2:N+1
            -S(:,:,i) <=PartialTranspose(S(:,:,i-1));
            PartialTranspose(S(:,:,i-1))<=S(:,:,i);
        end

I want to modify this code by adding a constraint, so that I force my S’s to be diagonalizable w.r.t. a certain choice of basis.

Meaning I can write` S=\sum_{i,j}^{d} x_{i,j} *(A_{j}\otimes B_{j}). Where A,B are some collection of matrices.

Is that possible with CVX, or do I need to use another package?

If the x_{i,.j} are variables, I think that is just an affine (linear) constraint.
So declare
variable x(n,n)
then add the constraint.

Does that do what you want?

But here variable x(n,n) is a matrix, so does it need to be defined properly. The idea is that i dont know them apriori, I only know what the different combinations of A_{i} and B_{j} and then the question becomes if there exists a sequence of constants such that i can rewrite them.
And have would one write such a sum within the cvx environment, does the subject to allow for nested for-loops?

I’m quite new to cvx, and am learning it myself, that is why I’m asking!

it’s your problem, not mine, so I don’;t really understand what you want. if you only need the existence of a matrix of x such that the constraint n S is satisfied, you should be able to do it. if you mean something else, i don’t know what that is.

CVX does allow the use of for loops, including nested for loops, provided that the indices in the for loops do not involve CVX variables or expressions.

You can build up CVX expressions using for loops. However, vectorization which eliminates or minimizes the number of levels of for loops allows the CVX modeling (processing) to be faster. There are many examples in this forum of vectorization and of one or more levels of nested for loops.

Hi, I think i modified the constraint such that it can be written as a linear combination of certain matrices(they are created in another function) My problem now is that the answer i now get is smaller then if i dont have the subspace constraint. I dont know if I have enforced the subspace constraint as i should do in CVX. (I’ve also tried using “==” ass well as some other variations of the code below )
Code with the subspace constraint:

function [opt, hierarchy, mu] = l_hierarchy_subpace_constraint(N, rho)
    PT_rho = PartialTranspose(rho);
    d=size(rho,1);
    % Generate P matrices
    P_array = generate_P_array();
    
    % Start CVX optimization
    cvx_begin  quiet
        % Define variables
        variable a(N+1,d); 
        expression S(d,d,N+1);
        
        minimize(a(N+1,1));

        subject to
        P_value=zeros(64,64);
              for n=1:64
                   P_value=P_value+((1/(2^3))*a(1,n)*P_array(:,:,n));
              end
             S(:,:,1)=P_value;
         % Enforce -S_1 <= PT_rho <= S_1
            -S(:,:,1) <= PT_rho;
            PT_rho <= S(:,:,1);
           
            for j=2:N+1
              P_value=zeros(64,64);
              for n=1:64
                   P_value=P_value+((1/(2^3))*a(j,n)*P_array(:,:,n));
              end
              S(:,:,j)=P_value;
              -S(:,:,j)<= PartialTranspose(S(:,:,j-1));
              PartialTranspose(S(:,:,j-1)) <= S(:,:,j);
             
           end          

    cvx_end

    % Output results
    opt = cvx_optval;
    hierarchy= S;
    mu = a;
end

First of all, don’t use quiet until you are sure your program is working properly. That way you can see all the solver and CVX output.

Are you saying that you have two programs, which are identical except for the addition of some (subspace constraints) in one of them; and that the program with the added constraints has a better (smaller) objective value than the other, and by more than just solver tolerance? If so, something must be amiss with your description or the execution of your program; and careful examination of solver and CVX output is called for. And if so, whether or not your constraints are “correct” (relative to what you want), something would be wrong. Caveat; if you are calling this in some iterative way such as SCA, then it would be possible that adding constraints could result in a better objective value, because one or both programs might not be finding the global minimum.

= is assignment for an expression.
== is an equality constraint.

There are no equality constraints in the code you posted. The only constraints are inequality constraints.

It is not apparent to me from your description where you tried using ==.

Your posts continue to be so vague as to make it difficult to help you.