Skip to content

Commit 856c650

Browse files
jit the paulistrsum
1 parent 313ddd3 commit 856c650

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

examples/hamiltonian_building.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
import quimb
1010
import scipy
1111
import tensorflow as tf
12-
import torch
1312

1413
import tensorcircuit as tc
1514

1615
tc.set_dtype("complex128")
1716

1817
nwires = 20
19-
# tc with numpy backend outperforms quimb in large nwires
2018

2119
print("--------------------")
2220

@@ -51,6 +49,14 @@
5149
time1 = time.perf_counter()
5250

5351
print("tc (jax) time: ", time1 - time0)
52+
53+
time0 = time.perf_counter()
54+
h12 = tc.quantum.heisenberg_hamiltonian(
55+
g, hzz=1, hxx=0, hyy=0, hz=0, hx=-1, hy=0, sparse=True
56+
)
57+
time1 = time.perf_counter()
58+
59+
print("tc (jax) time (after jit): ", time1 - time0)
5460
print("--------------------")
5561

5662
# 1.3 tc approach for TFIM (tensorflow backend)
@@ -67,6 +73,15 @@
6773
time1 = time.perf_counter()
6874

6975
print("tc (tensorflow) time: ", time1 - time0)
76+
77+
time0 = time.perf_counter()
78+
h13 = tc.quantum.heisenberg_hamiltonian(
79+
g, hzz=1, hxx=0, hyy=0, hz=0, hx=-1, hy=0, sparse=True
80+
)
81+
time1 = time.perf_counter()
82+
83+
print("tc (tensorflow) time (after jit): ", time1 - time0)
84+
7085
print("--------------------")
7186

7287

requirements/requirements-extra.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ qiskit-aer<1.0
44
qiskit-nature
55
mitiq
66
cirq
7-
torch
7+
torch==2.2.2
88
# jupyter
99
mthree==1.1.0
1010
openfermion

tensorcircuit/quantum.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,12 +1330,21 @@ def PauliStringSum2COO(
13301330
if weight is None:
13311331
weight = [1.0 for _ in range(nterms)]
13321332
weight = num_to_tensor(weight)
1333+
ls = num_to_tensor(ls)
13331334
# rsparse = get_backend("numpy").coo_sparse_matrix(
13341335
# indices=np.array([[0, 0]], dtype=np.int64),
13351336
# values=np.array([0.0], dtype=getattr(np, dtypestr)),
13361337
# shape=(s, s),
13371338
# )
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+
]
13391348
rsparse = _dc_sum(rsparses)
13401349
# auto transformed into csr format!!
13411350

@@ -1372,6 +1381,7 @@ def PauliStringSum2COO_tf(
13721381
) -> Tensor:
13731382
"""
13741383
Generate tensorflow sparse matrix from Pauli string sum
1384+
[deprecated]
13751385
13761386
:param ls: 2D Tensor, each row is for a Pauli string,
13771387
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:
14161426
:rtype: Tensor
14171427
"""
14181428
n = len(l)
1429+
l = backend.cast(l, dtype="int64")
14191430
one = num_to_tensor(0b1, dtype="int64")
14201431
idx_x = num_to_tensor(0b0, dtype="int64")
14211432
idx_y = num_to_tensor(0b0, dtype="int64")
14221433
idx_z = num_to_tensor(0b0, dtype="int64")
14231434
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)
14321450
i += 1
14331451

14341452
if weight is None:
14351453
weight = num_to_tensor(1.0, dtype="complex64")
14361454
return ps2coo_core(idx_x, idx_y, idx_z, weight, n)
14371455

14381456

1457+
PauliString2COO_jit = {"numpy": PauliString2COO}
1458+
1459+
14391460
def ps2coo_core(
14401461
idx_x: Tensor, idx_y: Tensor, idx_z: Tensor, weight: Tensor, nqubits: int
14411462
) -> Tuple[Tensor, Tensor]:

0 commit comments

Comments
 (0)