Skip to content

Commit ba27708

Browse files
new version black
1 parent 3bdf362 commit ba27708

33 files changed

+73
-78
lines changed

docs/source/tutorials/circuit_basics.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@
799799
}
800800
],
801801
"source": [
802-
"input_state = K.ones([2 ** n])\n",
802+
"input_state = K.ones([2**n])\n",
803803
"input_state /= K.norm(input_state)\n",
804804
"\n",
805805
"c = tc.Circuit(n, inputs=input_state)\n",

docs/source/tutorials/circuit_basics_cn.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@
798798
}
799799
],
800800
"source": [
801-
"input_state = K.ones([2 ** n])\n",
801+
"input_state = K.ones([2**n])\n",
802802
"input_state /= K.norm(input_state)\n",
803803
"\n",
804804
"c = tc.Circuit(n, inputs=input_state)\n",

docs/source/tutorials/operator_spreading.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"def get_unitary_of_circuit(v):\n",
122122
" layers = len(v)\n",
123123
" c = tc.Circuit(\n",
124-
" N, inputs=np.eye(2 ** N, dtype=dtype)\n",
124+
" N, inputs=np.eye(2**N, dtype=dtype)\n",
125125
" ) # when we choose inputs=np.eye(2**N,...), the output wavefunction\n",
126126
" # is the unitary matrix U(t) of the quantum circuit\n",
127127
"\n",
@@ -138,7 +138,7 @@
138138
" c.ry((i + 1) % N, theta=v[layer, (i + 1) % N])\n",
139139
" c.cz(i, (i + 1) % N)\n",
140140
" U = c.wavefunction() # the output of wavefunction is a vector with dim=2^(2N)\n",
141-
" U = tf.reshape(U, [2 ** N, 2 ** N]) # reshape vector to matrix\n",
141+
" U = tf.reshape(U, [2**N, 2**N]) # reshape vector to matrix\n",
142142
" return U"
143143
]
144144
},
@@ -215,7 +215,7 @@
215215
" σ_x_t = U_dagger @ σ_x @ U # O(t)\n",
216216
" σ_x_t_dagger = tf.transpose(σ_x_t, conjugate=True)\n",
217217
" C = tf.linalg.trace(σ_y @ σ_x_t_dagger @ σ_y @ σ_x_t)\n",
218-
" C = 1 - tf.math.real(C) / (2 ** N)\n",
218+
" C = 1 - tf.math.real(C) / (2**N)\n",
219219
" return C"
220220
]
221221
},

docs/source/tutorials/operator_spreading_cn.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"def get_unitary_of_circuit(v):\n",
122122
" layers = len(v)\n",
123123
" c = tc.Circuit(\n",
124-
" N, inputs=np.eye(2 ** N, dtype=dtype)\n",
124+
" N, inputs=np.eye(2**N, dtype=dtype)\n",
125125
" ) # 当我们选择inputs=np.eye(2**N,...)时,输出波函数\n",
126126
" # 是量子电路的幺正矩阵 U(t)\n",
127127
"\n",
@@ -138,7 +138,7 @@
138138
" c.ry((i + 1) % N, theta=v[layer, (i + 1) % N])\n",
139139
" c.cz(i, (i + 1) % N)\n",
140140
" U = c.wavefunction() # 波函数的输出是一个向量,它的 dim=2^(2N)\n",
141-
" U = tf.reshape(U, [2 ** N, 2 ** N]) # 将向量重塑为矩阵\n",
141+
" U = tf.reshape(U, [2**N, 2**N]) # 将向量重塑为矩阵\n",
142142
" return U"
143143
]
144144
},
@@ -215,7 +215,7 @@
215215
" σ_x_t = U_dagger @ σ_x @ U # O(t)\n",
216216
" σ_x_t_dagger = tf.transpose(σ_x_t, conjugate=True)\n",
217217
" C = tf.linalg.trace(σ_y @ σ_x_t_dagger @ σ_y @ σ_x_t)\n",
218-
" C = 1 - tf.math.real(C) / (2 ** N)\n",
218+
" C = 1 - tf.math.real(C) / (2**N)\n",
219219
" return C"
220220
]
221221
},

docs/source/tutorials/optimization_and_expressibility.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
"@tf.function\n",
243243
"def energy_loss(v, hamiltonian):\n",
244244
" ψ = get_state(v)\n",
245-
" ψ = tf.reshape(ψ, [2 ** N, 1])\n",
245+
" ψ = tf.reshape(ψ, [2**N, 1])\n",
246246
" loss = tf.transpose(ψ, conjugate=True) @ hamiltonian @ ψ\n",
247247
" loss = tc.backend.real(loss)\n",
248248
" return loss"

docs/source/tutorials/optimization_and_expressibility_cn.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241
"@tf.function\n",
242242
"def energy_loss(v, hamiltonian):\n",
243243
" ψ = get_state(v)\n",
244-
" ψ = tf.reshape(ψ, [2 ** N, 1])\n",
244+
" ψ = tf.reshape(ψ, [2**N, 1])\n",
245245
" loss = tf.transpose(ψ, conjugate=True) @ hamiltonian @ ψ\n",
246246
" loss = tc.backend.real(loss)\n",
247247
" return loss"

docs/source/tutorials/qml_scenarios.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
"\n",
290290
"qml_layer = tc.keras.QuantumLayer(qml, weights_shape=[blocks, n, 3])\n",
291291
"\n",
292-
"inputs = tf.keras.Input(shape=(2 ** n), dtype=tf.complex64)\n",
292+
"inputs = tf.keras.Input(shape=(2**n), dtype=tf.complex64)\n",
293293
"measurements = qml_layer(inputs)\n",
294294
"output = tf.keras.layers.Dense(1, activation=\"sigmoid\")(measurements)\n",
295295
"model = tf.keras.Model(inputs=inputs, outputs=output)"

docs/source/tutorials/qml_scenarios_cn.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
"\n",
290290
"qml_layer = tc.keras.QuantumLayer(qml, weights_shape=[blocks, n, 3])\n",
291291
"\n",
292-
"inputs = tf.keras.Input(shape=(2 ** n), dtype=tf.complex64)\n",
292+
"inputs = tf.keras.Input(shape=(2**n), dtype=tf.complex64)\n",
293293
"measurements = qml_layer(inputs)\n",
294294
"output = tf.keras.layers.Dense(1, activation=\"sigmoid\")(measurements)\n",
295295
"model = tf.keras.Model(inputs=inputs, outputs=output)"

docs/source/tutorials/vqe_h2o.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
"outputs": [],
431431
"source": [
432432
"mb_tf = tc.backend.coo_sparse_matrix(\n",
433-
" np.transpose(np.stack([mb.row, mb.col])), mb.data, shape=(2 ** n, 2 ** n)\n",
433+
" np.transpose(np.stack([mb.row, mb.col])), mb.data, shape=(2**n, 2**n)\n",
434434
")"
435435
]
436436
},

docs/source/tutorials/vqe_h2o_cn.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
"outputs": [],
431431
"source": [
432432
"mb_tf = tc.backend.coo_sparse_matrix(\n",
433-
" np.transpose(np.stack([mb.row, mb.col])), mb.data, shape=(2 ** n, 2 ** n)\n",
433+
" np.transpose(np.stack([mb.row, mb.col])), mb.data, shape=(2**n, 2**n)\n",
434434
")"
435435
]
436436
},

docs/source/tutorials/vqex_mbl.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280
" tape.watch(v)\n",
281281
" ρ = get_ρ_circuit(v, epochs)\n",
282282
" hexp = tf.linalg.trace(ρ @ hamiltonian)\n",
283-
" loss = tf.linalg.trace(ρ @ hamiltonian @ hamiltonian) - hexp ** 2\n",
283+
" loss = tf.linalg.trace(ρ @ hamiltonian @ hamiltonian) - hexp**2\n",
284284
" loss = tf.math.real(loss)\n",
285285
" gr = tape.gradient(loss, v)\n",
286286
" return loss, gr, ρ"

docs/source/tutorials/vqex_mbl_cn.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@
280280
" tape.watch(v)\n",
281281
" ρ = get_ρ_circuit(v, epochs)\n",
282282
" hexp = tf.linalg.trace(ρ @ hamiltonian)\n",
283-
" loss = tf.linalg.trace(ρ @ hamiltonian @ hamiltonian) - hexp ** 2\n",
283+
" loss = tf.linalg.trace(ρ @ hamiltonian @ hamiltonian) - hexp**2\n",
284284
" loss = tf.math.real(loss)\n",
285285
" gr = tape.gradient(loss, v)\n",
286286
" return loss, gr, ρ"

examples/checkpoint_memsave.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def totallayer(s, param):
9797

9898

9999
def vqe_forward(param):
100-
s = tc.backend.ones([2 ** nwires])
100+
s = tc.backend.ones([2**nwires])
101101
s /= tc.backend.norm(s)
102102
s = totallayer(s, param)
103103
e = tc.expectation((tc.gates.x(), [1]), ket=s)

examples/variational_dynamics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def update(theta, lhs, rhs, tau):
9696
# TFIM Hamiltonian defined on lattice graph g (1D OBC chain)
9797
h = tc.array_to_tensor(h)
9898

99-
psi0 = np.zeros(2 ** N)
99+
psi0 = np.zeros(2**N)
100100
psi0[0] = 1.0
101101
psi0 = tc.array_to_tensor(psi0)
102102

examples/vqeh2o_benchmark.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
mb = tc.quantum.PauliStringSum2COO_numpy(lsb, wb)
3737
mbd = mb.todense()
3838
mb = K.coo_sparse_matrix(
39-
np.transpose(np.stack([mb.row, mb.col])), mb.data, shape=(2 ** n, 2 ** n)
39+
np.transpose(np.stack([mb.row, mb.col])), mb.data, shape=(2**n, 2**n)
4040
)
4141
mbd = tc.array_to_tensor(mbd)
4242

requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pytest==6.2.4
33
pytest-cov
44
pytest-benchmark
55
pytest-xdist
6-
black==21.9b0
6+
black==22.3.0
77
sphinx>=4.0
88
pytest-lazy-fixture
99
pylint==2.11.1

requirements-docker.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pytest-cov
2828
pytest-benchmark
2929
pytest-xdist
3030
pytest-lazy-fixture
31-
black==21.9b0
31+
black==22.3.0
3232
sphinx>=4.0
3333
sphinx-intl
3434
sphinx-copybutton

tensorcircuit/applications/utils.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ def amplitude_encoding(
5454
norm = tf.linalg.norm(fig, axis=1)
5555
norm = norm[..., tf.newaxis]
5656
fig = fig / norm
57-
if fig.shape[1] < 2 ** qubits:
57+
if fig.shape[1] < 2**qubits:
5858
fig = tf.concat(
5959
[
6060
fig,
61-
tf.zeros([fig.shape[0], 2 ** qubits - fig.shape[1]], dtype=tf.float64),
61+
tf.zeros([fig.shape[0], 2**qubits - fig.shape[1]], dtype=tf.float64),
6262
],
6363
axis=1,
6464
)
@@ -205,7 +205,7 @@ def train_qml_vag(
205205
with tf.GradientTape() as tape:
206206
tape.watch(nnp)
207207
cnnp = tf.cast(nnp, dtype=tf.complex64)
208-
c = Circuit(nqubits, inputs=np.ones([1024], dtype=np.complex64) / 2 ** 5)
208+
c = Circuit(nqubits, inputs=np.ones([1024], dtype=np.complex64) / 2**5)
209209
for epoch in range(epochs):
210210
for i in range(nqubits):
211211
c.rz(i, theta=cnnp[3 * epoch, i]) # type: ignore
@@ -275,7 +275,7 @@ def validate_qml_vag(
275275
) -> Any:
276276
xs, ys = gdata
277277
cnnp = tf.cast(nnp, dtype=tf.complex64)
278-
c = Circuit(nqubits, inputs=np.ones([1024], dtype=np.complex64) / 2 ** 5)
278+
c = Circuit(nqubits, inputs=np.ones([1024], dtype=np.complex64) / 2**5)
279279
for epoch in range(epochs):
280280
for i in range(nqubits):
281281
c.rz(i, theta=cnnp[3 * epoch, i]) # type: ignore
@@ -401,7 +401,7 @@ def TFIM1Denergy(
401401
Jzz *= 4
402402
for i in range(L):
403403
q = np.pi * (2 * i - (1 + (-1) ** L) / 2) / L
404-
e -= np.abs(Jx) / 2 * np.sqrt(1 + Jzz ** 2 / 4 / Jx ** 2 - Jzz / Jx * np.cos(q))
404+
e -= np.abs(Jx) / 2 * np.sqrt(1 + Jzz**2 / 4 / Jx**2 - Jzz / Jx * np.cos(q))
405405
return e
406406

407407

tensorcircuit/applications/vags.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def GHZ_vag(
5454
gdata: Any, nnp: Tensor, preset: Sequence[int], verbose: bool = False, n: int = 3
5555
) -> Tuple[Tensor, Tensor]:
5656
# gdata = None
57-
reference_state = np.zeros([2 ** n])
57+
reference_state = np.zeros([2**n])
5858
# W states benchmarks
5959
# for i in range(n):
6060
# reference_state[2**(i)] = 1/np.sqrt(n)
@@ -80,7 +80,7 @@ def GHZ_vag(
8080
s = circuit.wavefunction()
8181
s = tf.reshape(
8282
s,
83-
[2 ** n],
83+
[2**n],
8484
)
8585
loss = tf.math.reduce_sum(
8686
tf.math.abs(s - reference_state)
@@ -1053,7 +1053,6 @@ def quantum_mp_qaoa_vag(
10531053
gmatrix = tf.constant(gmatrix)
10541054
return loss[0], gmatrix
10551055

1056-
10571056
except NameError as e:
10581057
logger.warning(e)
10591058
logger.warning("tfq related vags disabled due to missing packages")

tensorcircuit/applications/van.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def call(self, inputs):
4343
return tf.tensordot(inputs, w_mask, axes=[[-2, -1], [2, 3]]) + self.b
4444

4545
def regularization(self, lbd_w=1.0, lbd_b=1.0):
46-
return lbd_w * tf.reduce_sum(self.w ** 2) + lbd_b * tf.reduce_sum(self.b ** 2)
46+
return lbd_w * tf.reduce_sum(self.w**2) + lbd_b * tf.reduce_sum(self.b**2)
4747

4848

4949
class MADE(tf.keras.Model):

tensorcircuit/applications/vqes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def construct_matrix_tf(ham: List[List[float]], dtype: Any = tf.complex128) -> T
8686
def construct_matrix_v2(ham: List[List[float]], dtype: Any = tf.complex128) -> Tensor:
8787
# deprecated
8888
s = len(ham[0]) - 1
89-
h = tf.zeros([2 ** s, 2 ** s], dtype=dtype)
89+
h = tf.zeros([2**s, 2**s], dtype=dtype)
9090
for term in tqdm(ham, desc="Hamiltonian building"):
9191
term = list(term)
9292
for i, t in enumerate(term):

tensorcircuit/densitymatrix.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def densitymatrix(self, check: bool = False, reuse: bool = True) -> tn.Node.tens
239239
nodes, _ = self._copy_dm_tensor(conj=False, reuse=reuse)
240240
# t = contractor(nodes, output_edge_order=d_edges)
241241
dm = backend.reshape(
242-
nodes[0].tensor, shape=[2 ** self._nqubits, 2 ** self._nqubits]
242+
nodes[0].tensor, shape=[2**self._nqubits, 2**self._nqubits]
243243
)
244244
if check:
245245
self.check_density_matrix(dm)

tensorcircuit/mpscircuit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def apply_adjacent_double_gate(
277277
max_truncation_err=self.max_truncation_err,
278278
relative=self.relative,
279279
)
280-
self._fidelity *= 1 - backend.real(backend.sum(err ** 2))
280+
self._fidelity *= 1 - backend.real(backend.sum(err**2))
281281

282282
def apply_double_gate(
283283
self,

tensorcircuit/quantum.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,6 @@ def ps2coo_core(
12271227
)
12281228
return tf.SparseTensor(indices=indices, values=values, dense_shape=(s, s)) # type: ignore
12291229

1230-
12311230
except NameError:
12321231
logger.warning(
12331232
"tensorflow is not installed, and sparse Hamiltonian generation utilities are disabled"
@@ -1654,7 +1653,7 @@ def spin_by_basis(n: int, m: int, elements: Tuple[int, int] = (1, -1)) -> Tensor
16541653
backend.cast(
16551654
backend.convert_to_tensor(np.array([[elements[0]], [elements[1]]])), "int32"
16561655
),
1657-
[2 ** m, int(2 ** (n - m - 1))],
1656+
[2**m, int(2 ** (n - m - 1))],
16581657
)
16591658
return backend.reshape(s, [-1])
16601659

tensorcircuit/templates/dataset.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ def amplitude_encoding(
1919
fig = backend.reshape(fig, shape=[-1])
2020
norm = backend.norm(fig)
2121
fig = fig / norm
22-
if backend.shape_tuple(fig)[0] < 2 ** nqubits:
22+
if backend.shape_tuple(fig)[0] < 2**nqubits:
2323
fig = backend.concat(
2424
[
2525
fig,
2626
backend.zeros(
27-
[2 ** nqubits - backend.shape_tuple(fig)[0]], dtype=fig.dtype
27+
[2**nqubits - backend.shape_tuple(fig)[0]], dtype=fig.dtype
2828
),
2929
],
3030
)

tensorcircuit/translation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121

2222
def perm_matrix(n: int) -> Tensor:
23-
p_mat = np.zeros([2 ** n, 2 ** n])
24-
for i in range(2 ** n):
23+
p_mat = np.zeros([2**n, 2**n])
24+
for i in range(2**n):
2525
bit = i
2626
revs_i = 0
2727
for j in range(n):

0 commit comments

Comments
 (0)