Skip to content

Commit 7776411

Browse files
Create q_full_adder.py (TheAlgorithms#6735)
* Create q_full_adder.py This is for the #Hacktoberfest. This circuit is the quantum full adder. I saw that in the repo is the half adder so I decided to build the full adder to complete the set of adders. I hope that this is enough to be consider a contribution. Best, Kevin * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Erase the unused numpy library * Create the doctest. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * doctest for negative numbers, float, etc. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e7b6d28 commit 7776411

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

quantum/q_full_adder.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
Build the quantum full adder (QFA) for any sum of
3+
two quantum registers and one carry in. This circuit
4+
is designed using the Qiskit framework. This
5+
experiment run in IBM Q simulator with 1000 shots.
6+
.
7+
References:
8+
https://www.quantum-inspire.com/kbase/full-adder/
9+
"""
10+
11+
import math
12+
13+
import qiskit
14+
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute
15+
16+
17+
def quantum_full_adder(
18+
input_1: int = 1, input_2: int = 1, carry_in: int = 1
19+
) -> qiskit.result.counts.Counts:
20+
"""
21+
# >>> q_full_adder(inp_1, inp_2, cin)
22+
# the inputs can be 0/1 for qubits in define
23+
# values, or can be in a superposition of both
24+
# states with hadamard gate using the input value 2.
25+
# result for default values: {11: 1000}
26+
qr_0: ──■────■──────────────■──
27+
│ ┌─┴─┐ ┌─┴─┐
28+
qr_1: ──■──┤ X ├──■────■──┤ X ├
29+
│ └───┘ │ ┌─┴─┐└───┘
30+
qr_2: ──┼─────────■──┤ X ├─────
31+
┌─┴─┐ ┌─┴─┐└───┘
32+
qr_3: ┤ X ├─────┤ X ├──────────
33+
└───┘ └───┘
34+
cr: 2/═════════════════════════
35+
Args:
36+
input_1: input 1 for the circuit.
37+
input_2: input 2 for the circuit.
38+
carry_in: carry in for the circuit.
39+
Returns:
40+
qiskit.result.counts.Counts: sum result counts.
41+
>>> quantum_full_adder(1,1,1)
42+
{'11': 1000}
43+
>>> quantum_full_adder(0,0,1)
44+
{'01': 1000}
45+
>>> quantum_full_adder(1,0,1)
46+
{'10': 1000}
47+
>>> quantum_full_adder(1,-4,1)
48+
Traceback (most recent call last):
49+
...
50+
ValueError: inputs must be positive.
51+
>>> quantum_full_adder('q',0,1)
52+
Traceback (most recent call last):
53+
...
54+
TypeError: inputs must be integers.
55+
>>> quantum_full_adder(0.5,0,1)
56+
Traceback (most recent call last):
57+
...
58+
ValueError: inputs must be exact integers.
59+
>>> quantum_full_adder(0,1,3)
60+
Traceback (most recent call last):
61+
...
62+
ValueError: inputs must be less or equal to 2.
63+
"""
64+
if (type(input_1) == str) or (type(input_2) == str) or (type(carry_in) == str):
65+
raise TypeError("inputs must be integers.")
66+
67+
if (input_1 < 0) or (input_2 < 0) or (carry_in < 0):
68+
raise ValueError("inputs must be positive.")
69+
70+
if (
71+
(math.floor(input_1) != input_1)
72+
or (math.floor(input_2) != input_2)
73+
or (math.floor(carry_in) != carry_in)
74+
):
75+
raise ValueError("inputs must be exact integers.")
76+
77+
if (input_1 > 2) or (input_2 > 2) or (carry_in > 2):
78+
raise ValueError("inputs must be less or equal to 2.")
79+
80+
# build registers
81+
qr = QuantumRegister(4, "qr")
82+
cr = ClassicalRegister(2, "cr")
83+
# list the entries
84+
entry = [input_1, input_2, carry_in]
85+
86+
quantum_circuit = QuantumCircuit(qr, cr)
87+
88+
for i in range(0, 3):
89+
if entry[i] == 2:
90+
quantum_circuit.h(i) # for hadamard entries
91+
elif entry[i] == 1:
92+
quantum_circuit.x(i) # for 1 entries
93+
elif entry[i] == 0:
94+
quantum_circuit.i(i) # for 0 entries
95+
96+
# build the circuit
97+
quantum_circuit.ccx(0, 1, 3) # ccx = toffoli gate
98+
quantum_circuit.cx(0, 1)
99+
quantum_circuit.ccx(1, 2, 3)
100+
quantum_circuit.cx(1, 2)
101+
quantum_circuit.cx(0, 1)
102+
103+
quantum_circuit.measure([2, 3], cr) # measure the last two qbits
104+
105+
backend = Aer.get_backend("qasm_simulator")
106+
job = execute(quantum_circuit, backend, shots=1000)
107+
108+
return job.result().get_counts(quantum_circuit)
109+
110+
111+
if __name__ == "__main__":
112+
print(f"Total sum count for state is: {quantum_full_adder(1,1,1)}")

0 commit comments

Comments
 (0)