Skip to content

Commit 4c01a56

Browse files
add bp validation example
1 parent c506d9d commit 4c01a56

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ include HISTORY.md
44
include LICENSE
55
include CHANGELOG.md
66
include docs/source/*
7-
include examples/*
87
include tests/*

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ c.H(0)
4646
c.CNOT(0,1)
4747
c.rx(1, theta=0.2)
4848
print(c.wavefunction())
49-
print(c.expectation((tc.gates.z(), [1])))
50-
print(c.perfect_sampling())
49+
print(c.expectation_ps(z=[0, 1]))
50+
print(c.sample())
5151
```
5252

5353
Runtime behavior customization:
@@ -62,27 +62,31 @@ Automatic differentiations with jit:
6262

6363
```python
6464
def forward(theta):
65-
c = tc.Circuit(n=2)
65+
c = tc.Circuit(2)
6666
c.R(0, theta=theta, alpha=0.5, phi=0.8)
6767
return tc.backend.real(c.expectation((tc.gates.z(), [0])))
6868

6969
g = tc.backend.grad(forward)
7070
g = tc.backend.jit(g)
71-
theta = tc.gates.num_to_tensor(1.0)
71+
theta = tc.array_to_tensor(1.0)
7272
print(g(theta))
7373
```
7474

7575
## Install
7676

77-
`pip install tensorcircuit`.
77+
The package is purely written in python and can be obtained via pip as:
7878

79-
Extra package installation may be required for some features.
79+
```python
80+
pip install tensorcircuit
81+
```
82+
83+
We also have [Docker support](/docker).
8084

8185
## Contributing
8286

8387
For contribution guidelines and notes, see [CONTRIBUTING](/CONTRIBUTING.md).
8488

85-
For developers, we suggest first configuring a good conda environment. The versions of dependence packages may vary in terms of development requirements. The minimum requirement is the [TensorNetwork](https://github.com/google/TensorNetwork) package. [Dockerfile](/docker) is also provided.
89+
We welcome issues, PRs and discussions from everyone, and these are all hosted on GitHub.
8690

8791
## Researches and applications
8892

examples/bp_validation.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import numpy as np
2+
from tqdm import tqdm
3+
import tensorcircuit as tc
4+
5+
xx = tc.gates._xx_matrix
6+
yy = tc.gates._yy_matrix
7+
zz = tc.gates._zz_matrix
8+
9+
nqubits, nlayers = 8, 6
10+
# number of qubits and number of even-odd brick layers in the circuit
11+
K = tc.set_backend("tensorflow")
12+
13+
14+
def ansatz(param, gate_param):
15+
# gate_param here is a 2D-vector for theta and phi in the XXZ gate
16+
c = tc.Circuit(nqubits)
17+
# customize the circuit structure as you like
18+
for j in range(nlayers):
19+
for i in range(nqubits):
20+
c.ry(i, theta=param[j, 0, i, 0])
21+
c.rz(i, theta=param[j, 0, i, 1])
22+
c.ry(i, theta=param[j, 0, i, 2])
23+
for i in range(0, nqubits - 1, 2): # even brick
24+
c.exp(
25+
i,
26+
i + 1,
27+
theta=1.0,
28+
unitary=gate_param[0] * (xx + yy) + gate_param[1] * zz,
29+
)
30+
for i in range(nqubits):
31+
c.ry(i, theta=param[j, 1, i, 0])
32+
c.rz(i, theta=param[j, 1, i, 1])
33+
c.ry(i, theta=param[j, 1, i, 2])
34+
for i in range(1, nqubits - 1, 2): # odd brick with OBC
35+
c.exp(
36+
i,
37+
i + 1,
38+
theta=1.0,
39+
unitary=gate_param[0] * (xx + yy) + gate_param[1] * zz,
40+
)
41+
return c
42+
43+
44+
def measure_z(param, gate_param, i):
45+
c = ansatz(param, gate_param)
46+
# K.real(c.expectation_ps(x=[i, i+1])) for measure on <X_iX_i+1>
47+
return K.real(c.expectation_ps(z=[i]))
48+
49+
50+
gradf = K.jit(K.vvag(measure_z, argnums=0, vectorized_argnums=0), static_argnums=2)
51+
# vectorized parallel the computation on circuit gradients for different circuit parameters
52+
53+
if __name__ == "__main__":
54+
batch = 100
55+
# tune batch as large as possible if the memory allows
56+
reps = 20
57+
measure_on = nqubits // 2
58+
# which qubit the sigma_z observable is on
59+
60+
# we will average `reps*batch` different unitaries
61+
rlist = []
62+
gate_param = np.array([0, np.pi / 4])
63+
gate_param = tc.array_to_tensor(gate_param)
64+
for _ in tqdm(range(reps)):
65+
param = np.random.uniform(0, 2 * np.pi, size=[batch, nlayers, 2, nqubits, 3])
66+
param = tc.array_to_tensor(param, dtype="float32")
67+
_, gs = gradf(param, gate_param, measure_on)
68+
# gs.shape = [batch, nlayers, 2, nqubits, 3]
69+
gs = K.abs(gs) ** 2
70+
rlist.append(gs)
71+
gs2 = K.stack(rlist)
72+
# gs2.shape = [reps, batch, nlayers, 2, nqubits, 3]
73+
gs2 = K.reshape(gs2, [-1, nlayers, 2, nqubits, 3])
74+
gs2_mean = K.numpy(
75+
K.mean(gs2, axis=0)
76+
) # numpy array for the averaged abs square for gradient of each element
77+
print(gs2_mean)

0 commit comments

Comments
 (0)