-
Notifications
You must be signed in to change notification settings - Fork 21
/
qaoa.py
57 lines (41 loc) · 1.29 KB
/
qaoa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
QAOA
====
Here we generate and optimize pattern for QAOA circuit.
You can run this code on your browser with `mybinder.org <https://mybinder.org/>`_ - click the badge below.
.. image:: https://mybinder.org/badge_logo.svg
:target: https://mybinder.org/v2/gh/TeamGraphix/graphix-examples/HEAD?labpath=qaoa.ipynb
"""
# %%
import networkx as nx
import numpy as np
from graphix import Circuit
rng = np.random.default_rng()
n = 4
xi = rng.random(6)
theta = rng.random(4)
g = nx.complete_graph(n)
circuit = Circuit(n)
for i, (u, v) in enumerate(g.edges):
circuit.cnot(u, v)
circuit.rz(v, xi[i])
circuit.cnot(u, v)
for v in g.nodes:
circuit.rx(v, theta[v])
# %%
# transpile and get the graph state
pattern = circuit.transpile().pattern
pattern.standardize()
pattern.shift_signals()
pattern.draw_graph(flow_from_pattern=False)
# %%
# perform Pauli measurements and plot the new (minimal) graph to perform the same quantum computation
pattern.perform_pauli_measurements()
pattern.draw_graph(flow_from_pattern=False)
# %%
# finally, simulate the QAOA circuit
out_state = pattern.simulate_pattern()
state = circuit.simulate_statevector().statevec
print("overlap of states: ", np.abs(np.dot(state.psi.flatten().conjugate(), out_state.psi.flatten())))
# sphinx_gallery_thumbnail_number = 2
# %%