# qml.VQECost¶

class VQECost(ansatz, hamiltonian, device, interface='autograd', diff_method='best', **kwargs)[source]

Bases: object

Create a VQE cost function, i.e., a cost function returning the expectation value of a Hamiltonian.

Parameters
• ansatz (callable) –

The ansatz for the circuit before the final measurement step. Note that the ansatz must have the following signature:

ansatz(params, **kwargs)


where params are the trainable weights of the variational circuit, and kwargs are any additional keyword arguments that need to be passed to the template.

• hamiltonian (Hamiltonian) – Hamiltonian operator whose expectation value should be measured

• device (Device, Sequence[Device]) – Corresponding device(s) where the resulting cost function should be executed. This can either be a single device, or a list of devices of length matching the number of terms in the Hamiltonian.

• interface (str, None) – Which interface to use. This affects the types of objects that can be passed to/returned to the cost function. Supports all interfaces supported by the qnode() decorator.

• diff_method (str, None) – The method of differentiation to use with the created cost function. Supports all differentiation methods supported by the qnode() decorator.

Returns

a cost function with signature cost_fn(params, **kwargs) that evaluates the expectation of the Hamiltonian on the provided device(s)

Return type

callable

Example:

First, we create a device and design an ansatz:

dev = qml.device('default.qubit', wires=4)

def ansatz(params, **kwargs):
qml.BasisState(np.array([1, 1, 0, 0]), wires=[0, 1, 2, 3])
for i in range(4):
qml.Rot(*params[i], wires=i)
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[2, 0])
qml.CNOT(wires=[3, 1])


Now we can create the Hamiltonian that defines the VQE problem:

coeffs = [0.2, -0.543]
obs = [
qml.PauliX(0) @ qml.PauliZ(1) @ qml.PauliY(3),
]
H = qml.vqe.Hamiltonian(coeffs, obs)


Alternatively, the generate_hamiltonian() function from the Quantum Chemistry module can be used to generate a molecular Hamiltonian.

Next, we can define the cost function:

>>> cost = qml.VQECost(ansatz, H, dev, interface="torch")
>>> params = torch.rand([4, 3])
>>> cost(params)
tensor(0.0245, dtype=torch.float64)


The cost function can be minimized using any gradient descent-based optimizer.

 __call__(*args, **kwargs) Call self as a function. metric_tensor(args[, kwargs, diag_approx, …]) Evaluate the value of the metric tensor.
__call__(*args, **kwargs)[source]

Call self as a function.

metric_tensor(args, kwargs=None, diag_approx=False, only_construct=False)[source]

Evaluate the value of the metric tensor.

Parameters
• args (tuple[Any]) – positional (differentiable) arguments

• kwargs (dict[str, Any]) – auxiliary arguments

• diag_approx (bool) – iff True, use the diagonal approximation

• only_construct (bool) – Iff True, construct the circuits used for computing the metric tensor but do not execute them, and return None.

Returns

metric tensor

Return type

array[float]