QAOA
The Quantum Approximate Optimization Algorithm (QAOA) is designed to solve quadratic unconstrained binary optimization (QUBO) problems by leveraging quantum resources. As a hybrid classical-quantum algorithm, QAOA combines the strengths of both classical and quantum computing. The algorithm involves applying a quantum circuit with variational parameters to qubits, measuring the qubits, and repeating the process to gather statistics. These results are then used to compute a cost function, which is optimized by a classical CPU. This iterative process continues, alternating between quantum measurements and classical optimization, until the lowest cost solution is found.
QAOA addresses QUBO problems with binary variables , characterized by the general cost function:
In typical scenarios, to map the problem to a quantum format suitable for qubits, the binary variables are transformed into a new set of variables, , converting the cost function into a Quantum Ising Hamiltonian. However, in this service, this transformation step is omitted unless the 'ising' field in the problem parameter dictionary is set to True. This omission does not affect the validity or performance of the algorithm; it only changes the value of the cost function (the "energy") that is outputted. In both cases, the optimal solution remains the same.
When defining a QUBO problem for input into the service, the problem is directly minimized, and the value of the cost function is displayed. By default, when 'ising'=False, the QUBO is optimized rather than the Ising Hamiltonian.
strangeworks-qaoa
The strangeworks-qaoa
package allows users to run construct and solve problems with the QAOA algorithm on multiple hardware providers.
In order to submit quantum jobs through the Strangeworks Platform, you must have a Quantum Resources enabled in your workspace.
Installation
To get started, make sure you have Python 3.10 or 3.11 (installation) and are familiar with setting up and using virtual environments.
Install packages using pip:
pip install -U pip && pip install strangeworks-qaoa
Authentication
First, authenticate via the Strangeworks SDK with your api-key taken from the Portal homepage:
import strangeworks as sw
from strangeworks_qaoa.sdk import StrangeworksQAOA
sw.authenticate(api_key)
Backends
List compatible backends for the QAOA product:
sw_qaoa = StrangeworksQAOA()
sw_qaoa.backends()
You will need to have the particular Quantum Resources enabled in your workspace to run on those backends.
Company | Processor | Qubits | Framework | Processors |
---|---|---|---|---|
QuEra | Aquila | 256 | Braket | braket.aquila , arn:aws:braket:us-east-1::device/qpu/quera/Aquila |
IBM | Heron r2 | 156 | Qiskit | ibm_fez |
IBM | Heron r1 | 133 | Qiskit | ibm_torino |
IBM | Eagle r3 | 127 | Qiskit | ibm_brisbane , ibm_kyiv , ibm_sherbrooke |
Quantinuum | H2-1 | 56 | Azure | quantinuum.qpu.h2-1 |
Quantinuum | H1-1 | 20 | Azure | quantinuum.qpu.h1-1 |
IonQ | Aria | 25 | Azure | ionq.qpu.aria-1 , ionq.qpu.aria-2 |
IonQ | Aria | 25 | Braket | arn:aws:braket:us-east-1::device/qpu/ionq/Aria-1 , arn:aws:braket:us-east-1::device/qpu/ionq/Aria-2 |
IonQ | Forte 1 | 25 | Braket | arn:aws:braket:us-east-1::device/qpu/ionq/Forte-1 |
IQM | Garnet | 20 | Braket | arn:aws:braket:eu-north-1::device/qpu/iqm/Garnet |
Rigetti | Simulator | 9 | Rigetti | 9q-square-qvm |
Disclaimer: This list of backends is subject to change at any time.
Problems
NetworkX
import strangeworks_qaoa.utils as utils
nodes = 4
seed = 0
n = 3
problem = utils.get_nReg_MaxCut_graph(n, nodes, seed)
QUBO
import strangeworks_qaoa.utils as utils
nodes = 4
seed = 0
n = 3
problem = utils.get_nReg_MaxCut_QUBO(n, nodes, seed)
BQM
from dimod import BinaryQuadraticModel
import dimod
linear = {1: -2, 2: -2, 3: -3, 4: -3, 5: -2}
quadratic = {(1, 2): 2, (1, 3): 2, (2, 4): 2, (3, 4): 2, (3, 5): 2, (4, 5): 2}
bqm = BinaryQuadraticModel(linear, quadratic,dimod.Vartype.BINARY)
problem = {"BQM": bqm,
"variable_order": bqm.variables, # Can specify custom variable order. This will correspond to the solution bitstring.
}
Jobs
Run
sw_qaoa = StrangeworksQAOA()
sw_job = sw_qaoa.run(backend, problem, problem_params)
Status
sw_qaoa = StrangeworksQAOA()
status = sw_qaoa.update_status(sw_job)
Results
sw_qaoa = StrangeworksQAOA()
result = sw_qaoa.get_results(sw_job,calculate_exact_sol=True, display_results=True)
Retrieve Jobs
Retrieve a job by slug:
sw_job = sw.jobs(slug=slug)[0]
List jobs:
jobs = sw_qaoa.job_list(update_status=True)
Examples
AWS Braket
Here is an example run for a small problem on an AWS Braket simulator:
import strangeworks as sw
from strangeworks_qaoa.sdk import StrangeworksQAOA
import strangeworks_qaoa.utils as utils
sw.authenticate(api_key)
sw_qaoa = StrangeworksQAOA()
################################
######## Create problem from QUBO
nodes = 4
seed = 0
n = 3
problem = utils.get_nReg_MaxCut_QUBO(n, nodes, seed)
################################
################################
######## Define algorithm parameters
problem_params = {
"nqubits": nodes, # Number of variables in the problem / number of qubits
"maxiter": 50, # Maximum number of iterations in algorithm
"shotsin": 1000, # Number of times quantum circuit is measured at each iteration
"p": 1, # Optional Input: Controls the depth of the Quantum Circuit ansatz. Default p=1
"theta0": [1.0, 1.0], # Optional Input: Starting point for variational parameters. If specified must be equal to 2p
"alpha": 0.1, # Optional Input: Cvar parameter, float between 0 and 1 - https://arxiv.org/pdf/1907.04769.pdf. Default alpha=1.0
"optimizer": "COBYLA", # Optional Input: Classical optimizer to use:
# Possible Values:
# 'COBYLA' (Default), 'Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Newton-CG', 'L-BFGS-B', 'TNC',
# 'SLSQP', 'trust-constr', 'dogleg', 'trust-ncg', 'trust-exact', 'trust-krylov'
# Additionals for IBM backends: 'SPSA', 'NFT'
"ising": False, # Optional Input: is the problem an Ising problem or a QUBO problem, i.e. are the values of the variables {-1/2,+1/2} (Ising) or {0,1} (QUBO, Default)
"warm_start": False, # Optional Input: Run warm start qaoa or not - https://arxiv.org/abs/2009.10095. Default False: Standard QAOA ansatz
"qrr": False, # Optional Input: Quantum Relax and Round QAOA algorithm - https://arxiv.org/abs/2307.05821. Default False: Standard QAOA
}
backend = "SV1" # Change this to another ibm backend name or an aws device name
sw_job = sw_qaoa.run(backend, problem, problem_params)
result = sw_qaoa.get_results(sw_job,calculate_exact_sol=True, display_results=True)
🥳 Success! You may view your job in the portal.
😅 Something went wrong? Find us in Slack!
Qiskit Runtime
Here is an example run for a small problem on an Qiskit Runtime simulator:
import strangeworks as sw
from strangeworks_qaoa.sdk import StrangeworksQAOA
import strangeworks_qaoa.utils as utils
sw.authenticate(api_key)
sw_qaoa = StrangeworksQAOA()
################################
######## Create problem from QUBO
nodes = 4
seed = 0
n = 3
problem = utils.get_nReg_MaxCut_QUBO(n, nodes, seed)
################################
################################
######## Define algorithm parameters
problem_params = {
"nqubits": nodes, # Number of variables in the problem / number of qubits
"maxiter": 50, # Maximum number of iterations in algorithm
"shotsin": 1000, # Number of times quantum circuit is measured at each iteration
"p": 1, # Optional Input: Controls the depth of the Quantum Circuit ansatz. Default p=1
"theta0": [1.0, 1.0], # Optional Input: Starting point for variational parameters. If specified must be equal to 2p
"alpha": 0.1, # Optional Input: Cvar parameter, float between 0 and 1 - https://arxiv.org/pdf/1907.04769.pdf. Default alpha=1.0
"optimizer": "COBYLA", # Optional Input: Classical optimizer to use:
# Possible Values:
# 'COBYLA' (Default), 'Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Newton-CG', 'L-BFGS-B', 'TNC',
# 'SLSQP', 'trust-constr', 'dogleg', 'trust-ncg', 'trust-exact', 'trust-krylov'
# Additionals for IBM backends: 'SPSA', 'NFT'
"ising": False, # Optional Input: is the problem an Ising problem or a QUBO problem, i.e. are the values of the variables {-1/2,+1/2} (Ising) or {0,1} (QUBO, Default)
"warm_start": False, # Optional Input: Run warm start qaoa or not - https://arxiv.org/abs/2009.10095. Default False: Standard QAOA ansatz
"qrr": False, # Optional Input: Quantum Relax and Round QAOA algorithm - https://arxiv.org/abs/2307.05821. Default False: Standard QAOA
}
backend = "ibmq_qasm_simulator" # Change this to another ibm backend name or an aws device name
sw_job = sw_qaoa.run(backend, problem, problem_params)
result = sw_qaoa.get_results(sw_job,calculate_exact_sol=True, display_results=True)