%% function [cvx_optval,X,W] = RobCPTNI2(J,dim)
% computes the CPTNI robustness R' of the Hermiticity-preserving map J
% with input/output dimensions optionally provided in dim=[dimA,dimB]

function [cvx_optval,X,W] = RobCPTNI2(J,dim)
    
J = (J+J')/2; % make sure that J is Hermitian, avoid numerical issues

if nargin<2
    d = max(size(J));
    dim = [1,1]*sqrt(d);
else
    d = dim(1)*dim(2);
end
 
cvx_begin sdp quiet

   variable X(d,d) hermitian semidefinite
   variable lam nonnegative
   dual variable W
   
   PartialTrace(X,2,dim) <= (1+lam)*eye(dim(1));
   W : X >= J
   
   minimize lam
   
cvx_end
   
end