Suppose I have 2 H molecule in 3D space and I would like to optimize them without reducing to a 1D problem. I could see two solutions for that, one is VQE and the 2nd is QPE
VQE
I wrote some code to do that, but it seems Pennylane converting the coordinate to autograd tensor is at odd with my current implementation?
The code
Here I fix a H atom at position 0, 0, 0 and optimize the coordinate of the other atom H_1
import pennylane as qml
from pennylane import numpy as np
import jax
import optax
dev = qml.device("default.qubit", 4)
@qml.qnode(dev)
def circuit_expected(H):
qml.BasisState([1,1,0,0], wires=[0, 1, 2, 3])
qml.DoubleExcitation(0.2, wires=[0, 1, 2, 3])
return qml.expval(H)
def loss_f(coord):
symbols = ["H", "H"]
H_0 = np.array([0.,0.,0.]) # fixed
H, qb = qml.qchem.molecular_hamiltonian(symbols, np.array([0,0,0, *coord]))
return circuit_expected(H)
def optimize():
H_1 = np.array([1.,1.,1.])
# prepare for the 1st run
conv_tol = 1e-6
opt = optax.sgd(learning_rate=0.4)
max_iterations = 100
opt = optax.sgd(learning_rate=0.4)
opt_coords_state = opt.init(H_1)
for i in range(10):
grad_coordinates = jax.grad(loss_f, 0)(H_1)
updates, opt_coords_state = opt.update(grad_coordinates, opt_coords_state)
H_1 = optax.apply_updates(H_1, updates)
print(grad_coordinates)
optimize()
The error
TracerArrayConversionError: The numpy.ndarray conversion method __array__() was called on traced array with shape float32[].
See https://jax.readthedocs.io/en/latest/errors.html#jax.errors.TracerArrayConversionError
on the line of H, qb = qml.qchem.molecular_hamiltonian(symbols, np.array([0,0,0, *coord]))
I tried with np.array()
, jnp.array()
, jnp.concatenation()
but the error persists. What am I doing wrong here?
QPE
Suppose I found \phi in U |\psi \rangle = e^{i \phi} |\psi \rangle and use that to find eigenstate |\psi\rangle. How can I derive the position operator? My guess if I take the expected value of the position operator, then I can arrive at the answer, but again I don’t know yet how to define that position operator.