Oh, the Hamiltonian’s I provided above were to make simple the piece of code, because the part I was curious about was using cupy.
Here are the actual Hamiltonians I’m working with and trying to speedup:
################ Code from the 'Optimization of molecular geometries' tutorial##################
# imports and relevant defines:
import pennylane as qml
from pennylane import qchem
from pennylane import numpy as np
import time
import concurrent.futures
import multiprocessing
from multiprocessing import Value
# Simulation starting parameters:
symbols = ["O", "H", "H", "O", "H", "H"]
x = np.array([ 0. , 0. , 0. , 1.563923 , 0.98312309,
-0.60365174, -0.78395943, 1.43700227, 1.04747968, 4.26606551,
0.56799183, 1.68673518, 4.63354675, 1.15239926, 3.50338239,
6.10594353, 0.1835633 , 1.19292837], requires_grad=True)
active_electrons = 16
active_orbitals = 10
# define the hamiltonian needed to compute cost-function
def H(x):
return qml.qchem.molecular_hamiltonian(symbols, x, charge=0, active_electrons = active_electrons, active_orbitals = active_orbitals)[0]
hf = qml.qchem.hf_state(electrons=active_electrons, orbitals=active_orbitals*2)
print(hf)
num_wires = active_orbitals*2
H1 = qml.qchem.molecular_hamiltonian(symbols, x, charge=0,
active_electrons = active_electrons,
active_orbitals = active_orbitals, method = "pyscf")[0]
shift = np.zeros_like(x)
delta = 0.01
shift[4] += 0.5 * delta
H2 = qml.qchem.molecular_hamiltonian(symbols, x+shift, charge=0,
active_electrons = active_electrons,
active_orbitals = active_orbitals, method = "pyscf")[0]
# Adding Hamiltonians using +
start = time.time()
res = H1 + H2
time.time()-start