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

问题描述 投票:1回答:1

我正在使用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中将其设置为变量?

谢谢,

python matrix solver sdp cvxpy
1个回答
0
投票

我今天遇到的问题与您相同:我想创建一个新矩阵,该矩阵由我先前定义的变量组合组成。我在this answer中找到了我的问题的答案。第二种方法数组P_0_tp的问题在于,它不是使用cvxpy操作创建的。您可以使用cvxpy.vstackcvxpy.hstack或其他可以找到here的函数来构造它(似乎也有reshape函数...)。

在姿势here中的问题中,您可以看到我最终如何实现它。为了完整起见,我将其复制到此处:

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))
© www.soinside.com 2019 - 2024. All rights reserved.