-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.py
51 lines (36 loc) · 1.55 KB
/
AddTwoNumbers.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
import cirq
import cirq_google
def createCircuit(qubits):
circuit = cirq.Circuit()
# example, a=01, b=11
# apply X (equivalent of NOT) gate to set a and b to their respective values
circuit.append(cirq.X(qubits[1]))
circuit.append(cirq.X(qubits[3]))
# use CNOT (equivalent of XOR) to perform addition of least significant bits
circuit.append(cirq.CNOT(qubits[0], qubits[4]))
circuit.append(cirq.CNOT(qubits[1], qubits[4]))
# use toffoli gate to add carry logic
circuit.append(cirq.TOFFOLI(qubits[0], qubits[1], qubits[5]))
# to measure the output of our operation
circuit.append(cirq.measure(*qubits, key="result"))
return circuit
def addTwoNumbers():
""" perform addition of two 2-bit numbers and check if it can be done on quantum chip """
# create qubits to accomodate the data, we are using 6 qubits - 4 to store no. A and B and 2 to store the sum of A and B
qubits = [cirq.LineQubit(i) for i in range(6)]
circuit = createCircuit(qubits)
# get the result of the quantum circuit via simulation, and we do 10 repetitions to get a distribution of result
simulator = cirq.Simulator()
res = simulator.run(circuit, repetitions=10)
print("Quantum Adder Circuit Result:")
print(res)
# now the cool stuff: can this circuit run on any existing Quantum Chips
try:
cirq_google.Sycamore.validate_circuit(circuit)
except Exception as e:
print(e)
def main():
""" entry point for the script """
addTwoNumbers()
if __name__ == "__main__":
main()