Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal working example required #1

Open
LucidaLu opened this issue Mar 2, 2022 · 1 comment
Open

Minimal working example required #1

LucidaLu opened this issue Mar 2, 2022 · 1 comment

Comments

@LucidaLu
Copy link

LucidaLu commented Mar 2, 2022

Hello, I am wondering if you can provide a minimal working example of fixed-angle QAOA?

I tried to read this project and the qtensor library, but failed to obtain the expected result.

Here is the code I am trying:

from qtensor import QAOA_energy
import networkx as nx
import json

with open("angles.json", 'r') as fangles:
    ANGLES = json.load(fangles)

G = nx.from_edgelist([[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]])
opt = max(sum(0 if s >> x & 1 == s >> y & 1 else 1 for x, y in G.edges) for s in range(1 << G.number_of_nodes()))

p = 5

gamma, beta = ANGLES['3'][str(p)]['gamma'], ANGLES['3'][str(p)]['beta']

E = QAOA_energy(G, gamma, beta)

print(gamma, '\n', beta, '\n', E, E/opt, opt)

# outputs 
# [0.3595894069747186, 0.7065726026028729, 0.8225565476596165, 1.0047548948734175, 1.154274581864511] 
# [0.6318520293950315, 0.5226524412470716, 0.3897420609614237, 0.2754121638618102, 0.1491700495831238] 
# [2.15570478] [0.5389262] 4

# {"GW computation time (sec)": 1.5017328262329102, "edges": [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]], "maxcut": 4.0, "GWave": 3.69, "Nvertices": 4, "QAOA_1": 3.4880307849489687, "AR_1": 0.8720076962372422, "WS computation time (sec)": 0.031711578369140625, "QAOA_2": 3.443514444229522, "AR_2": 0.8608786110573805, "QAOA_3": 3.7257461621272516, "AR_3": 0.9314365405318129, "QAOA_4": 3.7994349269828813, "AR_4": 0.9498587317457203, "QAOA_5": 3.9023289956861937, "AR_5": 0.9755822489215484, "QAOA_6": 3.821206750231719, "AR_6": 0.9553016875579298, "QAOA_7": 3.8566061089160772, "AR_7": 0.9641515272290193, "QAOA_8": 3.902939231923547, "AR_8": 0.9757348079808867, "QAOA_9": 3.8698211110331737, "AR_9": 0.9674552777582934, "QAOA_10": 3.909461386351469, "AR_10": 0.9773653465878672, "QAOA_11": 3.8962754265951487, "AR_11": 0.9740688566487872}
# the first item in ALL_3r_graphs.json, saying that QAOA_5=3.9023289956861937

I must have made some stupid mistake, but I just can't see it. I'd really appreciate your help! Thanks!

@danlkv
Copy link
Owner

danlkv commented Apr 1, 2022

Hi! There's a bit of rescaling required. Just divide the angles by np.pi and it should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants