OpenQAOA Pyquil API Reference

Pyquil QCS Devices and QVMs

class openqaoa_pyquil.backends.devices.DevicePyquil(device_name, as_qvm=None, noisy=None, compiler_timeout=20.0, execution_timeout=20.0, client_configuration=None, endpoint_id=None, engagement_manager=None)[source]

Contains the required information and methods needed to access remote Rigetti QPUs via Pyquil.

n_qubits

The maximum number of qubits available for the selected backend. Available upon proper initialisation of the class.

Type:

int

check_connection()[source]

This method should allow a user to easily check if the credentials provided to access the remote QPU is valid.

If no device was specified in initialisation of object, just runs a test connection without a specific device. If device was specified, checks if connection to that device can be established.

TODO : Accessing Rigetti’s QCS is currently unsupported, so this part is empty until that is figured out.

Return type:

bool

connectivity()[source]

obtain the device connectivity as a list of qubit pairs

Return type:

List[List[int]]

Returns:

List[List[int]]

Pyquil Local Backends

class openqaoa_pyquil.backends.qaoa_pyquil_sim.QAOAPyQuilWavefunctionSimulatorBackend(qaoa_descriptor, prepend_state=None, append_state=None, init_hadamard=True, cvar_alpha=1)[source]

A local Wavefunction simulator backend for the PyQuil service provider

PYQUIL_ROTATIONGATES_LIBRARY = [<class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RXGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RYGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RZGateMap'>]
assign_angles(params)

Assigns the angle values of the variational parameters to the circuit gates specified as a list of gates in the abstract_circuit.

Parameters:

params (QAOAVariationalBaseParams) – The variational parameters(angles) to be assigned to the circuit gates

Return type:

None

bitstring_energy(bitstring)

Computes the energy of a given bitstring with respect to the cost Hamiltonian.

Parameters:

bitstring (Union[List[int],str]) – A list of integers 0 and 1 of length n_qubits representing a configuration.

Returns:

The energy of a given bitstring with respect to the cost Hamiltonian.

Return type:

float

circuit_to_qasm()[source]

Implement a method to construct a QASM string from the current state of the QuantumCircuit for the backends

property exact_solution

Computes exactly the minimum energy of the cost function and its corresponding configuration of variables using standard numpy module.

Returns:

(energy, config)

  • The minimum eigenvalue of the cost Hamiltonian,

  • The minimum energy eigenvector as a binary array configuration: qubit-0 as the first element in the sequence

Return type:

Tuple[float, list]

expectation(**kwargs)

Call the execute function on the circuit to compute the expectation value of the Quantum Circuit w.r.t cost operator

expectation_w_uncertainty(**kwargs)

Call the execute function on the circuit to compute the expectation value of the Quantum Circuit w.r.t cost operator along with its uncertainty

get_counts(params, n_shots)

Measurement outcome vs frequency information from a circuit execution represented as a python dictionary

Parameters:
  • params (VariationalBaseParams) – The QAOA parameters as a 1D array (derived from an object of one of the parameter classes, containing hyperparameters and variable parameters).

  • n_shots (int) – The number of measurement shots required; specified as integer

Returns:

A dictionary of measurement outcomes vs frequency sampled from a statevector

Return type:

Dict[str, float]

obtain_angles_for_pauli_list(input_gate_list, params)

This method uses the pauli gate list information to obtain the pauli angles from the VariationalBaseParams object. The floats in the list are in the order of the input GateMaps list.

Parameters:
  • input_gate_list (List[GateMap]) – The GateMap list including rotation gates

  • params (QAOAVariationalBaseParams) – The variational parameters(angles) to be assigned to the circuit gates

Returns:

angles_list – The list of angles in the order of gates in the GateMap list

Return type:

List[float]

probability_dict(params)

Get the counts style probability dictionary with all basis states and their corresponding probabilities. Constructed using the complete statevector

Parameters:

params (QAOAVariationalBaseParams) – The QAOA parameters as a 1D array (derived from an object of one of the parameter classes, containing hyperparameters and variable parameters).

Returns:

A dictionary of all basis states and their corresponding probabilities.

Return type:

Dict[str, float]

qaoa_circuit(params)[source]

Creates a QAOA circuit (pyquil.Program object), given the qubit pairs, single qubits with biases, and a set of circuit angles. Note that this function does not actually run the circuit. To do this, you will need to subsequently execute the command self.eng.flush().

Parameters:

params (QAOAVariationalBaseParams) –

Returns:

A pyquil.Program object.

Return type:

pyquil.Program

reset_circuit()[source]

Reset the circuit attribute

sample_from_wavefunction(params, n_samples)

Get the shot-based measurement results from the statevector. The return object is a list of shot-results.

Parameters:
  • params (QAOAVariationalBaseParams) – The QAOA parameters as a 1D array (derived from an object of one of the parameter classes, containing hyperparameters and variable parameters).

  • n_samples (int) – The number of measurement samples required; specified as integer

Returns:

A list of measurement outcomes sampled from a statevector

Return type:

np.ndarray

wavefunction(params)[source]

Get the wavefunction of the state produced by the parametric circuit.

Parameters:

params (QAOAVariationalBaseParams) – The QAOA parameters - an object of one of the parameter classes, containing the variational parameters (angles).

Returns:

wf – pyquil Wavefunction object.

Return type:

List[complex]

Pyquil Cloud Backend

class openqaoa_pyquil.backends.qaoa_pyquil_qpu.QAOAPyQuilQPUBackend(device, qaoa_descriptor, n_shots, prepend_state, append_state, init_hadamard, cvar_alpha, active_reset=False, rewiring='', initial_qubit_mapping=None)[source]

A QAOA backend object for real Rigetti QPUs

Parameters:
  • device (DevicePyquil) – The device object to access pyquil devices with credentials.

  • qaoa_descriptor (QAOADescriptor) – An object of the class QAOADescriptor which contains information on circuit construction and depth of the circuit.

  • n_shots (int) – The number of shots to be taken for each circuit.

  • prepend_state (pyquil.Program) – The state prepended to the circuit.

  • append_state (pyquil.Program) – The state appended to the circuit.

  • init_hadamard (bool) – Whether to apply a Hadamard gate to the beginning of the QAOA part of the circuit.

  • cvar_alpha (float) – Conditional Value-at-Risk (CVaR) – a measure that takes into account only the tail of the probability distribution arising from the circut’s count dictionary. Must be between 0 and 1. Check https://arxiv.org/abs/1907.04769 for further details.

  • active_reset (bool) – Whether to use the pyQuil’s active reset scheme to reset qubits between shots.

  • rewiring (str) – Rewiring scheme to be used for Pyquil. Either PRAGMA INITIAL_REWIRING “NAIVE” or PRAGMA INITIAL_REWIRING “PARTIAL”. If None, pyquil defaults according to: NAIVE: The qubits used in all instructions in the program satisfy the topological constraints of the device. PARTIAL: Otherwise.

assign_angles(params)

Assigns the angle values of the variational parameters to the circuit gates specified as a list of gates in the abstract_circuit.

Parameters:

params (QAOAVariationalBaseParams) – The variational parameters(angles) to be assigned to the circuit gates

Return type:

None

bitstring_energy(bitstring)

Computes the energy of a given bitstring with respect to the cost Hamiltonian.

Parameters:

bitstring (Union[List[int],str]) – A list of integers 0 and 1 of length n_qubits representing a configuration.

Returns:

The energy of a given bitstring with respect to the cost Hamiltonian.

Return type:

float

circuit_to_qasm(params)[source]

A method to convert the pyQuil program to a OpenQASM string.

Return type:

str

property exact_solution

Computes exactly the minimum energy of the cost function and its corresponding configuration of variables using standard numpy module.

Returns:

(energy, config)

  • The minimum eigenvalue of the cost Hamiltonian,

  • The minimum energy eigenvector as a binary array configuration: qubit-0 as the first element in the sequence

Return type:

Tuple[float, list]

expectation(**kwargs)

Call the execute function on the circuit to compute the expectation value of the Quantum Circuit w.r.t cost operator

expectation_w_uncertainty(**kwargs)

Call the execute function on the circuit to compute the expectation value of the Quantum Circuit w.r.t cost operator along with its uncertainty

get_counts(params, n_shots=None)[source]

Execute the circuit and obtain the counts.

Parameters:
  • params (QAOAVariationalBaseParams) – The QAOA parameters - an object of one of the parameter classes, containing variable parameters.

  • n_shots (int) – The number of times to run the circuit. If None, n_shots is set to the default: self.n_shots

Returns:

counts – A dictionary with the bitstring as the key and the number of counts as its value.

Return type:

dictionary

obtain_angles_for_pauli_list(input_gate_list, params)

This method uses the pauli gate list information to obtain the pauli angles from the VariationalBaseParams object. The floats in the list are in the order of the input GateMaps list.

Parameters:
  • input_gate_list (List[GateMap]) – The GateMap list including rotation gates

  • params (QAOAVariationalBaseParams) – The variational parameters(angles) to be assigned to the circuit gates

Returns:

angles_list – The list of angles in the order of gates in the GateMap list

Return type:

List[float]

property parametric_qaoa_circuit: pyquil.Program

Creates a parametric QAOA circuit (pyquil.Program object), given the qubit pairs, single qubits with biases, and a set of circuit angles. Note that this function does not actually run the circuit.

Parameters:

params (QAOAVariationalBaseParams) –

Returns:

A pyquil.Program object.

Return type:

pyquil.Program

qaoa_circuit(params)[source]

Injects angles into created executable parametric circuit.

Parameters:

params (QAOAVariationalBaseParams) –

Returns:

A pyquil.Program (executable) object.

Return type:

pyquil.Program

reset_circuit()[source]

Reset self.program after performing a computation. Also handle active reset and rewirings.