Hello,
the main problem is, you need to run 814 circuits, and the limit is 300.
I’ve tried to split the circuits into multiple jobs, but it doesn’t work.
I’ve tried to reduce the circuit number by running more of the required gates in a circuit, but that doesn’t help either.
It is just that this code needs many circuits to work, for the given molecule.(more than maximum 300).
I recommend you run the code on a local simulator, since it doesn’t have a circuit limit.
I also came up with this sample, give it a try. It uses code from the tutorial “A brief overview of VQE”, from the quantum chemistry tutorials in Pennylane.
It runs for h2, but also exceeds the limit for h3+. (it takes 318 circuits)
But you can do well with a smaller molecule.
Lastly, if you want to execute on a real quantum computer (ibm_kyoto), it will take about 20 seconds per job, so beware not to expend all your time.
from jax import numpy as np
import jax
jax.config.update("jax_platform_name", "cpu")
jax.config.update('jax_enable_x64', True)
!pip install --upgrade pennylane
!pip install pennylane-qiskit
import pennylane as qml
#choose a molecule from the dataset
dataset = qml.data.load('qchem', molname="H2")[0]
H, qubits = dataset.hamiltonian, len(dataset.hamiltonian.wires)
print("Number of qubits = ", qubits)
print("The Hamiltonian is ", H)
#choose a backend
#dev = qml.device("default.qubit", wires=qubits)
#or
#dev = qml.device('qiskit.ibmq', wires=qubits, backend='ibmq_qasm_simulator', ibmqx_token="")
electrons = 2
hf = qml.qchem.hf_state(electrons, qubits)
print(hf)
@qml.qnode(dev)
def circuit(param, wires):
qml.BasisState(hf, wires=wires)
qml.DoubleExcitation(param, wires=[0, 1, 2, 3])
return qml.expval(H)
def cost_fn(param):
return circuit(param, wires=range(qubits))
import optax
max_iterations = 20
conv_tol = 1e-06
opt = optax.sgd(learning_rate=0.4)
theta = np.array(0.)
# store the values of the cost function
energy = [cost_fn(theta)]
# store the values of the circuit parameter
angle = [theta]
opt_state = opt.init(theta)
for n in range(max_iterations):
gradient = jax.grad(cost_fn)(theta)
updates, opt_state = opt.update(gradient, opt_state)
theta = optax.apply_updates(theta, updates)
angle.append(theta)
energy.append(cost_fn(theta))
conv = np.abs(energy[-1] - energy[-2])
if n % 2 == 0:
print(f"Step = {n}, Energy = {energy[-1]:.8f} Ha")
if conv <= conv_tol:
break
print("\n" f"Final value of the ground-state energy = {energy[-1]:.8f} Ha")
print("\n" f"Optimal value of the circuit parameter = {angle[-1]:.4f}")
import matplotlib.pyplot as plt
fig = plt.figure()
fig.set_figheight(5)
fig.set_figwidth(12)
# Full configuration interaction (FCI) energy computed classically
E_fci = -1.136189454088
# Add energy plot on column 1
ax1 = fig.add_subplot(121)
ax1.plot(range(n + 2), energy, "go", ls="dashed")
ax1.plot(range(n + 2), np.full(n + 2, E_fci), color="red")
ax1.set_xlabel("Optimization step", fontsize=13)
ax1.set_ylabel("Energy (Hartree)", fontsize=13)
ax1.text(0.5, -1.1176, r"$E_\mathrm{HF}$", fontsize=15)
ax1.text(0, -1.1357, r"$E_\mathrm{FCI}$", fontsize=15)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
# Add angle plot on column 2
ax2 = fig.add_subplot(122)
ax2.plot(range(n + 2), angle, "go", ls="dashed")
ax2.set_xlabel("Optimization step", fontsize=13)
ax2.set_ylabel("Gate parameter $\\theta$ (rad)", fontsize=13)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)
plt.subplots_adjust(wspace=0.3, bottom=0.2)
plt.show()