12 - Integrating OpenQAOA and Azure quantum

In this notebook we showcase the OpenQAOA workflows. The notebook is divided into three parts:

  • The default QAOA workflow on the ionq.simulator

  • An advanced QAOA workflow on the rigetti QVM

  • An RQAOA workflow on the rigetti QVM

Default QAOA on the IonQ Emulator

Before using OpenQAOA with Azure quantum, you will be required to log in to your azure account through the Azure CLI. This can be done via az login in your command line terminal after installing the Azure CLI. After following the steps required for authentication, you will be able to use the Azure backend with OpenQAOA!

[1]:
from openqaoa.backends import create_device
device_azure = create_device(location='azure',
                             name='ionq.simulator',
                             resource_id='',
                             az_location='westus')

In order to create the Azure Device for OpenQAOA, you have to provide a valid resource ID that points to the Workspace that contains access to the QPU (and emulator) providers, resource_id, and the location of the azure server hosting that workspace, az_location.

Once you’ve created the Azure Device, you can use the OpenQAOA workflow like you normally do for other backends. (Only this time you pass in the Azure Device instead!)

Let’s generate a sample problem, and solve it using an OpenQAOA workflow

[2]:
from openqaoa.problems import NumberPartition

np_integer = NumberPartition([1,2,3])
np_qubo = np_integer.qubo
[3]:
# The qubo is a dictionary including terms and weigths
np_qubo.asdict()
[3]:
{'terms': [[0, 1], [0, 2], [1, 2]],
 'weights': [4.0, 6.0, 12.0],
 'constant': 14,
 'n': 3,
 'problem_instance': {'problem_type': 'number_partition',
  'numbers': [1, 2, 3],
  'n_numbers': 3},
 'metadata': {}}
[4]:
from openqaoa import QAOA

q = QAOA()
q.set_device(device_azure)
q.set_backend_properties(n_shots=1000)
q.set_classical_optimizer(maxiter=5)
[5]:
q.compile(np_qubo)
[6]:
q.optimize()
.........................................
[7]:
q.result.optimized
[7]:
{'angles': [1.35, 1.35],
 'cost': 9.528,
 'measurement_outcomes': {'000': 59,
  '100': 108,
  '010': 129,
  '110': 168,
  '001': 205,
  '101': 138,
  '011': 117,
  '111': 76},
 'job_id': 'a9a6a956-cbcb-11ee-bce7-0a03003b1899',
 'eval_number': 3}

And that’s it! You’ve completed your first OpenQAOA Workflow run on the Azure platform with the IonQ Emulator.

A more advanced QAOA workflow on the Rigetti QVM

Let’s try another QAOA Workflow, but this time on the Rigetti QVM hosted on Azure.

[8]:
device_azure_qvm = create_device(location='azure',
                                name='rigetti.sim.qvm',
                                resource_id='',
                                az_location='westus')
[9]:
from openqaoa import QAOA

q_custom = QAOA()
q_custom.set_circuit_properties(p=2, param_type='extended', init_type='ramp', mixer_hamiltonian='xy')
q_custom.set_device(device_azure_qvm)
q_custom.set_backend_properties(init_hadamard=True, n_shots=5000, cvar_alpha=0.8)
q_custom.set_classical_optimizer(maxiter=5)
[10]:
q_custom.compile(np_qubo)
[11]:
q_custom.optimize()
.........................................
[12]:
fig, ax = q_custom.result.plot_probabilities()
states kept: 8
../_images/notebooks_12_testing_azure_18_1.png

RQAOA on a QVM

The RQAOA Workflow also works with Azure. (Note: RQAOA requires multiple loops of the QAOA algorithm, this algorithm can require many executions on the QPU. Thus, it will take a while!)

[13]:
np_integer = NumberPartition([1,2,3,4,5,15])
np_qubo = np_integer.qubo
[14]:
from openqaoa import RQAOA

r = RQAOA(device=device_azure_qvm)

# Set parameters for RQAOA, in this case we fix the steps to 1 (default), the final cutoff value to 3
r.set_rqaoa_parameters(steps = 1, n_cutoff = 3)
r.set_backend_properties(n_shots=100)
r.set_classical_optimizer(maxiter=5)

[15]:
r.compile(np_qubo)
[16]:
r.optimize()
.........................................................................................................
[17]:
r.result
[17]:
{'solution': {'000001': 0.0, '111110': 0.0},
 'classical_output': {'minimum_energy': -26.0,
  'optimal_states': ['000', '111']},
 'elimination_rules': [[{'pair': (1, 4), 'correlation': 1.0}],
  [{'pair': (1, 4), 'correlation': -1.0}],
  [{'pair': (1, 3), 'correlation': 1.0}]],
 'schedule': [1, 1, 1],
 'number_steps': 3,
 'intermediate_steps': [{'counter': 0,
   'problem': <openqaoa.problems.qubo.QUBO at 0x7f685244d780>,
   'qaoa_results': <openqaoa.algorithms.qaoa.qaoa_result.QAOAResult at 0x7f6852997100>,
   'exp_vals_z': array([0., 0., 0., 0., 0., 0.]),
   'corr_matrix': array([[ 0.00000000e+00,  4.00000000e-02,  2.00000000e-02,
           -1.60000000e-01,  2.08166817e-17, -1.60000000e-01],
          [ 0.00000000e+00,  0.00000000e+00,  2.20000000e-01,
           -1.60000000e-01,  3.20000000e-01, -1.60000000e-01],
          [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
            2.20000000e-01,  2.20000000e-01, -3.00000000e-01],
          [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
            0.00000000e+00, -2.00000000e-01, -1.60000000e-01],
          [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
            0.00000000e+00,  0.00000000e+00, -1.60000000e-01],
          [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
            0.00000000e+00,  0.00000000e+00,  0.00000000e+00]])},
  {'counter': 1,
   'problem': <openqaoa.problems.qubo.QUBO at 0x7f687c19b520>,
   'qaoa_results': <openqaoa.algorithms.qaoa.qaoa_result.QAOAResult at 0x7f68b6b22290>,
   'exp_vals_z': array([0., 0., 0., 0., 0.]),
   'corr_matrix': array([[ 0.  , -0.32, -0.18,  0.22,  0.38],
          [ 0.  ,  0.  ,  0.06,  0.02, -0.66],
          [ 0.  ,  0.  ,  0.  , -0.6 ,  0.12],
          [ 0.  ,  0.  ,  0.  ,  0.  ,  0.12],
          [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ]])},
  {'counter': 2,
   'problem': <openqaoa.problems.qubo.QUBO at 0x7f685244f010>,
   'qaoa_results': <openqaoa.algorithms.qaoa.qaoa_result.QAOAResult at 0x7f6852897d00>,
   'exp_vals_z': array([0., 0., 0., 0.]),
   'corr_matrix': array([[ 0.  ,  0.04, -0.22, -0.12],
          [ 0.  ,  0.  , -0.02,  0.32],
          [ 0.  ,  0.  ,  0.  , -0.06],
          [ 0.  ,  0.  ,  0.  ,  0.  ]])}],
 'atomic_ids': {0: 'c95a410a-62a5-4266-bb9e-897fd347d40d',
  1: '0e9be1b0-7c08-4eac-9ca9-987d83f78e5b',
  2: '4cb539ab-d5ae-48bd-ac22-2980f264bff4'}}

And there you have it! You’ve just performed QAOA and RQAOA on Emulators hosted by Azure. Azure Quantum also contains QPUs hosted by hardware providers like Rigetti and IonQ. We can’t wait to see what you can do with OpenQAOA and this new backend! If you find something that you’d like to have as a feature or a bug while using the Azure backend. Raise an issue and we will keep in touch!