-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic.py
65 lines (53 loc) · 1.24 KB
/
arithmetic.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
60
61
62
63
64
65
import math
from IPython.display import display
from qiskit import BasicAer, QuantumCircuit, QuantumRegister, execute
from qiskit.visualization import array_to_latex
# Set up
a = QuantumRegister(3, name="a")
b = QuantumRegister(2, name="b")
qc = QuantumCircuit(a, b)
# a=sqrt(0.5)|1⟩+sqrt(0.5)|5⟩
qc.x(a[0])
qc.h(a[2])
qc.barrier()
# b=sqrt(0.5)|1⟩+45°sqrt(0.5)|3⟩
qc.x(b[0])
qc.h(b[1])
qc.rz(math.radians(90), b[1])
qc.barrier()
# a -= 3
qc.x(a[1])
qc.cx(a[1], a[2])
qc.x(a[0])
qc.cx(a[0], a[1])
qc.mcx([a[0], a[1]], a[2])
qc.barrier()
# if (a<0) then b++
qc.mcx([a[2], b[0]], b[1])
qc.cx(a[2], b[0])
qc.barrier()
# a += 3
qc.mcx([a[0], a[1]], a[2])
qc.cx(a[0], a[1])
qc.x(a[0])
qc.cx(a[1], a[2])
qc.x(a[1])
qc.barrier()
# Run circuit
backend = BasicAer.get_backend("statevector_simulator")
job = execute(qc, backend)
result = job.result()
outputstate = result.get_statevector(qc, decimals=3)
display(array_to_latex(outputstate, prefix="\\text{Statevector}", max_size=32))
for i, amp in enumerate(outputstate):
if abs(amp) > 0.000001:
print("|{}> {}".format(i, amp))
# Draw the circuit
qc.draw(
output="mpl",
style="iqp",
cregbundle=False,
initial_state=True,
fold=-1,
filename="./docs/_static/arithmetic.png",
)