Preformatted text
Hi Mark, first of all i want to say thank you for helping me. I will try to be more clear/precise. I have the following cvx, code that works wonderfully:
function[opt,hierarchy]=l_hierarchy(N,rho)
n = size(rho,1);
PT_rho=PartialTranspose(rho);
cvx_begin sdp
variable S(n,n,N+1) semidefinite;% This is a 3D slice where S(:,:,i+1) is S_i
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
cvx_end
opt=cvx_optval;
hierarchy=S;
end
My goal is to add another constraint to my optimization problem. This being a subspace constraint. I am able to to this in smaller dimentions, and where i don’t save alle the constraints-matrices (S-matrices) from my original problem. I use this code here:
function [opt,S_N,S_n] = l_subspace_d2(N,rho)
I=eye(4,4);
sx=[0,1;1,0];
sy=[0,-1i;1i,0];
sz=[1,0;0,-1];
PT_rho=PartialTranspose(rho);
cvx_begin
% Define variables
variable a(N+1,4);
minimize(trace(a(N+1,1)*I+a(N+1,2)*kron(sx,sx.')+a(N+1,3)*kron(sy,sy.')+a(N+1,3)*kron(sz,sz.')));
subject to
% Enforce -S_1 <= PT_rho <= S_1
-(a(1,1)*I+a(1,2)*kron(sx,sx.')+a(1,3)*kron(sy,sy.')+a(1,4)*kron(sz,sz.')) <= PT_rho;
PT_rho <= a(1,1)*I+a(1,2)*kron(sx,sx.')+a(1,3)*kron(sy,sy.')+a(1,4)*kron(sz,sz.');
for i=2:N+1
-(a(i,1)*I+a(i,2)*kron(sx,sx.')+a(i,3)*kron(sy,sy.')+a(i,4)*kron(sz,sz.'))<=PartialTranspose(a(i-1,1)*I+a(i-1,2)*kron(sx,sx.')+a(i-1,3)*kron(sy,sy.')+a(i-1,4)*kron(sz,sz.'));
PartialTranspose(a(i-1,1)*I+a(i-1,2)*kron(sx,sx.')+a(i-1,3)*kron(sy,sy.')+a(i-1,4)*kron(sz,sz.'))<=a(i,1)*I+a(i,2)*kron(sx,sx.')+a(i,3)*kron(sy,sy.')+a(i,4)*kron(sz,sz.');
end
cvx_end
opt = cvx_optval;
S_N = a(N+1,1)*I+a(N+1,2)*kron(sx,sx.')+a(N+1,3)*kron(sy,sy.')+a(N+1,4)*kron(sz,sz.');
S_n = a(N,1)*I+a(N,2)*kron(sx,sx.')+a(N,3)*kron(sy,sy.')+a(N,4)*kron(sz,sz.');
end
My problem comes when I want to generalize to higher dimentional matrices, which i create with the following function:
function P_array = generate_P_array()
I = [1, 0; 0, 1];
sx = [0, 1; 1, 0];
sy = [0, -1i; 1i, 0];
sz = [1, 0; 0, -1];
pauli_set = {I, sx, sy, sz};
P_array = zeros(64,64, 64);
i = 1;
for n1 = 1:4
for n2 = 1:4
for n3 = 1:4
P = kron(kron(pauli_set{n1}, pauli_set{n2}), pauli_set{n3});
P_t = kron(kron(pauli_set{n1}.', pauli_set{n2}.'), pauli_set{n3}.');
P_array(:,:,i) = kron(P, P_t); % Tensor with transposed version
i = i + 1;
end
end
end
end
My attempt at a code that tries to generalize the optimisation problem:
function [opt, hierarchy, mu] = l_subpace_gen(N, rho)
PT_rho = PartialTranspose(rho);
% Generate the Pauli basis expansion
P_array = generate_P_array(); % P_array is d × d × d (64×64×64)
% Start CVX optimization
cvx_begin sdp
% Define optimization variables
variable a(N+1, 64); % Coefficients for the Pauli basis
expression S(64, 64, N+1); % Store the S matrices
% Construct S matrices in the Pauli basis
for j = 1:N+1
%X=reshape(a(j, :), [1, 1, 64]);
%S(:,:,j) = sum(P_array .* X, 3);
S(:,:,j) = sum(P_array .* reshape(a(j, :), [1, 1, 64]), 3);
end
% Objective function
minimize(trace(S(:,:,N+1)));
% Constraints enforcing subspace structure
S(:,:,1) >= -PT_rho;
S(:,:,1) <= PT_rho;
for j = 2:N+1
S(:,:,j) >= -PartialTranspose(S(:,:,j-1));
S(:,:,j) <= PartialTranspose(S(:,:,j-1));
end
cvx_end
% Output results
opt = cvx_optval;
hierarchy = S;
mu = a;
end
The main error i encounter is this:
Error using .* (line 46)
Matrix dimensions must agree.
Error in l_subpace_gen (line 18)
S(:,:,j) = sum(P_array .* X, 3);
Even though when i test the following:
P=generate_P_array();
a=rand(4,64);
S=zeros(64,64,4);
for j = 1:4
%X=reshape(a(j, :), [1, 1, 64]);
%S(:,:,j) = sum(P_array .* X, 3);
S(:,:,j) = sum(P .* reshape(a(j, :), [1, 1, 64]), 3);
end
I dont get any errors. Thus it may seem that this is a problem with cvx, does declaring a and S as variable and expression make MATLAB view them differently? I know that they are not seen as double/int since I cant use “pagemtimes” function, this would also not be possible for MATLAB versions older then 2020(but I have 2024b).
Hope this clarifies everything, and that you may be able to help my predicament.