Skip to content

Commit 7207767

Browse files
committed
add hea_symm.py example
1 parent 81c77fe commit 7207767

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ jobs:
2727
pytest tencirchem tests --durations=0 --cov=tencirchem
2828
- name: Upload coverage to Codecov
2929
uses: codecov/codecov-action@v3
30+
- name: run examples
31+
run: |
32+
cd example; bash run.sh
33+
cd ..
3034
- name: test build doc
3135
run: |
3236
sudo apt install pandoc

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TenCirChem is based on [TensorCircuit](https://github.com/tencent-quantum-lab/te
1515
and is optimized for chemistry applications.
1616

1717
## Easy Installation
18-
Getting started with TenCirChem is a breeze. Simply install the package via pip:
18+
Getting started with TenCirChem by installing the package via pip:
1919

2020
```sh
2121
pip install tencirchem

example/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Examples
2+
3+
- `custom_excitation.py`: Specify custom excitation operators for unitary coupled-cluster (UCC) ansatz.
4+
- `hea_beh2.py`: VQE of BeH2 based on hardware-efficient ansatz (Ry ansatz), with (4e, 4o) active space.
5+
- `hea_lih.py`: VQE of LiH based on hardware-efficient ansatz (Ry ansatz), with (2e, 3o) active space.
6+
- `hea_symm.py`: VQE of H4 based on symmetry-preserving hardware-efficient ansatz.
7+
- `mr_uccsd.py`: Multi-reference UCCSD calculation of the bond dissociation limit of the water molecule.
8+
- `noisy_circuit.py`: Exemplary noisy circuit simulation based on hardware-efficient ansatz.
9+
- `print_circuit.py`: Draw UCCSD circuit for H2.
10+
- `pyrazine_spectra.py`: Absorption spectrum of the pyrazine molecule by variational quantum dynamics (VQD).
11+
- `pyscf_compatibility.py`: TenCirChem CI vector/reduced density matrices follow the convention of PySCF.
12+
- `sbm_dynamics.py`: Spin relaxation of the spin-boson model by variational quantum dynamics.
13+
- `simple_kupccgsd.py`: Simple k-UpCCGSD calculation with multiple initial guess.
14+
- `simple_uccsd.py`: Simple UCCSD calculation.
15+
- `switch_backend.py`: Switch backend at runtime.
16+
- `ucc_classes.py`: UCC subclasses share the same interface.
17+
- `water_pes.py`: Water potential energy curve with 6-31G(d) basis set, enabled by GPU acceleration.

example/hea_symm.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import numpy as np
2+
from tensorcircuit import Circuit
3+
4+
from tencirchem import UCC, HEA, set_backend
5+
from tencirchem.molecule import h4
6+
7+
K = set_backend("jax")
8+
9+
ucc = UCC(h4)
10+
# reference energies
11+
ucc.print_energy()
12+
13+
14+
# symmetry preserving ansatz: npjqi 6:10, 2020
15+
n_layers = 5
16+
n_params = n_layers * (ucc.n_qubits - 2)
17+
def circuit(params):
18+
params = params.reshape(n_layers, ucc.n_qubits - 2)
19+
c = Circuit(ucc.n_qubits)
20+
# HF initial state
21+
for i in range(ucc.n_elec // 2):
22+
c.x(ucc.n_qubits - 1 - i)
23+
c.x(ucc.n_qubits // 2 - 1 - i)
24+
for l in range(n_layers):
25+
indices = list(range(0, ucc.n_qubits - 1, 2)) + list(range(1, ucc.n_qubits - 1, 2))
26+
for i in indices:
27+
if i < ucc.n_qubits // 2 - 1:
28+
theta = params[l, i]
29+
elif i == ucc.n_qubits // 2 - 1:
30+
# preserve s_z
31+
theta = 0
32+
else:
33+
theta = params[l, i-1]
34+
unitary = K.convert_to_tensor([[1, 0, 0, 0],
35+
[0, K.cos(theta), K.sin(theta), 0],
36+
[0, K.sin(theta), -K.cos(theta), 0],
37+
[0, 0, 0, 1]])
38+
c.any(i, (i + 1), unitary=unitary)
39+
return c
40+
41+
42+
es = []
43+
for i in range(10):
44+
init_guess = (np.random.rand(n_params) - 0.5)
45+
hea = HEA(ucc.h_qubit_op, circuit, init_guess, engine="tensornetwork")
46+
# default parameter shift doesn't work for this type of circuit
47+
hea.grad = "autodiff"
48+
e = hea.kernel()
49+
es.append(e)
50+
print(sorted(es))

example/run.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2023. The TenCirChem Developers. All Rights Reserved.
3+
#
4+
# This file is distributed under ACADEMIC PUBLIC LICENSE
5+
# and WITHOUT ANY WARRANTY. See the LICENSE file for details.
6+
#
7+
8+
export PYTHONPATH=../:PYTHONPATH
9+
code=0
10+
for python_args in *.py; do
11+
echo ============================$python_args=============================
12+
timeout 20s python $python_args
13+
exit_code=$?
14+
echo ============================$python_args=============================
15+
# if not the time out exit code or normal exit code
16+
if [ $exit_code -ne 124 ] && [ $exit_code -ne 0 ]; then
17+
echo "The script failed with exit code $exit_code" >&2
18+
code=1
19+
fi
20+
done
21+
22+
exit $code

0 commit comments

Comments
 (0)