-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
48 lines (37 loc) · 1.22 KB
/
Copy pathcode
File metadata and controls
48 lines (37 loc) · 1.22 KB
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
# Define a simple graph
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (0, 2)])
# Define the optimization problem
qp = QuadraticProgram()
for i in range(len(G.nodes)):
qp.binary_var(f'x{i}')
# Define the objective function
linear = np.zeros(len(G.nodes))
quadratic = np.zeros((len(G.nodes), len(G.nodes)))
for i, j in G.edges:
quadratic[i, j] = 1 # Penalizing edges within the same set
qp.minimize(quadratic=quadratic)
print(qp)
] # If diffuser is a gate, create a QuantumCircuit and add it
diffuser_circuit = QuantumCircuit(2)
diffuser_circuit.append(diffuser(2), range(2))
# Print circuit diagrams
print(oracle.draw()) # Print Oracle Circuit
print(diffuser_circuit.draw()) # Print Diffuser Circuit
print(grover.draw()) # Print Full Grover Circuit
10] print(oracle)
print(diffuser(2))
11] print(grover.draw())
] # Initialize quantum circuit
qc = QuantumCircuit(2)
qc.h([0, 1])
qc.measure_all() # Measure all qubits
# Set up the Aer simulator
simulator = Aer.get_backend('qasm_simulator')
Transpile and run the circuit
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit).result()
# Get and plot the results
counts = result.get_counts()
plot_histogram(counts)
14] qc.draw('mpl')