### Install OpenQAOA from source Source: https://openqaoa.entropicalabs.com/install-openqaoa Installs the package locally from the cloned directory with optional modifiers. ```bash pip install . ``` ```bash pip install .[tests] ``` -------------------------------- ### Install OpenQAOA plugins Source: https://openqaoa.entropicalabs.com/install-openqaoa Installs a specific OpenQAOA plugin by replacing 'core' with the desired module name. ```bash pip install openqaoa-core ``` -------------------------------- ### Install OpenQAOA via pip Source: https://openqaoa.entropicalabs.com/install-openqaoa Installs the full OpenQAOA package including all available plugins. ```bash pip install openqaoa ``` -------------------------------- ### Custom QAOA Workflow Setup Source: https://openqaoa.entropicalabs.com/workflows/customise-the-QAOA-workflow Configure a custom QAOA workflow by setting specific circuit, backend, and classical optimizer properties before compiling and optimizing. ```python #Create the QAOA q = QAOA() # circuit properties q.set_circuit_properties(p=3, param_type='standard', init_type='ramp', mixer_hamiltonian='xy') # backend properties q.set_backend_properties(init_hadamard=True, n_shots=8000, cvar_alpha=0.85) # classical optimizer properties q.set_classical_optimizer(method='cobyla', maxiter=50, tol=0.05) q.compile(qubo_problem) q.optimize() ``` -------------------------------- ### Run QAOA with RMSProp Optimizer Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/rmsprop-optimizer Example of how to configure and run the QAOA algorithm using the RMSProp optimizer in OpenQAOA. Ensure the 'problem' variable is defined before compilation. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='rmsprop', jac="finite_difference", optimizer_options=dict( stepsize=0.001, decay=0.9, eps=1e-07 ) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Integrate custom routing into QAOA workflow Source: https://openqaoa.entropicalabs.com/qubit-routing/qaoa-qubit-routing Example of configuring a QAOA workflow to use a custom routing function for an IBMQ device. ```python from openqaoa import QAOA from openqaoa.problem import MaximumCut from openqaoa.backends import create_device #define a networkx graph for which you want to solve Maxcut maxcut = MaximumCut(my_problem_graph).qubo() #define the Quantum Computer to use for running the QAOA IBMQ.load_account() #load your IBMQ api_token credentials = { hub:'ibmq', group:'open', project:'main' } device = create_device(location='ibmq', name='ibmq_xyz', **credentials) #create QAOA workflow and optionally update the properties q = QAOA() q.set_device(device) #compile the circuit q.compile(problem=maxcut, routing_function = my_custom_routing_function) #run the optimization loop q.optimize() ``` -------------------------------- ### Get Problem Instance Details as Dictionary Source: https://openqaoa.entropicalabs.com/problems/minimum-vertex-cover Convert the Minimum Vertex Cover problem instance into a dictionary format. This provides a comprehensive view of the problem's parameters, graph structure, and QUBO terms. ```python > mvc_qubo.asdict() {'constant': 28.0, 'metadata': {}, 'n': 6, 'problem_instance': {'G': {'directed': False, 'graph': {}, 'links': [{'source': 0, 'target': 2}, {'source': 0, 'target': 3}, {'source': 0, 'target': 4}, {'source': 1, 'target': 2}, {'source': 1, 'target': 3}, {'source': 1, 'target': 5}, {'source': 2, 'target': 4}, {'source': 2, 'target': 5}, {'source': 3, 'target': 5}, {'source': 4, 'target': 5}], 'multigraph': False, 'nodes': [{'id': 0}, {'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}, {'id': 5}]}, 'field': 1.0, 'penalty': 10, 'problem_type': 'minimum_vertex_cover'}, 'terms': [[0, 2], [0, 3], [0, 4], [1, 2], [1, 3], [1, 5], [2, 4], [2, 5], [3, 5], [4, 5], [0], [1], [2], [3], [4], [5]], 'weights': [2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 7.0, 7.0, 9.5, 7.0, 7.0, 9.5]} ``` -------------------------------- ### Get Knapsack QUBO as Dictionary Source: https://openqaoa.entropicalabs.com/problems/knapsack Convert the knapsack QUBO problem instance into a dictionary format for detailed inspection. This includes problem parameters and QUBO terms. ```python knapsack_qubo.asdict() ``` -------------------------------- ### Instantiate a QVM device Source: https://openqaoa.entropicalabs.com/devices/rigetti-qvm Use the create_device function to set up a QVM backend for the QAOA object. ```python from openqaoa import QAOA, create_device q_qvm = QAOA() rigetti_args ={ 'as_qvm':True, 'execution_timeout':10, 'compiler_timeout':10 } n_qubits = np_qubo.n rigetti_device = create_device(location='qcs', name=f'{n_qubits}q-qvm', **rigetti_args) q_qvm.set_device(rigetti_device) ``` -------------------------------- ### Create and Set Device Source: https://openqaoa.entropicalabs.com/devices/device Instantiate a device object and set it for the QAOA instance. Replace '***' with actual location and name values. ```python q = QAOA() my_device = create_device(location='***' name='***') q.set_device(my_device) ``` -------------------------------- ### Create a Braket Device in OpenQAOA Source: https://openqaoa.entropicalabs.com/devices/amazon-braket Initialize a Braket device object and assign it to a QAOA instance. ```python from openqaoa import QAOA, create_device q = QAOA() sv1_device = create_device(location='aws', name='arn:aws:braket:::device/quantum-simulator/amazon/sv1', aws_region='us-west-1') q.set_device(sv1_device) ``` -------------------------------- ### Get Number Partition Problem Details as Dictionary Source: https://openqaoa.entropicalabs.com/problems/number-partition Access all details of the Number Partition problem instance, including its configuration and terms, in a dictionary format. ```python > np_qubo.asdict() {'constant': 130, 'metadata': {}, 'n': 5, 'problem_instance': {'n_numbers': 5, 'numbers': [1, 2, 3, 4, 10], 'problem_type': 'number_partition'}, 'terms': [[0, 1], [0, 2], [0, 3], [0, 4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]], 'weights': [4.0, 6.0, 8.0, 20.0, 12.0, 16.0, 40.0, 24.0, 60.0, 80.0]} ``` -------------------------------- ### Define QVM configuration arguments Source: https://openqaoa.entropicalabs.com/devices/rigetti-qvm Reference for available configuration parameters when setting up the Rigetti QVM device. ```python rigetti_args ={ as_qvm:True, noisy: bool = None, compiler_timeout: float = 20.0, # In seconds execution_timeout: float = 20.0, # In seconds client_configuration: QCSClientConfiguration = None, endpoint_id: str = None, engagement_manager: EngagementManager = None } ``` -------------------------------- ### RQAOA Result Object Structure Source: https://openqaoa.entropicalabs.com/workflows/recursive-qaoa Example of the dictionary structure returned by an RQAOA run, containing solution data, elimination history, and intermediate step details. ```python {'solution': {'10001000': -12.0, '01110111': -12.0}, 'classical_output': {'minimum_energy': -3.0, 'optimal_states': ['100', '011']}, 'elimination_rules': [[{'pair': (2, 4), 'correlation': -1.0}], [{'pair': (0, 4), 'correlation': -1.0}], [{'pair': (0, 4), 'correlation': -1.0}], [{'pair': (2, 3), 'correlation': 1.0}], [{'pair': (2, 3), 'correlation': 1.0}]], 'schedule': [1, 1, 1, 1, 1], 'number_steps': 5, 'intermediate_steps': [{'counter': 0, 'problem': , 'qaoa_results': , 'exp_vals_z': array([0., 0., 0., 0., 0., 0., 0., 0.]), 'corr_matrix': array([[ 0. , 0.12081413, -0.2361811 , 0.04850827, 0.29105518, -0.2361811 , -0.12081413, -0.18130702], [ 0. , 0. , 0.18130702, -0.12081413, 0.2361811 , 0.18130702, 0.18130702, 0.12081413], [ 0. , 0. , 0. , 0.2361811 , -0.35154807, 0.29105518, -0.18130702, 0.35154807], [ 0. , 0. , 0. , 0. , -0.29105518, 0.2361811 , 0.12081413, 0.18130702], [ 0. , 0. , 0. , 0. , 0. , -0.2361811 , 0.2361811 , -0.29105518], [ 0. , 0. , 0. , 0. , 0. , 0. , 0.29105518, -0.12081413], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.2361811 ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])}, {'counter': 1, 'problem': , 'qaoa_results': , 'exp_vals_z': array([0., 0., 0., 0., 0., 0., 0.]), 'corr_matrix': array([[ 0. , 0.12149926, -0.16779332, -0.03100176, -0.25414406, -0.03937175, -0.24998213], [ 0. , 0. , 0. , -0.12149926, 0.18034528, 0.18034528, 0.12149926], [ 0. , 0. , 0. , 0.16779332, 0.16779332, -0.19795548, 0.13763115], [ 0. , 0. , 0. , 0. , 0.25414406, 0.03937175, 0.24998213], [ 0. , 0. , 0. , 0. , 0. , 0.1050155 , -0.03937175], [ 0. , 0. , 0. , 0. , 0. , 0. , -0.25414406], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])}, {'counter': 2, 'problem': , 'qaoa_results': , 'exp_vals_z': array([0., 0., 0., 0., 0., 0.]), 'corr_matrix': array([[ 0. , 0. , 0.16085035, 0. , -0.62390462, 0. ], [ 0. , 0. , 0. , -0.17543822, 0.18647518, 0.17543822], [ 0. , 0. , 0. , 0.29324292, -0.06223818, 0.39421389], [ 0. , 0. , 0. , 0. , -0.01269842, 0.36664087], [ 0. , 0. , 0. , 0. , 0. , -0.16107545], [ 0. , 0. , 0. , 0. , 0. , 0. ]])}, {'counter': 3, 'problem': , 'qaoa_results': , 'exp_vals_z': array([0., 0., 0., 0., 0.]), 'corr_matrix': array([[ 0. , -0.18916614, -0.45557513, -0.35756255, -0.02233763], [ 0. , 0. , 0. , -0.18916614, 0.18916614], [ 0. , 0. , 0. , 0.6210754 , 0.45557513], [ 0. , 0. , 0. , 0. , 0.35756255], [ 0. , 0. , 0. , 0. , 0. ]])}, {'counter': 4, 'problem': , 'qaoa_results': , 'exp_vals_z': array([0., 0., 0., 0.]), 'corr_matrix': array([[ 0. , -0.12297744, -0.47157466, -0.01462733], [ 0. , 0. , -0.08821626, 0.12297744], [ 0. , 0. , 0. , 0.47157466], ``` -------------------------------- ### Initialize and Run QAOA Source: https://openqaoa.entropicalabs.com/hackatons/bath-challenge Basic workflow to initialize QAOA, compile a QUBO problem, and optimize. Ensure 'qubo_problem' is defined beforehand. ```python from openqaoa import QAOA q = QAOA() q.compile(qubo_problem) q.optimize() ``` -------------------------------- ### Configure an IBMQ emulator Source: https://openqaoa.entropicalabs.com/qubit-routing/qaoa-qubit-routing Create a device instance configured as an emulator to test routing solutions locally. ```python device = create_device(location='ibmq', name='ibmq_xyz', **credentials, as_emulator=True) ``` -------------------------------- ### Initialize StandardWithBiasParams Source: https://openqaoa.entropicalabs.com/parametrization/standard-parametrization Configures a QAOA circuit with standard-with-bias parameters using custom values for gammas and betas. ```python q = QAOA() q.set_circuit_properties(p=1, param_type='standard_w_bias', init_type='custom', variational_params_dict={ 'gammas_pairs':[0.42], 'gammas_singles':[0.97], 'betas':[0.13]} ) ``` -------------------------------- ### Get Shortest Path Problem Details as Dictionary Source: https://openqaoa.entropicalabs.com/problems/shortest-path-problem Convert the Shortest Path problem instance into a dictionary format for detailed inspection. This includes graph structure, source/destination, and QUBO terms. ```python sp_qubo.asdict() ``` -------------------------------- ### Create and Set IBMQ Simulator Device Source: https://openqaoa.entropicalabs.com/devices/ibmq Create an OpenQAOA device object for the `ibmq_qasm_simulator`. This involves specifying the location, name, hub, group, and project. Then, set this device for your QAOA instance. ```python from openqaoa import QAOA, create_device q = QAOA() qasm_sim_device = create_device(location='ibmq', name='ibmq_qasm_simulator', hub='ibm-q', group='open', project='main') q.set_device(qasm_sim_device) ``` -------------------------------- ### Initialize Fourier Parameters Source: https://openqaoa.entropicalabs.com/parametrization/fourier-parametrization Configures a QAOA circuit with Fourier parametrization using custom initial values for u and v parameters. ```python q.set_circuit_properties(p=4, q=2, param_type='fourier', init_type='custom', variational_params_dict={ "u":[0.1, 0.2], "v":[0.9, 0.8]} ) ``` -------------------------------- ### Create and set the analytical simulator device Source: https://openqaoa.entropicalabs.com/devices/analytical-simulator Use this snippet to explicitly configure the analytical simulator as the device for a QAOA instance. ```python from openqaoa import QAOA, create_device analytical_device = create_device(location='local', name='analytical_simulator') q.set_device(analytical_device) ``` -------------------------------- ### Create a Knapsack Problem Instance Source: https://openqaoa.entropicalabs.com/problems/knapsack Instantiate a Knapsack problem with random parameters. Requires importing the Knapsack class from openqaoa.problems. ```python from openqaoa.problems import Knapsack knapsack_prob = Knapsack.random_instance(n_items=4, seed=42) knapsack_qubo = knapsack_prob.qubo ``` -------------------------------- ### Create Minimum Vertex Cover Problem Instance Source: https://openqaoa.entropicalabs.com/the-simplest-workflow Instantiate a Minimum Vertex Cover problem using networkx and OpenQAOA. This sets up the QUBO problem for optimization. ```python import networkx from openqaoa.problems import MinimumVertexCover g = networkx.circulant_graph(6, [1]) vc = MinimumVertexCover(g, field=1.0, penalty=10) qubo_problem = vc.qubo ``` -------------------------------- ### Create a Bin Packing Problem Instance Source: https://openqaoa.entropicalabs.com/problems/bin-packing Initializes a Bin Packing problem with specified item weights, bin capacity, and number of bins. ```python import numpy as np from openqaoa.problems import BinPacking n_items = 3 # number of items n_bins = 2 # maximum number of bins the solution will be explored on min_weight = 1 # minimum weight of the items max_weight = 3 # maximum weight of the items weight_capacity = 5 # weight capacity of the bins weights = np.random.default_rng(seed=1234).integers(low=min_weight, high=max_weight, size=n_items) # random instance of the problem bpp = BinPacking(weights, weight_capacity, n_bins=n_bins, simplifications=False) bpp_qubo = bpp.qubo ``` -------------------------------- ### Execute a basic QAOA workflow Source: https://openqaoa.entropicalabs.com/making-sense-of-the-result Initializes the QAOA object, compiles the problem, and runs the optimization process. ```python from openqaoa import QAOA q = QAOA() q.compile(qubo_problem) q.optimize() ``` -------------------------------- ### Configure Extended Parametrization Source: https://openqaoa.entropicalabs.com/parametrization/extended-parametrization Initializes a QAOA circuit with extended parametrization using custom variational parameters for gammas and betas. ```python q = QAOA() q.set_circuit_properties(p=1, param_type='extended', init_type='custom', variational_params_dict={ 'gammas_pairs':[1.1, 1.2, 1.3], 'gammas_singles':[2.1, 2.2, 2.3], 'betas_pairs': [], 'betas_singles': [3.1, 3.2, 3.3]} ) ``` -------------------------------- ### Configure and Execute OpenQAOA Optimization Source: https://openqaoa.entropicalabs.com/workflows/recursive-qaoa Sets circuit parameters, optimizer settings, and device configuration before compiling and running the optimization. ```python # Set the properties you want - These values are actually the default ones! r.set_circuit_properties(p=1, param_type='standard', init_type='ramp', mixer_hamiltonian='x') r.set_classical_optimizer(method='cobyla', maxiter=200, tol=0.001) device = create_device(location='local', name='vectorized') r.set_device(device) r.compile(sk) r.optimize() ``` -------------------------------- ### Instantiate Shortest Path Problem Source: https://openqaoa.entropicalabs.com/problems/shortest-path-problem Create a Shortest Path problem instance using OpenQAOA, providing the graph, source, and destination nodes. Access the QUBO representation via the .qubo attribute. ```python from openqaoa.problems import ShortestPath sp_prob = ShortestPath(G, source=0, dest=5) sp_qubo = sp_prob.qubo ``` -------------------------------- ### Create and Set Local Qiskit Statevector Simulator Source: https://openqaoa.entropicalabs.com/devices/qiskit Use this to target the local qiskit.statevector_simulator. Ensure the QAOA object is initialized before setting the device. ```python q = QAOA() qiskit_sv = create_device(location='local', name='qiskit.statevector_simulator') q.set_device(qiskit_sv) ``` -------------------------------- ### Instantiate a Rigetti QCS device Source: https://openqaoa.entropicalabs.com/devices/rigetti-qcs Requires execution within the QCS JupyterLab environment to connect to Rigetti hardware. ```python from openqaoa import QAOA, create_device q_pyquil = QAOA() rigetti_device = create_device(location='qcs', name='Aspen-M-3') q_pyquil.set_device(rigetti_device) ``` -------------------------------- ### Define Minimum Vertex Cover Problem Source: https://openqaoa.entropicalabs.com/workflows/run-workflows-on-qpus-qpu Sets up a Minimum Vertex Cover problem instance using networkx and OpenQAOA. Requires networkx and openqaoa libraries. ```python import networkx from openqaoa.problems import MinimumVertexCover g = networkx.circulant_graph(6, [1]) vc = MinimumVertexCover(g, field=1.0, penalty=10) qubo_problem = vc.qubo ``` -------------------------------- ### Instantiate Minimum Vertex Cover Problem Source: https://openqaoa.entropicalabs.com/problems/minimum-vertex-cover Create an instance of the MinimumVertexCover problem class from OpenQAOA. This requires the graph object and parameters for field and penalty. The QUBO representation can then be accessed. ```python from openqaoa.problems import MinimumVertexCover mvc_prob = MinimumVertexCover(G, field=1.0, penalty=10) mvc_qubo = mvc_prob.qubo ``` -------------------------------- ### Load IBMQ Account and Run QAOA Source: https://openqaoa.entropicalabs.com/workflows/run-workflows-on-qpus-qpu Loads your IBMQ account credentials and executes a QAOA workflow on a specified IBMQ device. Configures the number of shots, classical optimizer, and compilation settings. ```python from openqaoa import QAOA, create_device from qiskit import IBMQ # Load account from disk IBMQ.load_account() #Create the QAOA q = QAOA() # Create a device ibmq_device = create_device(location='ibmq', name='ibm_oslo') q.set_device(ibmq_device) q.set_backend_properties(n_shots=8000) q.set_classical_optimizer(method='cobyla', maxiter=50, tol=0.05) q.compile(qubo_problem) q.optimize() ``` -------------------------------- ### Configure and Run QAOA with Nesterov Momentum Source: https://openqaoa.entropicalabs.com/optimizers/pennylane-optimizers Demonstrates initializing a QAOA object and configuring the pennylane_nesterov_momentum optimizer with specific hyperparameters. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='pennylane_nesterov_momentum', jac='finite_difference', maxiter=200, optimizer_options=dict( stepsize=0.015, momentum=0.5, ) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Inspect Problem Instance Details Source: https://openqaoa.entropicalabs.com/problems/traveling-sales-person Exports the full problem instance configuration as a dictionary. ```python > tsp_qubo.asdict() ``` -------------------------------- ### Initialize QAOA Object Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/gradient-computation Initializes the QAOA object for subsequent configuration. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() ``` -------------------------------- ### Explicitly initialize the vectorized device Source: https://openqaoa.entropicalabs.com/devices/entropica-labs-vectorized Use this snippet to manually configure the QAOA workflow to use the vectorized simulator instead of the default. ```python from openqaoa import QAOA, create_device q = QAOA() vect_device = create_device(location='local', name='vectorized') q.set_device(vect_device) ``` -------------------------------- ### Create Azure Quantum Device Source: https://openqaoa.entropicalabs.com/devices/azure-quantum Instantiates an Azure Quantum device for use with OpenQAOA. Ensure correct resource ID and Azure location are specified. ```python from openqaoa import QAOA, create_device q = QAOA() device_azure = create_device(location='azure', name='ionq.simulator', resource_id="/subscriptions/****/resourceGroups/****/providers/****/Workspaces/****", az_location='westus') q.set_device(device_azure) ``` -------------------------------- ### Configure QAOA with Annealing Parametrization Source: https://openqaoa.entropicalabs.com/parametrization/annealing-parametrization Initializes the QAOA object and sets the circuit properties to use the annealing parametrization with a ramp initialization scheme. ```python q = QAOA() q.set_circuit_properties(p=2, param_type='annealing', init_type='ramp') ``` -------------------------------- ### Build System Configuration for OpenQAOA Plugin Source: https://openqaoa.entropicalabs.com/contributing/add_new_backend This configuration is required in the pyproject.toml file for building the plugin package. It specifies the build system requirements. ```toml [build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" ``` -------------------------------- ### Configure QAOA with a shot-adaptive optimizer Source: https://openqaoa.entropicalabs.com/optimizers/shot-adaptive-optimizers Initializes a QAOA object and configures it to use a shot-based simulator, which is required for shot-adaptive optimizers. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # we need to use a QPU or a shot-based simulator to use shot-adaptive optimizers q.set_device(create_device('local', 'qiskit.shot_simulator')) # set the default number of shots q.set_backend_properties(n_shots=500) ``` -------------------------------- ### Build and push Docker image Source: https://openqaoa.entropicalabs.com/openqaoa-jobs/braket-container Commands to build the local image, tag it for the ECR repository, and push it to the registry. ```bash docker build -t ${REPOSITORY_NAME} ``` ```bash docker tag ${REPOSITORY_NAME}:latest ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${REPOSITORY_NAME}:latest ``` ```bash ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com/${REPOSITORY_NAME}:latest] ``` -------------------------------- ### Find and Check Available IBMQ Devices Source: https://openqaoa.entropicalabs.com/devices/ibmq Discover available IBMQ devices by creating a device object with an empty name and checking the connection. The `available_qpus` attribute lists devices accessible with your credentials. ```python from openqaoa import create_device q_device = create_device(location='ibmq', name='', hub='ibm-q', group='open', project='main') q_device.check_connection() q_device.available_qpus ``` -------------------------------- ### Initialize QUBO from Matrix and Vector Source: https://openqaoa.entropicalabs.com/hackatons/bath-challenge Construct a QUBO object using a symmetric matrix J and vector h. Ensure the 'terms' and 'weights' lists correspond correctly to the problem's structure. ```python qubo_problem = QUBO(terms= [[0,1], [0,2], [1,2], [0], [1], [2]], weights= [J_01, J_02, J_12, h_0, h_1, h_2], n=number_of_vertices) ``` -------------------------------- ### Verify Braket Connection and List Devices Source: https://openqaoa.entropicalabs.com/devices/amazon-braket Check authentication status and retrieve a list of available QPUs for a specific AWS region. ```python from openqaoa import create_device device = create_device(location='aws', name='', aws_region='us-west-1') device.check_connection() device.available_qpus ``` -------------------------------- ### Clone OpenQAOA repository Source: https://openqaoa.entropicalabs.com/install-openqaoa Downloads the source code from the official GitHub repository. ```bash git clone https://github.com/entropicalabs/openqaoa.git ``` -------------------------------- ### Configure and Execute CANS Optimizer Source: https://openqaoa.entropicalabs.com/optimizers/shot-adaptive-optimizers Sets the classical optimizer to CANS with parameter-shift Jacobian and custom shot budget options before running the optimization. ```python q.set_classical_optimizer( method='cans', jac="param_shift", maxiter=100, optimizer_options=dict( stepsize=0.001, mu=0.95, b=0.001, n_shots_min=10, n_shots_max=100, n_shots_budget=50000, ) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Initialize Fourier Parameters with Bias Source: https://openqaoa.entropicalabs.com/parametrization/fourier-parametrization Configures a QAOA circuit using the Fourier parametrization with additional bias terms for single-qubit Z-axis rotations. ```python q.set_circuit_properties(p=4, q=2, param_type='fourier_w_bias', init_type='custom', variational_params_dict={ "u_pairs":[0.1, 0.2], "u_singles":[0.5, 0.6], "v":[0.9, 0.8]} ) ``` -------------------------------- ### Setting Backend Properties Source: https://openqaoa.entropicalabs.com/workflows/customise-the-QAOA-workflow Configure backend properties such as the number of shots, whether to initialize with Hadamard gates, and the CVaR alpha value for expectation value calculation. ```python from openqaoa import QAOA, create_device #Create the QAOA q = QAOA() # circuit properties q.set_circuit_properties(p=3, param_type='standard', init_type='ramp', mixer_hamiltonian='xy') # backend properties q.set_backend_properties(init_hadamard=True, n_shots=8000, cvar_alpha=0.85) ``` -------------------------------- ### Configure VGD with Gradient SPSA Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/gradient-computation Demonstrates running QAOA with a gradient-based optimizer using the SPSA method to approximate the Jacobian with a defined step size. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='vgd', jac="grad_spsa", jac_options=dict( stepsize=0.0003, ), ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Configure the RQAOA workflow Source: https://openqaoa.entropicalabs.com/workflows/recursive-qaoa Initialize the RQAOA object and set parameters for the recursive elimination process. ```python from openqaoa import RQAOA, create_device r = RQAOA() # Set up RQAOA properties n_cutoff = 3 # Cutoff size at which to solve things classically n_steps = 1 # Number of eliminations per step # Set instance parameters r.set_rqaoa_parameters(n_cutoff=n_cutoff, steps=n_steps, rqaoa_type='custom') ``` -------------------------------- ### Configure AWS CLI Credentials Source: https://openqaoa.entropicalabs.com/devices/amazon-braket Use the AWS CLI to set up authentication credentials on a local machine. ```bash $ aws configure AWS Access Key ID [None]: AKIAI******EXAMPLE AWS Secret Access Key [None]: wJalrXU******EXAMPLEKEY Default region name [None]: us-west-1 Default output format [None]: json ``` -------------------------------- ### Run QAOA with SPSA Optimizer in OpenQAOA Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/spsa-optimizer This snippet demonstrates how to configure and run the QAOA algorithm using the SPSA optimizer within the OpenQAOA framework. Ensure the 'problem' variable is defined before compilation. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='spsa', optimizer_options=dict( a0=0.01, c0=0.01, A=1, alpha=0.602, gamma=0.101, ) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Execute QAOA workflow on IBMQ simulator Source: https://openqaoa.entropicalabs.com/workflows/run-workflows-on-qpus-QASM-simulator Configures and runs the QAOA optimization loop on the IBMQ QASM simulator. ```python from openqaoa import QAOA, create_device from qiskit import IBMQ #Create the QAOA q = QAOA() # Load account from disk IBMQ.load_account() # Create a device ibmq_device = create_device(location='ibmq', name='ibmq_qasm_simulator') q.set_device(ibmq_device) q.set_circuit_properties(p=1, param_type='standard', init_type='ramp', mixer_hamiltonian='x') q.set_backend_properties(init_hadamard=True, n_shots=8000, cvar_alpha=0.85) q.set_classical_optimizer(method='cobyla', maxiter=50, tol=0.05) q.compile(qubo_problem) q.optimize() ``` -------------------------------- ### Configure QAOA with Quantum Natural Gradient Descent Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/natural-gd-optimizer Sets up the QAOA object to use the natural gradient descent optimizer with a specified step size and finite difference Jacobian approximation. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='natural_grad_descent', jac="finite_difference", optimizer_options=dict( stepsize=0.1, ) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Compile and Optimize with Graph Coloring QUBO Source: https://openqaoa.entropicalabs.com/hackatons/bath-challenge Compile the QAOA object with a QUBO generated by the graph_coloring_qubo function and then optimize. This step translates the QUBO to a cost Hamiltonian and composes the QAOA circuit. ```python gc_qubo = graph_coloring_qubo(graph, k) q = QAOA() q.compile(gc_qubo) q.optimize() ``` -------------------------------- ### Configure VGD with Parameter Shift Jacobian Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/gradient-computation Sets up the VGD optimizer using a stochastic parameter shift Jacobian with specified beta and gamma evaluation counts. ```python q.set_classical_optimizer( method='vgd', jac="stoch_param_shift", jac_options=dict( n_beta_single=4, n_gamma_pair=6, ), ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Optimize with Azure Quantum Backend Source: https://openqaoa.entropicalabs.com/devices/azure-quantum Submits an optimization job to the configured Azure Quantum backend. This is the standard method for running optimization tasks. ```python q.optimize() ``` -------------------------------- ### Run QAOA with Parameter-Shift Gradient Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/gradient-computation Configures the QAOA optimizer to use the parameter-shift rule for exact gradient computation. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='vgd', jac="param_shift", ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Set QAOA Circuit Properties Source: https://openqaoa.entropicalabs.com/hackatons/bath-challenge Configure QAOA circuit parameters such as the number of layers (p), parameter type, and qubit count (q). Experiment with different values to find optimal settings. ```python q = QAOA() q.set_circuit_properties(p=3, param_type='fourier', q=2) ``` -------------------------------- ### Create Local Vectorized Device Source: https://openqaoa.entropicalabs.com/devices/device Create a device object for the local vectorized backend by specifying location as 'local'. ```python my_device = create_device(location='local' name='vectorized') ``` -------------------------------- ### Configure QAOA with Newton's Optimizer Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/newton-optimizer Use this snippet to set up a QAOA object to employ Newton's method for optimization. It specifies finite differences for approximating the Jacobian and Hessian, and sets a step size of 0.1. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='newton', jac="finite_difference", hess="finite_difference", optimizer_options=dict( stepsize=0.1, ) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Run QAOA with Gradient Descent Optimizer Source: https://openqaoa.entropicalabs.com/optimizers/gradient-based-optimizers/vgd-optimizer Configures the QAOA object to use the 'vgd' optimizer with a finite difference Jacobian and a specified step size. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='vgd', jac="finite_difference", optimizer_options=dict(stepsize=0.001) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ``` -------------------------------- ### Define Number Partition Problem in OpenQAOA Source: https://openqaoa.entropicalabs.com/problems/number-partition Instantiate the NumberPartition problem with a given set of integers and access its QUBO representation. ```python from openqaoa.problems import NumberPartition int_set = [1,2,3,4,10] np_prob = NumberPartition(numbers=int_set) np_qubo = np_prob.qubo ``` -------------------------------- ### Use COBYLA Optimizer in OpenQAOA Source: https://openqaoa.entropicalabs.com/optimizers/gradient-free-optimizers Configure and use the COBYLA optimizer for QAOA. Set maximum iterations and customize trust region size with `rhobeg`. ```python from openqaoa import QAOA # create the QAOA object q = QAOA() # set optimizer and properties q.set_classical_optimizer( method='cobyla', maxiter=200, optimizer_options=dict( rhobeg=0.5, ) ) # compile and optimize using the chosen optimizer q.compile(problem) q.optimize() ```