Python 在CVXPY中设置变量矩阵的部分转置约束

Python 在CVXPY中设置变量矩阵的部分转置约束,python,matrix,solver,sdp,cvxpy,Python,Matrix,Solver,Sdp,Cvxpy,我正在用CVXPY计算Python中的SDP问题,我想设置一个约束条件,即不仅我的变量矩阵是半正定的(psd),而且它在某个轴上的部分转置也是psd。我不知道如何设定这个要求。我的代码如下所示 #Set the variable matrix P_0 = cp.Variable((d,d), symmetric=True) 现在我想定义一下 def PT(d1, d2, rho): """Return rho^{T_1}, the partial trace of the densit

我正在用CVXPY计算Python中的SDP问题,我想设置一个约束条件,即不仅我的变量矩阵是半正定的(psd),而且它在某个轴上的部分转置也是psd。我不知道如何设定这个要求。我的代码如下所示

#Set the variable matrix
P_0 = cp.Variable((d,d), symmetric=True)
现在我想定义一下

def PT(d1, d2, rho):
    """Return rho^{T_1}, the partial trace of the density operator rho on C^{d_1} \ot C^{d_2} along the first system."""
    assert rho.shape == (d1 * d2, d1 * d2)

    # reshape into a 4-tensor (2 ket indices and 2 bra indices)
    rho = rho.reshape(d1, d2, d1, d2)

    # transpose the first subsystem
    rho = rho.transpose((0,3,2,1))

    # reshape back into a density operator
    return rho.reshape(d1 * d2, d1 * d2)
然后设置
PT(3,3,p_0)>>0
的要求,即它是psd。但这在cvxpy中是不允许的。我还可以为我的具体案例定义一个新的矩阵,如

P_0_tp = [[P_0[[0,0]], P_0[[1, 0]], P_0[[2, 0]], P_0[[0, 3]], P_0[[1, 3]], P_0[[2, 3]], 
  P_0[[0, 6]], P_0[[1, 6]], P_0[[2, 6]]], [P_0[[0, 1]], P_0[[1, 1]], P_0[[2, 1]], 
  P_0[[0, 4]], P_0[[1, 4]], P_0[[2, 4]], P_0[[0, 7]], P_0[[1, 7]], 
  P_0[[2, 7]]], [P_0[[0, 2]], P_0[[1, 2]], P_0[[2, 2]], P_0[[0, 5]], P_0[[1, 5]], 
  P_0[[2, 5]], P_0[[0, 8]], P_0[[1, 8]], P_0[[2, 8]]], [P_0[[3, 0]], P_0[[4, 0]], 
  P_0[[5, 0]], P_0[[3, 3]], P_0[[4, 3]], P_0[[5, 3]], P_0[[3, 6]], P_0[[4, 6]], 
  P_0[[5, 6]]], [P_0[[3, 1]], P_0[[4, 1]], P_0[[5, 1]], P_0[[3, 4]], P_0[[4, 4]], 
  P_0[[5, 4]], P_0[[3, 7]], P_0[[4, 7]], P_0[[5, 7]]], [P_0[[3, 2]], P_0[[4, 2]], 
  P_0[[5, 2]], P_0[[3, 5]], P_0[[4, 5]], P_0[[5, 5]], P_0[[3, 8]], P_0[[4, 8]], 
  P_0[[5, 8]]], [P_0[[6, 0]], P_0[[7, 0]], P_0[[8, 0]], P_0[[6, 3]], P_0[[7, 3]], 
  P_0[[8, 3]], P_0[[6, 6]], P_0[[7, 6]], P_0[[8, 6]]], [P_0[[6, 1]], P_0[[7, 1]], 
  P_0[[8, 1]], P_0[[6, 4]], P_0[[7, 4]], P_0[[8, 4]], P_0[[6, 7]], P_0[[7, 7]], 
  P_0[[8, 7]]], [P_0[[6, 2]], P_0[[7, 2]], P_0[[8, 2]], P_0[[6, 5]], P_0[[7, 5]], 
  P_0[[8, 5]], P_0[[6, 8]], P_0[[7, 8]], P_0[[8, 8]]]]
这是一个9x9矩阵,现在在第二维度和第三维度部分转置。但是如何将其设置为cvxpy中的变量


提前感谢,

我今天遇到了与您相同的问题:我想创建一个新的矩阵,该矩阵由我先前定义的变量组合组成。我在书中找到了我问题的答案。第二种方法,即数组
P\u 0\u tp
,的问题在于它不是使用cvxpy操作生成的。您可以使用例如
cvxpy.vstack
cvxpy.hstack
或您可以找到的其他可用函数(似乎也有一个
重塑
函数…)来构造它

在我提出的问题中,你可以看到我最终是如何实现它的。为了完整起见,我将其复制到这里:

import cvxpy as cp

X = cp.Variable((3,3), PSD=True)

row_1 = cp.hstack((0, 1, X[0,0]))
row_2 = cp.hstack((1, 0, X[1,2]))
row_3 = cp.hstack((X[0,0], X[1,2], 0))

W = cp.vstack((row_1, row_2, row_3))
constraint = [W >> 0]
如您所见,我没有将W定义为变量,但现在它是一个cvxpy对象:

In [1] W
Out[1]: Expression(AFFINE, UNKNOWN, (3, 3))

很好,看来这正是我想做的。
重塑
功能确实存在,但它不允许维度大于2的对象(即张量)。所以你不能将其用于部分跟踪。如果你认为它有效,你能接受我的回答吗?:)