Skip to content

Commit 3ed6603

Browse files
QottmannJerryChen97dwierichs
authored
[Bug fix] Update fixed depth hamiltonian simulation demo (#1344)
The problem is that the computed horizontal CSA $\mathfrak{h}$ is not necessarily composed of just Pauli words, but soms of Paulis (Pauli sentence). The old version was assuming everything was Pauli, did the decomposition in terms of Pauli words, and accordingly counted elements. Updating this here to decompose in the actual basis of the horizontal CSA $\mathfrak{h}$ and show explicitly that the decomposition is stricly in it. edit: this works all well locally with correct jax, optax and pl version, but for some reason fails in CI 🅰️ 🅰️ 🅰️ edit2: increasing the tolerance in `horizontal_cartan_subalgebra` fixes this. A bit problematic that this cannot be reproduced locally 😢 --------- Co-authored-by: Yushao Chen (Jerry) <chenys13@outlook.com> Co-authored-by: David Wierichs <davidwierichs@gmail.com>
1 parent 631a674 commit 3ed6603

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

demonstrations/tutorial_fixed_depth_hamiltonian_simulation_via_cartan_decomposition.metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
}
77
],
88
"dateOfPublication": "2024-12-19T00:00:00+00:00",
9-
"dateOfLastModification": "2025-03-26T00:00:00+00:00",
9+
"dateOfLastModification": "2025-04-11T00:00:00+00:00",
1010
"categories": [
1111
"Quantum Computing",
1212
"Algorithms"

demonstrations/tutorial_fixed_depth_hamiltonian_simulation_via_cartan_decomposition.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
# :func:`~.pennylane.liealg.horizontal_cartan_subalgebra` returns some additional information, which we will
170170
# not use here.
171171

172-
g, k, mtilde, h, _ = horizontal_cartan_subalgebra(k, m)
172+
g, k, mtilde, h, _ = horizontal_cartan_subalgebra(k, m, tol=1e-8)
173173
len(g), len(k), len(mtilde), len(h)
174174

175175
##############################################################################
@@ -220,10 +220,10 @@
220220
#
221221

222222
def run_opt(
223-
value_and_grad,
223+
loss,
224224
theta,
225-
n_epochs=500,
226-
lr=0.1,
225+
n_epochs=1000,
226+
lr=0.05,
227227
):
228228
"""Boilerplate JAX optimization"""
229229
value_and_grad = jax.jit(jax.value_and_grad(loss))
@@ -274,7 +274,7 @@ def loss(theta):
274274
A = K_m @ v_m @ K_m.conj().T
275275
return jnp.trace(A.conj().T @ H_m).real
276276

277-
theta0 = jnp.ones(len(k), dtype=float)
277+
theta0 = jax.random.normal(jax.random.PRNGKey(0), shape=(len(k),), dtype=float)
278278

279279
thetas, energy, _ = run_opt(loss, theta0, n_epochs=1000, lr=0.05)
280280
plt.plot(energy - np.min(energy))
@@ -297,13 +297,18 @@ def loss(theta):
297297
# .. math:: h_0 = K_c^\dagger H K_c.
298298

299299
h_0_m = Kc_m.conj().T @ H_m @ Kc_m
300-
h_0 = qml.pauli_decompose(h_0_m)
301300

302-
print(len(h_0))
301+
# decompose h_0_m in terms of the basis of h
302+
basis = [qml.matrix(op, wire_order=range(n_wires)) for op in h]
303+
coeffs = qml.pauli.trace_inner_product(h_0_m, basis)
304+
305+
# ensure that decomposition is correct, i.e. h_0_m is truely an element of just h
306+
h_0_m_recomposed = np.sum([c * op for c, op in zip(coeffs, basis)], axis=0)
307+
print("Decomposition of h_0 is faithful: ", np.allclose(h_0_m_recomposed, h_0_m, atol=1e-10))
308+
309+
# sanity check that the horizontal CSA is Abelian, i.e. all its elements commute
310+
print("All elements in h commute with each other: ", qml.liealg.check_abelian(h))
303311

304-
# assure that h_0 is in \mathfrak{h}
305-
h_vspace = qml.pauli.PauliVSpace(h)
306-
not h_vspace.is_independent(h_0.pauli_rep)
307312

308313
##############################################################################
309314
#
@@ -331,6 +336,7 @@ def loss(theta):
331336
t = 1.
332337
U_exact = qml.exp(-1j * t * H)
333338
U_exact_m = qml.matrix(U_exact, wire_order=range(n_wires))
339+
h_0 = qml.dot(coeffs, h)
334340

335341
def U_kak(theta_opt, t):
336342
qml.adjoint(K)(theta_opt, k)

0 commit comments

Comments
 (0)