common.title
Cloud support

Nobisuke

Dekisugi

RAG


autoQAOA
RAG for dev
Fortune telling app
Annealing
DEEPSCORE
Translation

Overview
Service overview
Terms of service

Privacy policy

Contact
Research

Sign in
Sign up
common.title

Let's do a combinatorial optimization problem on Rigetti's quantum gated quantum computer!

Yuichiro Minato

2022/10/14 01:11

Let's use amazon braket to solve a simple combinatorial optimization problem today.

The most common way to solve combinatorial optimization problems on quantum gated quantum computers is to use QAOA or VQE, QAOA is a cousin of quantum annealing, and VQE is a brute force algorithm to find the eigenvalues of a matrix.

If you want to know more about QAOA, you can do a search on blueqat.com. This time, I'm going to make it with some knowledge of QAOA.

from braket.circuits import Circuit from braket.devices import LocalSimulator import matplotlib.pyplot as plt %matplotlib inline

First, to solve the combinatorial optimization problem, we need to find the eigenvalues of the matrix, so we set up the matrix.

H=Z0Z1+Z0H = Z_0*Z_1 + Z_0

Z in this equation takes values ranging from -1 to +1. Each term is expected to have a minimum value when -1-1=-2. At that time, Z0 should be -1 and Z1 should be 1.
The values of the qubits in that case are 1 for the zeroth qubit and 0 for the first qubit.

In this case, the equation is simplified and two parameters are provided (if you want to use QAOA). Adiabatic time development, but since both of the above coefficients are 1, the circuit will be RZZ and RZ.

|0> -H-RZZ-RZ--RX--M
|0> -H-RZZ-----RX--M

The eigenstates are first prepared in superposition, and RX is the transverse magnetic field with the time evolution of X prepared. The parameters for RZZ and RZ are the same.

In this case, we will use rigetti. Check the gate set.

from braket.aws import AwsDevice device = AwsDevice("arn:aws:braket:us-west-1::device/qpu/rigetti/Aspen-M-2") print(device.properties.action['braket.ir.jaqcd.program'].supportedOperations)
['cz', 'xy', 'ccnot', 'cnot', 'cphaseshift', 'cphaseshift00', 'cphaseshift01', 'cphaseshift10', 'cswap', 'h', 'i', 'iswap', 'phaseshift', 'pswap', 'rx', 'ry', 'rz', 's', 'si', 'swap', 't', 'ti', 'x', 'y', 'z', 'start_verbatim_box', 'end_verbatim_box']

As expected, RZZ is not available, so cnot-RZ-cnot is used instead. As for the angle, we have two parameters for the initial value.

import numpy as np params = [np.random.rand(), np.random.rand()] circuit = Circuit().h(0).h(1).cnot(0,1).rz(1,params[0]).cnot(0,1).rz(0,params[0]).rx(0,params[1]).rx(1,params[1]) print(circuit)
T  : |0|1|   2    |3|   4    |   5    |

                                       

q0 : -H-C----------C-Rz(0.81)-Rx(0.08)-

        |          |                   

q1 : -H-X-Rz(0.81)-X-Rx(0.08)----------



T  : |0|1|   2    |3|   4    |   5    |

Now we are done, and in QAOA it is the expected value that we want to minimize, but the expected value of Z0Z1 can be easily obtained by counting the number of times it has the same sign and different signs. The expected value of Z0 can also be obtained by counting the number of times 0 and 1 appear.

device = LocalSimulator() shots = 10000 result = device.run(circuit, shots=shots).result() counts = result.measurement_counts print(counts) # plot using Counter plt.bar(counts.keys(), counts.values()); plt.xlabel('bitstrings'); plt.ylabel('counts')
Counter({'00': 2805, '11': 2566, '01': 2408, '10': 2221})
Text(0, 0.5, 'counts')
<Figure size 432x288 with 1 Axes>output
expectation = ((counts['00']+counts['11']-counts['01']-counts['10']) + (counts['00']+counts['01']-counts['10']-counts['11']))/shots print(expectation)
0.1168

Let's use this to optimize. We will use Scipy's COBYLA method for optimization.

import scipy.optimize shots = 100000 def f(para): circuit = Circuit().h(0).h(1).cnot(0,1).rz(1,-2*para[0]).cnot(0,1).rz(0,-2*para[0]).rx(0,-2*para[1]).rx(1,-2*para[1]) result = device.run(circuit, shots=shots).result() counts = result.measurement_counts expectation = ((counts['00']+counts['11']-counts['01']-counts['10']) + (counts['00']+counts['01']-counts['10']-counts['11']))/shots return expectation res = scipy.optimize.minimize(f, params, method='COBYLA') print(res)
     fun: -0.55094

   maxcv: 0.0

 message: 'Optimization terminated successfully.'

    nfev: 31

  status: 1

 success: True

       x: array([ 2.19370847, -1.09945378])
circuit = Circuit().h(0).h(1).cnot(0,1).rz(1,-2*res.x[0]).cnot(0,1).rz(0,-2*res.x[0]).rx(0,-2*res.x[1]).rx(1,-2*res.x[1]) result = device.run(circuit, shots=shots).result() counts = result.measurement_counts print(counts) # plot using Counter plt.bar(counts.keys(), counts.values()); plt.xlabel('bitstrings'); plt.ylabel('counts')
Counter({'10': 38645, '01': 26491, '11': 23699, '00': 11165})
Text(0, 0.5, 'counts')
<Figure size 432x288 with 1 Axes>output

Although the expected value has not decreased fully, we have confirmed that the probability of the answer, 10, has increased.

Try calling Rigetti again.

device = AwsDevice("arn:aws:braket:us-west-1::device/qpu/rigetti/Aspen-M-2") print(device.properties.action['braket.ir.jaqcd.program'].supportedOperations)
['ccnot', 'cnot', 'cphaseshift', 'cswap', 'cy', 'cz', 'h', 'i', 'phaseshift', 'rx', 'ry', 'rz', 's', 'si', 'swap', 't', 'ti', 'v', 'vi', 'x', 'y', 'z', 'ecr', 'start_verbatim_box', 'end_verbatim_box']
circuit = Circuit().h(0).h(1).cnot(0,1).rz(1,-2*res.x[0]).cnot(0,1).rz(0,-2*res.x[0]).rx(0,-2*res.x[1]).rx(1,-2*res.x[1]) task = device.run(circuit) print(task.result().measurement_counts) # plot using Counter counts = task.result().measurement_counts plt.bar(counts.keys(), counts.values()); plt.xlabel('bitstrings'); plt.ylabel('counts')
Counter({'10': 404, '11': 244, '01': 224, '00': 128})
Text(0, 0.5, 'counts')
<Figure size 432x288 with 1 Axes>output

You got the correct answer. You got QAOA on the actual machine! That's all.

© 2024, blueqat Inc. All rights reserved