Hi, I was trying to use dask for parallelization of the expected value of groups of sub hamiltonian terms (taking as a basis the demo VQE with parallel QPUs with Rigetti), and I get a non-expected behavior, and as I’m a novice using dask, I don’t know what I do wrong or what I should do to resolved. The code that I use is the bellow and at the end I plot the weird behavior.
symbols = ["H", "H"]
coordinates = np.array([0.0, 0.0, -0.6614, 0.0, 0.0, 0.6614], requires_grad=True)
hamil, qubits = qchem.molecular_hamiltonian( symbols= symbols, coordinates= coordinates, charge=1)
coeff, terms = hamil.terms()
terms, coeff = qml.pauli.group_observables(observables=terms, coefficients=coeff, grouping_type='qwc', method='rlf')
dev = qml.device('default.qubit', wires=4)
begin_state = qml.qchem.hf_state(electrons=2, orbitals=qubits)
singles, doubles = qml.qchem.excitations(2, qubits)
singles, doubles = qml.qchem.excitations_to_wires(singles, doubles)
def circuit2(theta, index):
qml.UCCSD(theta, range(qubits), singles, doubles, begin_state)
return [qml.expval(u) for u in terms[index]]
node = qml.QNode(circuit2, dev)
def process_group(theta, i):
result_probs = node(theta=theta, index=i)
return np.sum( coeff[i]*np.array(result_probs) )
def cost_function(theta):
results = []
for i in range(len(terms)):
results.append( dask.delayed(process_group)(theta, i) )
num_workers = 1
result = dask.compute(*results, scheduler="processes", num_workers=num_workers)
return np.sum( result )
number = len(singles) + len(doubles)
theta = np.random.random( size=number )*(np.pi/180.0)
theta_optimizer = qml.GradientDescentOptimizer(stepsize=0.3)
energy = [cost_function(theta)]
theta_evol = [theta]
for _ in range(40):
theta.requires_grad = True
theta = theta_optimizer.step(cost_function, theta)
energy.append(cost_function(theta))
theta_evol.append(theta)
prev_energy = energy[len(energy)-2]
print(energy[-1])
conv = np.abs(energy[-1] - prev_energy)
if conv <= 1e-6:
break
Thanks in advance and have a nice day.