-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Create q_full_adder.py #6735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create q_full_adder.py #6735
Changes from 2 commits
61a5708
6527950
18102cb
2bd9282
c3204d4
0b715a5
ab40bc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Build the quantum full adder (QFA) for any sum of | ||
two quantum registers and one carry in. This circuit | ||
is designed using the Qiskit framework. This | ||
experiment run in IBM Q simulator with 1000 shots. | ||
. | ||
References: | ||
https://www.quantum-inspire.com/kbase/full-adder/ | ||
""" | ||
|
||
import numpy as np | ||
import qiskit | ||
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute | ||
|
||
|
||
def q_full_adder( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
inp_1: int = 1, inp_2: int = 1, cin: int = 1 | ||
) -> qiskit.result.counts.Counts: | ||
""" | ||
# >>> q_full_adder(inp_1, inp_2, cin) | ||
# the inputs can be 0/1 for qubits in define | ||
# values, or can be in a superposition of both | ||
# states with hadamard gate using the input value 2. | ||
# result for default values: {11: 1000} | ||
qr_0: ──■────■──────────────■── | ||
│ ┌─┴─┐ ┌─┴─┐ | ||
qr_1: ──■──┤ X ├──■────■──┤ X ├ | ||
│ └───┘ │ ┌─┴─┐└───┘ | ||
qr_2: ──┼─────────■──┤ X ├───── | ||
┌─┴─┐ ┌─┴─┐└───┘ | ||
qr_3: ┤ X ├─────┤ X ├────────── | ||
└───┘ └───┘ | ||
cr: 2/═════════════════════════ | ||
Args: | ||
inp_1: input 1 for the circuit. | ||
inp_2: input 2 for the circuit. | ||
cin: carry in for the circuit. | ||
Returns: | ||
qiskit.result.counts.Counts: sum result counts. | ||
""" | ||
# build registers | ||
qr = QuantumRegister(4, "qr") | ||
cr = ClassicalRegister(2, "cr") | ||
# list the entries | ||
entry = [inp_1, inp_2, cin] | ||
|
||
quantum_circuit = QuantumCircuit(qr, cr) | ||
|
||
for i in range(0, 3): | ||
if entry[i] == 2: | ||
quantum_circuit.h(i) # for hadamard entries | ||
elif entry[i] == 1: | ||
quantum_circuit.x(i) # for 1 entries | ||
else: | ||
quantum_circuit.i(i) # for 0 entries | ||
|
||
# build the circuit | ||
quantum_circuit.ccx(0, 1, 3) # ccx = toffoli gate | ||
quantum_circuit.cx(0, 1) | ||
quantum_circuit.ccx(1, 2, 3) | ||
quantum_circuit.cx(1, 2) | ||
quantum_circuit.cx(0, 1) | ||
|
||
quantum_circuit.measure([2, 3], cr) # measure the last two qbits | ||
|
||
backend = Aer.get_backend("qasm_simulator") | ||
job = execute(quantum_circuit, backend, shots=1000) | ||
|
||
return job.result().get_counts(quantum_circuit) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"Total sum count for state is: {q_full_adder()}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
quantum/q_full_adder.py
, please provide doctest for the functionq_full_adder