@@ -1104,7 +1104,7 @@ def quimb2qop(qb_mpo: Any) -> QuOperator:
1104
1104
1105
1105
try :
1106
1106
compiled_jit = partial (get_backend ("tensorflow" ).jit , jit_compile = True )
1107
- # TODO(@refraction-ray): at least make the final returned sparse tensor backend agnostic?
1107
+
1108
1108
def heisenberg_hamiltonian (
1109
1109
g : Graph ,
1110
1110
hzz : float = 1.0 ,
@@ -1196,7 +1196,7 @@ def heisenberg_hamiltonian(
1196
1196
r = PauliStringSum2COO_numpy (ls , weight )
1197
1197
if numpy :
1198
1198
return r
1199
- return _numpy2tf_sparse (r )
1199
+ return backend . coo_sparse_matrix_from_numpy (r )
1200
1200
return PauliStringSum2Dense (ls , weight , numpy = numpy )
1201
1201
1202
1202
def PauliStringSum2Dense (
@@ -1205,50 +1205,60 @@ def PauliStringSum2Dense(
1205
1205
numpy : bool = False ,
1206
1206
) -> Tensor :
1207
1207
"""
1208
- Generate tensorflow dense matrix from Pauli string sum
1208
+ Generate dense matrix from Pauli string sum
1209
1209
1210
1210
:param ls: 2D Tensor, each row is for a Pauli string,
1211
1211
e.g. [1, 0, 0, 3, 2] is for :math:`X_0Z_3Y_4`
1212
1212
:type ls: Sequence[Sequence[int]]
1213
1213
:param weight: 1D Tensor, each element corresponds the weight for each Pauli string
1214
1214
defaults to None (all Pauli strings weight 1.0)
1215
1215
:type weight: Optional[Sequence[float]], optional
1216
+ :param numpy: default False. If True, return numpy coo
1217
+ else return backend compatible sparse tensor
1218
+ :type numpy: bool
1216
1219
:return: the tensorflow dense matrix
1217
1220
:rtype: Tensor
1218
1221
"""
1219
1222
sparsem = PauliStringSum2COO_numpy (ls , weight )
1220
1223
if numpy :
1221
1224
return sparsem .todense ()
1222
- sparsem = _numpy2tf_sparse (sparsem )
1223
- densem = get_backend ( "tensorflow" ) .to_dense (sparsem )
1225
+ sparsem = backend . coo_sparse_matrix_from_numpy (sparsem )
1226
+ densem = backend .to_dense (sparsem )
1224
1227
return densem
1225
1228
1226
- def _tf2numpy_sparse (a : Tensor ) -> Tensor :
1227
- return get_backend ("numpy" ).coo_sparse_matrix (
1228
- indices = a .indices ,
1229
- values = a .values ,
1230
- shape = a .get_shape (),
1231
- )
1232
-
1233
- def _numpy2tf_sparse (a : Tensor ) -> Tensor :
1234
- return get_backend ("tensorflow" ).coo_sparse_matrix (
1235
- indices = np .array ([a .row , a .col ]).T ,
1236
- values = a .data ,
1237
- shape = a .shape ,
1238
- )
1229
+ # already implemented as backend method
1230
+ #
1231
+ # def _tf2numpy_sparse(a: Tensor) -> Tensor:
1232
+ # return get_backend("numpy").coo_sparse_matrix(
1233
+ # indices=a.indices,
1234
+ # values=a.values,
1235
+ # shape=a.get_shape(),
1236
+ # )
1237
+
1238
+ # def _numpy2tf_sparse(a: Tensor) -> Tensor:
1239
+ # return get_backend("tensorflow").coo_sparse_matrix(
1240
+ # indices=np.array([a.row, a.col]).T,
1241
+ # values=a.data,
1242
+ # shape=a.shape,
1243
+ # )
1239
1244
1240
- def PauliStringSum2COO_numpy (
1241
- ls : Sequence [Sequence [int ]], weight : Optional [Sequence [float ]] = None
1245
+ def PauliStringSum2COO (
1246
+ ls : Sequence [Sequence [int ]],
1247
+ weight : Optional [Sequence [float ]] = None ,
1248
+ numpy : bool = False ,
1242
1249
) -> Tensor :
1243
1250
"""
1244
- Generate scipy sparse matrix from Pauli string sum
1251
+ Generate sparse tensor from Pauli string sum
1245
1252
1246
1253
:param ls: 2D Tensor, each row is for a Pauli string,
1247
1254
e.g. [1, 0, 0, 3, 2] is for :math:`X_0Z_3Y_4`
1248
1255
:type ls: Sequence[Sequence[int]]
1249
1256
:param weight: 1D Tensor, each element corresponds the weight for each Pauli string
1250
1257
defaults to None (all Pauli strings weight 1.0)
1251
1258
:type weight: Optional[Sequence[float]], optional
1259
+ :param numpy: default False. If True, return numpy coo
1260
+ else return backend compatible sparse tensor
1261
+ :type numpy: bool
1252
1262
:return: the scipy coo sparse matrix
1253
1263
:rtype: Tensor
1254
1264
"""
@@ -1267,11 +1277,16 @@ def PauliStringSum2COO_numpy(
1267
1277
shape = (s , s ),
1268
1278
)
1269
1279
for i in range (nterms ):
1270
- rsparse += _tf2numpy_sparse (PauliString2COO (ls [i ], weight [i ])) # type: ignore
1280
+ rsparse += get_backend ( "tensorflow" ). numpy (PauliString2COO (ls [i ], weight [i ])) # type: ignore
1271
1281
# auto transformed into csr format!!
1272
- return rsparse .tocoo ()
1282
+ rsparse = rsparse .tocoo ()
1283
+ if numpy :
1284
+ return rsparse
1285
+ return backend .coo_sparse_matrix_from_numpy (rsparse )
1273
1286
1274
- def PauliStringSum2COO (
1287
+ PauliStringSum2COO_numpy = partial (PauliStringSum2COO , numpy = True )
1288
+
1289
+ def PauliStringSum2COO_tf (
1275
1290
ls : Sequence [Sequence [int ]], weight : Optional [Sequence [float ]] = None
1276
1291
) -> Tensor :
1277
1292
"""
0 commit comments