@@ -1330,12 +1330,21 @@ def PauliStringSum2COO(
1330
1330
if weight is None :
1331
1331
weight = [1.0 for _ in range (nterms )]
1332
1332
weight = num_to_tensor (weight )
1333
+ ls = num_to_tensor (ls )
1333
1334
# rsparse = get_backend("numpy").coo_sparse_matrix(
1334
1335
# indices=np.array([[0, 0]], dtype=np.int64),
1335
1336
# values=np.array([0.0], dtype=getattr(np, dtypestr)),
1336
1337
# shape=(s, s),
1337
1338
# )
1338
- rsparses = [backend .numpy (PauliString2COO (ls [i ], weight [i ])) for i in range (nterms )] # type: ignore
1339
+ global PauliString2COO_jit
1340
+ if backend .name not in PauliString2COO_jit :
1341
+ PauliString2COO_jit [backend .name ] = backend .jit (
1342
+ PauliString2COO , jit_compile = True
1343
+ )
1344
+ rsparses = [
1345
+ backend .numpy (PauliString2COO_jit [backend .name ](ls [i ], weight [i ])) # type: ignore
1346
+ for i in range (nterms )
1347
+ ]
1339
1348
rsparse = _dc_sum (rsparses )
1340
1349
# auto transformed into csr format!!
1341
1350
@@ -1372,6 +1381,7 @@ def PauliStringSum2COO_tf(
1372
1381
) -> Tensor :
1373
1382
"""
1374
1383
Generate tensorflow sparse matrix from Pauli string sum
1384
+ [deprecated]
1375
1385
1376
1386
:param ls: 2D Tensor, each row is for a Pauli string,
1377
1387
e.g. [1, 0, 0, 3, 2] is for :math:`X_0Z_3Y_4`
@@ -1416,26 +1426,37 @@ def PauliString2COO(l: Sequence[int], weight: Optional[float] = None) -> Tensor:
1416
1426
:rtype: Tensor
1417
1427
"""
1418
1428
n = len (l )
1429
+ l = backend .cast (l , dtype = "int64" )
1419
1430
one = num_to_tensor (0b1 , dtype = "int64" )
1420
1431
idx_x = num_to_tensor (0b0 , dtype = "int64" )
1421
1432
idx_y = num_to_tensor (0b0 , dtype = "int64" )
1422
1433
idx_z = num_to_tensor (0b0 , dtype = "int64" )
1423
1434
i = num_to_tensor (0 , dtype = "int64" )
1424
- for j in l :
1425
- # i, j from enumerate is python, non jittable when cond using tensor
1426
- if j == 1 : # xi
1427
- idx_x += backend .left_shift (one , n - i - 1 )
1428
- elif j == 2 : # yi
1429
- idx_y += backend .left_shift (one , n - i - 1 )
1430
- elif j == 3 : # zi
1431
- idx_z += backend .left_shift (one , n - i - 1 )
1435
+ # for j in l:
1436
+ for j in range (n ):
1437
+ oh = backend .onehot (l [j ], 4 )
1438
+ s = backend .left_shift (one , n - i - 1 )
1439
+ oh = backend .cast (oh , dtype = "int64" )
1440
+ idx_x += oh [1 ] * s
1441
+ idx_y += oh [2 ] * s
1442
+ idx_z += oh [3 ] * s
1443
+
1444
+ # if j == 1: # xi
1445
+ # idx_x += backend.left_shift(one, n - i - 1)
1446
+ # elif j == 2: # yi
1447
+ # idx_y += backend.left_shift(one, n - i - 1)
1448
+ # elif j == 3: # zi
1449
+ # idx_z += backend.left_shift(one, n - i - 1)
1432
1450
i += 1
1433
1451
1434
1452
if weight is None :
1435
1453
weight = num_to_tensor (1.0 , dtype = "complex64" )
1436
1454
return ps2coo_core (idx_x , idx_y , idx_z , weight , n )
1437
1455
1438
1456
1457
+ PauliString2COO_jit = {"numpy" : PauliString2COO }
1458
+
1459
+
1439
1460
def ps2coo_core (
1440
1461
idx_x : Tensor , idx_y : Tensor , idx_z : Tensor , weight : Tensor , nqubits : int
1441
1462
) -> Tuple [Tensor , Tensor ]:
0 commit comments