Skip to content

Commit bcd105f

Browse files
mypy in vqnhe codebase
1 parent 75cf4e1 commit bcd105f

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

docs/source/tutorials/tfim_vqe_cn.ipynb

+1-3
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@
164164
"source": [
165165
"def vqe_tfim(param, n, nlayers):\n",
166166
" c = tc.Circuit(n)\n",
167-
" paramc = tc.backend.cast(\n",
168-
" param, tc.dtypestr\n",
169-
" ) # 我们假设输入参数的 dtype 为 float64\n",
167+
" paramc = tc.backend.cast(param, tc.dtypestr) # 我们假设输入参数的 dtype 为 float64\n",
170168
" for i in range(n):\n",
171169
" c.H(i)\n",
172170
" for j in range(nlayers):\n",

docs/source/tutorials/tfim_vqe_diffreph_cn.ipynb

+6-12
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"tc.set_dtype(\"complex128\")\n",
6565
"dtype = np.complex128\n",
6666
"\n",
67-
"xx = tc.gates._xx_matrix #要使用的 xx 门矩阵"
67+
"xx = tc.gates._xx_matrix # 要使用的 xx 门矩阵"
6868
]
6969
},
7070
{
@@ -84,7 +84,7 @@
8484
"source": [
8585
"n = 4 # 量子比特数\n",
8686
"nlayers = 2 # 电路层数\n",
87-
"ntrials = 2 #随机电路实例数"
87+
"ntrials = 2 # 随机电路实例数"
8888
]
8989
},
9090
{
@@ -169,7 +169,7 @@
169169
"source": [
170170
"vqe_tfim_paulistring_vvag = tc.backend.jit(\n",
171171
" tc.backend.vectorized_value_and_grad(vqe_tfim_paulistring)\n",
172-
") # 使用 vvag 获取不同随机电路实例的损失函数和梯度\n"
172+
") # 使用 vvag 获取不同随机电路实例的损失函数和梯度"
173173
]
174174
},
175175
{
@@ -222,9 +222,7 @@
222222
" ) # 初始参数\n",
223223
" opt = tf.keras.optimizers.Adam(1e-2)\n",
224224
" for i in range(maxiter):\n",
225-
" e, grad = vqe_tfim_paulistring_vvag(\n",
226-
" param.value(), n, nlayers\n",
227-
" ) # 能量和梯度\n",
225+
" e, grad = vqe_tfim_paulistring_vvag(param.value(), n, nlayers) # 能量和梯度\n",
228226
" opt.apply_gradients([(grad, param)])\n",
229227
" if i % 200 == 0:\n",
230228
" print(e)\n",
@@ -383,9 +381,7 @@
383381
"\n",
384382
" opt = tf.keras.optimizers.Adam(1e-2)\n",
385383
" for i in range(maxiter):\n",
386-
" e, grad = vqe_tfim_vvag(\n",
387-
" param.value(), n, nlayers, hamiltonian\n",
388-
" ) # 能量和梯度\n",
384+
" e, grad = vqe_tfim_vvag(param.value(), n, nlayers, hamiltonian) # 能量和梯度\n",
389385
" opt.apply_gradients([(grad, param)])\n",
390386
" if i % 200 == 0:\n",
391387
" print(e)\n",
@@ -424,9 +420,7 @@
424420
"\n",
425421
"Jx = np.array([1.0 for _ in range(n - 1)]) # xx 相互作用的强度 (OBC)\n",
426422
"Bz = np.array([1.0 for _ in range(n)]) # 横向场强\n",
427-
"hamiltonian_mpo = tn.matrixproductstates.mpo.FiniteTFI(\n",
428-
" Jx, Bz, dtype=dtype\n",
429-
") # 矩阵乘积算子\n",
423+
"hamiltonian_mpo = tn.matrixproductstates.mpo.FiniteTFI(Jx, Bz, dtype=dtype) # 矩阵乘积算子\n",
430424
"hamiltonian_mpo = quoperator_mpo(hamiltonian_mpo) # 从 mpo 生成 QuOperator"
431425
]
432426
},

examples/vqnhe_h6.py

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
sys.path.insert(0, "../")
77
import numpy as np
8-
import tensorflow as tf
98
import tensorcircuit as tc
109
from tensorcircuit.applications.vqes import VQNHE, JointSchedule, construct_matrix_v3
1110

tensorcircuit/applications/vqes.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def __init__(
239239
self.base = tf.constant(list(product(*[[0, 1] for _ in range(n)])))
240240
self.hamiltonian = hamiltonian
241241
self.shortcut = shortcut
242-
self.history = []
242+
self.history: List[float] = []
243243

244244
def assign(
245245
self, c: Optional[List[Tensor]] = None, q: Optional[Tensor] = None
@@ -503,7 +503,7 @@ def training(
503503
debug: int = 100,
504504
onlyq: int = 0,
505505
checkpoints: Optional[List[Tuple[int, float]]] = None,
506-
) -> Tuple[Array, Array, Array, int]:
506+
) -> Tuple[Array, Array, Array, int, List[float]]:
507507
loss_prev = 0
508508
loss_opt = 1e8
509509
j_opt = 0
@@ -513,25 +513,25 @@ def training(
513513
self.ccache = self.model.get_weights()
514514

515515
if not optc:
516-
optc = 0.001
516+
optc = 0.001 # type: ignore
517517
if isinstance(optc, float) or isinstance(
518518
optc, tf.keras.optimizers.schedules.LearningRateSchedule
519519
):
520520
optc = tf.keras.optimizers.Adam(optc)
521521
if isinstance(optc, tf.keras.optimizers.Optimizer):
522522
optcf = lambda _: optc
523523
else:
524-
optcf = optc
524+
optcf = optc # type: ignore
525525
if not optq:
526-
optq = 0.01
526+
optq = 0.01 # type: ignore
527527
if isinstance(optq, float) or isinstance(
528528
optq, tf.keras.optimizers.schedules.LearningRateSchedule
529529
):
530530
optq = tf.keras.optimizers.Adam(optq)
531531
if isinstance(optq, tf.keras.optimizers.Optimizer):
532532
optqf = lambda _: optq
533533
else:
534-
optqf = optq
534+
optqf = optq # type: ignore
535535

536536
nm = tf.constant(1.0)
537537
times = []
@@ -592,7 +592,7 @@ def training(
592592
quantume, _ = self.plain_evaluation(self.circuit_variable)
593593
print(
594594
"vqnhe prediction: ",
595-
loss_prev.numpy(),
595+
loss_prev.numpy(), # type: ignore
596596
"quantum part energy: ",
597597
quantume.numpy(),
598598
"classical model scale: ",
@@ -601,8 +601,8 @@ def training(
601601
print("----------END TRAINING------------")
602602
self.history += history[:j_opt]
603603
return (
604-
loss_opt.numpy(),
605-
np.real(nm_opt.numpy()),
604+
loss_opt.numpy(), # type: ignore
605+
np.real(nm_opt.numpy()), # type: ignore
606606
quantume.numpy(),
607607
j_opt,
608608
history[:j_opt],
@@ -666,6 +666,6 @@ def multi_training(
666666
if rs:
667667
es = [r["energy"] for r in rs]
668668
ind = np.argmin(es)
669-
self.assign(rs[ind]["model_weights"], rs[ind]["circuit_weights"])
670-
self.history = rs[ind]["history"]
669+
self.assign(rs[ind]["model_weights"], rs[ind]["circuit_weights"]) # type: ignore
670+
self.history = rs[ind]["history"] # type: ignore
671671
return rs

0 commit comments

Comments
 (0)