CustomVQA
CustomVQA
Custom Variational Quantum Algorithm
CustomVQA lets you optimize any parameterized quantum circuit by wrapping a PennyLane QuantumScript or Qiskit QuantumCircuit. This is useful when you have a custom ansatz or problem-specific circuit that doesn’t fit neatly into VQE or QAOA.
PennyLane QuantumScript
import pennylane as qml
from divi.qprog import CustomVQA
from divi.qprog.optimizers import ScipyOptimizer, ScipyMethod
from divi import ParallelSimulator
# Build a parameterized QuantumScript
with qml.queuing.AnnotatedQueue() as q:
qml.RX(0.5, wires=0)
qml.RY(0.3, wires=1)
qml.CNOT(wires=[0, 1])
qml.RZ(0.1, wires=0)
qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
tape = qml.tape.QuantumScript.from_queue(q)
vqa = CustomVQA(
qscript=tape,
optimizer=ScipyOptimizer(ScipyMethod.COBYLA),
max_iterations=50,
backend=ParallelSimulator(),
)
vqa.run()
print(f"Optimized energy: {min(vqa.losses[-1].values()):.4f}")Qiskit QuantumCircuit
You can also pass a Qiskit QuantumCircuit with parameters. Divi will automatically convert it to a PennyLane script:
from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
theta = Parameter("θ")
phi = Parameter("φ")
qc = QuantumCircuit(2)
qc.rx(theta, 0)
qc.ry(phi, 1)
qc.cx(0, 1)
vqa = CustomVQA(
qscript=qc,
optimizer=ScipyOptimizer(ScipyMethod.NELDER_MEAD),
max_iterations=30,
backend=ParallelSimulator(),
)
vqa.run()Parameter Shape
By default, CustomVQA infers the parameter shape from the circuit’s trainable parameters. You can override this with the param_shape argument:
vqa = CustomVQA(
qscript=tape,
param_shape=(3,), # Explicit shape
...
)When to Use CustomVQA
- You have a custom circuit design that doesn’t map to VQE or QAOA
- You want to benchmark a specific ansatz against a known Hamiltonian
- You’re prototyping a new variational algorithm
- You want to optimize a circuit from a different framework (e.g., Qiskit) using Divi’s parallel execution