OpenQAOA Qiskit API Reference
IBM Cloud Devices
- class openqaoa_qiskit.backends.devices.DeviceQiskit(device_name, hub=None, group=None, project=None, as_emulator=False)[source]
Contains the required information and methods needed to access remote qiskit QPUs.
- available_qpus
When connection to a provider is established, this attribute contains a list of backend names which can be used to access the selected backend by reinitialising the Access Object with the name of the available backend as input to the device_name parameter.
- Type:
list
- n_qubits
The maximum number of qubits available for the selected backend. Only available if check_connection method is executed and a connection to the qpu and provider is established.
- 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 backend was specified in initialisation of object, just runs a test connection without a specific backend. If backend was specified, checks if connection to that backend can be established.
- Returns:
True if successfully connected to IBMQ or IBMQ and the QPU backend if it was specified. False if unable to connect to IBMQ or failure in the attempt to connect to the specified backend.
- Return type:
Qiskit Local Backends
- class openqaoa_qiskit.backends.qaoa_qiskit_sim.QAOAQiskitBackendShotBasedSimulator(qaoa_descriptor, n_shots, prepend_state, append_state, init_hadamard, cvar_alpha, qiskit_simulation_method='automatic', seed_simulator=None, noise_model=None)[source]
Local Shot-based simulators offered by Qiskit
- Parameters:
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.
seed_simulator (int) – Pseudorandom list of numbers of a seed
prepend_state (QuantumCircuit) – The state prepended to the circuit.
append_state (QuantumCircuit) – 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) – The value of alpha for the CVaR cost function.
qiskit_simulation_method (str) – The method to be used for the simulation.
noise_model (NoiseModel) – The Qiskit noise model to be used for the simulation.
- QISKIT_GATEMAP_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'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RXXGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RYYGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RZZGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RZXGateMap'>]
- 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:
- 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:
- 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]
Returns the counts of the final QAOA circuit after binding angles from variational parameters.
- 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 – The counts of the final QAOA circuit after binding angles from variational parameters.
- Return type:
dict
- 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: qiskit.QuantumCircuit
Creates a parametric QAOA circuit, given the qubit pairs, single qubits with biases, and a set of circuit angles. Note that this function does not actually run the circuit.
- class openqaoa_qiskit.backends.qaoa_qiskit_sim.QAOAQiskitBackendStatevecSimulator(qaoa_descriptor, prepend_state, append_state, init_hadamard, cvar_alpha=1)[source]
Local Statevector-based simulators offered by Qiskit
- Parameters:
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 (np.ndarray or QuantumCircuit) – The state prepended to the circuit.
append_state (QuantumCircuit or np.ndarray) – 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) – The value of alpha for the CVaR cost function.
- QISKIT_GATEMAP_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'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RXXGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RYYGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RZZGateMap'>, <class 'openqaoa.qaoa_components.ansatz_constructor.gatemap.RZXGateMap'>]
- 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:
- 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:
- 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:
- 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: qiskit.QuantumCircuit
Creates a parametric QAOA circuit, 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 – Object of type QAOAVariationalBaseParams
- 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:
- qaoa_circuit(params)[source]
The final QAOA circuit to be executed on the QPU.
- Parameters:
params (QAOAVariationalBaseParams) –
- Returns:
qaoa_circuit – The final QAOA circuit after binding angles from variational parameters.
- Return type:
QuantumCircuit
- property qiskit_cost_hamiltonian
The qiskit cost hamiltonian for the QAOA circuit represented as a SparsePauliOp object.
- reset_circuit()[source]
Reset self.circuit after performing a computation
- TODO: Check if only the first instruction is needed. Two might
be redundant
- 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
IBM Cloud Backend
- class openqaoa_qiskit.backends.qaoa_qiskit_qpu.QAOAQiskitQPUBackend(qaoa_descriptor, device, n_shots, prepend_state, append_state, init_hadamard, initial_qubit_mapping=None, qiskit_optimization_level=1, cvar_alpha=1)[source]
A QAOA simulator as well as for real QPU using qiskit as the backend
- Parameters:
device (DeviceQiskit) – An object of the class
DeviceQiskit
which contains the credentials for accessing the QPU via cloud and the name of the device.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 (QuantumCircuit) – The state prepended to the circuit.
append_state (QuantumCircuit) – 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) – The value of alpha for the CVaR method.
- 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:
- 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:
- circuit_to_qasm(params)[source]
A method to convert the entire QAOA QuantumCircuit object into a OpenQASM string
- Return type:
- 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
- Return type:
- Returns:
A dictionary with the bitstring as the key and the number of counts as its value.
- 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: qiskit.QuantumCircuit
Creates a parametric QAOA circuit, 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 – Object of type QAOAVariationalBaseParams