-
Notifications
You must be signed in to change notification settings - Fork 2
/
bernstein-vazirani.py
59 lines (40 loc) · 1.28 KB
/
bernstein-vazirani.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
58
59
########################### Importations ###########################
import matplotlib.pyplot as plt
import numpy as np
from qiskit import IBMQ, BasicAer
from qiskit.providers.ibmq import least_busy
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
from qiskit.visualization import plot_histogram
########################### Helpers ###########################
def apply_gate(circuit, n, gate):
if gate == 'H':
for i in range(n):
circuit.h(i)
elif gate == 'M':
for i in range(n):
circuit.measure(i, i)
def apply(circuit, n, callback):
for i in range(n):
callback(circuit, i)
########################### Implementation ###########################
n = 4
s = '1011'
U_f = lambda c, x: c.i(x) if s[x] == '0' else c.cx(x, n)
# Circuit
circuit = QuantumCircuit(n+1,n)
circuit.h(range(n+1))
circuit.z(n)
circuit.barrier()
s = s[::-1]
apply(circuit, n, U_f)
circuit.barrier()
circuit.h(range(n))
circuit.measure(range(n),range(n))
print(circuit)
circuit.draw(output='mpl')
# Simulation
backend = BasicAer.get_backend('qasm_simulator')
answer = execute(circuit, backend=backend, shots=8).result().get_counts()
print(answer)
plot_histogram(answer)
plt.show()