# Strangeworks QAOA

The `strangeworks-qaoa`

package allows users to run construct and solve problems with the QAOA algorithm on multiple hardware providers.

## Overviewâ€‹

The QAOA algorithm allows you to solve quadratic unconstrained binary optimization (QUBO) problems by making use of the power of quantum resources. It is a classical-quantum hybrid algorithm: the general idea is that we propose a quantum ansatz circuit with some variational parameters as the coefficients for some of the quantum gates. We then apply this circuit to our qubits, measure the output and repeat many times in order to build up the required statistics. We take these measurement results and compute the cost function with our defined problem. We then hand the algorithm over to a classical CPU which optimizes the circuit parameters before passing back to the quantum side to re-measure the cost function but now for the new parameterized quantum circuit. The algorithm then iterates back and forth between the application of the quantum circuit and the classical optimization of the circuit parameters before eventually converging on a solution with the lowest cost.

This page will describe how to install and begin to use the QAOA service as well as giving some technical details of the algorithm. â€‹

## Prerequisitesâ€‹

In order to use the AWS Hybrid job service and/or the IBM Cloud devices, the user must have a billing account properly enabled. Without this, the user can still utilize qiskit runtime jobs through IBMs free services. â€‹

## Installationâ€‹

To get started, make sure you have **Python 3.9 or above** (installation) and are familiar with setting up and using virtual environments.

Install packages using pip: â€‹

`pip install strangeworks`

pip install strangeworks-qaoa

â€‹ Import package into python: â€‹

`from strangeworks_qaoa.sdk import StrangeworksQAOA`

â€‹

### Authenticationâ€‹

Any issues authenticating, you may need to add a QAOA resource to your account through the Platform, https://portal.strangeworks.com â€‹

## Usageâ€‹

Before running:

- Set up your environment and install
`strangeworks-qaoa`

and`strangeworks`

- In the portal, Activate Strangeworks QAOA Service to create a Resource
- Replace
`your-api-key`

with your key from the Portal homepage - Use examples below to utilize the various functionality. See Examples section below for an example script.

Authenticate with strangeworks python sdk using users API token:

`strangeworks.authenticate(api_key="your-api-key")`

â€‹ Get QAOA resource using qaoa sdk extension and users resource slug:

`sw_qaoa = StrangeworksQAOA(resource_slug=" "))`

â€‹ List compatable backends for the qaoa product:

`sw_qaoa.backends()`

â€‹ List all user qaoa jobs:

`jobs = sw_qaoa.job_list(update_status=True)`

â€‹ Run problem:

`sw_job = sw_qaoa.run(backend, problem, problem_params)`

â€‹ Retrieve job with specific slug:

`sw_job = strangeworks.jobs(slug=slug)[0]`

â€‹ Check the status of job and update

`status = sw_qaoa.update_status(sw_job)`

Display results

`result = sw_qaoa.get_results(sw_job,calculate_exact_sol=True)`

## Examplesâ€‹

Here is an example run for a small problem on an IBM simulator:

`import strangeworks`

from strangeworks_qaoa.sdk import StrangeworksQAOA

import strangeworks_qaoa.utils as utils

strangeworks.authenticate(api_key=" ", store_credentials=False)

sw_qaoa = StrangeworksQAOA(resource_slug=" "))

################################

######## Create problem from QUBO

nodes = 4

seed = 0

n = 3

problem = utils.get_nReg_MaxCut_QUBO(n, nodes, seed)

maxiter = 50

shotsin = 1000

theta0 = [1.0, 1.0]

p = 1

alpha = 0.1

optimizer = "COBYLA"

ansatz = "qaoa_strangeworks"

problem_params = {

"maxiter": maxiter,

"shotsin": shotsin,

"theta0": theta0, # optional

"p": p, # optional

"alpha": alpha, # optional

"optimizer": optimizer, # optional

"ansatz": ansatz, # optional

}

backend = "ibmq_qasm_simulator"

sw_job = sw_qaoa.run(backend, problem, problem_params)

result = sw_qaoa.get_results(sw_job,calculate_exact_sol=True)

ðŸ¥³ Success! You may view your job in the portal.

ðŸ˜… Something went wrong? Find us in Slack!

## Algorithm Detailsâ€‹

The QAOA algorithm solves quadratic unconstrained binary optimization (QUBO) problems with binary variables $x_n = \{0, 1 \}$, such as one with the (general) cost function,

Typically, upon mapping to a quantum problem so that it can be solved with qubits on a quantum computer, we transform the problem from $x_n = \{0, 1 \}$ variables to a new set of variables, $z_n = \{-1, 1 \}$ transforming the cost function into a Quantum Ising Hamiltonian. However, in this service we have omitted this step. Note that this does not change the validity or performance of the algorithm and in both cases the optimal solution is the same - this choice only changes the value of the cost function (the "energy") that is outputed.

Thus, when defining a QUBO problem to be inputted into the service, it is this problem that is directly minimised and the value of the cost function displayed. To reiterate, it is the QUBO that is optimized not the Ising Hamiltonian.

## Troubleshootingâ€‹

This section provides troubleshooting tips for the product or service this page covers. â€‹ â€‹ â€‹

## Further Readingâ€‹

This section provides links to other pages in the documentation that are relevant to the product or service this page covers to drive further exploration and engagement.

[1] - More info on the QAOA algorithm: https://qiskit.org/textbook/ch-applications/qaoa.html

[2] - A list of problems that can be solved with QAOA: https://blog.xa0.de/post/List-of-QUBO-formulations/