Bundle operators in qml.AdaptiveOptimizer()

Hi all,

I am following the example on qml.AdaptiveOptimizer()
and in the example all the excitation operators are individually trainable. I wonder if there is a way to bundle several operator together under one trainable parameter?

Such as

operator_pool = [qml.DoubleExcitation(0.0, [0, 1, 2, 3]) + qml.DoubleExcitation(0.0, [4, 5, 6, 7])]

and there is only one trainable parameter that will be trained in the AdaptiveOptimizer().step_and_cost() function, aka operator_pool[0].num_params = 1

Thanks in advance,
Andrew

Hi @andrewChen , welcome to the forum! Unfortunately I don’t think this will work with AdaptiveOptimizer.

If you just want to have different excitations with the same parameter and optimize the circuit with this parameter I’d recommend a different optimizer such as the AdamOptimizer.

See for example the code below, where I create a circuit with two double excitations which have the same parameter. Is this what you were looking for? Or were you looking for something else? If this is not what you were looking for please feel free to share a bit more context from your problem so that I can understand it better.

# Import your favourite libraries
import pennylane as qml
from pennylane import numpy as pnp

# Get the molecular Hamiltonian
symbols = ["H", "H", "H"]
geometry = pnp.array([[0.01076341, 0.04449877, 0.0],
                     [0.98729513, 1.63059094, 0.0],
                     [1.87262415, -0.00815842, 0.0]], requires_grad=False)
H, qubits = qml.qchem.molecular_hamiltonian(symbols, geometry, charge = 1)

# Get the Hartree-Fock state
n_electrons = 2
hf_state = qml.qchem.hf_state(n_electrons, qubits)

# Create your device
dev = qml.device("default.qubit", wires=qubits)
@qml.qnode(dev)
def circuit(x):
    # Initialize the circuit on the HF state
    qml.BasisState(hf_state, wires=range(qubits))
    # Add the excitations with the same parameter
    qml.DoubleExcitation(x, wires=[0, 1, 2, 3])
    qml.DoubleExcitation(x, wires=[0, 1, 4, 5])
    return qml.expval(H)

# Draw the original circuit
x = pnp.array([0.0],requires_grad=True)
print(qml.draw(circuit)(x))

# Optimize the parameter
opt = qml.AdamOptimizer()

for i in range(10):
    x, prev_cost = opt.step_and_cost(circuit,x)
    delta = pnp.abs(circuit(x)-prev_cost)
    if delta < 1e-3:
      break

# Draw the optimized circuit
print(qml.draw(circuit)(x))

I hope this helps!