# VQE cost functions¶

We provide two different cost functions for VQE. One for PyQuil’s WavefunctionSimulator, and one for Rigetti’s QVM that can also be run on the QPU.

## On the Wavefunction Simulator¶

class vqe.cost_function.PrepareAndMeasureOnWFSim(prepare_ansatz, make_memory_map, hamiltonian, sim=None, scalar_cost_function=True, nshots=0, enable_logging=False, qubit_mapping=None, hamiltonian_is_diagonal=False)

A cost function that prepares an ansatz and measures its energy w.r.t hamiltonian on the qvm

Parameters
• prepare_ansatz (param) – A parametric pyquil program for the state preparation

• make_memory_map (param) – A function that creates a memory map from the array of parameters

• hamiltonian (PauliSum) – The hamiltonian with respect to which to measure the energy.

• sim (Optional[WavefunctionSimulator]) – A WavefunctionSimulator instance to get the wavefunction from. Automatically created, if None is passed.

• scalar_cost_function (bool) – If True: __call__ returns only the expectation value If False: __call__ returns a tuple (exp_val, std_dev) Defaults to True.

• nshots (int) – Number of shots to assume to simulate the sampling noise. 0 corresponds to no sampling noise added and is the default.

• enable_logging (bool) – If true, a log is created which contains the parameter and function values at each function call. It is a list of namedtuples of the form (“x”, “fun”)

• qubit_mapping (Optional[Dict[QubitPlaceholder, Union[Qubit, int]]]) – A mapping to fix QubitPlaceholders to physical qubits. E.g. pyquil.quil.get_default_qubit_mapping(program) gives you on.

• hamiltonian_is_diagonal (bool) – Pass true, if the hamiltonian contains only Z terms and products thereof. This speeds up __init__ considerably. Defaults to False.

__call__(params, nshots=None)

Cost function that computes <psi|ham|psi> with psi prepared with prepare_ansatz(params).

Parameters
• params (Union[list, ndarray]) – Parameters of the state preparation circuit.

• nshots (Optional[int]) – Overrides nshots from __init__ if passed

Returns

Either only the cost or a tuple of the cost and the standard deviation estimate based on the samples.

Return type

float or tuple (cost, cost_stdev)

get_wavefunction(params)

Same as __call__ but returns the wavefunction instead of cost

Parameters

params (Union[List[~T], ndarray]) – Parameters of the state preparation circuit

Returns

The wavefunction prepared with parameters params

Return type

Wavefunction

## On the QVM / QPU¶

class vqe.cost_function.PrepareAndMeasureOnQVM(prepare_ansatz, make_memory_map, hamiltonian, qvm, scalar_cost_function=True, nshots=1, base_numshots=100, qubit_mapping=None, enable_logging=False, hamiltonian_is_diagonal=False)

A cost function that prepares an ansatz and measures its energy w.r.t hamiltonian on a quantum computer (or simulator).

This cost_function makes use of pyquils parametric circuits and thus has to be supplied with a parametric circuit and a function to create memory maps that can be passed to qvm.run.

Parameters
• prepare_ansatz (Program) – A parametric pyquil program for the state preparation

• make_memory_map (Callable[[Iterable[+T_co]], dict]) – A function that creates a memory map from the array of parameters

• hamiltonian (PauliSum) – The hamiltonian

• qvm (Union[QuantumComputer, str]) – Connection the QC to run the program on OR a name string like expected by pyquil.api.get_qc

• scalar_cost_function (bool) – If True: __call__ returns only the expectation value If False: __call__ returns a tuple (exp_val, std_dev) Defaults to True.

• nshots (int) – Fixed multiple of base_numshots for each estimation of the expectation value. Defaults to 1

• base_numshots (int) –

numshots multiplier to compile into the binary. The argument nshots of

__call__ is then a multplier of this.

• qubit_mapping (Optional[Dict[QubitPlaceholder, Union[Qubit, int]]]) – A mapping to fix all QubitPlaceholders to physical qubits. E.g. pyquil.quil.get_default_qubit_mapping(program) gives you on.

• enable_logging (bool) – If true, a log is created which contains the parameter and function values at each function call. It is a list of namedtuples of the form (“x”, “fun”)

__call__(params, nshots=None)
Parameters
• params (param) – the parameters to run the state preparation circuit with

• nshots (param) – Overrides nshots in __init__ if passed

Returns

Either only the cost or a tuple of the cost and the standard deviation estimate based on the samples.

Return type

float or tuple (cost, cost_stdev)