samyak152002 commited on
Commit
6efeab4
1 Parent(s): e9675cf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ multiply.py: Multiply two numbers using repeated fourier
5
+ transform based addition.
6
+ """
7
+
8
+ from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
9
+ from qiskit import Aer, execute
10
+ from math import pi
11
+
12
+ def createInputState(qc, reg, n, pie):
13
+ """
14
+ Computes the quantum Fourier transform of reg, one qubit at
15
+ a time.
16
+ Apply one Hadamard gate to the nth qubit of the quantum register reg, and
17
+ then apply repeated phase rotations with parameters being pi divided by
18
+ increasing powers of two.
19
+ """
20
+ qc.h(reg[n])
21
+ for i in range(0, n):
22
+ qc.cp(pie / float(2**(i + 1)), reg[n - (i + 1)], reg[n])
23
+
24
+
25
+ def evolveQFTState(qc, reg_a, reg_b, n, pie, factor):
26
+ """
27
+ Evolves the state |F(ψ(reg_a))> to |F(ψ(reg_a+reg_b))> using the quantum
28
+ Fourier transform conditioned on the qubits of the reg_b.
29
+ Apply repeated phase rotations with parameters being pi divided by
30
+ increasing powers of two.
31
+ """
32
+ l = len(reg_b)
33
+ for i in range(0, n + 1):
34
+ if (n - i) > l - 1:
35
+ pass
36
+ else:
37
+ qc.cp(factor*pie / float(2**(i)), reg_b[n - i], reg_a[n])
38
+
39
+
40
+ def inverseQFT(qc, reg, n, pie):
41
+ """
42
+ Performs the inverse quantum Fourier transform on a register reg.
43
+ Apply repeated phase rotations with parameters being pi divided by
44
+ decreasing powers of two, and then apply a Hadamard gate to the nth qubit
45
+ of the register reg.
46
+ """
47
+ for i in range(0, n):
48
+ qc.cp(-1 * pie / float(2**(n - i)), reg[i], reg[n])
49
+ qc.h(reg[n])
50
+
51
+
52
+ def add(reg_a, reg_b, circ, factor):
53
+ """
54
+ Add two quantum registers reg_a and reg_b, and store the result in
55
+ reg_a.
56
+ """
57
+ pie = pi
58
+ n = len(reg_a) - 1
59
+
60
+ # Compute the Fourier transform of register a
61
+ for i in range(0, n + 1):
62
+ createInputState(circ, reg_a, n - i, pie)
63
+ # Add the two numbers by evolving the Fourier transform F(ψ(reg_a))>
64
+ # to |F(ψ(reg_a+reg_b))>
65
+ for i in range(0, n + 1):
66
+ evolveQFTState(circ, reg_a, reg_b, n - i, pie, factor)
67
+ # Compute the inverse Fourier transform of register a
68
+ for i in range(0, n + 1):
69
+ inverseQFT(circ, reg_a, i, pie)
70
+
71
+
72
+ # Take two numbers as user input in binary form
73
+ multiplicand_in = input("Enter the multiplicand.")
74
+ l1 = len(multiplicand_in)
75
+ multiplier_in = input("Enter the multiplier.")
76
+ l2 = len(multiplier_in)
77
+ # Make sure multiplicand_in holds the larger number
78
+ if l2 > l1:
79
+ multiplier_in, multiplicand_in = multiplicand_in, multiplier_in
80
+ l2, l1 = l1, l2
81
+
82
+ multiplicand = QuantumRegister(l1)
83
+ multiplier = QuantumRegister(l2)
84
+ accumulator = QuantumRegister(l1 + l2)
85
+ cl = ClassicalRegister(l1 + l2)
86
+ d = QuantumRegister(1)
87
+
88
+ circ = QuantumCircuit(accumulator, multiplier, multiplicand,
89
+ d, cl, name="qc")
90
+
91
+ circ.x(d)
92
+ # Store bit strings in quantum registers
93
+ for i in range(l1):
94
+ if multiplicand_in[i] == '1':
95
+ circ.x(multiplicand[l1 - i - 1])
96
+
97
+ for i in range(l2):
98
+ if multiplier_in[i] == '1':
99
+ circ.x(multiplier[l1 - i - 1])
100
+
101
+ multiplier_str = '1'
102
+ # Perform repeated addition until the multiplier
103
+ # is zero
104
+ while(int(multiplier_str) != 0):
105
+ add(accumulator, multiplicand, circ, 1)
106
+ add(multiplier, d, circ, -1)
107
+ for i in range(len(multiplier)):
108
+ circ.measure(multiplier[i], cl[i])
109
+ result = execute(circ, backend=Aer.get_backend('qasm_simulator'),
110
+ shots=2).result().get_counts(circ.name)
111
+ multiplier_str = list(result.keys())[0]
112
+
113
+ circ.measure(accumulator, cl)
114
+ result = execute(circ, backend=Aer.get_backend('qasm_simulator'),
115
+ shots=2).result().get_counts(circ.name)
116
+
117
+ print(result)