VQEProblem#
- class VQEProblem(hamiltonian, ansatz_function, num_params, init_function=None, callback=False)[source]#
Central structure to facilitate treatment of VQE problems. This class encapsulates the Hamiltonian, the ansatz, and the initial state preparation function for a specific VQE problem instance.
- Parameters:
- hamiltonianQubitOperator or FermionicOperator
The problem Hamiltonian.
- ansatz_functionfunction
A function receiving a QuantumVariable or QuantumArray and a parameter list. This function implements the unitary corresponding to one layer of the ansatz.
- num_paramsint
The number of parameters per layer.
- init_functionfunction, optional
A function preparing the initial state. By default, the inital state is the \(\ket{0}\) state.
- callbackbool, optional
If
True
, intermediate results are stored. The default isFalse
.
Examples
For a quick demonstration, we show how to calculate the ground state energy of the \(H_2\) molecule using VQE, as explained here.
from qrisp import * from qrisp.operators.qubit import X,Y,Z # Problem Hamiltonian c = [-0.81054, 0.16614, 0.16892, 0.17218, -0.22573, 0.12091, 0.166145, 0.04523] H = c[0] \ + c[1]*Z(0)*Z(2) \ + c[2]*Z(1)*Z(3) \ + c[3]*(Z(3) + Z(1)) \ + c[4]*(Z(2) + Z(0)) \ + c[5]*(Z(2)*Z(3) + Z(0)*Z(1)) \ + c[6]*(Z(0)*Z(3) + Z(1)*Z(2)) \ + c[7]*(Y(0)*Y(1)*Y(2)*Y(3) + X(0)*X(1)*Y(2)*Y(3) + Y(0)*Y(1)*X(2)*X(3) + X(0)*X(1)*X(2)*X(3)) # Ansatz def ansatz(qv,theta): for i in range(4): ry(theta[i],qv[i]) for i in range(3): cx(qv[i],qv[i+1]) cx(qv[3],qv[0]) from qrisp.vqe.vqe_problem import * vqe = VQEProblem(hamiltonian = H, ansatz_function = ansatz, num_params=4, callback=True) energy = vqe.run(qarg = QuantumVariable(4), depth = 1, max_iter=50) print(energy) # Yields -1.864179046
Note that for comparing to the results in the aforementioned paper, we have to add the nuclear repulsion energy \(E_{\text{nuc}}=0.72\) to the calculated electronic energy \(E_{\text{el}}\).
We visualize the optimization process:
>>> vqe.visualize_energy(exact=True)
Methods#
|
Set the initial state preparation function for the VQE problem. |
|
Run the specific VQE problem instance with given quantum arguments, depth of VQE circuit, measurement keyword arguments (mes_kwargs) and maximum iterations for optimization (max_iter). |
|
This function allows for training of a circuit with a given instance of a |
|
Compiles the circuit that is evaluated by the |
|
This method enables convenient data collection regarding performance of the implementation. |
|
Visualizes the energy during the optimization process. |