Skip to content

Commit 78e0f15

Browse files
version0.2.0
1 parent 33638eb commit 78e0f15

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
## 0.2.0
6+
57
### Added
68

79
- Add PyTorch nn Module wrapper in `torchnn`
@@ -10,6 +12,8 @@
1012

1113
- Brand new `sample` API with batch support and sampling from state support
1214

15+
- add more methods in global namespace, and add alias `KerasLayer`/`TorchLayer`
16+
1317
### Fixed
1418

1519
- Fixed bug in merge single gates when all gates are single-qubit ones

tensorcircuit/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.3"
1+
__version__ = "0.2.0"
22
__author__ = "TensorCircuit Authors"
33
__creator__ = "refraction-ray"
44

@@ -25,14 +25,17 @@
2525
from . import interfaces
2626
from . import templates
2727
from . import quantum
28+
from .quantum import QuOperator, QuVector, QuAdjointVector, QuScalar
2829

2930
try:
3031
from . import keras
32+
from .keras import QuantumLayer as KerasLayer
3133
except ModuleNotFoundError:
3234
pass # in case tf is not installed
3335

3436
try:
3537
from . import torchnn
38+
from .torchnn import QuantumNet as TorchLayer
3639
except ModuleNotFoundError:
3740
pass # in case torch is not installed
3841

tests/test_keras.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import numpy as np
1111
import tensorflow as tf
1212
import tensorcircuit as tc
13-
from tensorcircuit import keras as K
1413

1514

1615
dtype = np.complex128
@@ -55,13 +54,15 @@ def vqe_f2(inputs, xweights, zzweights, nlayers, n):
5554

5655
def test_vqe_layer2(tfb, highp):
5756
vqe_fp = partial(vqe_f2, nlayers=3, n=6)
58-
vqe_layer = K.QuantumLayer(vqe_fp, [(3, 6), (3, 6)])
57+
vqe_layer = tc.KerasLayer(vqe_fp, [(3, 6), (3, 6)])
5958
inputs = np.zeros([1])
6059
with tf.GradientTape() as tape:
6160
e = vqe_layer(inputs)
6261
print(e, tape.gradient(e, vqe_layer.variables))
6362
model = tf.keras.Sequential([vqe_layer])
64-
model.compile(loss=K.output_asis_loss, optimizer=tf.keras.optimizers.Adam(0.01))
63+
model.compile(
64+
loss=tc.keras.output_asis_loss, optimizer=tf.keras.optimizers.Adam(0.01)
65+
)
6566
model.fit(np.zeros([1, 1]), np.zeros([1]), batch_size=1, epochs=300)
6667

6768

@@ -87,12 +88,14 @@ def vqe_f(inputs, weights, nlayers, n):
8788

8889
def test_vqe_layer(tfb, highp):
8990
vqe_fp = partial(vqe_f, nlayers=6, n=6)
90-
vqe_layer = K.QuantumLayer(vqe_fp, (6 * 2, 6))
91+
vqe_layer = tc.keras.QuantumLayer(vqe_fp, (6 * 2, 6))
9192
inputs = np.zeros([1])
9293
inputs = tf.constant(inputs)
9394
model = tf.keras.Sequential([vqe_layer])
9495

95-
model.compile(loss=K.output_asis_loss, optimizer=tf.keras.optimizers.Adam(0.01))
96+
model.compile(
97+
loss=tc.keras.output_asis_loss, optimizer=tf.keras.optimizers.Adam(0.01)
98+
)
9699

97100
model.fit(np.zeros([2, 1]), np.zeros([2, 1]), batch_size=2, epochs=500)
98101

@@ -104,7 +107,7 @@ def test_function_io(tfb, tmp_path, highp):
104107

105108
vqe_f_p = tf.function(vqe_f_p)
106109
vqe_f_p(weights=tf.ones([6, 6], dtype=tf.float64), nlayers=3, n=6)
107-
K.save_func(vqe_f_p, str(tmp_path))
108-
loaded = K.load_func(str(tmp_path), fallback=vqe_f_p)
110+
tc.keras.save_func(vqe_f_p, str(tmp_path))
111+
loaded = tc.keras.load_func(str(tmp_path), fallback=vqe_f_p)
109112
print(loaded(weights=tf.ones([6, 6], dtype=tf.float64), nlayers=3, n=6))
110113
print(loaded(weights=tf.ones([6, 6], dtype=tf.float64), nlayers=3, n=6))

tests/test_torchnn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def qpred(x, weights):
4141
else:
4242
use_interface = True
4343

44-
ql = tc.torchnn.QuantumNet(
44+
ql = tc.TorchLayer(
4545
qpred, weights_shape=[2 * nlayers, n], use_interface=use_interface
4646
)
4747

0 commit comments

Comments
 (0)