求助大佬,无法进入并行计算
mathematica吧
全部回复
仅看楼主
level 2
大佬们,我生成了一个矩阵,再定义了对矩阵进行一些计算一个的Module,单独计算这个module大概五秒钟可以出结果,但是我使用Parallel一系列的函数程序都会卡死,无法进入计算,不知道是为什么。下面是我的代码
2025年05月09日 04点05分 1
level 2
Clear["Global`*"]
sx1 = KroneckerProduct [KroneckerProduct [KroneckerProduct[( {
{0, 1},
{1, 0}
} ), IdentityMatrix[2]], IdentityMatrix[2]], IdentityMatrix[2]];
sy1 = KroneckerProduct [KroneckerProduct[KroneckerProduct[( {
{0, -I},
{I, 0}
} ), IdentityMatrix[2]], IdentityMatrix[2]], IdentityMatrix[2]];
sz1 = KroneckerProduct [ KroneckerProduct[KroneckerProduct[( {
{1, 0},
{0, -1}
} ), IdentityMatrix[2]], IdentityMatrix[2]], IdentityMatrix[2]];
sup1 = KroneckerProduct [KroneckerProduct[KroneckerProduct [( {
{0, 1},
{0, 0}
} ), IdentityMatrix[2]], IdentityMatrix[2]], IdentityMatrix[2]];
sdown1 = KroneckerProduct [KroneckerProduct[KroneckerProduct[( {
{0, 0},
{1, 0}
} ), IdentityMatrix[2]], IdentityMatrix[2]], IdentityMatrix[2]];
sx2 = KroneckerProduct [
KroneckerProduct [KroneckerProduct[IdentityMatrix[2], ( {
{0, 1},
{1, 0}
} )], IdentityMatrix[2]], IdentityMatrix[2]];
sy2 = KroneckerProduct [
KroneckerProduct[KroneckerProduct[IdentityMatrix[2], ( {
{0, -I},
{I, 0}
} )], IdentityMatrix[2]], IdentityMatrix[2]];
sz2 = KroneckerProduct [
KroneckerProduct[KroneckerProduct[IdentityMatrix[2], ( {
{1, 0},
{0, -1}
} )], IdentityMatrix[2]], IdentityMatrix[2]];
sup2 = KroneckerProduct [
KroneckerProduct[KroneckerProduct [IdentityMatrix[2], ( {
{0, 1},
{0, 0}
} )], IdentityMatrix[2]], IdentityMatrix[2]];
sdown2 =
KroneckerProduct [
KroneckerProduct[KroneckerProduct[IdentityMatrix[2], ( {
{0, 0},
{1, 0}
} )], IdentityMatrix[2]], IdentityMatrix[2]];
sx3 = KroneckerProduct [
KroneckerProduct [
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]], ( {
{0, 1},
{1, 0}
} )], IdentityMatrix[2]];
sy3 = KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]], ( {
{0, -I},
{I, 0}
} )], IdentityMatrix[2]];
sz3 = KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]], ( {
{1, 0},
{0, -1}
} )], IdentityMatrix[2]];
sup3 = KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]], ( {
{0, 1},
{0, 0}
} )], IdentityMatrix[2]];
sdown3 =
KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]], ( {
{0, 0},
{1, 0}
} )], IdentityMatrix[2]];
sx4 = KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]],
IdentityMatrix[2]], ( {
{0, 1},
{1, 0}
} )];
sy4 = KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]],
IdentityMatrix[2]], ( {
{0, -I},
{I, 0}
} )];
sz4 = KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]],
IdentityMatrix[2]], ( {
{1, 0},
{0, -1}
} )];
sup4 = KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]],
IdentityMatrix[2]], ( {
{0, 1},
{0, 0}
} )];
sdown4 =
KroneckerProduct [
KroneckerProduct[
KroneckerProduct[IdentityMatrix[2], IdentityMatrix[2]],
IdentityMatrix[2]], ( {
{0, 0},
{1, 0}
} )];
H0 = -I *\[Gamma] /
2*( sdown1 . sup1 + sdown2 . sup2 + sdown3 . sup3 +
sdown4 . sup4) + \[CapitalOmega]*(sx1 + sx2 + sx3 +
sx4) - \[Xi]*(sx1 . sx2 . sx3 + sx1 . sx2 . sx4 +
sx2 . sx3 . sx4 + sx1 . sx3 . sx4) + \[Gamma]*I *
IdentityMatrix[16] + \[Delta] \!\(\*
TagBox[
RowBox[{"(", "", GridBox[{
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]},
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}}], "", ")"}],
Function[BoxForm`e$,
MatrixForm[BoxForm`e$]]]\);
2025年05月09日 04点05分 2
level 2
{RE, RV} = Eigensystem[H0];
(*定义自动归一化并计算GM Concurrence的函数*)
ComputeGMConcurrenceNormalized[psiRaw_List] :=
Module[{psi, dims, traceSystemsList, purities, cGM},(*第一步:自动归一化输入态*)
psi = Normalize[psiRaw];
(*Print["输入态已归一化为:",psi//N];*)
(*设置四个量子比特的维度,每个维度为2*)dims = {2, 2, 2, 2};
(*所有可能的二分划:1vs3和2vs2*)
traceSystemsList = {{2, 3, 4}, {1, 3, 4}, {1, 2, 4}, {1, 2,
3}, {1}, {2}, {3}, {4},(*1-
vs-3分割*){3, 4}, {2, 4}, {2, 3} , {1, 2}, {1, 3}, {1,
4} (*2-vs-2分割*)};
(*计算每个二分划的纯度*)
purities =
Table[Module[{traceSystems, keep, dKeep, dTrace, rho, rhoReshaped,
transposedRho, rhoReshaped2, rhoRed},(*当前二分划:trace掉的子系统*)
traceSystems = traceSystemsList[[j]];
(*保留的子系统*)keep = Complement[Range[4], traceSystems];
(*计算保留和trace部分的维度*)dKeep = Times @@ dims[[keep]];
dTrace = Times @@ dims[[traceSystems]];
(*构造密度矩阵|\[Psi]⟩⟨\[Psi]|*)
rho = KroneckerProduct[psi, Conjugate[psi]];
(*将密度矩阵重塑为8阶张量 (2\[Times]2\[Times]2\[Times]2)\[Times](2\[Times]2\
\[Times]2\[Times]2)*)rhoReshaped = ArrayReshape[rho, Join[dims, dims]];
(*重排张量指标:将保留的子系统放在前面*)
transposedRho =
Transpose[rhoReshaped,
Join[keep, traceSystems, keep + 4, traceSystems + 4]];
(*二次重塑:分组为保留和trace部分*)
rhoReshaped2 =
ArrayReshape[transposedRho, {dKeep, dTrace, dKeep, dTrace}];
(*缩并(部分迹):消去trace部分的指标*)
rhoRed = TensorContract[rhoReshaped2, {{2, 4}}];
(*计算纯度 Tr(\[Rho]Red^2)*)Tr[rhoRed . rhoRed]], {j, 1,
Length[traceSystemsList]}];
(*打印所有二分划的纯度*)(*Print["所有二分划的纯度:",purities//N];*)
(*计算GM Concurrence:取所有二分划的最小值*)
cGM = Min[Re[Sqrt[2 (1 - #)]] & /@ purities];
(*返回结果*)cGM]
ParallelTable[
ComputeGMConcurrenceNormalized[
N[RV[[13]] /. {\[Gamma] -> 1, \[Xi] ->
N[1/8 (-1 + 3/2^(2/3) + 2^(2/3))], \[CapitalOmega] ->
N[(5 (4 - 2^(2/3)))/(
16 (-2 + 3 2^(1/3) + 2 2^(2/3)))], \[Delta] -> +0.000000001*
j}]], {j, 0, 10, 1}];
2025年05月09日 04点05分 3
level 2
单独计算都是可以出结果的,正常来讲我本来要做10000次计算,但是我在并行里面用10次计算都无法得到结果
2025年05月09日 04点05分 4
吧务
level 15
好像是因为RV太大了。
ByteCount[RV // N]/1024^2. "MB"
(* 390.694 "MB" *)
并行的话,应该是每个核都会需要一份RV的拷贝。你有多少个核,多少内存?
总之我的笔记本上,改成
ParallelTable[
ComputeGMConcurrenceNormalized[
N[# /. {γ -> 1, ξ ->
N[1/8 (-1 + 3/2^(2/3) + 2^(2/3))], Ω ->
N[(5 (4 - 2^(2/3)))/(16 (-2 + 3 2^(1/3) +
2 2^(2/3)))], δ -> +0.000000001*j}]], {j, 0, 5,
1}] &[RV[[13]]] // AbsoluteTiming
也就是只分配RV[[13]]的话还是能算的。
2025年06月07日 00点06分 5
谢谢吧主!应该是内存的问题,电脑有12个核,16G内存。我直接用table倒是算得更快一些,已经能算出结果了,我再用你的代码试试[太开心]
2025年06月10日 08点06分
1