Skip to content

Commit 18692a7

Browse files
Merge branch 'beta' into cloud
2 parents cbe2232 + 49106eb commit 18692a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+538
-42
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,26 @@
66

77
- Add `tc.about()` to print related software versions and configs
88

9-
- Torch support is updraded to 2.0, and now support native vmap and native functional grad, and thus `vvag`. Still jit support is conflict with these functional transformations and be turned off by default
9+
- Torch support is upgraded to 2.0, and now support native vmap and native functional grad, and thus `vvag`. Still jit support is conflict with these functional transformations and be turned off by default
1010

1111
- Add `torch_interfaces_kws` that support static keyword arguments when wrapping with the interface
1212

13+
- Add `gpu_memory_share` function and enable it by default
14+
15+
- Add `scan` methods for backends
16+
17+
- Add example demontrating how jax compiling time can be accelerated by `jax.lax.scan`
18+
1319
### Fixed
1420

1521
- Add tests and fixed some missing methods for cupy backend, cupy backend is now ready to use (though still not guaranteed)
1622

23+
- Fix adjoint gate numpy conversion for fixed gate case
24+
25+
### Changed
26+
27+
- Upgraded black and mypy==1.2.0 (breaking change for developers)
28+
1729
## 0.8.0
1830

1931
### Added

docs/source/quickstart.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,10 @@ There is also a more flexible torch interface that support static non-tensor inp
696696
.. code-block:: python
697697
698698
def f(a, i):
699-
s = 0.
700-
for _ in range(i):
701-
s += a
702-
return s
699+
s = 0.
700+
for _ in range(i):
701+
s += a
702+
return s
703703
704704
f_torch = tc.interfaces.torch_interface_kws(f)
705705
f_torch(torch.ones([2]), i=3)

examples/adiabatic_vqnhe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Calculate the molecule dissociation curve using VQNHE.
33
"""
4+
45
from functools import partial
56
import sys
67

examples/bp_benchmark.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
benchmark on barren plateau using tfq and tc
33
"""
4+
45
import os
56

67
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"

examples/bp_validation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Compute the circuit gradient with single qubit random Haar averaged to
33
demonstrate the gradient vanishing, aka, barren plateau.
44
"""
5+
56
import numpy as np
67
from tqdm import tqdm
78
import tensorcircuit as tc

examples/chaotic_behavior.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Some chaotic properties calculations from the circuit state.
33
"""
4+
45
from functools import partial
56
import sys
67

examples/ghz_dqas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
DQAS for GHZ state preparation circuit, deprecated DQAS implementation
33
"""
4+
45
import sys
56

67
sys.path.insert(0, "../")

examples/hamiltonian_building.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
benchmark sparse hamiltonian building
33
"""
4+
45
import time
56
import numpy as np
67
import quimb

examples/hea_scan_jit_acc.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
reducing jit compiling time by general scan magic
3+
"""
4+
5+
import numpy as np
6+
import tensorcircuit as tc
7+
8+
n = 10
9+
nlayers = 16
10+
param_np = np.random.normal(size=[nlayers, n, 2])
11+
12+
for backend in ["tensorflow", "jax"]:
13+
with tc.runtime_backend(backend) as K:
14+
print("running %s" % K.name)
15+
16+
def energy_reference(param, n, nlayers):
17+
c = tc.Circuit(n)
18+
for i in range(n):
19+
c.h(i)
20+
for i in range(nlayers):
21+
for j in range(n - 1):
22+
c.rzz(j, j + 1, theta=param[i, j, 0])
23+
for j in range(n):
24+
c.rx(j, theta=param[i, j, 1])
25+
return K.real(c.expectation_ps(z=[0, 1]) + c.expectation_ps(x=[2]))
26+
27+
vg_reference = K.jit(
28+
K.value_and_grad(energy_reference, argnums=0), static_argnums=(1, 2)
29+
)
30+
31+
# a jit efficient way to utilize scan
32+
33+
def energy(param, n, nlayers, each):
34+
def loop_f(s_, param_):
35+
c_ = tc.Circuit(n, inputs=s_)
36+
for i in range(each):
37+
for j in range(n - 1):
38+
c_.rzz(j, j + 1, theta=param_[i, j, 0])
39+
for j in range(n):
40+
c_.rx(j, theta=param_[i, j, 1])
41+
s_ = c_.state()
42+
return s_
43+
44+
c = tc.Circuit(n)
45+
for i in range(n):
46+
c.h(i)
47+
s = c.state()
48+
s1 = K.scan(loop_f, K.reshape(param, [nlayers // each, each, n, 2]), s)
49+
c1 = tc.Circuit(n, inputs=s1)
50+
return K.real(c1.expectation_ps(z=[0, 1]) + c1.expectation_ps(x=[2]))
51+
52+
vg = K.jit(
53+
K.value_and_grad(energy, argnums=0),
54+
static_argnums=(1, 2, 3),
55+
jit_compile=True,
56+
)
57+
# set to False can improve compile time for tf
58+
59+
param = K.convert_to_tensor(param_np)
60+
61+
for each in [1, 2, 4]:
62+
print(" scan impl with each=%s" % str(each))
63+
r1 = tc.utils.benchmark(vg, param, n, nlayers, each)
64+
print(r1[0][0])
65+
66+
print(" plain impl")
67+
r0 = tc.utils.benchmark(vg_reference, param, n, nlayers) # too slow
68+
np.testing.assert_allclose(r0[0][0], r1[0][0], atol=1e-5)
69+
np.testing.assert_allclose(r0[0][1], r1[0][1], atol=1e-5)
70+
# correctness check
71+
72+
73+
# jit_compile=True icrease runtime while degrades jit time for tensorflow
74+
# and in general jax improves better with scan methodology,
75+
# both compile time and running time can outperform tf

examples/incremental_twoqubit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Optimizing the parameterized circuit with progressively dense two-qubit gates,
33
as a potential approach to alleviate barren plateau.
44
"""
5+
56
import sys
67

78
sys.path.insert(0, "../")

0 commit comments

Comments
 (0)