QAOA cost functions¶
As with VQE, we also provide two different cost functions for QAOA that derive from their VQE counterparts: one for the WavefunctionSimulator, and one for the QVM that can also be run on Rigetti’s QPU.
On the Wavefunction Simulator¶
-
class
qaoa.cost_function.
QAOACostFunctionOnWFSim
(hamiltonian, params, sim=None, scalar_cost_function=True, nshots=0, enable_logging=False, initial_state=None, qubit_mapping=None)¶ A cost function that inherits from PrepareAndMeasureOnWFSim and implements the specifics of QAOA
- Parameters
hamiltonian (
PauliSum
) – The cost hamiltonianparams (
Type
[AbstractParams
]) – Form of the QAOA parameters (with n_steps and type fixed for this instance)sim (
Optional
[WavefunctionSimulator
]) – connection to the WavefunctionSimulator to run the simulation on Automatically creates one, 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.log – List to keep log of function calls
initial_state (
Optional
[Program
]) – A Program to run for state preparation. Defaults to applying a Hadamard on each qubit (all plust state).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.
-
__call__
(params, nshots=None)¶ Cost function that computes <psi|ham|psi> with psi prepared with prepare_ansatz(params).
- Parameters
params – 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
,ndarray
]) – _Raw_(!) QAOA parameters for the state preparation. Can be obtained from Type[AbstractParams] objects via.raw()
- Returns
The wavefunction prepared with raw QAOA parameters
params
- Return type
Wavefunction
On the QVM / QPU¶
-
class
qaoa.cost_function.
QAOACostFunctionOnQVM
(hamiltonian, params, qvm, scalar_cost_function=True, nshots=1, base_numshots=100, enable_logging=False, initial_state=None, qubit_mapping=None)¶ A cost function that inherits from PrepareAndMeasureOnQVM and implements the specifics of QAOA
- Parameters
hamiltonian (
PauliSum
) – The cost hamiltonianparams (
Type
[AbstractParams
]) – Form of the QAOA parameters (with n_steps and type fixed for this instance)qvm (
Union
[QuantumComputer
,str
]) – Connection the QC to run the program on OR a name string like expected bypyquil.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 ofbase_numshots
for each estimation of the expectation value. Defaults to 1base_numshots (
int
) –- numshots multiplier to compile into the binary. The argument nshots of
__call__ is then a multplier of this.
log – List to keep log of function calls
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.
-
__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)